[Rt-commit] rt branch 5.0/load-by-user-cf created. rt-5.0.3-146-gc77c31b9ae

BPS Git Server git at git.bestpractical.com
Mon Nov 14 20:55:40 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/load-by-user-cf has been created
        at  c77c31b9ae453a3239b7a150bd085239070f52b9 (commit)

- Log -----------------------------------------------------------------
commit c77c31b9ae453a3239b7a150bd085239070f52b9
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Nov 14 15:44:35 2022 -0500

    Tests for loading users via UserCFs

diff --git a/t/externalauth/ldap.t b/t/externalauth/ldap.t
index ad304a7319..7ec21081c3 100644
--- a/t/externalauth/ldap.t
+++ b/t/externalauth/ldap.t
@@ -214,6 +214,54 @@ diag "test user update via login";
     );
 }
 
+diag 'Login with UserCF as username';
+
+RT::Test->stop_server();
+
+RT->Config->Set(
+    ExternalSettings => {    # AN EXAMPLE DB SERVICE
+        'My_LDAP' => {
+            'type'            => 'ldap',
+            'server'          => "127.0.0.1:$ldap_port",
+            'base'            => $base,
+            'filter'          => '(objectClass=*)',
+            'd_filter'        => '()',
+            'tls'             => 0,
+            'net_ldap_args'   => [ version => 3 ],
+            'attr_match_list' => [ 'UserCF.Employee ID', 'EmailAddress' ],
+            'attr_map'        => {
+                'Name'                 => 'uid',
+                'EmailAddress'         => 'mail',
+                'FreeformContactInfo'  => [ 'uid', 'mail' ],
+                'CF.Employee Type'     => 'employeeType',
+                'UserCF.Employee Type' => 'employeeType',
+                'UserCF.Employee ID'   => 'employeeID',
+            }
+        },
+    }
+);
+RT->Config->PostLoadCheck;
+
+( $baseurl, $m ) = RT::Test->started_ok();
+
+diag "test uri login";
+{
+    my $testuser = RT::User->new($RT::SystemUser);
+    my ($ok,$msg) = $testuser->Load( 'testuser' );
+    ok($ok,$msg);
+
+    # Reset employee ID to just id
+    $testuser->AddCustomFieldValue( Field => 'Employee ID', Value => '234' );
+    is( $testuser->FirstCustomFieldValue('Employee ID'), 234, 'Employee ID set to 234');
+
+    # Can't use usual login method because it checks for username and for this test,
+    # the username is not the user CF value we are sending
+
+    $m->get($baseurl . "?user=234;pass=password");
+    ok( $m->content =~ m/Logout/i, 'Logged in' );
+    ok( $m->logged_in_as('testuser'), 'Logged in as testuser' );
+}
+
 $ldap->unbind();
 
 done_testing;

commit d46dc0472e78de73afba5b7adc49be6f98370266
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Nov 14 15:42:40 2022 -0500

    Support loading users via user custom fields
    
    External auth allows admins to provide a list of attributes
    for users to use in the username box on login, like 'Name'
    or 'EmailAddress' and it will try both. 566b8fa1 added support
    for using UserCFs in mappings, but not as login attributes.
    
    This commit adds support for loading users with UserCFs, so
    configurations like this can now be used:
    
        'attr_match_list' => [
            'UserCF.Employee ID',
            'EmailAddress',
        ],

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index f7d28b09eb..39a18d3aea 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -668,6 +668,56 @@ sub LoadByEmail {
     return $self->LoadByCol( "EmailAddress", $address );
 }
 
+=head2 LoadByCols
+
+Custom LoadByCols that can try to load a user from a user custom field
+in the form 'UserCF.Foo' passed as the field. If passed a standard user
+field, loads using the normal LoadByCols.
+
+=cut
+
+sub LoadByCols {
+    my $self = shift;
+
+    # Don't shift off passed values, we may need to pass them along below
+    my $field = $_[0];
+    my $value = $_[1];
+    my ( $ret, $msg );
+
+    if ( my ($cf_name) = $field =~ /^UserCF\.(.+)$/i ) {
+        # A user custom field was passed, so try to find a user with that
+        my $user_obj = RT::User->new(RT->SystemUser);
+
+        my $cf_obj = RT::CustomField->new(RT->SystemUser);
+        my ($cf_ret, $cf_msg) = $cf_obj->LoadByName(
+            Name => $cf_name, LookupType => $user_obj->CustomFieldLookupType );
+        RT->Logger->warn("Unable to load custom field with name $cf_name: $cf_msg") unless $cf_ret;
+
+        my $ocfv_obj = RT::ObjectCustomFieldValue->new(RT->SystemUser);
+        my ($ocfv_ret, $ocfv_msg) = $ocfv_obj->LoadByCols(
+            ObjectType  => ref($user_obj),
+            CustomField => $cf_obj->Id,
+            Content     => $value,
+            Disabled    => 0,
+        );
+
+        if ( $ocfv_ret && $ocfv_obj->Id ) {
+            # Found a user with that user CF, so try to load it
+            ( $ret, $msg ) = $self->SUPER::LoadByCols( Id => $ocfv_obj->ObjectId );
+        }
+        else {
+            # No user CF found with that field/value
+            ( $ret, $msg ) = ( 0, "Couldn't find row" );
+        }
+    }
+    else {
+        # No user CF, normal load
+        ( $ret, $msg ) = $self->SUPER::LoadByCols( @_ );
+    }
+
+    return wantarray ? ( $ret, $msg ) : $ret;
+}
+
 =head2 LoadOrCreateByEmail ADDRESS
 
 Attempts to find a user who has the provided email address. If that fails, creates an unprivileged user with

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list