[Rt-commit] rt branch, 4.4/external-auth-on-user-create, created. rt-4.4.4-41-g8575f4ddd

Jim Brandt jbrandt at bestpractical.com
Fri May 10 15:45:44 EDT 2019


The branch, 4.4/external-auth-on-user-create has been created
        at  8575f4ddd63795ab10cb26a148a6d8f36c7ecd55 (commit)

- Log -----------------------------------------------------------------
commit e1afd2e7b88da4321aeb635bd8129aa69bd3c0f3
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri May 10 15:14:36 2019 -0400

    Add support for setting user CFs on create
    
    This was previously added in the context of ExternalAuth only.
    Move the core functionality to RT::User while keeping the
    update feature in ExternalAuth.

diff --git a/lib/RT/Authen/ExternalAuth.pm b/lib/RT/Authen/ExternalAuth.pm
index b047e57cf..bf08f8e76 100644
--- a/lib/RT/Authen/ExternalAuth.pm
+++ b/lib/RT/Authen/ExternalAuth.pm
@@ -605,7 +605,7 @@ sub UpdateUserInfo {
         for @results;
 
     AddCustomFieldValue( %args );
-    UpdateObjectCustomFieldValues( %args, Object => $UserObj );
+    $UserObj->UpdateObjectCustomFieldValues( %args );
 
     # Confirm update success
     $updated = 1;
@@ -751,37 +751,6 @@ sub AddCustomFieldValue {
     return;
 }
 
-sub UpdateObjectCustomFieldValues {
-    my %args   = @_;
-    my $object = $args{Object};
-    foreach my $rtfield ( sort keys %args ) {
-        next unless $rtfield =~ /^UserCF\.(.+)$/i;
-        my $cf_name = $1;
-        my $value   = $args{$rtfield};
-        $value = '' unless defined $value;
-
-        my $current = $object->FirstCustomFieldValue($cf_name);
-        $current = '' unless defined $current;
-
-        if ( not length $current and not length $value ) {
-            $RT::Logger->debug("\tCF.$cf_name\tskipping, no value in RT and external source");
-            next;
-        }
-        elsif ( $current eq $value ) {
-            $RT::Logger->debug("\tCF.$cf_name\tunchanged => $value");
-            next;
-        }
-
-        $current = 'unset' unless length $current;
-        $RT::Logger->debug("\tCF.$cf_name\t$current => $value");
-
-        my ( $ok, $msg ) = $object->AddCustomFieldValue( Field => $cf_name, Value => $value );
-        $RT::Logger->error( $object->Name . ": Couldn't add value '$value' for '$cf_name': $msg" )
-          unless $ok;
-    }
-    return;
-}
-
 RT::Base->_ImportOverlays();
 
 1;
diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index e60c0b429..b6ea8a796 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -108,7 +108,13 @@ sub _OverlayAccessible {
 
 =head2 Create { PARAMHASH }
 
+Create accepts all core RT::User fields (Name, EmailAddress, etc.) and
+user custom fields in the form UserCF.Foo where Foo is the name of the
+custom field.
 
+    my ($ret, $msg) = $user->Create( Name => 'mycroft',
+                                     EmailAddress => 'mycroft at example.com',
+                                     UserCF.Relationship => 'Brother' );
 
 =cut
 
@@ -196,6 +202,9 @@ sub Create {
         return ( 0, $self->loc('Could not create user') );
     }
 
+    # Handle any user CFs
+    $self->UpdateObjectCustomFieldValues( %args );
+
     my $aclstash = RT::Group->new($self->CurrentUser);
     my $stash_id = $aclstash->_CreateACLEquivalenceGroup($principal);
 
@@ -257,6 +266,44 @@ sub Create {
     return ( $id, $self->loc('User created') );
 }
 
+=head2 UpdateObjectCustomFieldValues
+
+Set User CFs from incoming args in the form UserCF.Foo.
+
+=cut
+
+sub UpdateObjectCustomFieldValues {
+    my $self = shift;
+    my %args   = @_;
+
+    foreach my $rtfield ( sort keys %args ) {
+        next unless $rtfield =~ /^UserCF\.(.+)$/i;
+        my $cf_name = $1;
+        my $value   = $args{$rtfield};
+        $value = '' unless defined $value;
+
+        my $current = $self->FirstCustomFieldValue($cf_name);
+        $current = '' unless defined $current;
+
+        if ( not length $current and not length $value ) {
+            $RT::Logger->debug("\tCF.$cf_name\tskipping, no value provided");
+            next;
+        }
+        elsif ( $current eq $value ) {
+            $RT::Logger->debug("\tCF.$cf_name\tunchanged => $value");
+            next;
+        }
+
+        $current = 'unset' unless length $current;
+        $RT::Logger->debug("\tCF.$cf_name\t$current => $value");
+
+        my ( $ok, $msg ) = $self->AddCustomFieldValue( Field => $cf_name, Value => $value );
+        $RT::Logger->error( $self->Name . ": Couldn't add value '$value' for '$cf_name': $msg" )
+          unless $ok;
+    }
+    return;
+}
+
 =head2 ValidateName STRING
 
 Returns either (0, "failure reason") or 1 depending on whether the given

commit 7aa3edf8a97547071f3c4d99b83b70c2775fd0d0
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri May 10 15:20:52 2019 -0400

    Add tests for setting user CFs on create

diff --git a/t/api/user.t b/t/api/user.t
index 94494f162..06ef2bf68 100644
--- a/t/api/user.t
+++ b/t/api/user.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 use RT;
-use RT::Test tests => 122;
+use RT::Test tests => undef;
 
 
 {
@@ -84,6 +84,28 @@ my $u10 = RT::User->new(RT->SystemUser);
 ok (!$id, $msg);
 RT->Config->Set('ValidateUserEmailAddresses' => undef);
 
+# Set User CFs on create
+my $cf = RT::CustomField->new(RT->SystemUser);
+ok($cf, "Have a CustomField object");
+
+# Use the old Queue field to set up a ticket CF
+($id, $msg) =  $cf->Create(
+    Name        => 'Testing CF',
+    Description => 'A Testing custom field',
+    Type        => 'Freeform',
+    MaxValues   => 1,
+    LookupType  => RT::User->CustomFieldLookupType,
+);
+ok($id, 'User custom field correctly created');
+ok( $cf->AddToObject( RT::User->new( RT->SystemUser ) ), 'applied Testing CF globally' );
+
+my $u11 = RT::User->new(RT->SystemUser);
+($id, $msg) = $u11->Create( Name => 'CreateTest11'.$$,
+                            EmailAddress => $$.'create-test10 at .com',
+                            'UserCF.Testing CF' => 'Testing' );
+ok ($id, $msg);
+is ( $u11->FirstCustomFieldValue('Testing CF'), 'Testing', 'Got Testing for Testing CF');
+
 }
 
 {
@@ -362,3 +384,4 @@ ok($rqv, "Revoked the right successfully - $rqm");
     is $marks[0], $b_ticket->id;
 }
 
+done_testing();

commit 8575f4ddd63795ab10cb26a148a6d8f36c7ecd55
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri May 10 15:38:46 2019 -0400

    Fix pod warnings in RT::User docs

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index b6ea8a796..c1dc4f490 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -48,7 +48,7 @@
 
 =head1 NAME
 
-  RT::User - RT User object
+RT::User - RT User object
 
 =head1 SYNOPSIS
 
@@ -56,6 +56,8 @@
 
 =head1 DESCRIPTION
 
+Object to operate on a single RT user record.
+
 =head1 METHODS
 
 =cut

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


More information about the rt-commit mailing list