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=62dab984fc968de087c5eb1635d2e5398e45a72d;hp=2456ca48e4e3b721d180be7f509da093bf933917;hb=b66dd084987c7d2a8612ed3b3d7a9f9b8a33887d;hpb=444d6b277933a652eb38bbeae072dbdfbe47c1c8 diff --git a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod index 2456ca4..62dab98 100644 --- a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod +++ b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod @@ -98,8 +98,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 +127,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 +152,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'); - - -C: + __PACKAGE__->many_to_many(roles => 'user_roles', 'role'); - # - # 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 @@ -336,7 +292,6 @@ for the tutorial, but if you wish to use C, just convert to the following code: - use_session 1 password_type clear user_model DB::User @@ -389,11 +344,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 } )) { @@ -405,6 +360,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 @@ -660,7 +618,8 @@ 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 @@ -739,7 +698,7 @@ Then run the following command: $ DBIC_TRACE=1 perl -Ilib set_hashed_passwords.pl -We had to use the C<-Ilib> arguement to tell perl to look under the +We had to use the C<-Ilib> argument to tell perl to look under the C directory for our C model. The DBIC_TRACE output should show that the update worked: