[Rt-devel] [PATCH] Call LookupExternalUserInfo() when creating or finding user

Petter Reinholdtsen pere at hungry.com
Sun Aug 29 16:46:50 EDT 2004


[Ruslan U. Zakirov]
> RT can't handle everything and LDAP is not only interface for external
> user lookups.

Sure.  But LDAP is fairly common, so it would be nice if it was
possible to enable LDAP synchronizing in the official version.

Anyway, by making sure the hook provided in LookupExternalUserInfo()
is used in the code, it is easier to add ones own synchronizing code
for LDAP, and it should make it easier to implement your idea of
making this configurable using RTx::Authen.

Here is the complete patch to enable this.  I've tested it at our test
installation of RT.

 - Move LookupExternalUserInfo() from RT::EmailParser to RT::User.  It
   is a more natural resting place for this hook.

 - Make LookupExternalUserInfo() a class function (and not a member
   function), as it should also be called before a user object is
   created.

 - Call LookupExternalUserInfo() when creating new users and when
   looking up users based on email From header info.

Please include this patch in a future version of RT, or let me know if
there is something with the patch making it unfit for inclusion in the
official source.

Index: lib/RT/EmailParser.pm
===================================================================
--- lib/RT/EmailParser.pm	(revision 1372)
+++ lib/RT/EmailParser.pm	(working copy)
@@ -267,70 +267,6 @@
 
 # }}}
 
-# {{{ LookupExternalUserInfo
-
-
-# LookupExternalUserInfo is a site-definable method for synchronizing
-# incoming users with an external data source. 
-#
-# This routine takes a tuple of EmailAddress and FriendlyName
-#   EmailAddress is the user's email address, ususally taken from
-#       an email message's From: header.
-#   FriendlyName is a freeform string, ususally taken from the "comment" 
-#       portion of an email message's From: header.
-#
-# If you define an AutoRejectRequest template, RT will use this   
-# template for the rejection message.
-
-
-=item LookupExternalUserInfo
-
- LookupExternalUserInfo is a site-definable method for synchronizing
- incoming users with an external data source. 
-
- This routine takes a tuple of EmailAddress and FriendlyName
-    EmailAddress is the user's email address, ususally taken from
-        an email message's From: header.
-    FriendlyName is a freeform string, ususally taken from the "comment" 
-        portion of an email message's From: header.
-
- It returns (FoundInExternalDatabase, ParamHash);
-
-   FoundInExternalDatabase must  be set to 1 before return if the user was
-   found in the external database.
-
-   ParamHash is a Perl parameter hash which can contain at least the following
-   fields. These fields are used to populate RT's users database when the user 
-   is created
-
-    EmailAddress is the email address that RT should use for this user.  
-    Name is the 'Name' attribute RT should use for this user. 
-         'Name' is used for things like access control and user lookups.
-    RealName is what RT should display as the user's name when displaying 
-         'friendly' names
-
-=cut
-
-sub LookupExternalUserInfo {
-  my $self = shift;
-  my $EmailAddress = shift;
-  my $RealName = shift;
-
-  my $FoundInExternalDatabase = 1;
-  my %params;
-
-  #Name is the RT username you want to use for this user.
-  $params{'Name'} = $EmailAddress;
-  $params{'EmailAddress'} = $EmailAddress;
-  $params{'RealName'} = $RealName;
-
-  # See RT's contributed code for examples.
-  # http://www.fsck.com/pub/rt/contrib/
-  return ($FoundInExternalDatabase, %params);
-}
-
-# }}}
-
 # {{{ Accessor methods for parsed email messages
 
 =head2 Head
Index: lib/RT/User_Overlay.pm
===================================================================
--- lib/RT/User_Overlay.pm	(revision 1372)
+++ lib/RT/User_Overlay.pm	(working copy)
@@ -107,6 +107,55 @@
 
 # }}}
 
