1 package Apache2
::AuthzCaps
0.001;
6 use subs qw
/OK DECLINED/;
7 no if $] >= 5.017011, warnings
=> 'experimental::smartmatch';
9 use if $ENV{MOD_PERL
}, 'Apache2::Access';
10 use if $ENV{MOD_PERL
}, 'Apache2::Const' => qw
/OK DECLINED/;
11 use if $ENV{MOD_PERL
}, 'Apache2::RequestRec';
12 use YAML
::Any qw
/LoadFile DumpFile/;
14 use parent qw
/Exporter/;
16 our @EXPORT_OK = qw
/setcap hascaps/;
18 ##################################################
23 my ($user, $cap, $value) = @_;
24 my $config = eval { LoadFile
"$rootdir/$user.yml" } // {};
26 my $caps=$config->{caps
};
28 delete $caps->{$cap} unless $value;
29 $caps->{$cap} = 1 if $value;
30 DumpFile
"$rootdir/$user.yml", $config
34 my ($user, @caps) = @_;
35 my $config = LoadFile
"$rootdir/$user.yml";
36 my $caps = $config->{caps
};
38 return 0 unless $caps->{$_}
46 local $rootdir = $r->dir_config('AuthzCapsRootdir');
49 LOOP
: for my $requirement (map { $_->{requirement
} } @
{$r->requires}) {
50 my ($command, @args) = split ' ', $requirement;
54 return OK
if hascaps
$user, @args
69 Apache2::AuthzCaps - mod_perl2 capability authorization
73 use Apache2::AuthzCaps qw/setcap hascaps/;
74 $Apache2::AuthzCaps::rootdir = "/path/to/user/directory"
75 setcap marius => deleteusers => 1; # Grant marius the deleteusers capability
76 setcap marius => createusers => 0;
77 hascaps marius => qw/deleteusers/; # returns 1, since marius can delete users
78 hascaps marius => qw/deleteusers createusers/; # returns 0, since marius can delete users but cannot create users
82 # Insert authentication here
83 PerlAuthzHandler Apache2::AuthzCaps
84 PerlSetVar AuthzCapsRootdir /path/to/user/directory
85 Require cap staff important
89 # 1) Let important staff members access /protected
90 # 2) Let admins access /protected
91 # 3) Not let anyone else (such as an important non-staff member or an non-important staff member) access /protected
95 Apache2::AuthzCaps is a perl module which provides simple Apache2 capability-based authorization. It contains a PerlAuthzHandler and some utility functions.
97 The user data is stored in YAML files in a user-set directory. Set this directory using:
99 $Apache2::AuthzCaps::rootdir = "/path/to/directory"; # From perl
100 PerlSetVar AuthzCapsRootdir /path/to/directory # From Apache2 config
106 =item B<setcap>(I<$username>, I<$capability>, I<$value>)
108 If I<$value> is true, grants I<$username> the I<$capability> capability. Otherwise denies I<$username> that capability.
110 =item B<hascaps>(I<$username>, I<$cap>, ...)
112 Returns true if and only of I<$username> has ALL of the listed capabilities. Dies if I<$username> does not exist.
116 The PerlAuthzHandler for use in apache2.
122 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
124 =head1 COPYRIGHT AND LICENSE
126 Copyright (C) 2013 by Marius Gavrilescu
128 This library is free software; you can redistribute it and/or modify
129 it under the same terms as Perl itself, either Perl version 5.14.2 or,
130 at your option, any later version of Perl 5 you may have available.
This page took 0.025201 seconds and 4 git commands to generate.