[Jifty-commit] r5821 - jifty/trunk/lib/Jifty/Plugin/Authentication/Password/Action

Jifty commits jifty-commit at lists.jifty.org
Fri Sep 12 13:58:20 EDT 2008


Author: sunnavy
Date: Fri Sep 12 13:58:20 2008
New Revision: 5821

Modified:
   jifty/trunk/lib/Jifty/Plugin/Authentication/Password/Action/Login.pm

Log:
refactor a bit, limit only one login way, either by email or by username

Modified: jifty/trunk/lib/Jifty/Plugin/Authentication/Password/Action/Login.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Authentication/Password/Action/Login.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Authentication/Password/Action/Login.pm	Fri Sep 12 13:58:20 2008
@@ -13,6 +13,21 @@
 
 use constant TOKEN_EXPIRE_TIME => 30;
 
+__PACKAGE__->mk_accessors( 'login_by' );
+
+=head2 new
+
+=cut
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new(@_);
+    my $plugin =
+        Jifty->find_plugin('Jifty::Plugin::Authentication::Password');
+    $self->login_by( $plugin->{login_by} || 'email' );
+    return $self;
+}
+
 =head2 arguments
 
 Return the email and password form fields
@@ -20,14 +35,30 @@
 =cut
 
 sub arguments { 
-    return( { email => { label => _('Email'),
-                           mandatory => 0,
-                           ajax_validates => 1,
-                            },
-              username => { label => _('Username'),
-                           mandatory => 0,
-                           ajax_validates => 1,
-                            },
+    my $self = shift;
+
+    my $login_para;
+    if ( $self->login_by eq 'username' ) {
+        $login_para = {
+            username => {
+                label          => _( 'Username' ),
+                mandatory      => 1,
+                ajax_validates => 1,
+            }
+        };
+    }
+    else {
+        $login_para = {
+            email => {
+                label          => _( 'Email' ),
+                mandatory      => 1,
+                ajax_validates => 1,
+            }
+        };
+    }
+
+    return( { 
+              %$login_para,
 
               password => { type => 'password',
                             label => _('Password'),
@@ -62,12 +93,7 @@
     my $self  = shift;
     my $email = shift;
 
-    unless ( $self->_validate_email_or_username(email => $email) ) {
-        return 1;
-    }
-    else {
-        return $self->validate_username( $self->argument_value('username') );
-    }
+    return $self->_validate_email_or_username(email => $email);
 }
 
 =head2 validate_username ADDRESS
@@ -91,7 +117,7 @@
     my $value = shift;
 
     if ($value) {
-        my $u = $self->load_user($value);
+        my $u = $self->load_user( $value);
         return $self->validation_error(
             $name => _("It doesn't look like there's an account by that name.")
         ) unless ( $u->id );
@@ -165,9 +191,7 @@
 
 sub take_action {
     my $self = shift;
-    my $user =
-      $self->load_user( $self->argument_value('email')
-          || $self->argument_value('username') );
+    my $user = $self->load_user( $self->argument_value( $self->login_by ) );
     my $password = $self->argument_value('password');
     my $token    = $self->argument_value('token') || '';
     my $hashedpw = $self->argument_value('hashed_password');
@@ -211,10 +235,13 @@
 
 sub load_user {
     my $self = shift;
-    my $username = shift;
+    my $value = shift;
     my $user = Jifty->app_class('Model', 'User')->new(current_user => Jifty->app_class('CurrentUser')->superuser);
-    $user->load_by_cols( email => $username);
-    return $user
+
+    # normally we use name as column name instead of usernmae
+    my $column = $self->login_by eq 'username' ? 'name' : $self->login_by;
+    $user->load_by_cols( $column => $value );
+    return $user;
 
 }
 


More information about the Jifty-commit mailing list