/usr/share/perl5/CGI/Application/Plugin/Authorization/Driver/DBI.pm is in libcgi-application-plugin-authorization-perl 0.07-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | package CGI::Application::Plugin::Authorization::Driver::DBI;
use strict;
use warnings;
use base qw(CGI::Application::Plugin::Authorization::Driver);
=head1 NAME
CGI::Application::Plugin::Authorization::Driver::DBI - DBI Authorization driver
=head1 SYNOPSIS
use base qw(CGI::Application);
use CGI::Application::Plugin::Authorization;
# Simple task based authentication
__PACKAGE__->authz->config(
DRIVER => [ 'DBI',
TABLES => ['account', 'task'],
JOIN_ON => 'account.id = task.accountid',
USERNAME => 'account.name',
CONSTRAINTS => {
'task.name' => '__PARAM_1__',
}
],
);
if ($self->authz->authorize('editfoo') {
# User is allowed access if it can 'editfoo'
}
=head1 DESCRIPTION
This Authorization driver uses the DBI module to allow you to gather
authorization information from any database for which there is a DBD module.
You can either provide an active database handle, or provide the parameters
necessary to connect to the database.
=head2 DBH
The DBI database handle to use. Defaults to C<$self-E<gt>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
=item TABLE(S), JOIN_ON, USERNAME and CONSTRAINTS:
Use these values to describe the table structure, and an sql statement
will be automatically generated to query the database
=item SQL:
just provide one SQL parameters that gives a complete sql statement that will
be used to query the database
=back
Following is a description of all the available parameters:
=head2 TABLE(S)
Provide either a single table name, or an array of table names. You can give
the table names aliases which can be referenced in later columns.
TABLE => 'group',
- or -
TABLES => ['user U', 'group G'],
=head2 JOIN_ON
If you have specified multiple tables, then you need to provide an SQL
expression that can be used to join those tables.
JOIN_ON => 'user.id = group.userid',
- or -
JOIN_ON => 'U.id = G.userid',
=head2 USERNAME
This should be set to the column name that contains the username. This column
will be compared against the currently logged in user.
USERNAME => 'name'
- or -
USERNAME => 'U.name'
=head2 CONSTRAINTS
Constraints are used to restrict the database query against the options that
are passed to the C<authorize> method. In the common case, you will check these
parameters against a group permission table, although there is no limit to the
number of parameters that can be used. Each constraint can be set to a static
value, or it can be set to '__PARAM_n__' where 'n' is the position of the
parameter that is passed in to the C<authorize> method.
CONSTRAINTS => {
'user.active' => 't',
'group.type' => '__PARAM_1__',
'group.name' => '__PARAM_2__',
}
=head2 SQL
If you need to perform a complex query that can not be defined by the above
syntax, then you can provide your own SQL statment where the first placeholder
is used to fill in the username, and the rest of the placeholders are filled in
using the parameters passed to the authorize method.
SQL => 'SELECT count(*)
FROM account
LEFT JOIN ip ON (account.id = ip.accountid)
LEFT JOIN task ON (account.id = task.accountid)
WHERE account.name = ?
AND (ip.address >> inet ? OR task.name = ?)
',
=head1 EXAMPLE
#
# Example table structure (for PostgreSQL):
#
CREATE TABLE account (
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE task (
id SERIAL NOT NULL PRIMARY KEY,
accountid INTEGER NOT NULL REFERENCES account(id),
name VARCHAR(50) NOT NULL
);
CREATE TABLE ip (
id SERIAL NOT NULL PRIMARY KEY,
accountid INTEGER NOT NULL REFERENCES account(id),
address INET NOT NULL
);
INSERT INTO account (name) VALUES ('testuser');
INSERT INTO task (accountid, name) VALUES (1, 'editfoo');
INSERT INTO ip (accountid, address) VALUES (1, '192.168.1.0/24');
# Simple task based authentication
__PACKAGE__->authz->config(
DRIVER => [ 'DBI',
# the handle comes from $self->dbh, via the "DBH" plugin.
TABLES => ['account', 'task'],
JOIN_ON => 'account.id = task.accountid',
USERNAME => 'account.name',
CONSTRAINTS => {
'task.name' => '__PARAM_1__',
'task.active' => 't'
}
],
);
if ($self->authz->authorize('editfoo') {
# User is allowed access if they can 'editfoo'
}
# IP address configuration
__PACKAGE__->authz('byIP')->config(
DRIVER => [ 'DBI',
SQL => 'SELECT count(*)
FROM account JOIN ip ON (account.id = ip.accountid)
WHERE account.name = ?
AND ip.address >> inet ?
',
],
);
if ($self->authz('byIP')->authorize($ENV{REMOTE_ADDR}) {
# User is allowed to connect from this address
}
# both together in one test
# IP address configuration
__PACKAGE__->authz->config(
DRIVER => [ 'DBI',
SQL => 'SELECT count(*)
FROM account
JOIN ip ON (account.id = ip.accountid)
JOIN task ON (account.id = task.accountid)
WHERE account.name = ?
AND task.name = ?
AND ip.address >> inet ?
',
],
);
if ($self->authz->authorize('editfoo', $ENV{REMOTE_ADDR}) {
# User is allowed to connect from this address if they can
# also 'editfoo'
}
=head1 METHODS
=head2 authorize_user
This method accepts a username followed by a list of parameters and will
return true if the configured query returns at least one row based on the
given parameters.
=cut
sub authorize_user {
my $self = shift;
my $username = shift;
my @params = @_;
# verify that all the options are OK
my @_options = $self->options;
die "The DBI driver requires a hash of options" if @_options % 2;
my %options = @_options;
# 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 ( $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
if ( $options{SQL} ) {
# prepare and execute the SQL
my $sth = $dbh->prepare_cached( $options{SQL} )
|| die "Failed to prepare SQL statement: " . $dbh->errstr;
$sth->execute( $username, @params ) or die $dbh->errstr;
# Since we are not pulling specific columns we just check
# to see if we matched at least one row
my ($count) = $sth->fetchrow_array;
$sth->finish;
return $count ? 1 : 0;
}
# Grab the database table names (TABLE and TABLES are synonymous)
my $tables = $options{TABLES} || $options{TABLE};
$tables = [$tables] unless ref $tables eq 'ARRAY';
# Process the constraints.
# We need to check for values indicate they should be replaced by
# a parameter (__PARAM_\d+__)
my %constraints;
my $used_username = 0;
if ( $options{CONSTRAINTS} ) {
die "CONSTRAINTS must be a hashref"
unless ref $options{CONSTRAINTS} eq 'HASH';
while ( my ( $column, $value ) = each %{ $options{CONSTRAINTS} } ) {
if ( $value =~ /^__PARAM_(\d+)__$/ ) {
$value = $params[ $1 - 1 ];
}
elsif ( $value =~ /^__USERNAME__$/ ) {
$value = $username;
$used_username = 1;
}
elsif ( $value =~ /^__GROUP__$/ ) {
$value = $params[ 0 ];
}
$constraints{$column} = $value;
}
}
# Add in the username constraint if it was provided
if ($options{USERNAME}) {
$constraints{$options{USERNAME}} = $username;
} elsif ( ! $used_username && ! $options{NO_USERNAME} ) {
warn "Your configuration did not provide for a match against a username column, make sure to provide the USERNAME option, or use the special __USERNAME__ variable in your CONSTRAINTS";
}
# If we have multiple tables, then we need a join constraint
my $join_on = $options{JOIN_ON};
# Build the SQL statement
my $sql = 'SELECT count(*) FROM ' . join( ', ', @$tables ) . ' WHERE ';
my @where;
push @where, $join_on if $join_on;
push @where, map { $_ . ' = ?' } keys %constraints;
$sql .= join( ' AND ', @where );
my @db_values = values %constraints;
# prepare and execute the SQL
my $sth = $dbh->prepare_cached($sql)
|| die "Failed to prepare SQL statement: " . $dbh->errstr;
$sth->execute(@db_values) or die $dbh->errstr;
# Since we are not pulling specific columns we just check
# to see if we matched at least one row
my ($count) = $sth->fetchrow_array;
$sth->finish;
return $count ? 1 : 0;
}
=head1 SEE ALSO
L<CGI::Application::Plugin::Authorization::Driver>,
L<CGI::Application::Plugin::Authorization>, perl(1)
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2005, SiteSuite. All rights reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE
SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE
SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER
SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
=cut
1;
|