X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2F05_Authentication.pod;h=358c3988a11268a9954aca52dc6588c1f6c8bcbe;hp=777913c4bf138db4b0745d45402f688ba91a78b1;hb=ddfbd8506f744fe1dc407bdaa7a1e9211b2dd1d7;hpb=c12b0d35e3f85926c6cfd873fdfb08496b3770fd diff --git a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod index 777913c..358c398 100644 --- a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod +++ b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod @@ -81,6 +81,7 @@ add the role information here although it will not be used until the authorization section, Chapter 6). Create a new SQL script file by opening C in your editor and insert: + PRAGMA foreign_keys = ON; -- -- Add user and role tables, along with a many-to-many join table -- @@ -98,8 +99,8 @@ C in your editor and insert: role TEXT ); CREATE TABLE user_role ( - user_id INTEGER, - role_id INTEGER, + user_id INTEGER REFERENCES user(id) ON DELETE CASCADE ON UPDATE CASCADE, + role_id INTEGER REFERENCES role(id) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (user_id, role_id) ); -- @@ -127,7 +128,8 @@ the new tables added in the previous step, let's use the C option on the DBIC model helper to do most of the work for us: $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ - create=static components=TimeStamp dbi:SQLite:myapp.db + create=static components=TimeStamp dbi:SQLite:myapp.db \ + on_connect_do="PRAGMA foreign_keys = ON" exists "/root/dev/MyApp/script/../lib/MyApp/Model" exists "/root/dev/MyApp/script/../t" Dumping manual schema for MyApp::Schema to directory /root/dev/MyApp/script/../lib ... @@ -151,63 +153,18 @@ DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and the closing C<1;>: C: - # - # Set relationships: - # - - # has_many(): - # args: - # 1) Name of relationship, DBIC will create accessor with this name - # 2) Name of the model class referenced by this relationship - # 3) Column name in *foreign* table (aka, foreign key in peer table) - __PACKAGE__->has_many(map_user_roles => 'MyApp::Schema::Result::UserRole', 'user_id'); - + # many_to_many(): # args: # 1) Name of relationship, DBIC will create accessor with this name # 2) Name of has_many() relationship this many_to_many() is shortcut for # 3) Name of belongs_to() relationship in model class of has_many() above # You must already have the has_many() defined to use a many_to_many(). - __PACKAGE__->many_to_many(roles => 'map_user_roles', 'role'); - - -C: - - # - # Set relationships: - # - - # has_many(): - # args: - # 1) Name of relationship, DBIC will create accessor with this name - # 2) Name of the model class referenced by this relationship - # 3) Column name in *foreign* table (aka, foreign key in peer table) - __PACKAGE__->has_many(map_user_roles => 'MyApp::Schema::Result::UserRole', 'role_id'); + __PACKAGE__->many_to_many(roles => 'user_roles', 'role'); -C: - - # - # Set relationships: - # - - # belongs_to(): - # args: - # 1) Name of relationship, DBIC will create accessor with this name - # 2) Name of the model class referenced by this relationship - # 3) Column name in *this* table - __PACKAGE__->belongs_to(user => 'MyApp::Schema::Result::User', 'user_id'); - - # belongs_to(): - # args: - # 1) Name of relationship, DBIC will create accessor with this name - # 2) Name of the model class referenced by this relationship - # 3) Column name in *this* table - __PACKAGE__->belongs_to(role => 'MyApp::Schema::Result::Role', 'role_id'); - -The code for these three sets of updates is obviously very similar to -the edits we made to the C, C, and C -classes created in Chapter 3. +The code for this update is obviously very similar to the edits we made to the +C and C classes created in Chapter 3. Note that we do not need to make any change to the C schema file. It simply tells DBIC to load all @@ -287,17 +244,17 @@ configuration (see below). Make sure you include the additional plugins as new dependencies in the Makefile.PL file something like this: - requires ( - 'Catalyst::Plugin::Authentication' => '0', - 'Catalyst::Plugin::Session' => '0', - 'Catalyst::Plugin::Session::Store::FastMmap' => '0', - 'Catalyst::Plugin::Session::State::Cookie' => '0', - ); + requires 'Catalyst::Plugin::Authentication'; + requires 'Catalyst::Plugin::Session'; + requires 'Catalyst::Plugin::Session::Store::FastMmap'; + requires 'Catalyst::Plugin::Session::State::Cookie'; Note that there are several options for L -(L -is generally a good choice if you are on Unix; try + +(L or +L is +generally a good choice if you are on Unix; try L if you are on Win32) -- consult L and its subclasses @@ -388,11 +345,11 @@ and update the definition of C to match: my ($self, $c) = @_; # Get the username and password from form - my $username = $c->request->params->{username} || ""; - my $password = $c->request->params->{password} || ""; + my $username = $c->request->params->{username}; + my $password = $c->request->params->{password}; # If the username and password values were found in form - if (defined($username) && defined($password)) { + if ($username && $password) { # Attempt to log the user in if ($c->authenticate({ username => $username, password => $password } )) { @@ -404,6 +361,9 @@ and update the definition of C to match: # Set an error message $c->stash->{error_msg} = "Bad username or password."; } + } else { + # Set an error message + $c->stash->{error_msg} = "Empty username or password."; } # If either of above don't work out, send to the login page @@ -505,7 +465,7 @@ the following method: # Note that 'auto' runs after 'begin' but before your actions and that # 'auto's "chain" (all from application path to most specific class are run) # See the 'Actions' section of 'Catalyst::Manual::Intro' for more info. - sub auto : Private { + sub auto :Private { my ($self, $c) = @_; # Allow unauthenticated users to reach the login page. This @@ -659,14 +619,15 @@ saw in Chapters 3 and 4, but add C<,EncodedColumn> to the C argument: $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ - create=static components=TimeStamp,EncodedColumn dbi:SQLite:myapp.db + create=static components=TimeStamp,EncodedColumn dbi:SQLite:myapp.db \ + on_connect_do="PRAGMA foreign_keys = ON" If you then open one of the Result Classes, you will see that it includes EncodedColumn in the C line. Take a look at C since that's the main class where we want to use hashed and salted passwords: - __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp", "EncodedColumn", "Core"); + __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp", "EncodedColumn"); =head2 Modify the "password" Column to Use EncodedColumn @@ -884,7 +845,7 @@ C<__PACKAGE__-Econfig> setting to something like: __PACKAGE__->config( name => 'MyApp', - session => {flash_to_stash => 1}, + session => { flash_to_stash => 1 }, ); B add the following to C: