[Bps-public-commit] RT-Extension-MandatoryOnTransition branch, roles-2, created. 0.17-5-gc9d1f69

Craig Kaiser craig at bestpractical.com
Thu Feb 14 11:21:48 EST 2019


The branch, roles-2 has been created
        at  c9d1f69f82c05d0be3bc1d5d3509dfbbdf87ccda (commit)

- Log -----------------------------------------------------------------
commit d3c735ff1786fd2e5d88951fbb28dd9e8957af97
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Fri Jan 4 11:11:15 2019 -0500

    Support customroles for mandatoryontransition conditions
    
    Use the syntax 'CustomRole.MyRole' to set mandatory conditions for
    customrole values.

diff --git a/README b/README
index ad7e034..10ec6f6 100644
--- a/README
+++ b/README
@@ -96,7 +96,7 @@ CONFIGURATION
 
         Set( %MandatoryOnTransition,
             'QueueName' => {
-                'from -> to' => [ 'BasicField', 'CF.MyField', ],
+                'from -> to' => [ 'BasicField', 'CF.MyField', 'CustomRole.MyRole' ],
             },
         );
 
@@ -115,7 +115,8 @@ CONFIGURATION
 
         Set( %MandatoryOnTransition,
             Helpdesk => {
-                '* -> resolved' => ['TimeWorked', 'CF.Resolution'],
+                '* -> resolved'      => ['TimeWorked', 'CF.Resolution', 'CustomRole.Analyst'],
+                'CustomRole.Analyst' => {transition => '* -> open', group => 'Engineering'},
             },
             '*' => {
                 '* -> resolved' => ['CF.Category'],
diff --git a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked
index b00e7d7..4f5da6e 100644
--- a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked
+++ b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked
@@ -2,11 +2,11 @@
 $Ticket
 </%args>
 <%init>
-my ($core, $cfs) = RT::Extension::MandatoryOnTransition->RequiredFields(
+my ($core, $cfs, $roles) = RT::Extension::MandatoryOnTransition->RequiredFields(
     Ticket  => $Ticket,
     To      => $ARGS{'Status'} || $ARGS{'DefaultStatus'},
 );
-return unless @$cfs;
+return unless @$cfs or @$roles;
 
 my $comp = '/Elements/EditCustomFields';
 my %obj_args = ( Object => $Ticket );
@@ -17,11 +17,49 @@ if (!$m->comp_exists('/Elements/EditCustomFields')) {
     %obj_args = ( TicketObj => $Ticket );
 }
 
+my @roles;
+foreach my $role (@{$roles}) {
+    if ( $role =~ s/^CustomRole\.//i ) {
+        my $role_object = RT::CustomRole->new($session{CurrentUser});
+        my ($ret, $msg) = $role_object->Load($role);
+
+        RT::Logger->error($msg) unless $ret;
+        push @roles, $role_object if $ret;
+    } else {
+        my $role_group = RT::Group->new($session{CurrentUser});
+        my ($ret, $msg) = $role_group->LoadRoleGroup(Object => $Ticket, Name => $role);
+
+        RT::Logger->error($msg) unless $ret;
+        push @roles, $role_group if $ret;
+    }
+}
 </%init>
 %# 'Named' is handled by this extension in the MassageCustomFields callback
-<& $comp,
-    %ARGS,
-    %obj_args,
-    InTable     => 1,
-    Named       => $cfs,
-    &>
+% if ( @$cfs ) {
+    <& $comp,
+        %ARGS,
+        %obj_args,
+        InTable     => 1,
+        Named       => $cfs,
+        &>
+% }
+% if ( @$roles ) {
+% my $count = 0;
+%     foreach my $role (@roles) {
+%     next if $role->Name eq 'Owner';
+        <tr>
+            <td class="label"><% $role->Name %>:</td>
+            <td class="entry">
+            <input type='hidden' name="WatcherTypeEmail<% $count %>" value=<% $role->Name %>></input>
+                <& /Elements/EmailInput,
+                    Name => grep ($role->Name eq $_, qw/Requestor AdminCc Cc/) ? "WatcherAddressEmail$count" : 'RT::CustomRole-' . $role->Id,
+                    Autocomplete       => 1,
+                    AutocompleteNobody => 1,
+                    AutocompleteReturn => "Name",
+                    Size               => 20,
+                &>
+            </td>
+        </tr>
+%     $count++;
+%     }
+% }
diff --git a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate
index 171893f..91be5c0 100644
--- a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate
+++ b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate
@@ -16,4 +16,11 @@ if (@$errors_ref) {
     $$skip_update = 1;
     push @$results, @$errors_ref;
 }
+
+# Allow mandatory roles to be set on update page
+foreach my $key (keys %{$ARGSRef}) {
+    if ( $key =~ /^WatcherAddressEmail(\d*)$/ && not $ARGSRef->{$key} ) {
+        $ARGSRef->{"WatcherTypeEmail$1"} = undef;
+    }
+}
 </%init>
diff --git a/lib/RT/Extension/MandatoryOnTransition.pm b/lib/RT/Extension/MandatoryOnTransition.pm
index d238c18..60dafa5 100644
--- a/lib/RT/Extension/MandatoryOnTransition.pm
+++ b/lib/RT/Extension/MandatoryOnTransition.pm
@@ -127,7 +127,7 @@ config option.  This option takes the generic form of:
 
     Set( %MandatoryOnTransition,
         'QueueName' => {
-            'from -> to' => [ 'BasicField', 'CF.MyField', ],
+            'from -> to' => [ 'BasicField', 'CF.MyField', 'CustomRole.MyRole' ],
         },
     );
 
@@ -146,7 +146,8 @@ Category selection before resolving tickets in every other queue.
 
     Set( %MandatoryOnTransition,
         Helpdesk => {
-            '* -> resolved' => ['TimeWorked', 'CF.Resolution'],
+            '* -> resolved'      => ['TimeWorked', 'CF.Resolution', 'CustomRole.Analyst'],
+            'CustomRole.Analyst' => {transition => '* -> open', group => 'Engineering'},
         },
         '*' => {
             '* -> resolved' => ['CF.Category'],
@@ -257,18 +258,16 @@ pair to %CORE_FOR_UPDATE and/or %CORE_FOR_CREATE.
 
 =cut
 
-our @CORE_SUPPORTED  = qw(Content TimeWorked TimeTaken Owner);
-our @CORE_TICKET     = qw(TimeWorked Owner);
+our @CORE_SUPPORTED  = qw(Content TimeWorked TimeTaken);
+our @CORE_TICKET     = qw(TimeWorked);
 our %CORE_FOR_UPDATE = (
     TimeWorked  => 'UpdateTimeWorked',
     TimeTaken   => 'UpdateTimeWorked',
     Content     => 'UpdateContent',
-    Owner       => 'Owner',
 );
 our %CORE_FOR_CREATE = (
     TimeWorked  => 'TimeWorked',
     Content     => 'Content',
-    Owner       => 'Owner',
 );
 
 =head2 Methods
@@ -323,7 +322,7 @@ sub RequiredFields {
     my %config = ();
     %config = $self->Config($args{Queue});
 
-    return ([], []) unless %config;
+    return ([], [], []) unless %config;
 
    $to ||= '';
    $from ||= '';
@@ -340,6 +339,8 @@ sub RequiredFields {
     my @core = grep { !/^CF\./i && $core_supported{$_} } @$required;
     my @cfs  =  map { /^CF\.(.+)$/i; $1; }
                grep { /^CF\./i } @$required;
+    my @roles = map { /^(:?[CustomRole\.]?.+)$/i; $1; }
+               grep { /^CustomRole\.|^AdminCc|^Cc|^Requestor|^Owner/i } @$required;
 
     # Pull out any must_be or must_not_be rules
     my %cf_must_values = ();
@@ -359,7 +360,8 @@ sub RequiredFields {
             }
         }
     }
-    return (\@core, \@cfs, \%cf_must_values);
+
+    return (\@core, \@cfs, \@roles, \%cf_must_values);
 }
 
 =head3 CheckMandatoryFields
@@ -426,15 +428,14 @@ sub CheckMandatoryFields {
         return \@errors;
     }
 
-    my ($core, $cfs, $must_values) = $self->RequiredFields(
+    my ($core, $cfs, $roles, $must_values) = $self->RequiredFields(
         Ticket  => $args{'Ticket'},
         Queue   => $args{'Queue'} ? $args{'Queue'}->Name : undef,
         From    => $args{'From'},
         To      => $args{'To'},
         NewQueue => $$ARGSRef{'Queue'},
     );
-
-    return \@errors unless @$core or @$cfs;
+    return \@errors unless @$core or @$cfs or @$roles;
 
     my $transition =  ($args{'From'} ||'') ne ($args{'To'} || '') ? 'Status' : 'Queue';
 
@@ -457,36 +458,6 @@ sub CheckMandatoryFields {
             : $CORE_FOR_CREATE{$field};
         next unless $arg;
 
-        if ($field eq 'Owner') {
-            my $value;
-
-            # There are 2 Owner fields on Jumbo page, copied the same handling from it.
-            if (ref $ARGSRef->{$arg}) {
-                foreach my $owner (@{$ARGSRef->{$arg}}) {
-                    if (defined($owner) && $owner =~ /\D/) {
-                        $value = $owner unless ($args{'Ticket'}->OwnerObj->Name eq $owner);
-                    }
-                    elsif (length $owner) {
-                        $value = $owner unless ($args{'Ticket'}->OwnerObj->id == $owner);
-                    }
-                }
-            }
-            else {
-                $value = $ARGSRef->{$arg};
-            }
-
-            if (($value || $args{'Ticket'}->$field()) == $RT::Nobody->id) {
-                push @errors,
-                  $CurrentUser->loc(
-                    "[_1] is required when changing [_2] to [_3]",
-                    $field,
-                    $CurrentUser->loc($transition),
-                    $CurrentUser->loc($field_label{$transition})
-                  );
-                next;
-            }
-        }
-
         next if defined $ARGSRef->{$arg} and length $ARGSRef->{$arg};
 
         # Do we have a value currently?
@@ -500,6 +471,80 @@ sub CheckMandatoryFields {
             $label, $CurrentUser->loc($transition),  $CurrentUser->loc($field_label{$transition}));
     }
 
+    if (@$roles) {
+        foreach my $role (@$roles) {
+            my ( $role_values, $owner_value );
+            my ( $role_arg, $role_full ) = ( $role, $role );
+
+            if ( $role =~ s/^CustomRole\.//i ) {
+                my $role_object = RT::CustomRole->new( $args{Ticket}->CurrentUser );
+
+                my ( $ret, $msg ) = $role_object->Load($role);
+                push @errors, $CurrentUser->loc("Failed to load custom role $role") unless $ret;
+                RT::Logger->error($msg) unless $ret;
+                next unless $role_object->Id;
+
+                $role_arg = 'RT::CustomRole-' . $role_object->Id;
+
+                $role_values = $args{Ticket}->RoleGroup( $role_object->GroupType );
+                push @errors, $CurrentUser->loc("Could not load current user") unless $role_values;
+            } elsif ( $role eq 'Owner' ) {
+                # There are 2 Owner fields on Jumbo page, copied the same handling from it.
+                if ( ref $ARGSRef->{$role} ) {
+                    foreach my $owner ( @{ $ARGSRef->{$role} } ) {
+                        if ( defined($owner) && $owner =~ /\D/ ) {
+                            $owner_value = $owner unless ( $args{'Ticket'}->OwnerObj->Name eq $owner );
+                        }
+                        elsif ( length $owner ) {
+                            $owner_value = $owner unless ($args{'Ticket'}->OwnerObj->id == $owner );
+                        }
+                    }
+                }
+                else {
+                    $owner_value = $ARGSRef->{$role};
+                }
+                if ( $owner_value ) {
+                    my $user = RT::User->new( $args{Ticket}->CurrentUser );
+                    $user->Load($owner_value);
+                    push @errors, $CurrentUser->loc("Could not load user: $owner_value") unless $user->Id;
+                    $role_values = $user->EmailAddress if $user->Id;
+                }
+            }
+            else {
+                $role_values = RT::Group->new( $args{Ticket}->CurrentUser );
+                my ( $ret, $msg ) = $role_values->LoadRoleGroup(
+                    Object => $args{Ticket},
+                    Name   => $role
+                );
+                push @errors, $CurrentUser->loc("Failed to load role $role for ticket") unless $ret;
+                RT::Logger->error($msg) unless $ret;
+            }
+
+            my @role_values;
+            # Use this to keep track of input fields that use a count increment
+            # example: WatcherAddressEmail1 or WatcherAddressEmail2
+            my @row_input_num = grep $role_arg eq $ARGSRef->{$_}, keys %{$ARGSRef};
+            map { push @role_values, $ARGSRef->{$_} if $ARGSRef->{$_} } grep /$role_arg/, keys %{$ARGSRef};
+
+            # Grab our arguments for current role
+            if (@row_input_num) {
+                map { push @role_values, $ARGSRef->{"WatcherAddressEmail$_"}
+                      if $ARGSRef->{"WatcherAddressEmail$_"}
+                  }
+                map { $_ =~ /^WatcherTypeEmail(\d*)$/; $1 } @row_input_num;
+            }
+
+            if ( not scalar @role_values ) {
+                push @errors, $CurrentUser->loc("[_1] is required when changing [_2] to [_3]",
+                    $role,
+                    $CurrentUser->loc($transition),
+                    $CurrentUser->loc( $args{'To'} )
+                );
+                next;
+            }
+        }
+    }
+
     return \@errors unless @$cfs;
 
     if ( not $CFs ){

commit 61e8d2372803669e338f3a322c008f2c27adffc8
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Wed Jan 23 15:48:24 2019 -0500

    Support requiring a role to be a member of a group
    
    Provide a group key for the role specific key in the
    mandatory-on-transition config hash to require at least one
    member of the role be a member of at least one of the groups
    in the groups array.

diff --git a/lib/RT/Extension/MandatoryOnTransition.pm b/lib/RT/Extension/MandatoryOnTransition.pm
index 60dafa5..0a0d246 100644
--- a/lib/RT/Extension/MandatoryOnTransition.pm
+++ b/lib/RT/Extension/MandatoryOnTransition.pm
@@ -361,7 +361,38 @@ sub RequiredFields {
         }
     }
 
-    return (\@core, \@cfs, \@roles, \%cf_must_values);
+    my %role_group_values;
+    my $cr = RT::CustomRole->new(RT->SystemUser);
+    foreach my $role (@roles){
+        if ( $role =~ /^CustomRole\.(.*)/ ) {
+            my $role_name = $1;
+            my ($ret, $msg) = $cr->Load($role_name);
+            if ( not $cr and $cr->Id ) {
+                RT::Logger->error("Could not load Custom role $role_name: $msg");
+                @roles = grep/$_ ne $role_name/, at roles;
+                next;
+            } elsif ( not $cr->IsAdded($args{Ticket}->QueueObj->Id) or $cr->Disabled ) {
+                RT::Logger->error("Custom role $role_name is not applied to: " . $args{Ticket}->QueueObj->Name );
+                @roles = grep/$_ ne $role_name/, at roles;
+                next;
+            }
+        }
+        if ( $config{$role} ){
+            my $transition = $config{$role}->{'transition'};
+            unless ( $transition ){
+                RT->Logger->error("No transition defined in group rules for $role");
+                next;
+            }
+
+            if ( $transition eq "$from -> $to"
+                || $transition eq "* -> $to"
+                || $transition eq "$from -> *" ) {
+
+                $role_group_values{$role} = $config{$role};
+            }
+        }
+    }
+    return (\@core, \@cfs, \@roles, \%cf_must_values, \%role_group_values);
 }
 
 =head3 CheckMandatoryFields
@@ -428,7 +459,7 @@ sub CheckMandatoryFields {
         return \@errors;
     }
 
-    my ($core, $cfs, $roles, $must_values) = $self->RequiredFields(
+    my ($core, $cfs, $roles, $must_values, $role_group_values) = $self->RequiredFields(
         Ticket  => $args{'Ticket'},
         Queue   => $args{'Queue'} ? $args{'Queue'}->Name : undef,
         From    => $args{'From'},
@@ -534,7 +565,47 @@ sub CheckMandatoryFields {
                 map { $_ =~ /^WatcherTypeEmail(\d*)$/; $1 } @row_input_num;
             }
 
-            if ( not scalar @role_values ) {
+            # We could end up with a RT::Group option or a single value in the case of Owner
+            if ( ref $role_values eq 'RT::Group' ) {
+                my @temp_array = $role_values->MemberEmailAddresses;
+                push @role_values, @temp_array if scalar @temp_array;
+            }
+            else {
+                push @role_values, $role_values;
+            }
+
+            # Check for mandatory group configuration, supports multiple groups where only
+            # one true case needs to be found.
+            if ( $role_group_values->{$role_full}->{group} ) {
+                my $has_valid_member;
+
+                foreach my $group_name ( @{ $role_group_values->{$role_full}->{group} } ) {
+                    next if $has_valid_member;
+                    my $group = RT::Group->new( $args{Ticket}->CurrentUser );
+
+                    my ( $ret, $msg ) = $group->LoadUserDefinedGroup($group_name);
+                    push @errors, $CurrentUser->loc("Failed to load group: '$group_name' $msg") unless $ret;
+                    next unless $ret;
+
+                    my $user = RT::User->new( $args{Ticket}->CurrentUser );
+                    foreach my $member (@role_values) {
+                        next unless $member;
+
+                        $user->Load($member);
+                        $user->LoadByEmail($member) unless $user->Id;
+                        RT::Logger->error("Failed to load: $member") unless $user->Id;
+                        next unless $user->Id;
+
+                        $has_valid_member = $group->HasMemberRecursively( $user->Id );
+                    }
+                }
+                my $roles = join( ' or ', @{ $role_group_values->{$role_full}->{group} } );
+                push @errors, $CurrentUser->loc("A member of group $roles is required for role: $role")
+                    unless $has_valid_member;
+                next unless $has_valid_member;
+            }
+            if ( not scalar @role_values or ($role eq 'Owner' and !$owner_value or
+                ( $owner_value && $owner_value == $RT::Nobody->id) ) ) {
                 push @errors, $CurrentUser->loc("[_1] is required when changing [_2] to [_3]",
                     $role,
                     $CurrentUser->loc($transition),

commit ea168c4abafa85e60484745e80739dc21bc06df0
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Fri Jan 11 16:59:07 2019 -0500

    Create test file for roles

diff --git a/xt/require_owner_for_resolve.t b/xt/require_owner_for_resolve.t
deleted file mode 100644
index 20fd377..0000000
--- a/xt/require_owner_for_resolve.t
+++ /dev/null
@@ -1,122 +0,0 @@
-use strict;
-use warnings;
-
-use RT::Extension::MandatoryOnTransition::Test tests => undef, config => <<CONFIG
-Set( %MandatoryOnTransition,
-     '*' => {
-         '* -> resolved' => ['Owner',],
-     }
-    );
-CONFIG
-  ;
-
-use_ok('RT::Extension::MandatoryOnTransition');
-
-my ($baseurl, $m) = RT::Test->started_ok();
-
-ok($m->login('root', 'password'), 'logged in');
-$m->get_ok($m->rt_base_url);
-
-my $root = RT::User->new(RT->SystemUser);
-$root->Load('root');
-ok($root->id, 'Loaded root');
-
-diag "Resolve ticket through Update with required Owner";
-{
-    my $t = RT::Test->create_ticket(
-        Queue   => 'General',
-        Subject => 'Test Mandatory Owner On Resolve',
-        Content => 'Testing',
-    );
-
-    ok($t->id, 'Created test ticket: ' . $t->id);
-
-    $m->goto_ticket($t->id);
-
-    $m->follow_link_ok({ text => 'Resolve' }, 'Try to resolve ticket');
-    $m->text_contains('Test Mandatory Owner On Resolve');
-    $m->submit_form_ok(
-        {   form_name => 'TicketUpdate',
-            button    => 'SubmitTicket',
-        },
-        'Submit resolve with no Owner set'
-    );
-    $m->text_contains('Owner is required when changing Status to resolved');
-    $m->submit_form_ok(
-        {   form_name => 'TicketUpdate',
-            button    => 'SubmitTicket',
-            fields    => { Owner => $root->id }
-        },
-        'Submit resolve with Owner set'
-    );
-    $m->text_lacks('Owner is required when changing Status to resolved');
-    $m->text_contains('Owner changed from Nobody to root');
-    $m->text_contains("Status changed from 'new' to 'resolved'");
-}
-
-diag "Resolve ticket through Basics with required Owner";
-{
-    my $t = RT::Test->create_ticket(
-        Queue   => 'General',
-        Subject => 'Test Mandatory Owner On Resolve',
-        Content => 'Testing',
-    );
-    ok($t->id, 'Created ticket to resolve through Modify.html: ' . $t->id);
-
-    $m->goto_ticket($t->id);
-    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
-    $m->submit_form_ok(
-        {   form_name => 'TicketModify',
-            fields    => { Status => 'resolved', },
-            button    => 'SubmitTicket',
-        },
-        'Submit resolve with no Owner set'
-    );
-    $m->text_contains('Owner is required when changing Status to resolved');
-
-    $m->submit_form_ok(
-        {   form_name => 'TicketModify',
-            fields    => { Status => 'resolved', Owner => $root->id, },
-            button    => 'SubmitTicket',
-        },
-        'Submit resolve with no Owner set'
-    );
-    $m->text_lacks('Owner is required when changing Status to resolved');
-    $m->text_contains('Owner changed from Nobody to root');
-    $m->text_contains("Status changed from 'new' to 'resolved'");
-}
-
-diag "Resolve ticket through Jumbo with required Owner";
-{
-    my $t = RT::Test->create_ticket(
-        Queue   => 'General',
-        Subject => 'Test Mandatory Owner On Resolve',
-        Content => 'Testing',
-    );
-    ok($t->id, 'Created ticket to resolve through ModifyAll.html: ' . $t->id);
-
-    $m->goto_ticket($t->id);
-    $m->follow_link_ok({ text => 'Jumbo' }, 'Get ModifyAll.html of ticket');
-    $m->submit_form_ok(
-        {   form_name => 'TicketModifyAll',
-            fields    => { Status => 'resolved', },
-            button    => 'SubmitTicket',
-        },
-        'Submit resolve with no Owner set'
-    );
-    $m->text_contains('Owner is required when changing Status to resolved');
-
-    $m->submit_form_ok(
-        {   form_name => 'TicketModifyAll',
-            fields    => { Status => 'resolved', Owner => $root->id, },
-            button    => 'SubmitTicket',
-        },
-        'Submit resolve with no Owner set'
-    );
-    $m->text_lacks('Owner is required when changing Status to resolved');
-    $m->text_contains('Owner changed from Nobody to root');
-    $m->text_contains("Status changed from 'new' to 'resolved'");
-}
-
-undef $m;
-done_testing;
diff --git a/xt/required_fields.t b/xt/required_fields.t
index 4f2135c..cd1fda7 100644
--- a/xt/required_fields.t
+++ b/xt/required_fields.t
@@ -14,8 +14,8 @@ diag "Test RequiredFields without a ticket";
                        );
     is( $core->[0], 'TimeWorked', 'Got TimeWorked for required core');
 
-    my $must_values;
-    ($core, $cf, $must_values) = RT::Extension::MandatoryOnTransition->RequiredFields(
+    my ($must_values, $role, $role_group_values);
+    ($core, $cf, $role, $must_values, $role_group_values) = RT::Extension::MandatoryOnTransition->RequiredFields(
                            From => "''",
                            To   => 'resolved',
                            Queue => 'General',
@@ -41,7 +41,7 @@ diag "Test RequiredFields with a ticket";
 
     ok( $t->id, 'Created test ticket: ' . $t->id);
 
-    my ($core, $cf, $must_values) = RT::Extension::MandatoryOnTransition->RequiredFields(
+    my ($core, $cf, $roles, $must_values, $role_group_values) = RT::Extension::MandatoryOnTransition->RequiredFields(
                            Ticket => $t,
                            To   => 'resolved',
                        );
diff --git a/xt/roles.t b/xt/roles.t
new file mode 100644
index 0000000..76b6976
--- /dev/null
+++ b/xt/roles.t
@@ -0,0 +1,248 @@
+use strict;
+use warnings;
+
+use RT::Extension::MandatoryOnTransition::Test tests => undef, config => <<CONFIG
+Set( %MandatoryOnTransition,
+     '*' => {
+         '* -> resolved'  => ['Owner'],
+         '* -> stalled'   => ['AdminCc'],
+         '* -> deleted'   => ['CustomRole.vip'],
+         'AdminCc'        => { transition => '* -> stalled', group => ['Admins'] },
+         'CustomRole.vip' => { transition => '* -> deleted' },
+        }
+    );
+CONFIG
+  ;
+
+use_ok('RT::Extension::MandatoryOnTransition');
+
+my ($baseurl, $m) = RT::Test->started_ok();
+
+ok($m->login('root', 'password'), 'logged in');
+$m->get_ok($m->rt_base_url);
+
+my $root = RT::User->new(RT->SystemUser);
+$root->Load('root');
+ok($root->id, 'Loaded root');
+
+diag "Resolve ticket through Update with required Owner";
+{
+    my $t = RT::Test->create_ticket(
+        Queue   => 'General',
+        Subject => 'Test Mandatory Owner On Resolve',
+        Content => 'Testing',
+    );
+
+    ok($t->id, 'Created test ticket: ' . $t->id);
+
+    $m->goto_ticket($t->id);
+
+    $m->follow_link_ok({ text => 'Resolve' }, 'Try to resolve ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketUpdate',
+            button    => 'SubmitTicket',
+        },
+        'Submit resolve with no Owner set'
+    );
+    $m->text_contains('Owner is required when changing Status to resolved');
+    $m->submit_form_ok(
+        {   form_name => 'TicketUpdate',
+            button    => 'SubmitTicket',
+            fields    => { Owner => $root->id }
+        },
+        'Submit resolve with Owner set'
+    );
+    $m->text_lacks('Owner is required when changing Status to resolved');
+    $m->text_contains('Owner changed from Nobody to root');
+    $m->text_contains("Status changed from 'new' to 'resolved'");
+}
+
+diag "Resolve ticket through Basics with required Owner";
+{
+    my $t = RT::Test->create_ticket(
+        Queue   => 'General',
+        Subject => 'Test Mandatory Owner On Resolve',
+        Content => 'Testing',
+    );
+    ok($t->id, 'Created ticket to resolve through Modify.html: ' . $t->id);
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'resolved', },
+            button    => 'SubmitTicket',
+        },
+        'Submit resolve with no Owner set'
+    );
+    $m->text_contains('Owner is required when changing Status to resolved');
+
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'resolved', Owner => $root->id, },
+            button    => 'SubmitTicket',
+        },
+        'Submit resolve with no Owner set'
+    );
+    $m->text_lacks('Owner is required when changing Status to resolved');
+    $m->text_contains('Owner changed from Nobody to root');
+    $m->text_contains("Status changed from 'new' to 'resolved'");
+}
+
+diag "Resolve ticket through Jumbo with required Owner";
+{
+    my $t = RT::Test->create_ticket(
+        Queue   => 'General',
+        Subject => 'Test Mandatory Owner On Resolve',
+        Content => 'Testing',
+    );
+    ok($t->id, 'Created ticket to resolve through ModifyAll.html: ' . $t->id);
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Jumbo' }, 'Get ModifyAll.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModifyAll',
+            fields    => { Status => 'resolved', },
+            button    => 'SubmitTicket',
+        },
+        'Submit resolve with no Owner set'
+    );
+    $m->text_contains('Owner is required when changing Status to resolved');
+
+    $m->submit_form_ok(
+        {   form_name => 'TicketModifyAll',
+            fields    => { Status => 'resolved', Owner => $root->id, },
+            button    => 'SubmitTicket',
+        },
+        'Submit resolve with no Owner set'
+    );
+    $m->text_lacks('Owner is required when changing Status to resolved');
+    $m->text_contains('Owner changed from Nobody to root');
+    $m->text_contains("Status changed from 'new' to 'resolved'");
+}
+
+diag "Test core role fields";
+{
+    my $role = qw/AdminCc/;
+    my $t = RT::Test->create_ticket(
+        Queue   => 'General',
+        Subject => 'Test Mandatory Owner On Resolve',
+        Content => 'Testing',
+    );
+    ok($t->id, 'Created test ticket: ' . $t->id);
+    my ($ret, $msg) = $t->SetStatus('open');
+    ok $ret, $msg;
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'stalled', },
+            button    => 'SubmitTicket',
+        },
+        "Submit stalled with no $role member"
+    );
+    $m->text_contains("A member of group Admins is required for role: $role");
+
+    my $role_group = $t->$role;
+    ok $role_group->Id;
+
+    my $root = RT::User->new(RT->SystemUser);
+    $root->Load('root');
+    ok($root->id, 'Loaded root');
+
+    ($ret, $msg) = $role_group->AddMember($root->PrincipalId);
+    ok $ret, $msg;
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'stalled', },
+            button    => 'SubmitTicket',
+        },
+        "Try to stall ticket with no Admins group created"
+    );
+    $m->text_contains("A member of group Admins is required for role: $role");
+
+    my $group = RT::Group->new(RT->SystemUser);
+    ($ret, $msg) = $group->CreateUserDefinedGroup(Name => 'Admins');
+    ok $ret, "Failed to create Admins group: $msg";
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'stalled', },
+            button    => 'SubmitTicket',
+        },
+        "Try to stall ticket with no $role but not a member of required group"
+    );
+    $m->text_contains("A member of group Admins is required for role: $role");
+
+    ($ret, $msg) = $group->AddMember($root->PrincipalId);
+    ok $ret, $msg;
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'stalled', },
+            button    => 'SubmitTicket',
+        },
+        "Try to stall ticket with $role and  group required"
+    );
+    $m->text_contains("Status changed from 'open' to 'stalled'");
+}
+
+diag "Test custom role mandatory fields";
+{
+    my $t = RT::Test->create_ticket(
+        Queue   => 'General',
+        Subject => 'Test Mandatory Owner On Resolve',
+        Content => 'Testing',
+    );
+    ok($t->id, 'Created test ticket: ' . $t->id);
+    my ($ret, $msg) = $t->SetStatus('open');
+    ok $ret, $msg;
+
+    my $id = $t->id;
+
+    my $customrole = RT::CustomRole->new(RT->SystemUser);
+    ($ret, $msg) = $customrole->Create(Name => 'vip');
+    ok $ret, $msg;
+
+    ($ret, $msg) = $customrole->Load('vip');
+    ok $ret, $msg;
+
+    ($ret, $msg) = $customrole->AddToObject($t->Queue);
+    ok $ret, $msg;
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'deleted', },
+            button    => 'SubmitTicket',
+        },
+        "Submit deleted with no value for required custom role"
+    );
+    $m->text_contains("vip is required when changing Status to deleted");
+
+    ($ret, $msg) = $t->AddWatcher(Type => $customrole->GroupType, Principal => $root->PrincipalObj);
+    ok $ret, $msg;
+
+    $m->goto_ticket($t->id);
+    $m->follow_link_ok({ text => 'Basics' }, 'Get Modify.html of ticket');
+    $m->submit_form_ok(
+        {   form_name => 'TicketModify',
+            fields    => { Status => 'deleted', },
+            button    => 'SubmitTicket',
+        },
+        "Submit deleted with manatory custom role requirements met."
+    );
+    $m->text_contains("Ticket deleted");
+}
+
+undef $m;
+done_testing;

