Subject: | PATCH: improved docs |
Attached is a mostly doc patch I submitted in November. I'm posting it
here for easier reference by Cees and to make it visible to others until
it becomes merged.
Mark
Subject: | authorization.patch |
Tue Nov 15 10:28:54 EST 2005 Mark Stosberg <mark@summersault.com>
* fix delegate class
Mon Nov 14 17:23:50 EST 2005 Mark Stosberg <mark@summersault.com>
* de-mystify somethings by removing prerun_callback and explaining about authz_forbidden being added
diff -rN -u old-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization/Driver/DBI.pm new-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization/Driver/DBI.pm
--- old-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization/Driver/DBI.pm 2006-02-04 08:17:12.000000000 -0500
+++ new-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization/Driver/DBI.pm 2006-02-04 08:17:13.000000000 -0500
@@ -18,7 +18,6 @@
# Simple task based authentication
__PACKAGE__->authz->config(
DRIVER => [ 'DBI',
- DBH => $self->dbh,
TABLES => ['account', 'task'],
JOIN_ON => 'account.id = task.accountid',
USERNAME => 'account.name',
@@ -39,6 +38,11 @@
You can either provide an active database handle, or provide the parameters
necesary to connect to the database.
+=head2 DBH
+
+The DBI database handle to use. Defaults to C<$self->dbh()>, which is provided and configured
+through L<CGI::Application::Plugin::DBH|CGI::Application::Plugin::DBH>
+
When describing the database structure you have two options:
=over 4
@@ -151,7 +155,7 @@
# Simple task based authentication
__PACKAGE__->authz->config(
DRIVER => [ 'DBI',
- DBH => $self->dbh,
+ # the handle comes from $self->dbh, via the "DBH" plugin.
TABLES => ['account', 'task'],
JOIN_ON => 'account.id = task.accountid',
USERNAME => 'account.name',
@@ -168,7 +172,6 @@
# IP address configuration
__PACKAGE__->authz('byIP')->config(
DRIVER => [ 'DBI',
- DBH => $self->dbh,
SQL => 'SELECT count(*)
FROM account JOIN ip ON (account.id = ip.accountid)
WHERE account.name = ?
@@ -184,7 +187,6 @@
# IP address configuration
__PACKAGE__->authz->config(
DRIVER => [ 'DBI',
- DBH => $self->dbh,
SQL => 'SELECT count(*)
FROM account
JOIN ip ON (account.id = ip.accountid)
@@ -223,20 +225,15 @@
# Get a database handle either one that is given to us, or connect using
# the information given in the configuration
+ # Get a database handle either one that is given to us, or connect using the
+ # information given in the configuration
my $dbh;
if ( $options{DBH} ) {
$dbh = $options{DBH};
- }
- elsif ( $options{DSN} ) {
- no warnings qw(uninitialized);
- $dbh = DBI->connect(
- $options{DSN}, $options{DB_USER},
- $options{DB_PASSWORD}, $options{DBI_OPTIONS}
- )
- or die $DBI::errstr;
- }
- else {
- die "No DBH or DSN parameter passed to the DBI Driver";
+ } elsif ( $self->authen->_cgiapp->can('dbh') ) {
+ $dbh = $self->authen->_cgiapp->dbh;
+ } else {
+ die "No DBH or passed to the DBI Driver, and no dbh() method detected";
}
# See if the user provided an SQL option
diff -rN -u old-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization.pm new-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization.pm
--- old-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization.pm 2006-02-04 08:17:13.000000000 -0500
+++ new-CGI-Application-Plugin-Authorization-0.02/lib/CGI/Application/Plugin/Authorization.pm 2006-02-04 08:17:13.000000000 -0500
@@ -26,10 +26,10 @@
}
elsif ( !UNIVERSAL::can( $callpkg, 'add_callback' ) ) {
warn
- "You are using an older version of CGI::Application that does not support callbacks, so the prerun method can not be registered automatically (Lookup the prerun_callback method in the docs for more info)";
+ "You are using an older version of CGI::Application that does not support callbacks, so the prerun method can not be registered automatically (Lookup 'CGI::Application CALLBACKS' in the docs for more info)";
}
else {
- $callpkg->add_callback( prerun => \&prerun_callback );
+ $callpkg->add_callback( prerun => \&setup_runmodes );
}
}
@@ -445,7 +445,7 @@
foreach my $driver_config (@$driver_configs) {
my ( $drivername, @params ) = @$driver_config;
# Load the the class for this driver
- my $driver_class = _find_deligate_class(
+ my $driver_class = _find_delegate_class(
'CGI::Application::Plugin::Authorization::Driver::'
. $drivername, $drivername
)
@@ -475,44 +475,32 @@
=head2 setup_runmodes
-This method is called during the prerun stage to register some custom
-runmodes that the Authorization plugin requires in order to function.
+This CGI::App method is called during the prerun stage to register the "authz_forbidden" method
+that the Authorization plugin requires in order to function.
=cut
sub setup_runmodes {
my $self = shift;
- my $config = $self->_config;
-
- $self->cgiapp->run_modes( authz_forbidden => \&authz_forbidden, );
+ $self->run_modes( authz_forbidden => \&authz_forbidden, );
return;
}
=head1 CGI::Application CALLBACKS
-=head2 prerun_callback
+We'll automatically add the C<authz_forbidden> run mode if you are using
+CGI::Application 4.0 or greater.
-This method is a CGI::Application prerun callback that will be automatically
-registered for you if you are using CGI::Application 4.0 or greater. If you
-are using an older version of CGI::Application you will have to create your own
-cgiapp_prerun method and make sure you call this method from there.
+If you are using an older version of CGI::Application you will need to add it yourself.
sub cgiapp_prerun {
my $self = shift;
- $self->CGI::Application::Plugin::Authorization::prerun_callback();
+ $self->run_modes( authz_forbidden => \&CGI::Application::Plugin::Authorization::authz_forbidden, );
}
=cut
-sub prerun_callback {
- my $self = shift;
- my $authz = $self->authz;
-
- # setup the default login and logout runmodes
- $authz->setup_runmodes;
-}
-
=head2 forbidden
This will return a forbidden page. It checks the configuration to see if there
@@ -571,7 +559,7 @@
### Helper methods
###
-sub _find_deligate_class {
+sub _find_delegate_class {
foreach my $class (@_) {
$class->require && return $class;
}