CGI::A...r::DBI(3pm)
π NAME π
okay so bestie, the whole entire name is CGI::Application::Plugin::Authorization::Driver::DBI and it is literally the DBI Authorization driver. that's it. that's the name. no notes π
and yeah, the page header shortens it to CGI::A...r::DBI(3pm) and files it under User Contributed Perl Documentation β the truncation is real and we respect it π«Ά
β¨ SYNOPSIS β¨
here's the whole vibe, copy-paste ready:
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'
}
simple. task based. auth. iconic. π
π DESCRIPTION π
so this Authorization driver uses the DBI module to let you gather authorization information from any database that has a DBD module. any of them. all of them. π«
you've got two paths, pick your fighter:
- you can provide an active database handle, or
- you can provide the parameters necessary to connect to the database.
both valid. both slay.
π¦ DBH π¦
the DBI database handle to use. defaults to $self->dbh(), which is provided and configured through CGI::Application::Plugin::DBH. π
when you're describing the database structure, you have two options:
TABLE(S),JOIN_ON,USERNAMEandCONSTRAINTS: use these values to describe the table structure, and an sql statement will be automatically generated to query the database. β¨SQL: just provide one SQL parameters that gives a complete sql statement that will be used to query the database.
and now, a description of all the available parameters:
π TABLE(S) π
provide either a single table name, or an array of table names. and you can give the table names aliases, which you can then reference in later columns. so fancy. π
TABLE => 'group',
or
TABLES => ['user U', 'group G'],
πΈ JOIN_ON πΈ
if you specified multiple tables, then bestie you need to provide an SQL expression that can be used to join those tables. no cap.
JOIN_ON => 'user.id = group.userid',
or
JOIN_ON => 'U.id = G.userid',
π USERNAME π
this should be set to the column name that contains the username. that column gets compared against the currently logged in user. π«Ά
USERNAME => 'name'
or
USERNAME => 'U.name'
π CONSTRAINTS π
Constraints are used to restrict the database query against the options that are passed to the authorize method. in the common case you'll 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 authorize method. so much power. π₯
CONSTRAINTS => {
'user.active' => 't',
'group.type' => '__PARAM_1__',
'group.name' => '__PARAM_2__',
}
π 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 statement β 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. the way that works is so clean honestly. β
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 = ?)
',
π§ EXAMPLE π§
okay here's a whole example, table structure and everything, for PostgreSQL specifically:
#
# 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'
}
three scenarios, one driver, absolutely ate. π½οΈβ¨
π METHODS π
π authorize_user π
this method accepts a username followed by a list of parameters, and it will return true if the configured query returns at least one row based on the given parameters. one row. that's all it takes. π«Ά
π¦ SEE ALSO π¦
CGI::Application::Plugin::Authorization::DriverCGI::Application::Plugin::Authorizationperl(1)
πΈ 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. π
π DISCLAIMER OF WARRANTY π
okay bestie, this part is in ALL CAPS for a reason, and that reason is it's serious. no warranty. read it, it's the whole thing, we don't skip:
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.
and that's the whole page, bestie. this one came bundled with perl v5.30.3 and it's dated 2020-09-25 π go forth and authorize, you've got this, mwah π¦π
π¦ where this page comes from
- debian bookworm: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcgi-application-plugin-authorization-perl 0.07+~cs0.1-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms