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