commit 322e2aa6462d6bc08ccc448b5d0682c403d0c85d
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Fri Jan 11 11:37:10 2019 -0500

    Update README and Changelog

diff --git a/Changes b/Changes
index 5eb32fc..3f54ca7 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,8 @@
+0.17 2019-01-23
+ - Add support for custom roles as a mandatory field
+ - Add support for all RT core roles as a mandatory field
+ - Add support for requiring group membership for roles
+
 0.17 2018-10-26
  - Add support for Owner as a mandatory field
 
diff --git a/README b/README
index 10ec6f6..45c8fa4 100644
--- a/README
+++ b/README
@@ -36,10 +36,6 @@ DESCRIPTION
     TimeTaken
         Requires that the Worked field on the update page is non-zero.
 
-    Owner
-        Requires that the ticket has a real Owner or the real Owner will be
-        set on the update page.
-
     A larger set of basic fields may be supported in future releases. If
     you'd like to see additional fields added, please email your request to
     the bug address at the bottom of this documentation.
@@ -126,6 +122,26 @@ CONFIGURATION
     The transition syntax is similar to that found in RT's Lifecycles. See
     perldoc /opt/rt4/etc/RT_Config.pm.
 
+  Requiring role values
+    You can require any core or custom role on a RT::Ticket object, below is
+    an example of requiring a custom role "customer" be set on transition
+    from open and the owner also be set for the ticket on transition from a
+    status of open.
+
+    Set( %MandatoryOnTransition, 'General' => { '* -> resolved' =>
+    ['CustomRole.customer', 'Owner'] 'CustomRole.customer' => { transition
+    => 'open -> *' }, 'Owner' => { transition => 'open -> *' }, }, );
+
+  Role Membership in a Group
+    Roles can require the members of the role to also be a member of a group
+    before satisfying to mandatory condition. Below we require that the
+    Owner role be set and that the member it is set to is a member of the
+    group 'SupportReps' or 'Admins'.
+
+    Set( %MandatoryOnTransition, 'General' => { 'open -> *' => ['Owner'],
+    'Owner' => { transition => 'open -> *', group => ['SupportReps',
+    'Admins'] } } );
+
   Restrictions on Queue Transitions
     The default behavior for MandatoryOnTransition operates on status
     transitions, so a change from new to open or from open to resolved. It
