[Rt-devel] scrip check group membership of Requestor not CurrentUser

Dale L Bewley dlbewley at ucdavis.edu
Fri Feb 24 17:40:05 EST 2006


Hello. I'm trying my hand at my first scrip and it's close, but I
obviously have some conceptual issues.

I would like to add a group as Cc on a ticket at creation time,
but not if the requestor is in that group. Nobody wants to get
duplicate emails.

Since the ticket could be opened on the requestor's behalf by
a phone support tech, I believe I can't use 
$self->CurrentUser->PrincipalObj when checking for group membership.

This all works except the group membership check never catches.
Would someone mind pointing out my inexperience? Much appreciated.


* Custom Condition: (fine)

  # make sure we are called from a on create condition and
  # make sure department is filled out
  return undef unless (
 	($self->TransactionObj->Type eq "Create") &&
 	($self->TicketObj->FirstCustomFieldValue('Department')) &&
 	($self->TicketObj->FirstCustomFieldValue('Department') ne 'None')
 	);

  return 1;


* Custom action preparation code: (bug)

  # example: for 'Systems' dept. the group is 'Head Systems'
  my $headGroupName = 'Head ' . $self->TicketObj->FirstCustomFieldValue('Department');

  # instantiated a group object that shall not be obscured by ACLs
  my $headGroupObj = RT::Group->new($RT::SystemUser);
  $headGroupObj->LoadUserDefinedGroup($headGroupName);
  return undef unless $headGroupObj;

  # determine the requestor
  # current user may not be requestor if phone desk enters ticket,
  # so i need to make a requestor principal obj to toss to Group::HasMember()
  # theoretically requestor may be plural. i'll pretend not though...
  my $requestorsGroupObj = RT::Group->new($RT::SystemUser);
  $requestorsGroupObj->LoadTicketRoleGroup(Type => 'Requestor', Ticket => $self->TicketObj->id);
  $RT::Logger->info("Instantiated group '" . $requestorsGroupObj->SelfDescription() .
     "' from ticket #" . $self->TicketObj->id );

  # add group as CC unless requestor is in that group.
  # this could certainly be a problem if there are multiple members in a group,
  # but i don't care right now.
  ##if ($headGroupObj->HasMember($self->CurrentUser->PrincipalObj)) {
  if ($headGroupObj->HasMember($requestorsGroupObj->PrincipalObj)) {
      $RT::Logger->info("Requestor is in group '$headGroupName' not adding Cc on ticket #" .
 	 $self->TicketObj->id );
      return undef;
  }

  # add group as Cc on ticket. see Ticket_Overlay.pm
  $RT::Logger->info("Add group '$headGroupName' as Cc on ticket #". $self->TicketObj->id );
  my ($success, $msg)= $self->TicketObj->AddWatcher(
 			      Type => "Cc",
 			      PrincipalId => $headGroupObj->Id);
  if (! $success) {
   $RT::Logger->info($msg);
   return undef;
  } else {
   return 1;
  }


-- 
Dale Bewley - dlbewley a/t lib.ucdavis.edu
Unix Administrator - Shields Library UC Davis


More information about the Rt-devel mailing list