Added Catalyst::Plugin::Authentication::Credential::NoPassword
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
index 08256fc..98415ce 100644 (file)
@@ -12,7 +12,7 @@ use Tie::RefHash;
 use Class::Inspector;
 use Catalyst::Authentication::Realm;
 
-our $VERSION = "0.10015";
+our $VERSION = "0.10018";
 
 sub set_authenticated {
     my ( $c, $user, $realmname ) = @_;
@@ -53,8 +53,8 @@ sub user {
 # change this to allow specification of a realm - to verify the user is part of that realm
 # in addition to verifying that they exist.
 sub user_exists {
-       my $c = shift;
-       return defined($c->_user) || defined($c->find_realm_for_persisted_user);
+    my $c = shift;
+    return defined($c->_user) || defined($c->find_realm_for_persisted_user);
 }
 
 # works like user_exists - except only returns true if user
@@ -227,7 +227,7 @@ sub _authentication_initialize {
     $app->mk_classdata( '_auth_realm_restore_order' => []);
 
     my $cfg = $app->config->{'Plugin::Authentication'};
-       my $realmshash;
+    my $realmshash;
     if (!defined($cfg)) {
         if (exists($app->config->{'authentication'})) {
             $cfg = $app->config->{'authentication'};
@@ -236,16 +236,16 @@ sub _authentication_initialize {
             $cfg = {};
         }
     } else {
-               # the realmshash contains the various configured realms.  By default this is
-               # the main $app->config->{'Plugin::Authentication'} hash - but if that is
-               # not defined, or there is a subkey {'realms'} then we use that.
-               $realmshash = $cfg;
-       }
+        # the realmshash contains the various configured realms.  By default this is
+        # the main $app->config->{'Plugin::Authentication'} hash - but if that is
+        # not defined, or there is a subkey {'realms'} then we use that.
+        $realmshash = $cfg;
+    }
 
-       ## If we have a sub-key of {'realms'} then we use that for realm configuration
-       if (exists($cfg->{'realms'})) {
-               $realmshash = $cfg->{'realms'};
-       }
+    ## If we have a sub-key of {'realms'} then we use that for realm configuration
+    if (exists($cfg->{'realms'})) {
+        $realmshash = $cfg->{'realms'};
+    }
 
     # old default was to force use_session on.  This must remain for that
     # reason - but if use_session is already in the config, we respect its setting.
@@ -262,16 +262,16 @@ sub _authentication_initialize {
 
         foreach my $realm (sort keys %{$realmshash}) {
             if (ref($realmshash->{$realm}) eq 'HASH' &&
-                               (exists($realmshash->{$realm}{credential}) || exists($realmshash->{$realm}{class}))) {
+                (exists($realmshash->{$realm}{credential}) || exists($realmshash->{$realm}{class}))) {
 
-                   $app->setup_auth_realm($realm, $realmshash->{$realm});
+                $app->setup_auth_realm($realm, $realmshash->{$realm});
 
-                   if (exists($realmshash->{$realm}{'user_restore_priority'})) {
-                       $auth_restore_order{$realm} = $realmshash->{$realm}{'user_restore_priority'};
-                   } else {
-                       $auth_restore_order{$realm} = $authcount++;
-                   }
-                       }
+                if (exists($realmshash->{$realm}{'user_restore_priority'})) {
+                    $auth_restore_order{$realm} = $realmshash->{$realm}{'user_restore_priority'};
+                } else {
+                    $auth_restore_order{$realm} = $authcount++;
+                }
+            }
         }
 
         # if we have a 'default_realm' in the config hash and we don't already
@@ -308,7 +308,6 @@ sub _authentication_initialize {
             my $realmcfg = {
                 store => { class => $cfg->{'stores'}{$storename} },
             };
-            print STDERR "Foo, ok?\n";
             $app->setup_auth_realm($storename, $realmcfg);
         }
     }
@@ -635,19 +634,19 @@ This means that our application will begin like this:
                         },
                         store => {
                             class => 'Minimal',
-                               users => {
-                                   bob => {
-                                       password => "s00p3r",                   
-                                       editor => 'yes',
-                                       roles => [qw/edit delete/],
-                                   },
-                                   william => {
-                                       password => "s3cr3t",
-                                       roles => [qw/comment/],
-                                   }
-                               }       
-                           }
-                       }
+                            users => {
+                                bob => {
+                                    password => "s00p3r",
+                                    editor => 'yes',
+                                    roles => [qw/edit delete/],
+                                },
+                                william => {
+                                    password => "s3cr3t",
+                                    roles => [qw/comment/],
+                                }
+                            }
+                        }
+                    }
                 }
     );
 
@@ -756,10 +755,10 @@ changing your config:
                             },
                             store => {
                                 class => 'DBIx::Class',
-                                   user_class => 'MyApp::Users',
-                                   role_column => 'roles'      
-                               }
-                       }
+                                user_model => 'MyApp::Users',
+                                role_column => 'roles',
+                            }
+                        }
                     }
     );
 
@@ -782,21 +781,21 @@ new source. The rest of your application is completely unchanged.
                         },
                         store => {
                             class => 'DBIx::Class',
-                           user_class => 'MyApp::Users',
-                           role_column => 'roles'      
-                       }
-                       },
-                       admins => {
-                           credential => {
-                               class => 'Password',
-                               password_field => 'password',
+                            user_model => 'MyApp::Users',
+                            role_column => 'roles',
+                        }
+                    },
+                    admins => {
+                        credential => {
+                            class => 'Password',
+                            password_field => 'password',
                             password_type => 'clear'
-                           },
-                           store => {
-                               class => '+MyApp::Authentication::Store::NetAuth',
-                               authserver => '192.168.10.17'
-                           }
-                       }
+                        },
+                        store => {
+                            class => '+MyApp::Authentication::Store::NetAuth',
+                            authserver => '192.168.10.17'
+                        }
+                    }
                 }
     );
 
@@ -864,14 +863,20 @@ default realm is checked.
 =head2 $c->user( )
 
 Returns the currently logged in user, or undef if there is none.
+Normally the user is re-retrieved from the store.
+For L<Catalyst::Authentication::Store::DBIx::Class> the user is re-restored 
+using the primary key of the user table. 
+Thus B<user> can throw an error even though B<user_exists>
+returned true.
 
 =head2 $c->user_exists( )
 
 Returns true if a user is logged in right now. The difference between
-user_exists and user is that user_exists will return true if a user is logged
+B<user_exists> and B<user> is that user_exists will return true if a user is logged
 in, even if it has not been yet retrieved from the storage backend. If you only
 need to know if the user is logged in, depending on the storage mechanism this
 can be much more efficient.
+B<user_exists> only looks into the session while B<user> is trying to restore the user.
 
 =head2 $c->user_in_realm( $realm )
 
@@ -880,13 +885,17 @@ logged in right now and was retrieved from the realm provided.
 
 =head2 $c->logout( )
 
-Logs the user out. Deletes the currently logged in user from C<< $c->user >> and the session.
+Logs the user out. Deletes the currently logged in user from C<< $c->user >>
+and the session.  It does not delete the session.
 
 =head2 $c->find_user( $userinfo, $realm )
 
 Fetch a particular users details, matching the provided user info, from the realm
 specified in $realm.
 
+    $user = $c->find_user({ id => $id });
+    $c->set_authenticated($user); # logs the user in and calls persist_user
+
 =head2 persist_user()
 
 Under normal circumstances the user data is only saved to the session during
@@ -911,7 +920,8 @@ for reference.
 =head2 $c->set_authenticated( $user, $realmname )
 
 Marks a user as authenticated. This is called from within the authenticate
-routine when a credential returns a user. $realmname defaults to 'default'
+routine when a credential returns a user. $realmname defaults to 'default'.
+You can use find_user to get $user
 
 =head2 $c->auth_restore_user( $user, $realmname )
 
@@ -1077,7 +1087,7 @@ This is set to C<< $c->config( 'Plugin::Authentication' => { store => # Store} )
 or by using a Store plugin:
 
     # load the Minimal authentication store.
-       use Catalyst qw/Authentication Authentication::Store::Minimal/;
+    use Catalyst qw/Authentication Authentication::Store::Minimal/;
 
 Sets the default store to
 L<Catalyst::Plugin::Authentication::Store::Minimal>.
@@ -1118,11 +1128,20 @@ kmx
 
 Nigel Metheringham
 
+Florian Ragwitz C<rafl@debian.org>
+
+Stephan Jauernick C<stephanj@cpan.org>
+
+Oskari Ojala (Okko), C<perl@okko.net>
+
 =head1 COPYRIGHT & LICENSE
 
-        Copyright (c) 2005 the aforementioned authors. All rights
-        reserved. This program is free software; you can redistribute
-        it and/or modify it under the same terms as Perl itself.
+Copyright (c) 2005 - 2011
+the Catalyst::Plugin::Authentication L</AUTHORS>
+as listed above.
+
+This program is free software; you can redistribute
+it and/or modify it under the same terms as Perl itself.
 
 =cut