WE/Util/Permissions.pm

filter: Statementstmt 85.9% 116 / 135 Branchbran 70.9% 44 / 62 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 94.1% 16 / 17 Podpod 100% 7 / 7 totaltotal 82.3% 186 / 226 CC 39 SCAR 21.5
CC
39
Cov
81%
new
19.1
_match
14.0
_eval_obj
12.0
CRAP 8.6
« WebEditor/OldHandler.pm ↑ Summary WE_Content/Base.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Permissions.pm,v 1.10 2004/10/11 22:08:40 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002,2004 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE::Util::Permissions;
18 2 ▶use WE::Util::GenericTree::FromString;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 $VERSION = sprintf("%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/);
23  
24 2 ▶use constant DEBUG => 0; # possible values 0 .. 2
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 WE::Util::Permissions - rule-based permission model
29  
30 =head1 SYNOPSIS
31  
32 use WE::Util::Permissions;
33 $p = WE::Util::Permissions->new(-file => $permissionsfile);
34 $p->is_allowed(-user => "user", -group => \@groups, -process => "delete");
35  
36 =head1 DESCRIPTION
37  
38 This is a rule-based permission system. All permissions are stored in
39 a single file, so it is easy to see all the permissions on one look.
40  
41 See L</PERMISSION FILE SYNTAX> for a description of this file's
42 syntax.
43  
44 =head2 METHODS
45  
46 =over
47  
48 =item new($class, %args)
49  
50 Create a C<WE::Util::Permissions> object. At least one of the following named parameters should be given:
51  
52 =over
53  
54 =item -string => $string
55  
56 A string with the permission data.
57  
58 =item -file => $file
59  
60 A file with the permission data.
61  
62 =item -objectfromfile => $file
63  
64 A file containg a dump of a Permission object.
65  
66 =back
67  
68 =cut
69  
70 13 ▶sub new {
Subroutine new: called CC=6 SCAR=29.5
Pod documented
71 13 my($class, %args) = @_;
72 13 my $self = {};
73 13 $self->{Directives} = {};
74 13 bless $self, $class;
75 13 ▶ if ($args{'-string'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'-string'}) { } 13 0 13
elsif ($args{'-file'}) { } 0 0 0
elsif ($args{'-objectfromfile'}) { } 0 0 0
76 13 $self->parse($args{'-string'});
77 } elsif ($args{'-file'}) {
78 0 local $/ = undef;
79 0 ▶ open(F, $args{'-file'}) or die "Can't open $args{'-file'}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, $args{"-file"} or die qq[Can't open $args{"-file"}: $!] 0 0 0
80 0 my($s) = <F>;
81 0 close F;
82 0 $self->parse($s);
83 } elsif ($args{'-objectfromfile'}) {
84 0 local $/ = undef;
85 0 ▶ open(F, $args{'-objectfromfile'}) or die "Can't open $args{'-objectfromfile'}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, $args{"-objectfromfile"} or die qq[Can't open $args{"-objectfromfile"}: $!] 0 0 0
86 0 my($s) = <F>;
87 0 close F;
88 0 $self->_eval_obj($s);
89 } else {
90 0 die "Either -string or -file should be given as argument!";
91 }
92 12 $self;
93 }
94  
95 =item parse($string)
96  
97 Internal function. Parse the C<$string> into the internal
98 representation. Normally, this is called automatically on construction
99 time.
100  
101 =cut
102  
103 13 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
104 13 my($self, $string) = @_;
105 13 my $new_s = "";
106 13 my $directives = {};
107 13 foreach my $line (split/\n/, $string) {
108 91 $line =~ s/\#.*$//; # delete comments
109 91 $line =~ s/\s+$//; # delete whitespace on end
110 91 ▶ next if $line eq ''; # ignore empty lines
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 7 84 91
111 84 ▶ if (my($key,$val) = $line =~ /^!\s*(\S+)\s*:\s*(.+)$/) { # directive
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /^!\s*(\S+)\s*:\s*(.+)$/) 10 74 84
112 10 ▶ if (exists $directives->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $directives->{$key}) 1 9 10
113 1 die "Can't set multiple $key directives in permissions file";
114 }
115 9 $directives->{$key} = $val;
116 }
117 83 $new_s .= $line . "\n";
118 }
119 12 my $tree = WE::Util::GenericTree::FromString->new($new_s);
120 12 $self->{Def} = $tree;
121 12 $self->{Directives} = $directives;
122 }
123  
124 0 ▶sub _eval_obj {
Subroutine _eval_obj: not called CC=3 SCAR=24.8
125 0 my($self, $s) = @_;
126 0 eval 'package Permissions::_eval_; ' . $s;
127 0 ▶ die "Can't eval $s: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
128 0 ▶ die "No Def object in file" unless defined $Permissions::_eval_::Def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Permissions::_eval_::Def 0 0 0
129 0 $self->{Def} = $Permissions::_eval_::Def;
130 0 undef $Permissions::_eval_::Def;
131 }
132  
133 =item save($file)
134  
135 Save the Permission object to file C<$file>. The file may be reread
136 using the C<-objectfromfile> argument in C<new>.
137  
138 =cut
139  
140 1 ▶sub save {
Subroutine save: called CC=2 SCAR=7.0
Pod documented
141 1 my($self, $file) = @_;
142 1 ▶ open(F, ">$file") or die "Can't write to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, ">$file" or die "Can't write to ${file}: $!" 1 0 1
143 1 require Data::Dumper;
144 1 print F Data::Dumper->Dump([$self->{Def}], ['Def']);
145 1 close F;
146 }
147  
148 =item is_allowed(%args)
149  
150 Return true, if the process for the specified user/group and specified
151 page is allowed. The keys of C<%args> may be: C<-user>, C<-group> (an
152 array reference to a group list), C<-process>, and C<-page>.
153  
154 =cut
155  
156 124 ▶sub is_allowed {
Subroutine is_allowed: called CC=2 SCAR=6.9
Pod documented
157 124 my($self, %args) = @_;
158 124 my %new_args;
159 124 ▶ while(my($k,$v) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 420 124 544
160 420 $new_args{substr($k,1)} = $v; # strip dash
161 }
162 124 $self->_is_allowed($self->{Def}, \%new_args);
163 }
164  
165 =item get_all_users($usersref, $process, $page)
166  
167 Return a list of all users which are allowed to do C<$process> in
168 C<$page>. The C<$usersref> should contain all users in the system and
169 may be a reference to an array or a reference to an hash. In the
170 latter case, the keys are the user names and the values an array
171 reference to the groups of the user. For example:
172  
173 $p->get_all_users([qw(eserte ole veit)], "publish", "home");
174  
175 $p->get_all_users({eserte => ['admin','editor'],
176 ole => ['admin'],
177 veit => ['editor']}, "publish", "home");
178  
179 =cut
180  
181 9 ▶sub get_all_users {
Subroutine get_all_users: called CC=7 SCAR=19.7
Pod documented
182 9 my($self, $usersref, $process, $page) = @_;
183  
184 9 my @res;
185 my @all_users;
186 9 my %groups;
187 9 ▶ if (ref $usersref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $usersref eq 'HASH') { } 9 0 9
188 9 @all_users = keys %$usersref;
189 9 %groups = %$usersref;
190 } else {
191 0 @all_users = @$usersref;
192 }
193  
194 9 my @args;
195 9 ▶ if (defined $process) { push @args, -process => $process }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $process) 9 0 9
196 9 ▶ if (defined $page) { push @args, -page => $page }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page) 6 3 9
197 9 foreach my $user (@all_users) {
198 my @group_arg = (exists $groups{$user}
199 63 ▶ ? (-group => $groups{$user})
Branch: 1 branch
BranchTrueFalseTotal
exists $groups{$user} ? : 63 0 63
200 : ()
201 );
202 63 ▶ push @res, $user if $self->is_allowed(-user => $user,
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_allowed("-user", $user, @group_arg, @args) 28 35 63
203 @group_arg,
204 @args);
205 }
206 9 @res;
207 }
208  
209 =item get_all_page_permissions($usersref, $processref, $page)
210  
211 Return permissions for all users for the specified C<$page>. Arguments
212 are similar to that of C<get_all_users>, except that C<$processref>
213 takes an array reference with all allowed processes. The returned
214 object is a hash reference with the following format:
215  
216 { process1 => [user1, user2, ...],
217 process2 => [user3, user4, ...],
218 ...
219 }
220  
221 =cut
222  
223 1 ▶sub get_all_page_permissions {
Subroutine get_all_page_permissions: called CC=2 SCAR=6.9
Pod documented
224 1 my($self, $usersref, $processref, $page, %args) = @_;
225 1 my $info = {};
226 1 foreach my $process (@$processref) {
227 4 $info->{$process} = [$self->get_all_users($usersref, $process, $page)];
228 }
229 1 $info;
230 }
231  
232 264 ▶sub _is_allowed {
Subroutine _is_allowed: called CC=5 SCAR=16.1
233 264 my($self, $tree, $args_ref) = @_;
234 264 foreach my $subtree ($tree->subtree) {
235 532 ▶ if ($self->_match($subtree->data, $args_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($subtree->data, $args_ref)) 199 331 530
236 199 ▶ if (@{$subtree->subtree}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$subtree->subtree;}) { } 140 59 199
237 140 my $r = $self->_is_allowed($subtree, $args_ref);
238 140 ▶ return 1 if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 75 65 140
239 } else {
240 59 return 1;
241 }
242 }
243 }
244 128 0;
245 }
246  
247 532 ▶sub _match {
Subroutine _match: called CC=14 SCAR=26.4
248 532 my($self, $perm, $args_ref) = @_;
249 my $matchtype = ($self->{Directives} && $self->{Directives}{match}
250 532 ▶ ? $self->{Directives}{match} : 'glob');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Directives'} && $self->{'Directives'}{'match'} ? : 206 326 532
Condition66%2/3
$self->{"Directives"} and $self->{"Directives"}{"match"}
!ll&&!rl&&r
0326206
MC/DC50%1/2
$self->{"Directives"} and $self->{"Directives"}{"match"}
$self->{"Directives"} $self->{"Directives"}{"match"}
Truth table66%2/3
A and B
A= $self->{"Directives"}B= $self->{"Directives"}{"match"}
ABresult
0X0
100
111
251 532 my(@big_or) = split /\s*;\s*/, $perm;
252 532 foreach my $term (@big_or) {
253 605 my(@args) = split /[\s,]+/, $term;
254 605 my $permtype = shift @args;
255 605 my $args_permtype = $args_ref->{$permtype};
256 605 warn "term @args against " .
257 (defined $args_permtype
258 ? (ref $args_permtype eq 'ARRAY'
259 ? "@$args_permtype"
260 : $args_permtype
261 )
262 : "<undef>"
263 ) . " ...\n" if DEBUG;
264 605 ▶ if ($args_permtype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_permtype) 548 57 605
265 my @terms = (ref $args_permtype eq 'ARRAY'
266 548 ▶ ? @{ $args_permtype }
Branch: 1 branch
BranchTrueFalseTotal
ref $args_permtype eq 'ARRAY' ? : 132 416 548
267 : $args_permtype
268 );
269 548 foreach my $arg_ (@args) {
270 686 my $arg = $arg_;
271 686 my $no = 0;
272 686 ▶ if ($arg =~ /^!(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^!(.*)/) 41 645 686
273 41 $arg = $1;
274 41 $no = 1;
275 }
276 686 my $check_sub;
277 686 ▶ if ($matchtype eq 'glob') {
Branch: 2 branches
BranchTrueFalseTotal
if ($matchtype eq 'glob') { } 611 75 686
elsif ($matchtype =~ /^(rx|regexp?)$/) { } 74 1 75
278 611 my $repl = { '*' => '.*',
279 '?' => '.',
280 };
281 611 ▶ if ($arg =~ /[\*\?]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /[\*\?]/) { } 57 554 611
282 57 $arg =~ s/(.*?)([\*\?])([^\*\?]*)/"\Q$1\E" . $repl->{$2} . "\Q$3\E"/ge;
283 57 $arg = qr/^$arg$/;
284 57 warn "Glob -> regexp: $arg\n" if DEBUG >= 2;
285 57 ▶ $check_sub = sub { /$arg/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
286 } else {
287 554 ▶ $check_sub = sub { $_ eq $arg };
Subroutine __ANON__: called CC=1 SCAR=0.0
288 }
289 } elsif ($matchtype =~ /^(rx|regexp?)$/) {
290 74 ▶ $check_sub = sub { /^$arg$/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
291 } else {
292 1 die "Invalid match type: $matchtype";
293 }
294  
295 685 ▶ if ($no) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no) { } 41 644 685
296 41 ▶ return 0 if grep { $check_sub->() } @terms;
Branch: 1 branch
BranchTrueFalseTotal
if grep {&$check_sub();} @terms 21 20 41
297 } else {
298 644 ▶ return 1 if grep { $check_sub->() } @terms;
Branch: 1 branch
BranchTrueFalseTotal
if grep {&$check_sub();} @terms 199 444 643
299 }
300 }
301 }
302 }
303 310 0;
304 }
305  
306 =item get_directive($directive)
307  
308 Return the value of the global directive C<$directive>, or undef.
309  
310 =cut
311  
312 1 ▶sub get_directive {
Subroutine get_directive: called CC=2 SCAR=7.6
Pod documented
313 1 my($self, $directive) = @_;
314 1 ▶ if (exists $self->{Directives}{$directive}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'Directives'}{$directive}) { } 1 0 1
315 1 $self->{Directives}{$directive};
316 } else {
317 0 undef;
318 }
319 }
320  
321 2 1;
322  
323 __END__
« WebEditor/OldHandler.pm ↑ Summary WE_Content/Base.pm »