diff --git a/lib/RT/Extension/MandatoryOnTransition.pm b/lib/RT/Extension/MandatoryOnTransition.pm
index 0a0d246..c5bb210 100644
--- a/lib/RT/Extension/MandatoryOnTransition.pm
+++ b/lib/RT/Extension/MandatoryOnTransition.pm
@@ -50,11 +50,6 @@ field on the update page.
 
 Requires that the Worked field on the update page is non-zero.
 
-=item Owner
-
-Requires that the ticket has a real Owner or the real Owner will be set on
-the update page.
-
 =back
 
 A larger set of basic fields may be supported in future releases.  If you'd
@@ -157,6 +152,34 @@ Category selection before resolving tickets in every other queue.
 The transition syntax is similar to that found in RT's Lifecycles.  See
 C<perldoc /opt/rt4/etc/RT_Config.pm>.
 
+=head2 Requiring role values
+
+You can require any core or custom role on a RT::Ticket object, below is an
+example of requiring a custom role "customer" be set on transition from open
+and the owner also be set for the ticket on transition from a status of open.
+
+Set( %MandatoryOnTransition,
+    'General' => {
+        '* -> resolved' => ['CustomRole.customer', 'Owner']
+        'CustomRole.customer' => { transition => 'open -> *' },
+        'Owner' => { transition => 'open -> *' },
+    },
+);
+
+=head2 Role Membership in a Group
+
+Roles can require the members of the role to also be a member of a group
+before satisfying to mandatory condition. Below we require that the Owner
+role be set and that the member it is set to is a member of the group
+'SupportReps' or 'Admins'.
+
+Set( %MandatoryOnTransition,
+    'General' => {
+        'open -> *' => ['Owner'],
+        'Owner' => { transition => 'open -> *', group => ['SupportReps', 'Admins'] }
+    }
+);
+
 =head2 Restrictions on Queue Transitions
 
 The default behavior for C<MandatoryOnTransition> operates on status transitions,

