From: Kennedy Clark
Date: Thu, 22 May 2008 17:35:28 +0000 (+0000)
Subject: Rework tutorial. Lots of things changed, but key items include: new content in Catal...
X-Git-Tag: v5.8005~307
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=commitdiff_plain;h=3533daff0314522f79dff9c618da087568f1378c
Rework tutorial. Lots of things changed, but key items include: new content in CatalystBasics from Gerda S, move most of old CatalystBasics to MoreCatalystBasics, fix Authentication and Authorization, add PostgreSQL code from Louis M, etc.
---
diff --git a/lib/Catalyst/Manual/Tutorial.pod b/lib/Catalyst/Manual/Tutorial.pod
index f6db547..61a15ac 100644
--- a/lib/Catalyst/Manual/Tutorial.pod
+++ b/lib/Catalyst/Manual/Tutorial.pod
@@ -16,39 +16,43 @@ only navigate inside this page).
=over 4
-=item *
+=item 1
L
-=item *
+=item 2
L
-=item *
+=item 3
+
+L
+
+=item 4
L
-=item *
+=item 5
L
-=item *
+=item 6
L
-=item *
+=item 7
L
-=item *
+=item 8
L
-=item *
+=item 9
L
-=item *
+=item 10
L
@@ -60,6 +64,7 @@ L
=over 4
@@ -93,33 +98,39 @@ CREATE A CATALYST PROJECT
=item *
-CREATE A SQLITE DATABASE
+HELLO WORLD
+
+=over 4
=item *
-EDIT THE LIST OF CATALYST PLUGINS
+The Simplest Way
=item *
-DATABASE ACCESS WITH DBIx::Class
-
+Hello, World! Using a View and a Template
-=over 4
+=back
=item *
-Create a DBIC Schema File
+CREATE A SIMPLE CONTROLLER AND AN ACTION
+
+=back
-=item *
-Create the DBIC ``Result Source'' Files
+=head2 L
+
+
+=over 4
=item *
-Use Catalyst::Model::DBIC::Schema to Load the Model Class
+CREATE A NEW APPLICATION
-=back
+=item *
+EDIT THE LIST OF CATALYST PLUGINS
=item *
@@ -138,18 +149,49 @@ Create a Catalyst View Using TTSite
=item *
-Using RenderView for the Default View
+Globally Customize Every View
=item *
-Globally Customize Every View
+Create a TT Template Page
+
+=back
=item *
-Create a TT Template Page
+CREATE A SQLITE DATABASE
+
+=item *
+
+DATABASE ACCESS WITH DBIx::Class
+
+=over 4
+
+=item *
+
+Create a Dynamic DBIC Model
=back
+=item *
+
+RUN THE APPLICATION
+
+=item *
+
+A STATIC DATABASE MODEL WITH DBIx::Class
+
+=over 4
+
+=item *
+
+Create Static DBIC Schema Files
+
+=item *
+
+Updating the Generated DBIC Schema Files
+
+=back
=item *
@@ -157,11 +199,32 @@ RUN THE APPLICATION
=item *
-USING THE DEFAULT TEMPLATE NAME
+RUNNING THE APPLICATION FROM THE COMMAND LINE
+
+=item *
+
+UPDATING THE VIEW
+
+=over 4
+
+=item *
+
+Using RenderView for the Default View
+
+=item *
+
+Using The Default Template Name
+
+=item *
+
+Return To A Manually-Specified Template
+
+=back
=back
-=head2 L
+
+=head2 L
=over 4
@@ -227,11 +290,28 @@ Add a Delete Action to the Controller
Try the Delete Feature
+=item *
+
+Fixing a Dangerous URL
+
+=item *
+
+Try the Delete and Redirect Logic
+
+=item *
+
+Using uri_for to Pass Query Parameters
+
+=item *
+
+Try the Delete and Redirect With Query Param Logic
+
=back
=back
-=head2 L
+
+=head2 L
=over 4
@@ -251,10 +331,6 @@ Add User and Role Information to DBIC Schema
=item *
-Create New ``Result Source Objects''
-
-=item *
-
Sanity-Check Reload of Development Server
=item *
@@ -311,9 +387,26 @@ Try Out the Hashed Passwords
=back
+=item *
+
+USING THE SESSION FOR FLASH
+
+=over 4
+
+=item *
+
+Try Out Flash
+
+=item *
+
+Switch To Flash-To-Stash
+
=back
-=head2 L
+=back
+
+
+=head2 L
=over 4
@@ -367,7 +460,8 @@ Add a Method to Handle Access Violations
=back
-=head2 L
+
+=head2 L
=over 4
@@ -385,7 +479,8 @@ DEBUGGING MODULES FROM CPAN
=back
-=head2 L
+
+=head2 L
=over 4
@@ -407,85 +502,19 @@ SUPPORTING BOTH PRODUCTION AND TEST DATABASES
=back
-=head2 L
-
-=over 4
-
-=item *
-HTML::WIDGET FORM CREATION
+=head2 L
=over 4
=item *
-Add the HTML::Widget Plugin
-
-=item *
-
-Add a Form Creation Helper Method
-
-=item *
-
-Add Actions to Display and Save the Form
-
-=item *
-
-Update the CSS
-
-=item *
-
-Create a Template Page To Display The Form
-
-=item *
-
-Add Links for Create and Update via HTML::Widget
-
-=item *
-
-Test The Create Form
-
-=back
-
-=item *
-
-HTML::WIDGET VALIDATION AND FILTERING
-
-=over 4
-
-=item *
-
-Add Constraints and Filters to the Widget Creation Method
-
-=item *
-
-Rebuild the Form Submission Method to Include Validation
-
-=item *
-
-Try Out the Form
+ADVANCED CRUD OPTIONS
=back
-=item *
-
-Enable DBIx::Class::HTMLWidget Support
-
-=over 4
-
-=item *
-
-Add DBIx::Class::HTMLWidget to DBIC Model
-
-=item *
-
-Use populate_from_widget in hw_create_do
-=back
-
-=back
-
-=head2 L
+=head2 L
=over 4
@@ -557,7 +586,11 @@ key Catalyst modules.
Other Catalyst documentation folks like Kieren Diment, Gavin Henry,
and Jess Robinson (including their work on the original Catalyst
-tutorial).
+tutorial).
+
+=item *
+
+Kieren Diment for currently maintaining this document on CPAN.
=item *
@@ -565,11 +598,18 @@ Everyone on #catalyst and #catalyst-dev.
=item *
+Louis Moore (who thanks Marcello Romani and Tom Lanyon) for the
+PostgreSQL content in the Appendix.
+
+=item *
+
People who have emailed me with corrections and suggestions on the
tutorial. As of the most recent release, this include: Florian Ragwitz,
Mauro Andreolini, Jim Howard, Giovanni Gigante, William Moreno,
Bryan Roach, Ashley Berlin, David Kamholz, Kevin Old, Henning Sprang,
-Jeremy Jones, David Kurtz, Ingo Wichmann, and Shlomi Fish.
+Jeremy Jones, David Kurtz, Ingo Wichmann, and Shlomi Fish. I'm sure I
+am missing some names here... apologies for that (please let me know
+if you name should be here).
=back
diff --git a/lib/Catalyst/Manual/Tutorial/Appendices.pod b/lib/Catalyst/Manual/Tutorial/Appendices.pod
index eca6830..cad622e 100644
--- a/lib/Catalyst/Manual/Tutorial/Appendices.pod
+++ b/lib/Catalyst/Manual/Tutorial/Appendices.pod
@@ -1,11 +1,11 @@
=head1 NAME
-Catalyst::Manual::Tutorial::Appendices - Catalyst Tutorial - Part 9: Appendices
+Catalyst::Manual::Tutorial::Appendices - Catalyst Tutorial - Part 10: Appendices
=head1 OVERVIEW
-This is B of the Catalyst tutorial.
+This is B for the Catalyst tutorial.
L
@@ -21,30 +21,34 @@ L
=item 3
-L
+L
=item 4
-L
+L
=item 5
-L
+L
=item 6
-L
+L
=item 7
-L
+L
=item 8
-L
+L
=item 9
+L
+
+=item 10
+
B
=back
@@ -400,8 +404,353 @@ Load the user/roles data:
=head2 PostgreSQL
-B -- Please see the latest version of this document for possible updates:
-L
+Use the following steps to adapt the tutorial to PostgreSQL. Thanks to
+Louis Moore for the help who was in turn helped by Marcello Romani and
+Tom Lanyon.
+
+=over 4
+
+=item *
+
+Part 2: Catalyst Basics
+
+=over 4
+
+=item *
+
+Install the required software:
+
+=over 4
+
+=item *
+
+The PostgreSQL database server and client.
+
+=item *
+
+The Perl C module
+
+=back
+
+=item *
+
+Create the database and a user for the database
+
+ $ createuser -P catmyapp
+ Enter password for new role:
+ Enter it again:
+ Shall the new role be a superuser? (y/n) n
+ Shall the new role be allowed to create databases? (y/n) n
+ Shall the new role be allowed to create more new roles? (y/n) n
+ CREATE ROLE
+ $ createdb -O catmyapp mycatapp
+ CREATE DATABASE
+
+=item *
+
+Create the C<.sql> file and load the data:
+
+=over 4
+
+=item *
+
+Open the C in your editor and enter:
+
+
+ --
+ -- Create a very simple database to hold book and author information
+ --
+ -- The sequence is how we get a unique id in PostgreSQL
+ --
+ CREATE SEQUENCE books_seq START 5 ;
+ SELECT nextval ('books_seq');
+
+ CREATE TABLE books (
+ id INTEGER PRIMARY KEY DEFAULT nextval('books_seq'),
+ title TEXT ,
+ rating INTEGER
+ );
+
+ -- 'book_authors' is a many-to-many join table between books & authors
+ CREATE TABLE book_authors (
+ book_id INTEGER,
+ author_id INTEGER,
+ PRIMARY KEY (book_id, author_id)
+
+ );
+
+ CREATE SEQUENCE authors_seq START 8 ;
+ SELECT nextval ('authors_seq');
+
+ CREATE TABLE authors (
+ id INTEGER PRIMARY KEY DEFAULT nextval('authors_seq'),
+ first_name TEXT,
+ last_name TEXT
+ );
+ ---
+ --- Load some sample data
+ ---
+ INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
+ INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
+ INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
+ INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
+ INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
+ INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
+ INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
+ INSERT INTO authors VALUES (3, 'Christian', 'Degu');
+ INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
+ INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
+ INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
+ INSERT INTO authors VALUES (7, 'Nathan', 'Torkington');
+ INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
+ INSERT INTO book_authors VALUES (1, 1);
+ INSERT INTO book_authors VALUES (1, 2);
+ INSERT INTO book_authors VALUES (1, 3);
+ INSERT INTO book_authors VALUES (2, 4);
+ INSERT INTO book_authors VALUES (3, 5);
+ INSERT INTO book_authors VALUES (4, 6);
+ INSERT INTO book_authors VALUES (4, 7);
+ INSERT INTO book_authors VALUES (5, 8);
+
+=item *
+
+Load the data:
+
+ $ psql -U catmyapp -W mycatapp
+ Password for user catmyapp: catalyst
+ Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
+
+ Type: \copyright for distribution terms
+ \h for help with SQL commands
+ \? for help with psql commands
+ \g or terminate with semicolon to execute query
+ \q to quit
+
+ mycatapp=> \i myapp01_psql.sql
+
+ CREATE SEQUENCE
+ nextval
+ ---------
+ 5
+ (1 row)
+
+ psql:myapp01_psql.sql:11: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "books_pkey" for table "books"
+ CREATE TABLE
+ psql:myapp01_psql.sql:19: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "book_authors_pkey" for table
+ "book_authors"
+ CREATE TABLE
+ CREATE SEQUENCE
+ nextval
+ ---------
+ 8
+ (1 row)
+
+ psql:myapp01_psql.sql:30: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "authors_pkey" for table "authors"
+ CREATE TABLE
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ ...
+
+=item *
+
+Make sure the data loaded correctly:
+
+ mycatapp=> \dt
+ List of relations
+ Schema | Name | Type | Owner
+ --------+--------------+-------+----------
+ public | authors | table | catmyapp
+ public | book_authors | table | catmyapp
+ public | books | table | catmyapp
+ (3 rows)
+
+ mycatapp=> select * from books;
+ id | title | rating
+ ----+------------------------------------+--------
+ 1 | CCSP SNRS Exam Certification Guide | 5
+ 2 | TCP/IP Illustrated, Volume 1 | 5
+ 3 | Internetworking with TCP/IP Vol.1 | 4
+ 4 | Perl Cookbook | 5
+ 5 | Designing with Web Standards | 5
+ (5 rows)
+
+ mycatapp=> \q
+
+=back
+
+=item *
+
+After the steps where you:
+
+ edit lib/MyApp.pm
+
+ create lib/MyAppDB.pm
+
+ create lib/MyAppDB/Book.pm
+
+ create lib/MyAppDB/Author.pm
+
+ create lib/MyAppDB/BookAuthor.pm
+
+
+=item *
+
+Generate the model using the Catalyst "_create.pl" script:
+
+ script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB 'dbi:Pg:dbname=mycatapp' 'catmyapp' 'catalyst' '{ AutoCommit => 1 }'
+
+
+=back
+
+=item *
+
+Part 4: Authentication
+
+=over 4
+
+=item *
+
+Create the C<.sql> file for the user/roles data:
+
+Open C in your editor and enter:
+
+ --
+ -- Add users and roles tables, along with a many-to-many join table
+ --
+
+ CREATE SEQUENCE users_seq START 3 ;
+ SELECT nextval ('users_seq');
+
+ CREATE TABLE users (
+ id INTEGER PRIMARY KEY DEFAULT nextval('users_seq'),
+ username TEXT,
+ password TEXT,
+ email_address TEXT,
+ first_name TEXT,
+ last_name TEXT,
+ active INTEGER
+ );
+
+ CREATE SEQUENCE roles_seq START 2 ;
+ SELECT nextval ('roles_seq');
+
+ CREATE TABLE roles (
+ id INTEGER PRIMARY KEY DEFAULT nextval('roles_seq'),
+ role TEXT
+ );
+
+ CREATE TABLE user_roles (
+ user_id INTEGER,
+ role_id INTEGER,
+ PRIMARY KEY (user_id, role_id)
+ );
+
+ --
+ -- Load up some initial test data
+ --
+ INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
+ INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
+ INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
+ INSERT INTO roles VALUES (1, 'user');
+ INSERT INTO roles VALUES (2, 'admin');
+ INSERT INTO user_roles VALUES (1, 1);
+ INSERT INTO user_roles VALUES (1, 2);
+ INSERT INTO user_roles VALUES (2, 1);
+ INSERT INTO user_roles VALUES (3, 1);
+
+=item *
+
+Load the data:
+
+ $ psql -U catmyapp -W mycatapp
+ Password for user catmyapp: catalyst
+ Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
+
+ Type: \copyright for distribution terms
+ \h for help with SQL commands
+ \? for help with psql commands
+ \g or terminate with semicolon to execute query
+ \q to quit
+
+ mycatapp=> \i myapp02_psql.sql
+
+ CREATE SEQUENCE
+ nextval
+ ---------
+ 3
+ (1 row)
+
+ psql:myapp02_psql.sql:16: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
+ CREATE TABLE
+ CREATE SEQUENCE
+ nextval
+ ---------
+ 2
+ (1 row)
+
+ psql:myapp02_psql.sql:24: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roles_pkey" for table "roles"
+ CREATE TABLE
+ psql:myapp02_psql.sql:30: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "user_roles_pkey" for table "user_roles"
+ CREATE TABLE
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ INSERT 0 1
+ mycatapp=>
+
+ mycatapp=> select * from users;
+ id | username | password | email_address | first_name | last_name | active
+ ----+----------+----------+---------------+------------+-----------+--------
+ 1 | test01 | mypass | t01@na.com | Joe | Blow | 1
+ 2 | test02 | mypass | t02@na.com | Jane | Doe | 1
+ 3 | test03 | mypass | t03@na.com | No | Go | 0
+ (3 rows)
+
+
+=item *
+
+Create the C<.sql> file for the hashed password data:
+
+Open C in your editor and enter:
+
+ --
+ -- Convert passwords to SHA-1 hashes
+ --
+ UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
+ UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
+ UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
+
+=item *
+
+Load in the data
+
+ $ psql -U catmyapp -W mycatapp
+ Password for user catmyapp:
+ Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
+
+ Type: \copyright for distribution terms
+ \h for help with SQL commands
+ \? for help with psql commands
+ \g or terminate with semicolon to execute query
+ \q to quit
+
+ mycatapp=> \i myapp03_psql.sql
+ UPDATE 1
+ UPDATE 1
+ UPDATE 1
+
+
+
+=back
+
+=back
=head1 APPENDIX 3: IMPROVED HASHING SCRIPT
diff --git a/lib/Catalyst/Manual/Tutorial/Authentication.pod b/lib/Catalyst/Manual/Tutorial/Authentication.pod
index 8c24726..fa577d3 100644
--- a/lib/Catalyst/Manual/Tutorial/Authentication.pod
+++ b/lib/Catalyst/Manual/Tutorial/Authentication.pod
@@ -1,11 +1,11 @@
=head1 NAME
-Catalyst::Manual::Tutorial::Authentication - Catalyst Tutorial - Part 4: Authentication
+Catalyst::Manual::Tutorial::Authentication - Catalyst Tutorial - Part 5: Authentication
=head1 OVERVIEW
-This is B for the Catalyst tutorial.
+This is B for the Catalyst tutorial.
L
@@ -21,49 +21,43 @@ L
=item 3
-L
+L
=item 4
-B
+L
=item 5
-L
+B
=item 6
-L
+L
=item 7
-L
+L
=item 8
-L
+L
=item 9
-L
+L
-=back
+=item 10
+L
-=head1 IMPORTANT NOTE
+=back
-Since this tutorial was written, there has been a new Authentication
-API released (Catalyst::Plugin::Authentication version 0.1 and later).
-Some of this tutorial does not work with this API, and requires
-minimal changes. For an example application that uses the new API see
-L. It
-is recommended that you read this tutorial first, and then download
-the source code linked above to understand the differences.
=head1 DESCRIPTION
-Now that we finally have a simple yet functional application, we can
-focus on providing authentication (with authorization coming next in
+Now that we finally have a simple yet functional application, we can
+focus on providing authentication (with authorization coming next in
Part 5).
This part of the tutorial is divided into two main sections: 1) basic,
@@ -127,58 +121,29 @@ Then load this into the C database with the following command:
=head2 Add User and Role Information to DBIC Schema
-This step adds DBIC-based classes for the user-related database tables
-(the role information will not be used until Part 5):
-
-Edit C and update the contents to match (only the
-C [qw/Book BookAuthor Author User UserRole Role/]> line
-has changed):
+Although we could manually edit the DBIC schema information to include
+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:
- package MyAppDB;
-
- =head1 NAME
-
- MyAppDB -- DBIC Schema Class
-
- =cut
-
- # Our schema needs to inherit from 'DBIx::Class::Schema'
- use base qw/DBIx::Class::Schema/;
-
- # Need to load the DB Model classes here.
- # You can use this syntax if you want:
- # __PACKAGE__->load_classes(qw/Book BookAuthor Author User UserRole Role/);
- # Also, if you simply want to load all of the classes in a directory
- # of the same name as your schema class (as we do here) you can use:
- # __PACKAGE__->load_classes(qw//);
- # But the variation below is more flexible in that it can be used to
- # load from multiple namespaces.
- __PACKAGE__->load_classes({
- MyAppDB => [qw/Book BookAuthor Author User UserRole Role/]
- });
-
- 1;
+ $ script/myapp_create.pl model MyAppDB DBIC::Schema MyApp::Schema::MyAppDB create=static dbi:SQLite:myapp.db
+ $ ls lib/MyApp/Schema/MyAppDB
+ Authors.pm BookAuthors.pm Books.pm Roles.pm UserRoles.pm Users.pm
+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-editted
+enhancements would have been preserved.
-=head2 Create New "Result Source Objects"
-Create the following three files with the content shown below.
+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:
+C:
- package MyAppDB::User;
-
- use base qw/DBIx::Class/;
-
- # Load required DBIC stuff
- __PACKAGE__->load_components(qw/PK::Auto Core/);
- # Set the table name
- __PACKAGE__->table('users');
- # Set columns in table
- __PACKAGE__->add_columns(qw/id username password email_address first_name last_name/);
- # Set the primary key for the table
- __PACKAGE__->set_primary_key('id');
-
#
# Set relationships:
#
@@ -188,41 +153,19 @@ C:
# 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
- __PACKAGE__->has_many(map_user_role => 'MyAppDB::UserRole', 'user_id');
-
-
- =head1 NAME
-
- MyAppDB::User - A model object representing a person with access to the system.
+ __PACKAGE__->has_many(map_user_role => 'MyApp::Schema::MyAppDB::UserRoles', 'user_id');
- =head1 DESCRIPTION
-
- This is an object that represents a row in the 'users' table of your application
- database. It uses DBIx::Class (aka, DBIC) to do ORM.
-
- For Catalyst, this is designed to be used through MyApp::Model::MyAppDB.
- Offline utilities may wish to use this class directly.
-
- =cut
-
- 1;
+ # 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_role', 'role');
-C:
+C:
- package MyAppDB::Role;
-
- use base qw/DBIx::Class/;
-
- # Load required DBIC stuff
- __PACKAGE__->load_components(qw/PK::Auto Core/);
- # Set the table name
- __PACKAGE__->table('roles');
- # Set columns in table
- __PACKAGE__->add_columns(qw/id role/);
- # Set the primary key for the table
- __PACKAGE__->set_primary_key('id');
-
#
# Set relationships:
#
@@ -232,42 +175,11 @@ C:
# 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
- __PACKAGE__->has_many(map_user_role => 'MyAppDB::UserRole', 'role_id');
-
-
- =head1 NAME
-
- MyAppDB::Role - A model object representing a class of access permissions to
- the system.
-
- =head1 DESCRIPTION
-
- This is an object that represents a row in the 'roles' table of your
- application database. It uses DBIx::Class (aka, DBIC) to do ORM.
-
- For Catalyst, this is designed to be used through MyApp::Model::MyAppDB.
- "Offline" utilities may wish to use this class directly.
-
- =cut
-
- 1;
+ __PACKAGE__->has_many(map_user_role => 'MyApp::Schema::MyAppDB::UserRoles', 'role_id');
-C:
+C:
- package MyAppDB::UserRole;
-
- use base qw/DBIx::Class/;
-
- # Load required DBIC stuff
- __PACKAGE__->load_components(qw/PK::Auto Core/);
- # Set the table name
- __PACKAGE__->table('user_roles');
- # Set columns in table
- __PACKAGE__->add_columns(qw/user_id role_id/);
- # Set the primary key for the table
- __PACKAGE__->set_primary_key(qw/user_id role_id/);
-
#
# Set relationships:
#
@@ -277,41 +189,33 @@ C:
# 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 => 'MyAppDB::User', 'user_id');
+ __PACKAGE__->belongs_to(user => 'MyApp::Schema::MyAppDB::Users', '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 => 'MyAppDB::Role', 'role_id');
-
-
- =head1 NAME
-
- MyAppDB::UserRole - A model object representing the JOIN between Users and Roles.
-
- =head1 DESCRIPTION
-
- This is an object that represents a row in the 'user_roles' table of your application
- database. It uses DBIx::Class (aka, DBIC) to do ORM.
-
- You probably won't need to use this class directly -- it will be automatically
- used by DBIC where joins are needed.
-
- For Catalyst, this is designed to be used through MyApp::Model::MyAppDB.
- Offline utilities may wish to use this class directly.
-
- =cut
-
- 1;
+ __PACKAGE__->belongs_to(role => 'MyApp::Schema::MyAppDB::Roles', 'role_id');
-The code for these three result source classes is obviously very familiar to the C, C, and C classes created in Part 2.
+
+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 Part 3.
+
+Note that we do not need to make any change to the
+C schema file. It simple tells DBIC to
+load all of the result source files it finds in below the
+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:
+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:
$ script/myapp_server.pl
@@ -325,21 +229,23 @@ Look for the three new model objects in the startup debug output:
| MyApp::Controller::Root | instance |
| MyApp::Model::MyAppDB | instance |
| MyApp::Model::MyAppDB::Author | class |
- | MyApp::Model::MyAppDB::Book | class |
- | MyApp::Model::MyAppDB::BookAuthor | class |
- | MyApp::Model::MyAppDB::Role | class |
- | MyApp::Model::MyAppDB::User | class |
- | MyApp::Model::MyAppDB::UserRole | class |
+ | MyApp::Model::MyAppDB::Books | class |
+ | MyApp::Model::MyAppDB::BookAuthors | class |
+ | MyApp::Model::MyAppDB::Roles | class |
+ | MyApp::Model::MyAppDB::Users | class |
+ | MyApp::Model::MyAppDB::UserRoles | class |
| MyApp::View::TT | instance |
'-------------------------------------------------------------------+----------'
...
-Again, notice that your "result source" classes have been "re-loaded" by Catalyst under C.
+Again, notice that your "result source" classes have been "re-loaded"
+by Catalyst under C.
=head2 Include Authentication and Session Plugins
-Edit C and update it as follows (everything below C is new):
+Edit C and update it as follows (everything below
+C is new):
use Catalyst qw/
-Debug
@@ -355,37 +261,37 @@ Edit C and update it as follows (everything below C is
Session::State::Cookie
/;
-The C plugin supports
-Authentication while the C plugins are required to maintain
-state across multiple HTTP requests.
+The C plugin supports Authentication while the
+C plugins are required to maintain state across multiple HTTP
+requests.
-Note that the only required Authentication class is the main
-one. This is a change that occured in version 0.09999_01
-of the C plugin. You B to specify a
-particular Authentication::Store or Authentication::Credential plugin.
-Instead, indicate the Store and Credential you want to use in your application
+Note that the only required Authentication class is the main one. This
+is a change that occurred in version 0.09999_01 of the
+C plugin. You B to specify a particular
+Authentication::Store or Authentication::Credential plugin. Instead,
+indicate the Store and Credential you want to use in your application
configuration (see below).
-Note that there are several
-options for L
-(L
-is generally a good choice if you are on Unix; try
-L if you
-are on Win32) -- consult
-L and its subclasses
-for additional information and options (for example to use a
-database-backed session store).
+Note that there are several options for
+L
+(L
+is generally a good choice if you are on Unix; try
+L if you
+are on Win32) -- consult
+L and its subclasses
+for additional information and options (for example to use a database-
+backed session store).
=head2 Configure Authentication
-Although C<__PACKAGE__-Econfig(name =E 'value');> is still
-supported, newer Catalyst applications tend to place all configuration
-information in C and automatically load this information into
-Cconfig> using the
-L plugin. Here, we need
+Although C<__PACKAGE__-Econfig(name =E 'value');> is still
+supported, newer Catalyst applications tend to place all configuration
+information in C and automatically load this information
+into Cconfig> using the
+L plugin. Here, we need
to load several parameters that tell
-L
+L
where to locate information in your database. To do this, edit the
C YAML and update it to match:
@@ -396,29 +302,39 @@ C YAML and update it to match:
realms:
dbic:
credential:
- class: Password
+ # Note this first definition would be the same as setting
+ # __PACKAGE__->config->{authentication}->{realms}->{dbic}
+ # ->{credential} = 'Password' in lib/MyApp.pm
+ # (IOW, each hash key becomes a "name:" in the YAML file).
+ #
+ # Specify that we are going to do password-based auth
+ class: Password
+ # This is the name of the field in the users table with the
+ # password stored in it
password_field: password
- password_type: self_check
+ # We are using an unencrypted password now
+ password_type: clear
store:
+ # Use DBIC to retrieve username, password & role information
class: DBIx::Class
- # This is the model object created by Catalyst::Model::DBIC from your
- # schema (you created 'MyAppDB::User' but as the Catalyst startup
- # debug messages show, it was loaded as 'MyApp::Model::MyAppDB::User').
- # NOTE: Omit 'MyApp::Model' to avoid a component lookup issue in Catalyst 5.66
- user_class: MyApp::Users
- # This is the name of the field in your 'users' table that contains the user's name
+ # This is the model object created by Catalyst::Model::DBIC
+ # from your schema (you created 'MyAppDB::User' but as the
+ # Catalyst startup debug messages show, it was loaded as
+ # 'MyApp::Model::MyAppDB::Users').
+ # NOTE: Omit 'MyApp::Model' here just as you would when using
+ # '$c->model("MyAppDB::Users)'
+ user_class: MyAppDB::Users
+ # This is the name of the field in your 'users' table that
+ # contains the user's name
id_field: username
- role_relation: roles
- role_field: rolename
- ignore_fields_in_find: [ 'remote_name' ]
Inline comments in the code above explain how each field is being used.
-B: Although YAML uses a very simple and easy-to-ready format, it
-does require the use of a consistent level of indenting. Be sure you
-line up everything on a given 'level' with the same number of indents.
-Also, be sure not to use C characters (YAML does not support them
-because they are handled inconsistently across editors).
+B: Although YAML uses a very simple and easy-to-ready format, it
+does require the use of a consistent level of indenting. Be sure you
+line up everything on a given 'level' with the same number of indents.
+Also, be sure B to use C characters (YAML does not support
+them because they are handled inconsistently across editors).
=head2 Add Login and Logout Controllers
@@ -497,8 +413,8 @@ We make the match even more specific with the C<:Args(0)> action
modifier -- this forces the match on I C, not
C.
-Next, update the corresponding method in C
-to match:
+Next, update the corresponding method in
+C to match:
=head2 index
@@ -593,22 +509,41 @@ the following method:
return 1;
}
-B Catalyst provides a number of different types of actions, such
-as C, C, and C. You should refer to
-L for a more detailed explanation, but the
-following bullet points provide a quick introduction:
+
+B Catalyst provides a number of different types of actions,
+such as C, C, C and the new C. You
+should refer to L for
+a more detailed explanation, but the following bullet points provide a
+quick introduction:
=over 4
=item *
-The majority of application use C actions for items that respond
-to user requests and C actions for those that do not directly
-respond to user input.
+The majority of application have traditionally use C actions
+for items that respond to user requests and C actions for
+those that do not directly respond to user input.
=item *
-There are five types of C actions: C, C,
+Newer Catalyst applications tend to use C actions and the
+C attribute because of their power and flexibility. You can
+specify the path to match relative to the namespace of the current
+module as an argument to C. For example C in
+C would match on the URL
+C but C would
+match on C.
+
+=item *
+
+Automatic "chaining" of actions by the dispatcher is a powerful
+feature that allows multiple methods to handle a single URL. See
+L
+for more information on chained actions.
+
+=item *
+
+There are five types of build-in C actions: C, C,
C, C, and C.
=item *
@@ -658,7 +593,7 @@ lines to the bottom of the file:
TT code, it's probably a little too subtle for use in "normal"
comments.
%]
-
+
Although most of the code is comments, the middle few lines provide a
"you are already logged in" reminder if the user returns to the login
@@ -695,7 +630,7 @@ username C and password C, and you should be taken to
the Book List page.
Open C and add the following lines to the
-bottom:
+bottom (below the closing tag):
Login
@@ -779,26 +714,37 @@ C are new, everything else is the same):
---
name: MyApp
authentication:
- dbic:
- # Note this first definition would be the same as setting
- # __PACKAGE__->config->{authentication}->{dbic}->{user_class} = 'MyAppDB::User'
- # in lib/MyApp.pm (IOW, each hash key becomes a "name:" in the YAML file).
- #
- # This is the model object created by Catalyst::Model::DBIC from your
- # schema (you created 'MyAppDB::User' but as the Catalyst startup
- # debug messages show, it was loaded as 'MyApp::Model::MyAppDB::User').
- # NOTE: Omit 'MyApp::Model' here just as you would when using
- # '$c->model("MyAppDB::User)'
- user_class: MyAppDB::User
- # This is the name of the field in your 'users' table that contains the user's name
- user_field: username
- # This is the name of the field in your 'users' table that contains the password
- password_field: password
- # Other options can go here for hashed passwords
- # Enabled hashed passwords
- password_type: hashed
- # Use the SHA-1 hashing algorithm
- password_hash_type: SHA-1
+ default_realm: dbic
+ realms:
+ dbic:
+ credential:
+ # Note this first definition would be the same as setting
+ # __PACKAGE__->config->{authentication}->{realms}->{dbic}
+ # ->{credential} = 'Password' in lib/MyApp.pm
+ # (IOW, each hash key becomes a "name:" in the YAML file).
+ #
+ # Specify that we are going to do password-based auth
+ class: Password
+ # This is the name of the field in the users table with the
+ # password stored in it
+ password_field: password
+ # Switch to more secure hashed passwords
+ password_type: hashed
+ # Use the SHA-1 hashing algorithm
+ password_hash_type: SHA-1
+ store:
+ # Use DBIC to retrieve username, password & role information
+ class: DBIx::Class
+ # This is the model object created by Catalyst::Model::DBIC
+ # from your schema (you created 'MyAppDB::User' but as the
+ # Catalyst startup debug messages show, it was loaded as
+ # 'MyApp::Model::MyAppDB::Users').
+ # NOTE: Omit 'MyApp::Model' here just as you would when using
+ # '$c->model("MyAppDB::Users)'
+ user_class: MyAppDB::Users
+ # This is the name of the field in your 'users' table that
+ # contains the user's name
+ id_field: username
=head2 Try Out the Hashed Passwords
@@ -812,15 +758,6 @@ You should now be able to go to L and
login as before. When done, click the "Logout" link on the login page
(or point your browser at L).
-B If you receive the debug screen in your browser with a
-C error message,
-make sure that you are using v0.07 of
-L.
-The following command can be a useful way to quickly dump the version number
-of this module on your system:
-
- perl -MCatalyst::Plugin::Authorization::ACL -e 'print $Catalyst::Plugin::Authorization::ACL::VERSION, "\n";'
-
=head1 USING THE SESSION FOR FLASH
@@ -835,7 +772,8 @@ L part of the
tutorial.
First, open C and modify C
-to match the following:
+to match the following (everything after the model search line of code
+has changed):
=head2 delete
@@ -848,12 +786,12 @@ to match the following:
my ($self, $c, $id) = @_;
# Search for the book and then delete it
- $c->model('MyAppDB::Book')->search({id => $id})->delete_all;
+ $c->model('MyAppDB::Books')->search({id => $id})->delete_all;
# Use 'flash' to save information across requests until it's read
$c->flash->{status_msg} = "Book deleted";
- # Redirect the user back to the list page with status msg as an arg
+ # Redirect the user back to the list page
$c->response->redirect($c->uri_for('/books/list'));
}
@@ -874,10 +812,10 @@ flash vs. the C query parameter:
=head2 Try Out Flash
Restart the development server and point your browser to
-L to create an extra
-book. Click the "Return to list" link and delete the "Test" book you
-just added. The C mechanism should retain our "Book deleted"
-status message across the redirect.
+L to create an extra
+several books. Click the "Return to list" link and delete one of the
+"Test" books you just added. The C mechanism should retain our
+"Book deleted" status message across the redirect.
B While C will save information across multiple requests,
I. In general, this is
@@ -887,6 +825,43 @@ after that first time (unless you reset it). Please refer to
L for additional
information.
+=head2 Switch To Flash-To-Stash
+
+Although the a use of flash above is certainly an improvement over the
+C we employed in Part 4 of the tutorial, the C statement is a little ugly. A nice
+alternative is to use the C feature that automatically
+copies the content of flash to stash. This makes your code controller
+and template code work regardless of where it was directly access, a
+forward, or a redirect. To enable C, you can either
+set the value in C by changing the default
+C<__PACKAGE__-Econfig> setting to something like:
+
+ __PACKAGE__->config(
+ name => 'MyApp',
+ session => {flash_to_stash => 1}
+ );
+
+B add the following to C:
+
+ session:
+ flash_to_stash: 1
+
+The C<__PACKAGE__-Econfig> option is probably preferable here
+since it's not something you will want to change at runtime without it
+possibly breaking some of your code.
+
+Then edit C and change the C line
+to look like the following:
+
+ [% status_msg %]
+
+Restart the development server and go to
+L in your browser. Delete another
+of the "Test" books you added in the previous step. Flash should still
+maintain the status message across the redirect even though you are no
+longer explicitly accessing C.
+
=head1 AUTHOR
diff --git a/lib/Catalyst/Manual/Tutorial/Authorization.pod b/lib/Catalyst/Manual/Tutorial/Authorization.pod
index d6be44f..5f4e4ef 100644
--- a/lib/Catalyst/Manual/Tutorial/Authorization.pod
+++ b/lib/Catalyst/Manual/Tutorial/Authorization.pod
@@ -1,11 +1,11 @@
=head1 NAME
-Catalyst::Manual::Tutorial::Authorization - Catalyst Tutorial - Part 5: Authorization
+Catalyst::Manual::Tutorial::Authorization - Catalyst Tutorial - Part 6: Authorization
=head1 OVERVIEW
-This is B for the Catalyst tutorial.
+This is B for the Catalyst tutorial.
L
@@ -21,36 +21,39 @@ L
=item 3
-L
+L
=item 4
-L
+L
=item 5
-B
+L
=item 6
-L
+B
=item 7
-L
+L
=item 8
-L
+L
=item 9
+L
+
+=item 10
+
L
=back
-
=head1 DESCRIPTION
This part of the tutorial adds role-based authorization to the existing
@@ -79,8 +82,6 @@ Edit C and add C to the list:
StackTrace
Authentication
- Authentication::Store::DBIC
- Authentication::Credential::Password
Authorization::Roles
Session
@@ -91,47 +92,47 @@ Edit C and add C to the list:
=head2 Add Config Information for Authorization
-Edit C and update it to match (everything from the
-"authorization:" line down is new):
+Edit C and update it to match the following (the
+C and C definitions are new):
---
name: MyApp
authentication:
- dbic:
- # Note this first definition would be the same as setting
- # __PACKAGE__->config->{authentication}->{dbic}->{user_class} = 'MyAppDB::User'
- # in lib/MyApp.pm (IOW, each hash key becomes a "name:" in the YAML file).
- #
- # This is the model object created by Catalyst::Model::DBIC from your
- # schema (you created 'MyAppDB::User' but as the Catalyst startup
- # debug messages show, it was loaded as 'MyApp::Model::MyAppDB::User').
- # NOTE: Omit 'MyApp::Model' here just as you would when using
- # '$c->model("MyAppDB::User)'
- user_class: MyAppDB::User
- # This is the name of the field in your 'users' table that contains the user's name
- user_field: username
- # This is the name of the field in your 'users' table that contains the password
- password_field: password
- # Other options can go here for hashed passwords
- # Enabled hashed passwords
- password_type: hashed
- # Use the SHA-1 hashing algorithm
- password_hash_type: SHA-1
- authorization:
- dbic:
- # This is the model object created by Catalyst::Model::DBIC from your
- # schema (you created 'MyAppDB::Role' but as the Catalyst startup
- # debug messages show, it was loaded as 'MyApp::Model::MyAppDB::Role').
- # NOTE: Omit 'MyApp::Model' here just as you would when using
- # '$c->model("MyAppDB::User)'
- role_class: MyAppDB::Role
- # The name of the field in the 'roles' table that contains the role name
- role_field: role
- # The name of the accessor used to map a role to the users who have this role
- # See the has_many() in MyAppDB/Role.pm
- role_rel: map_user_role
- # The name of the field in the user_role table that references the user
- user_role_user_field: user_id
+ default_realm: dbic
+ realms:
+ dbic:
+ credential:
+ # Note this first definition would be the same as setting
+ # __PACKAGE__->config->{authentication}->{realms}->{dbic}
+ # ->{credential} = 'Password' in lib/MyApp.pm
+ # (IOW, each hash key becomes a "name:" in the YAML file).
+ #
+ # Specify that we are going to do password-based auth
+ class: Password
+ # This is the name of the field in the users table with the
+ # password stored in it
+ password_field: password
+ # We are using an unencrypted password now
+ password_type: clear
+ store:
+ # Use DBIC to retrieve username, password & role information
+ class: DBIx::Class
+ # This is the model object created by Catalyst::Model::DBIC
+ # from your schema (you created 'MyAppDB::User' but as the
+ # Catalyst startup debug messages show, it was loaded as
+ # 'MyApp::Model::MyAppDB::Users').
+ # NOTE: Omit 'MyApp::Model' here just as you would when using
+ # '$c->model("MyAppDB::Users)'
+ user_class: MyAppDB::Users
+ # This is the name of the field in your 'users' table that
+ # contains the user's name
+ id_field: username
+ # This is the name of a many_to_many relation in the users
+ # object that points to the roles for that user
+ role_relation: roles
+ # This is the name of field in the roles table that contains
+ # the role information
+ role_field: role
=head2 Add Role-Specific Logic to the "Book List" Template
@@ -193,7 +194,7 @@ updating C to match the following code:
if ($c->check_user_roles('admin')) {
# Call create() on the book model object. Pass the table
# columns/field values we want to set as hash values
- my $book = $c->model('MyAppDB::Book')->create({
+ my $book = $c->model('MyAppDB::Books')->create({
title => $title,
rating => $rating
});
diff --git a/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod
index 25d419e..2e688bb 100644
--- a/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod
+++ b/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod
@@ -1,11 +1,11 @@
=head1 NAME
-Catalyst::Manual::Tutorial::BasicCRUD - Catalyst Tutorial - Part 3: Basic CRUD
+Catalyst::Manual::Tutorial::BasicCRUD - Catalyst Tutorial - Part 4: Basic CRUD
=head1 OVERVIEW
-This is B for the Catalyst tutorial.
+This is B for the Catalyst tutorial.
L
@@ -21,57 +21,61 @@ L
=item 3
-B
+L
=item 4
-L
+B
=item 5
-L
+L
=item 6
-L
+L
=item 7
-L
+L
=item 8
-L
+L
=item 9
+L
+
+=item 10
+
L
=back
-
=head1 DESCRIPTION
This part of the tutorial builds on the fairly primitive application
-created in Part 2 to add basic support for Create, Read, Update, and
+created in Part 3 to add basic support for Create, Read, Update, and
Delete (CRUD) of C objects. Note that the 'list' function in Part
2 already implements the Read portion of CRUD (although Read normally
refers to reading a single object; you could implement full read
functionality using the techniques introduced below). This section will
focus on the Create and Delete aspects of CRUD. More advanced
capabilities, including full Update functionality, will be addressed in
-Part 8.
+Part 9.
You can checkout the source code for this example from the catalyst
subversion repository as per the instructions in
L
+
=head1 FORMLESS SUBMISSION
-Our initial attempt at object creation will utilize the "URL arguments"
-feature of Catalyst (we will employ the more common form-based
-submission in the sections that follow).
+Our initial attempt at object creation will utilize the "URL
+arguments" feature of Catalyst (we will employ the more common form-
+based submission in the sections that follow).
=head2 Include a Create Action in the Books Controller
@@ -93,7 +97,7 @@ Edit C and enter the following method:
# Call create() on the book model object. Pass the table
# columns/field values we want to set as hash values
- my $book = $c->model('MyAppDB::Book')->create({
+ my $book = $c->model('MyAppDB::Books')->create({
title => $title,
rating => $rating
});
@@ -172,7 +176,7 @@ The TT C