]> iEval git - apache2-authzcaps.git/blame - lib/Apache2/AuthzCaps.pm
Bump version and update Changes
[apache2-authzcaps.git] / lib / Apache2 / AuthzCaps.pm
CommitLineData
2096e267 1package Apache2::AuthzCaps;
7442fd06
MG
2
3use 5.014000;
4use strict;
5use warnings;
75943539 6use subs qw/OK DECLINED/;
139b3491 7
b8d9e21f 8our $VERSION = '0.002';
7442fd06 9
75943539
MG
10use if $ENV{MOD_PERL}, 'Apache2::Access';
11use if $ENV{MOD_PERL}, 'Apache2::Const' => qw/OK DECLINED/;
12use if $ENV{MOD_PERL}, 'Apache2::RequestRec';
d586dfdf 13use if $ENV{MOD_PERL}, 'Apache2::RequestUtil';
7442fd06
MG
14use YAML::Any qw/LoadFile DumpFile/;
15
16use parent qw/Exporter/;
17
18our @EXPORT_OK = qw/setcap hascaps/;
19
20##################################################
21
22our $rootdir;
23
24sub setcap{
59f2225d
MG
25 my ($user, $cap, $value) = @_;
26 my $config = eval { LoadFile "$rootdir/$user.yml" } // {};
27 $config->{caps}//={};
28 my $caps=$config->{caps};
29
30 delete $caps->{$cap} unless $value;
31 $caps->{$cap} = 1 if $value;
32 DumpFile "$rootdir/$user.yml", $config
7442fd06
MG
33}
34
35sub hascaps{
59f2225d
MG
36 my ($user, @caps) = @_;
37 my $config = LoadFile "$rootdir/$user.yml";
38 my $caps = $config->{caps};
39 for (@caps) {
40 return 0 unless $caps->{$_}
41 }
42 1
7442fd06
MG
43}
44
45sub handler{
59f2225d
MG
46 my $r=shift;
47 my $user = $r->user;
48 local $rootdir = $r->dir_config('AuthzCapsRootdir');
7442fd06 49
59f2225d 50 if ($user) {
139b3491 51 for my $requirement (map { $_->{requirement} } @{$r->requires}) {
59f2225d 52 my ($command, @args) = split ' ', $requirement;
7442fd06 53
139b3491 54 return OK if $command eq 'cap' && hascaps $user, @args;
59f2225d 55 }
7442fd06 56 }
7442fd06 57
59f2225d 58 DECLINED
7442fd06
MG
59}
60
611;
62__END__
63
64=head1 NAME
65
66Apache2::AuthzCaps - mod_perl2 capability authorization
67
68=head1 SYNOPSIS
69
70 use Apache2::AuthzCaps qw/setcap hascaps/;
71 $Apache2::AuthzCaps::rootdir = "/path/to/user/directory"
72 setcap marius => deleteusers => 1; # Grant marius the deleteusers capability
73 setcap marius => createusers => 0;
74 hascaps marius => qw/deleteusers/; # returns 1, since marius can delete users
75 hascaps marius => qw/deleteusers createusers/; # returns 0, since marius can delete users but cannot create users
76
77 # In Apache2 config
78 <Location /protected>
79 # Insert authentication here
80 PerlAuthzHandler Apache2::AuthzCaps
81 PerlSetVar AuthzCapsRootdir /path/to/user/directory
82 Require cap staff important
83 Require cap admin
84 </Location>
85 # This will:
86 # 1) Let important staff members access /protected
87 # 2) Let admins access /protected
88 # 3) Not let anyone else (such as an important non-staff member or an non-important staff member) access /protected
89
90=head1 DESCRIPTION
91
92Apache2::AuthzCaps is a perl module which provides simple Apache2 capability-based authorization. It contains a PerlAuthzHandler and some utility functions.
93
d586dfdf
MG
94B<< For Apache 2.4, use L<Apache2_4::AuthzCaps>. >>
95
7442fd06
MG
96The user data is stored in YAML files in a user-set directory. Set this directory using:
97
98 $Apache2::AuthzCaps::rootdir = "/path/to/directory"; # From perl
99 PerlSetVar AuthzCapsRootdir /path/to/directory # From Apache2 config
100
101=head1 FUNCTIONS
102
103=over
104
105=item B<setcap>(I<$username>, I<$capability>, I<$value>)
106
107If I<$value> is true, grants I<$username> the I<$capability> capability. Otherwise denies I<$username> that capability.
108
109=item B<hascaps>(I<$username>, I<$cap>, ...)
110
111Returns true if and only of I<$username> has ALL of the listed capabilities. Dies if I<$username> does not exist.
112
113=item B<handler>
114
115The PerlAuthzHandler for use in apache2.
116
117=back
118
119=head1 AUTHOR
120
121Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
122
123=head1 COPYRIGHT AND LICENSE
124
d586dfdf 125Copyright (C) 2013-2015 by Marius Gavrilescu
7442fd06
MG
126
127This library is free software; you can redistribute it and/or modify
128it under the same terms as Perl itself, either Perl version 5.14.2 or,
129at your option, any later version of Perl 5 you may have available.
130
131
132=cut
This page took 0.047845 seconds and 5 git commands to generate.