[Rt-commit] rt branch, 4.0/validate-user-name, created. rt-4.0.7-60-gebeed9f

? sunnavy sunnavy at bestpractical.com
Tue Sep 25 12:49:28 EDT 2012


The branch, 4.0/validate-user-name has been created
        at  ebeed9ffab81b944dff74e9422d07e9483a80b0e (commit)

- Log -----------------------------------------------------------------
commit ebeed9ffab81b944dff74e9422d07e9483a80b0e
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Sep 26 00:29:06 2012 +0800

    validate user name on both "create" and "update"
    
    see also #18722

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index 56a22cd..1cc5492 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -166,18 +166,10 @@ sub Create {
         return ( 0, $self->loc("Must specify 'Name' attribute") );
     }
 
-    #SANITY CHECK THE NAME AND ABORT IF IT'S TAKEN
-    if (RT->SystemUser) {   #This only works if RT::SystemUser has been defined
-        my $TempUser = RT::User->new(RT->SystemUser);
-        $TempUser->Load( $args{'Name'} );
-        return ( 0, $self->loc('Name in use') ) if ( $TempUser->Id );
-
-        my ($val, $message) = $self->ValidateEmailAddress( $args{'EmailAddress'} );
-        return (0, $message) unless ( $val );
-    } else {
-        $RT::Logger->warning( "$self couldn't check for pre-existing users");
-    }
-
+    my ( $val, $msg ) = $self->ValidateName( $args{'Name'} );
+    return ( 0, $msg ) unless $val;
+    ( $val, $msg ) = $self->ValidateEmailAddress( $args{'EmailAddress'} );
+    return ( 0, $msg ) unless ($val);
 
     $RT::Handle->BeginTransaction();
     # Groups deal with principal ids, rather than user ids.
@@ -269,6 +261,30 @@ sub Create {
     return ( $id, $self->loc('User created') );
 }
 
+=head2 ValidateName STRING
+
+Returns either (0, "failure reason") or 1 depending on whether the given
+name is valid.
+
+=cut
+
+sub ValidateName {
+    my $self = shift;
+    my $name = shift;
+
+    return ( 0, $self->loc('empty name') ) unless defined $name && length $name;
+
+    my $TempUser = RT::User->new( RT->SystemUser );
+    $TempUser->Load($name);
+
+    if ( $TempUser->id && ( !$self->id || $TempUser->id != $self->id ) ) {
+        return ( 0, $self->loc('Name in use') );
+    }
+    else {
+        return 1;
+    }
+}
+
 =head2 ValidatePassword STRING
 
 Returns either (0, "failure reason") or 1 depending on whether the given
@@ -571,6 +587,25 @@ sub ValidateEmailAddress {
     }
 }
 
+=head2 SetName
+
+Check to make sure someone else isn't using this name already
+
+=cut
+
+sub SetName {
+    my $self  = shift;
+    my $Value = shift;
+
+    my ( $val, $message ) = $self->ValidateName($Value);
+    if ($val) {
+        return $self->_Set( Field => 'Name', Value => $Value );
+    }
+    else {
+        return ( 0, $message );
+    }
+}
+
 =head2 SetEmailAddress
 
 Check to make sure someone else isn't using this email address already

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


More information about the Rt-commit mailing list