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=8a7ec0648d705aeff66398204604259545f9e8b9;hp=40fd38fe6188d428e145bb15660680e98841d59d;hb=4768184b3b277399116fbd53cae3697a9767fee5;hpb=2a6eb5f9e3b1b3a8dacd724bb8ab87ba18f733a5 diff --git a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod index 40fd38f..8a7ec06 100644 --- a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod +++ b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod @@ -84,6 +84,7 @@ C in your editor and insert: -- -- Add user and role tables, along with a many-to-many join table -- + PRAGMA foreign_keys = ON; CREATE TABLE user ( id INTEGER PRIMARY KEY, username TEXT, @@ -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) ); -- @@ -119,6 +120,7 @@ Then load this into the C database with the following command: $ sqlite3 myapp.db < myapp02.sql + =head2 Add User and Role Information to DBIC Schema Although we could manually edit the DBIC schema information to include @@ -126,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 ... @@ -136,77 +139,41 @@ option on the DBIC model helper to do most of the work for us: $ ls lib/MyApp/Schema/Result Author.pm BookAuthor.pm Book.pm Role.pm User.pm UserRole.pm -Notice how the helper has added three new table-specific result source +Notice how the helper has added three new table-specific Result Source files to the C directory. And, more importantly, even if there were changes to the existing result source files, those changes would have only been written above the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and your hand-edited enhancements would have been preserved. -Speaking of "hand-editted enhancements," we should now add -relationship information to the three new result source files. Edit -each of these files and add the following information between the C<# -DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and the closing C<1;>: - -C: +Speaking of "hand-editted enhancements," we should now add the +C relationship information to the User Result Source file. +As with the Book, BookAuthor, and Author files in +L, +L has +automatically created the C and C relationships +for the new User, UserRole, and Role tables. However, as a convenience +for mapping Users to their assigned roles (see +L), we will +also manually add a C relationship. Edit +C add the following information between +the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and the closing +C<1;>: - # - # 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: - - # - # 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'); + __PACKAGE__->many_to_many(roles => 'user_roles', 'role'); -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 with one +exception: we only defined the C relationship in one +direction. Whereas we felt that we would want to map Authors to Books +B Books to Authors, here we are only adding the convenience +C in the Users to Roles direction. Note that we do not need to make any change to the C schema file. It simply tells DBIC to load all @@ -215,16 +182,15 @@ C directory, so it will automatically pick up our new table information. -=head2 Sanity-Check Reload of Development Server - -We aren't ready to try out the authentication just yet; we only want -to do a quick check to be sure our model loads correctly. Press -C to kill the previous server instance (if it's still running) -and restart it: +=head2 Sanity-Check of the Development Server Reload - $ script/myapp_server.pl - -Look for the three new model objects in the startup debug output: +We aren't ready to try out the authentication just yet; we only want to +do a quick check to be sure our model loads correctly. Assuming that you +are following along and using the "-r" option on C, +then the development server should automatically reload (if not, press +C to break out of the server if it's running and then enter +C