CcManagers

From Request Tracker Wiki
Jump to navigation Jump to search

Overview

If you have a similar group structure like the one defined in rights (see section departments of a company as groups), you might need that the users of each departments are not able to see the tickets of the queues and that managers of those departments can do. If creating a queue for each group is not an option, keep reading.

Note that I habe been using RT for over a week now; this is my very first custom action, so there might be ways of improving it.

--Gmirandaupc 10:50, December 13, 2010 (UTC)

Custom action code

Parameters

The code assumes that there is a top-level group that must be passed as a parameter (in the installation script). I call that group 'Customers', although it could be just 'Departments'. There is also a hardcoded value, the suffix of the manager groups. Each department group with managers must have a subgroup whose name is the name of the parent group succeeded by 'Mgr'.

Sample structure:

  • Customers
    • OrderDept
      • OrderDeptMgr
    • TeacherDept
      • TeacherDeptMgr

Install script

   # To install, install CcManagers.pm in local/lib/RT/Action, and
   # this script in local/etc/CcManagers.install
   #     /opt/rt3/sbin/rt-setup-database --action insert \
   #         --datafile /opt/rt3/local/etc/CcManagers.install
   #


   @ScripActions = (
      {
          Name         => 'On Create Add Manager Group CC',
          Description  => 'When a ticket is created, if the user belongs to a group under a predefined parent group (the argument), the manager subgroup will be added as Cc.',
          ExecModule   => 'CcManagers',
          Argument     => 'Customers'
      },
   );

Code

Save the following code to the local action lib dir (i.e. /opt/rt3/local/lib/RT/Action) as CcManagers.pm

 #
 # This custom action sets the users of the manager group of the ESSI or LSI
 # departments as CC of the ticket, depending on the group of the user that
 # created the ticket.
 #
 # @author Guillermo Miranda Álamo - LCLSI UPC <gmiranda@lsi.upc.edu>
 # @date 09/12/2010 (dd/mm/yyyy).
 #
 
 package RT::Action::CcManagers;
 use base qw(RT::Action::Generic);
 
 use RT::Groups;
 
 my $actionDesc = "On Create Add Manager Group CC";
 
 
 =head2 Prepare
 
 When the ticket is created, if the user is in a group (or more) of 'Customers'
 (note that 'Customers' is the argument), the manager subgroup of that group
 will be added as Cc.
 
 It is mandatory that the managers subgroup are named with a suffix "Mgr".
 So if the group name is "TestingDept", the managers must be in "TestingDeptMgr",
 a subgroup of "TestingDept".
 
 =cut
 
 sub Prepare {
 	my $self = shift;
 
 	$RT::Logger->debug( "[" . $actionDesc . "] Custom action prep init. Argument is " . $self->Argument );
 
 	my $ticket = $self->TicketObj;
 	my $transaction = $self->TransactionObj;
 	#my $owner = $ticket->OwnerObj;	#nobody
 	my $owner = RT::User->new( $RT::SystemUser );
 	$owner->Load( $transaction->Creator );
 	my $queue = $ticket->QueueObj;
 
 	# Change this, it should be a user-defined value
 	my $managerSuffix = "Mgr";				# The suffix of the name of every manager group
 	my $parentGroupName = $self->Argument;	# The name of the parent group of all the departments
 
 	my $parentGroupObj = RT::Group->new( $RT::SystemUser );
 	$parentGroupObj->LoadUserDefinedGroup( $parentGroupName );
 
 	return undef unless $parentGroupObj;
 
 	my $ownerGroups = RT::Groups->new( $RT::SystemUser );
 	#$ownerGroups->LimitToRolesForQueue( $queue->Id );	# Doesn't seem to work
 	$ownerGroups->LimitToUserDefinedGroups();
 	$ownerGroups->UnLimit();
 
 	# For each group
 	while( my $ownerGroup = $ownerGroups->Next() ){
 		# If it's a department and the user is in that department
 		if( $parentGroupObj->HasMember( $ownerGroup->Id ) ){
 			$RT::Logger->info( "[" . $actionDesc . "] Checking group '"
 				. $ownerGroup->Name . "' (" . $ownerGroup->Id . ") for user "
 				. $owner->Name . "-" . $owner->PrincipalId );
 			
 			if( $ownerGroup->HasMember( $owner->Id ) ){
 				$RT::Logger->info( "[" . $actionDesc . "] User is member of '" 
 					. $ownerGroup->Name . "' (" . $ownerGroup->Id . ")" );
 			
 				# This action will be performed for each dept the user belongs to
 				my $groupName = $ownerGroup->Name . $managerSuffix;
 			
 				# $RT::Logger->info( "[" . $actionDesc . "] Manager group name should be " . $groupName  );
 				# Retrieve the group
 				my $groupObj = RT::Group->new( $RT::SystemUser );
 			
 				$groupObj->LoadUserDefinedGroup( $groupName );
 				# If the group doesn't have a manager subgroup, skip
 				if( !$groupObj ){
 					next;
 				}
 			
 				$RT::Logger->debug( "[" . $actionDesc . "] Adding '" . $groupName ."'(" . $groupObj->Id  . ") to ticket \#" .$ticket->id );
 			
 				# Add the group (as a group, not every user of the group) as Cc
 				$ticket->AddWatcher( Type => 'Cc',
 					PrincipalId => $groupObj->PrincipalId );
 			}
 		}
 	}
 
 	return 1;
 }
 
  sub Commit{
 	my $self = shift;
 	
 	return 1;
 }
 
 1;

You can use the following install script

Scrip creation

Condition: Create
Action: On Create Add Manager Group Cc
Template: Global template: Blank
Stage: TransactionCreate

Credits

This custom action is based on the code of OnCreateAddGroupCc and OnCreateSetDeptHeadCc.