+# {{{ LookupExternalUserInfo
+
+=item LookupExternalUserInfo
+
+ LookupExternalUserInfo is a site-definable method for synchronizing
+ incoming users with an external data source. 
+
+ This routine takes a tuple of EmailAddress and FriendlyName
+   EmailAddress is the user's email address, ususally taken from
+       an email message's From: header.
+   RealName is a freeform string, ususally taken from the "comment" 
+       portion of an email message's From: header.
+
+ It returns (FoundInExternalDatabase, ParamHash);
+
+   FoundInExternalDatabase must be set to 1 before return if the user
+   was found in the external database.
+
+   ParamHash is a Perl parameter hash which can contain at least the
+   following fields. These fields are used to populate RT's users
+   database when the user is created
+
+     EmailAddress is the email address that RT should use for this user.  
+     Name is the 'Name' attribute RT should use for this user. 
+         'Name' is used for things like access control and user lookups.
+     RealName is what RT should display as the user's name when displaying 
+         'friendly' names
+
+=cut
+
+sub LookupExternalUserInfo {
+  my $EmailAddress = shift;
+  my $RealName = shift;
+
+  my $FoundInExternalDatabase = 1;
+  my %params;
+
+  #Name is the RT username you want to use for this user.
+  $params{'Name'} = $EmailAddress;
+  $params{'EmailAddress'} = $EmailAddress;
+  $params{'RealName'} = $RealName;
+
+  # See RT's contributed code for examples.
+  # http://www.fsck.com/pub/rt/contrib/
+  return ($FoundInExternalDatabase, %params);
+}
+
+# }}}
+
 # {{{ sub Create 
 
 =head2 Create { PARAMHASH }
@@ -701,6 +750,14 @@
     my $args = shift;
     my $success = 1;
 
+    my ($UserFoundInExternalDatabase, %ExternalUserInfo) =
+	LookupExternalUserInfo($argsref->{'EmailAddress'},
+			       $argsref->{'RealName'});
+    if ($UserFoundInExternalDatabase) {
+        for my $key (keys %ExternalUserInfo) {
+            $argsref->{$key} = $ExternalUserInfo{$key};
+        }
+    }
     return ($success);
 }
 
Index: lib/RT/Interface/Email/Auth/MailFrom.pm
===================================================================
--- lib/RT/Interface/Email/Auth/MailFrom.pm	(revision 1372)
+++ lib/RT/Interface/Email/Auth/MailFrom.pm	(working copy)
@@ -58,12 +58,26 @@
                  @_ );
 
 
-    # We don't need to do any external lookups
-    my ( $Address, $Name ) = ParseSenderAddressFromHead( $args{'Message'}->head );
-    my $CurrentUser = RT::CurrentUser->new();
-    $CurrentUser->LoadByEmail($Address);
+    my $Name = undef;
+    my ( $Address, $RealName ) = ParseSenderAddressFromHead( $args{'Message'}->head );
 
+    my ($UserFoundInExternalDatabase, %ExternalUserInfo) =
+	RT::User::LookupExternalUserInfo( $Address, $Name );
+    if ($UserFoundInExternalDatabase) {
+	$Name     = $ExternalUserInfo{'Name'} if ($ExternalUserInfo{'Name'});
+	$Address  = $ExternalUserInfo{'EmailAddress'} if ($ExternalUserInfo{'EmailAddress'});
+	$RealName = $ExternalUserInfo{'RealName'} if ($ExternalUserInfo{'RealName'});
+    }
+
+    $CurrentUser = RT::CurrentUser->new();
+
+    # Try user name first if it was fetched from external source
+    $CurrentUser->LoadByName($Name) if $Name;
+
     unless ( $CurrentUser->Id ) {
+	$CurrentUser->LoadByEmail($Address);
+    }
+    unless ( $CurrentUser->Id ) {
         $CurrentUser->LoadByName($Address);
     }
 
@@ -139,7 +153,7 @@
 
     }
 
-    $CurrentUser = CreateUser( undef, $Address, $Name, $Address, $args{'Message'} );
+    $CurrentUser = CreateUser( $Name, $Address, $RealName, $Address, $args{'Message'} );
 
     return ( $CurrentUser, 1 );
 }


More information about the Rt-devel mailing list