Scrip check group membership of Requestor not CurrentUser

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

After much poking and prodding, I have a working scrip now. I would
appreciate if anyone pointed out blatant inefficiencies in how I’m doing
this. Any other comments are also welcome.

I’ll contribute this to the Wiki once it shakes out a bit.

BTW, I found that my custom condition code is ignored, so I have to
replicate it in the action portion. Any idea why?

I’m on rt-3.4.5.

Problem:
Department heads want to see and comment on all tickets
emminating from their staff.

Solution:
All tickets have a Department custom field mapping the ticket to a dept.
I globally grant the Cc role ShowTicket and ReplyToTicket permission and
then add the department head as Cc on their staff’s tickets. This way they can
add comments and be aware of issues in their department. I do not yet wish
to give them powers such as ModifyTicket, so they can’t monkey with
priorities etc. Therefore, I felt Cc more appropriate than AdminCc even
after granting extra rights to Cc.

Scrip follows:

  • Description: ‘‘OnCreateSetDeptHeadCc’’

  • Condition: ‘‘On Create’’

  • Action: ‘‘User Defined’’

  • Template: ‘‘Global template: Blank’’

  • Stage: ‘‘TransactionCreate’’

  • Custom Condition:

Actually, I have discovered that this is not working. So, you should
instead put this in the preparation code portion. (without the return 1;
of course)

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:

    example: for ‘Systems’ the group is ‘Head Systems’

    my $derivedGroupName = 'Head ’ .
    $self->TicketObj->FirstCustomFieldValue(‘Department’);

    instantiated a group object

    my $groupObj = RT::Group->new($RT::SystemUser);
    $groupObj->LoadUserDefinedGroup($derivedGroupName);
    return undef unless $groupObj;

    one doesn’t want to be a requestor and a Cc on the same ticket.

    don’t add group as CC if requestor is in that group.

    this could certainly be a problem if there are multiple members

    my $requestorsGroupObj = $self->TicketObj->Requestors;

    UMO grabs group members in subgroups too. it is recursive

    my $requestorsMembersObj = $requestorsGroupObj->UserMembersObj;

    my $userObj;
    while ($userObj = $requestorsMembersObj->Next) {
    if ($groupObj->HasMember($userObj->PrincipalObj)) {
    $RT::Logger->info(“Requestor '” . $userObj->Name .
    “’ is in group ‘$derivedGroupName’ 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 ‘$derivedGroupName’ as Cc on ticket #” .
    $self->TicketObj->id );
    my ($success, $msg)= $self->TicketObj->AddWatcher(
    Type => “Cc”,
    PrincipalId => $groupObj->PrincipalId);

    if (! $success) {
    $RT::Logger->info($msg);
    return undef;
    } else {
    return 1;
    }

  • Custom action cleanup code:

    blank

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

  • Condition: ‘‘On Create’’

Duh. Changing that to ‘‘User Defined’’ fixed the ignored custom
condition.

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