commit c9d1f69f82c05d0be3bc1d5d3509dfbbdf87ccda
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Thu Feb 14 11:07:45 2019 -0500

    Do not use 'WatcherType' style for adding watchers to ticket on update

diff --git a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked
index 4f5da6e..3953b24 100644
--- a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked
+++ b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/AfterWorked
@@ -17,16 +17,20 @@ if (!$m->comp_exists('/Elements/EditCustomFields')) {
     %obj_args = ( TicketObj => $Ticket );
 }
 
-my @roles;
+my (@roles, %crs);
 foreach my $role (@{$roles}) {
+    my $role_group = RT::Group->new($session{CurrentUser});
     if ( $role =~ s/^CustomRole\.//i ) {
         my $role_object = RT::CustomRole->new($session{CurrentUser});
         my ($ret, $msg) = $role_object->Load($role);
+        RT::Logger->error($msg) unless $ret;
 
+        ($ret, $msg) = $role_group->LoadRoleGroup(Object => $Ticket, Name => $role_object->GroupType);
         RT::Logger->error($msg) unless $ret;
-        push @roles, $role_object if $ret;
+        $crs{$role_group->Name} = { Id => $role_object->Id, Name => $role_object->Name };
+
+        push @roles, $role_group if $ret;
     } else {
-        my $role_group = RT::Group->new($session{CurrentUser});
         my ($ret, $msg) = $role_group->LoadRoleGroup(Object => $Ticket, Name => $role);
 
         RT::Logger->error($msg) unless $ret;
@@ -44,22 +48,20 @@ foreach my $role (@{$roles}) {
         &>
 % }
 % if ( @$roles ) {
-% my $count = 0;
 %     foreach my $role (@roles) {
 %     next if $role->Name eq 'Owner';
         <tr>
-            <td class="label"><% $role->Name %>:</td>
+            <td class="label"><% $role->Name =~ /CustomRole/ ? $crs{$role->Name}->{Name} : $role->Name %>:</td>
             <td class="entry">
-            <input type='hidden' name="WatcherTypeEmail<% $count %>" value=<% $role->Name %>></input>
                 <& /Elements/EmailInput,
-                    Name => grep ($role->Name eq $_, qw/Requestor AdminCc Cc/) ? "WatcherAddressEmail$count" : 'RT::CustomRole-' . $role->Id,
+                    Name => grep ($role->Name eq $_, qw/Requestor AdminCc Cc/) ? $role->Name : 'RT::CustomRole-' . $crs{$role->Name}->{Id},
                     Autocomplete       => 1,
                     AutocompleteNobody => 1,
-                    AutocompleteReturn => "Name",
+                    AutocompleteReturn => "Email",
                     Size               => 20,
+                    Default            => $role->MemberEmailAddressesAsString,
                 &>
             </td>
         </tr>
-%     $count++;
 %     }
 % }
diff --git a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate
index 91be5c0..d412a66 100644
--- a/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate
+++ b/html/Callbacks/RT-Extension-MandatoryOnTransition/Ticket/Update.html/BeforeUpdate
@@ -17,10 +17,24 @@ if (@$errors_ref) {
     push @$results, @$errors_ref;
 }
 
-# Allow mandatory roles to be set on update page
-foreach my $key (keys %{$ARGSRef}) {
-    if ( $key =~ /^WatcherAddressEmail(\d*)$/ && not $ARGSRef->{$key} ) {
-        $ARGSRef->{"WatcherTypeEmail$1"} = undef;
+# The update page does not look for core roles, this allows us to
+# set them if they are present.
+my %core_roles;
+for (keys %{$ARGSRef}, qw/AdminCc Requestor Cc/) {
+    $core_roles{$_}++;
+}
+if ( not @$errors_ref ) {
+    my @core_roles;
+    for (keys %core_roles) {
+        push @core_roles, $_ if $core_roles{$_} > 1;
+    }
+
+    my $role_group = RT::Group->new($session{CurrentUser});
+    foreach my $role (@core_roles) {
+        if ( not $TicketObj->IsWatcher(Type => $role, Email => $ARGSRef->{$role}) ) {
+            my ($ret, $msg) = $TicketObj->AddWatcher(Type => $role, Email => $ARGSRef->{$role});
+            RT::Logger->error($msg);
+        }
     }
 }
 </%init>
diff --git a/lib/RT/Extension/MandatoryOnTransition.pm b/lib/RT/Extension/MandatoryOnTransition.pm
index c5bb210..7e619ae 100644
--- a/lib/RT/Extension/MandatoryOnTransition.pm
+++ b/lib/RT/Extension/MandatoryOnTransition.pm
@@ -575,19 +575,6 @@ sub CheckMandatoryFields {
             }
 
             my @role_values;
-            # Use this to keep track of input fields that use a count increment
-            # example: WatcherAddressEmail1 or WatcherAddressEmail2
-            my @row_input_num = grep $role_arg eq $ARGSRef->{$_}, keys %{$ARGSRef};
-            map { push @role_values, $ARGSRef->{$_} if $ARGSRef->{$_} } grep /$role_arg/, keys %{$ARGSRef};
-
-            # Grab our arguments for current role
-            if (@row_input_num) {
-                map { push @role_values, $ARGSRef->{"WatcherAddressEmail$_"}
-                      if $ARGSRef->{"WatcherAddressEmail$_"}
-                  }
-                map { $_ =~ /^WatcherTypeEmail(\d*)$/; $1 } @row_input_num;
-            }
-
             # We could end up with a RT::Group option or a single value in the case of Owner
             if ( ref $role_values eq 'RT::Group' ) {
                 my @temp_array = $role_values->MemberEmailAddresses;
@@ -596,6 +583,7 @@ sub CheckMandatoryFields {
             else {
                 push @role_values, $role_values;
             }
+            push @role_values, $ARGSRef->{$role_arg} if $ARGSRef->{$role_arg};
 
             # Check for mandatory group configuration, supports multiple groups where only
             # one true case needs to be found.

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list