Access.pm

filter: Statementstmt 78.3% 163 / 208 Branchbran 71.1% 84 / 118 Conditioncond 66.6% 14 / 21 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 95.8% 23 / 24 Podpod 94.4% 17 / 18 totaltotal 76.6% 309 / 403 CC 85 SCAR 43.0
CC
85
Cov
75%
write_pretty
380.0
parse_acl
17.1
write_acl
14.6
CRAP 73.8
↑ Summary Access/Group.pm »
1 package SVN::Access;
2  
3 1 ▶use SVN::Access::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use SVN::Access::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use open ':encoding(utf8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.10';
13  
14 sub new {
15 9 ▶ my ($class, %attr) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
16 9 my $self = bless(\%attr, $class);
17
18 # it's important that we have this.
19 9 ▶ die "No ACL file specified!" unless $attr{acl_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $attr{"acl_file"} 0 9 9
20  
21 9 ▶ if (-e $attr{acl_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $attr{'acl_file'}) { } 8 1 9
22 # parse the file in.
23 8 $self->parse_acl;
24 } else {
25 # empty acl.
26 1 $attr{acl} = {};
27 }
28 9 return $self;
29 }
30  
31 sub parse_acl {
32 8 ▶ my ($self) = @_;
Subroutine parse_acl: called CC=17 SCAR=28.4
Pod undocumented
33 8 ▶ open(ACL, '<', $self->{acl_file}) or die "Can't open SVN Access file " . $self->{acl_file} . ": $!";
Branch: 1 branch
BranchTrueFalseTotal
open ACL, "<", $self->{"acl_file"} or die q[Can't open SVN Access file ] . $self->{"acl_file"} . ": $!" 0 8 8
34 8 my $current_resource;
35 my $statement;
36 8 ▶ while (my $line = <ACL>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline ACL)) 129 8 137
37 # ignore comments (properly defined)
38 129 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 4 125 129
39 125 ▶ $line =~ s/\s*#.*$// unless $self->{pedantic};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pedantic"} 114 11 125
40  
41 # get rid of trailing whitespace.
42 125 $line =~ s/[\s\r\n]+$//;
43 125 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 31 94 125
44  
45 # handle line continuations
46 94 ▶ if ($line =~ /^\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s+(.+)$/) { } 4 90 94
47 4 $statement .= " $1";
48 } else {
49 90 $statement = $line;
50 }
51  
52 # lookahead and see if the next line is a line continuation
53 94 my $pos = tell(ACL);
54 94 my $nextline = <ACL>;
55 94 seek(ACL, $pos, 0); # rewind the filehandle to where we were.
56 94 ▶ if ($nextline && $nextline =~ /^[ \t]+\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextline and $nextline =~ /^[ \t]+\S/) 4 90 94
Condition100%3/3
$nextline and $nextline =~ /^[ \t]+\S/
!ll&&!rl&&r
1894
MC/DC100%2/2
$nextline and $nextline =~ /^[ \t]+\S/
$nextline $nextline =~ /^[ \t]+\S/
Truth table100%3/3
A and B
A= $nextlineB= $nextline =~ /^[ \t]+\S/
ABresult
0X0
100
111
57 4 next;
58 }
59  
60 90 ▶ next unless $statement;
Branch: 1 branch
BranchTrueFalseTotal
unless $statement 0 90 90
61  
62 90 ▶ if ($statement =~ /^\[\s*(.+?)\s*\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement =~ /^\[\s*(.+?)\s*\]$/) { } 31 59 90
63 # this statement is defining a new resource.
64 31 $current_resource = $1;
65 31 ▶ unless ($current_resource =~ /^(?:groups|aliases)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current_resource =~ /^(?:groups|aliases)$/) 20 11 31
66 20 $self->add_resource($current_resource);
67 }
68 } else {
69 # both groups and resources need this parsed.
70 59 my ($k, $v) = $statement =~ /^(.+?)\s*=\s*(.*?)$/;
71  
72 # if the previous split didn't work, there's a syntax error
73 59 ▶ unless ($k) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k) 2 57 59
74 2 warn "Unrecognized line $statement\n";
75 2 next;
76 }
77  
78 57 ▶ if ($current_resource eq "groups") {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_resource eq 'groups') { } 17 40 57
elsif ($current_resource eq 'aliases') { } 4 36 40
79 # this is a group
80 17 $self->add_group($k, split(/\s*,\s*/, $v));
81 } elsif ($current_resource eq "aliases") {
82 # aliases are simple k=v, so let's just store them in ourselves.
83 4 $self->add_alias($k, $v);
84 } else {
85 # this is a generic resource
86 36 ▶ if (my $resource = $self->resource($current_resource)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resource = $self->resource($current_resource)) { } 36 0 36
87 36 $resource->authorize($k => $v);
88 } else {
89 0 $self->add_resource($current_resource, $k, $v);
90 }
91 }
92 }
93  
94 88 $statement = undef;
95 }
96
97 # make sure this isn't empty.
98 8 ▶ unless (ref($self->{acl}->{aliases}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"acl"}{"aliases"} eq "HASH") 4 4 8
99 4 $self->{acl}->{aliases} = {};
100 }
101
102 8 close (ACL);
103 }
104  
105 sub verify_acl {
106 6 ▶ my ($self) = @_;
Subroutine verify_acl: called CC=10 SCAR=23.0
Pod documented
107  
108 # Check for references to undefined groups (Thanks Jesse!)
109 6 my (%groups, @errors);
110 6 ▶ if ($self->groups) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->groups) 3 3 6
111 3 foreach my $group ($self->groups) {
112 3 $groups{$group->name}++;
113 }
114 }
115  
116 6 foreach my $resource ($self->resources) {
117 20 ▶ if (defined($resource) && $resource->authorized) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resource and $resource->authorized) 19 1 20
Condition66%2/3
defined $resource and $resource->authorized
!ll&&!rl&&r
1019
MC/DC50%1/2
defined $resource and $resource->authorized
defined $resource $resource->authorized
Truth table66%2/3
A and B
A= defined $resourceB= $resource->authorized
ABresult
0X0
100
111
118 19 foreach my $k (keys %{$resource->authorized}) {
119 35 ▶ if ( $k =~ /^@(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^@(.*)/) 5 30 35
120 5 ▶ unless ( $groups{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($groups{$1}) 2 3 5
121 2 push(@errors, "[error] An authz rule (" . $resource->name . ") refers to group '\@$1', which is undefined");
122 }
123 }
124 }
125 }
126 }
127  
128 6 ▶ return scalar(@errors) ? join("\n", @errors) : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @errors ? : 1 5 6
129 }
130  
131 sub write_acl {
132 6 ▶ my ($self, $out) = @_;
Subroutine write_acl: called CC=14 SCAR=26.8
Pod documented
133  
134 # verify the ACL has no errors before writing it out
135 6 ▶ if (my $error = $self->verify_acl) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->verify_acl) 1 5 6
136 1 die "Error found in ACL:\n$error\n";
137 }
138  
139 5 ▶ if (ref \$out eq "GLOB" or ref $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$out eq 'GLOB' or ref $out) { } 0 5 5
Condition33%1/3
ref \$out eq "GLOB" or ref $out
l!l&&r!l&&!r
005
MC/DC0%0/2
ref \$out eq "GLOB" or ref $out
ref \$out eq "GLOB" ref $out
Truth table33%1/3
A or B
A= ref \$out eq "GLOB"B= ref $out
ABresult
1X1
011
000
140 0 *ACL = $out;
141 }
142 else {
143 5 ▶ $out = $self->{acl_file} unless $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $out 5 0 5
144 5 ▶ open (ACL, '>', $out) or warn "Can't open ACL file " . $out . " for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ACL, ">", $out or warn q[Can't open ACL file ] . $out . " for writing: $!\n" 0 5 5
145 }
146
147 # aliases now supported!
148 5 ▶ if (scalar(keys %{$self->aliases})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->aliases;}) 2 3 5
149 2 print ACL "[aliases]\n";
150 2 foreach my $alias (keys %{$self->aliases}) {
151 2 print ACL $alias . " = " . $self->aliases->{$alias} . "\n";
152 }
153 2 print ACL "\n";
154 }
155
156 # groups now second to aliases
157 5 ▶ if ($self->groups) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->groups) 3 2 5
158 3 print ACL "[groups]\n";
159 3 foreach my $group ($self->groups) {
160 3 print ACL $group->name . " = " . join(', ', $group->members) . "\n";
161 }
162 3 print ACL "\n";
163 }
164
165 5 foreach my $resource ($self->resources) {
166 14 ▶ if (defined($resource) && $resource->authorized) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resource and $resource->authorized) 13 1 14
Condition66%2/3
defined $resource and $resource->authorized
!ll&&!rl&&r
1013
MC/DC50%1/2
defined $resource and $resource->authorized
defined $resource $resource->authorized
Truth table66%2/3
A and B
A= defined $resourceB= $resource->authorized
ABresult
0X0
100
111
167 13 print ACL "[" . $resource->name . "]\n";
168 13 ▶ while (my ($k, $v) = (each %{$resource->authorized})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$resource->authorized;}) 23 13 36
169 23 print ACL "$k = $v\n";
170 }
171 13 print ACL "\n";
172 }
173 }
174
175 5 close(ACL);
176 }
177  
178 sub write_pretty {
179 0 ▶ my ($self) = @_;
Subroutine write_pretty: not called CC=19 SCAR=59.4
Pod documented
180  
181 # verify the ACL has no errors before writing it out
182 0 ▶ if (my $error = $self->verify_acl) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->verify_acl) 0 0 0
183 0 die "Error found in ACL:\n$error\n";
184 }
185  
186 0 my $max_len = 0;
187  
188 # Compile a list of names that will appear on the left side
189 0 my @names;
190 0 ▶ if (scalar(keys %{$self->aliases})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->aliases;}) 0 0 0
191 0 foreach my $alias (keys %{$self->aliases}) {
192 0 push(@names, $alias);
193 }
194 }
195
196 0 ▶ if ($self->groups) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->groups) 0 0 0
197 0 for ($self->groups) {
198 0 push(@names, $_->name);
199 }
200 }
201 0 ▶ if ($self->resources) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resources) 0 0 0
202 0 for ($self->resources) {
203 0 push(@names, keys(%{$_->authorized}));
204 }
205 }
206  
207 # Go through that list looking for the longest name
208 0 for (@names) {
209 0 ▶ $max_len = length($_) >= $max_len ? length($_) : $max_len;
Branch: 1 branch
BranchTrueFalseTotal
length $_ >= $max_len ? : 0 0 0
210 }
211  
212 0 ▶ open (ACL, '>', $self->{acl_file}) or warn "Can't open ACL file " . $self->{acl_file} . " for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ACL, ">", $self->{"acl_file"} or warn q[Can't open ACL file ] . $self->{"acl_file"} . " for writing: $!\n" 0 0 0
213
214 # aliases now fully supported!
215 0 ▶ if (scalar(keys %{$self->aliases})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->aliases;}) 0 0 0
216 0 print ACL "[aliases]\n";
217 0 foreach my $alias (keys %{$self->aliases}) {
218 0 print ACL $alias . " " x ($max_len - length($alias)) . " = " . $self->aliases->{$alias} . "\n";
219 }
220 0 print "\n";
221 }
222
223 # groups now second?
224 0 ▶ if ($self->groups) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->groups) 0 0 0
225 0 print ACL "[groups]\n";
226 0 foreach my $group ($self->groups) {
227 0 print ACL $group->name . " " x ($max_len - length($group->name)) . " = " . join(', ', $group->members) . "\n";
228 }
229 0 print "\n";
230 }
231
232 0 foreach my $resource ($self->resources) {
233 0 ▶ if (defined($resource) && $resource->authorized) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resource and $resource->authorized) 0 0 0
Condition0%0/3
defined $resource and $resource->authorized
!ll&&!rl&&r
000
MC/DC0%0/2
defined $resource and $resource->authorized
defined $resource $resource->authorized
Truth table0%0/3
A and B
A= defined $resourceB= $resource->authorized
ABresult
0X0
100
111
234 0 print ACL "[" . $resource->name . "]\n";
235 0 ▶ while (my ($k, $v) = (each %{$resource->authorized})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$resource->authorized;}) 0 0 0
236 0 print ACL "$k" . " " x ($max_len - length($k)) . " = $v\n";
237 }
238 0 print ACL "\n";
239 }
240 }
241 0 close(ACL);
242 }
243  
244 sub add_alias {
245 6 ▶ my ($self, $alias_name, $aliased) = @_;
Subroutine add_alias: called CC=1 SCAR=0.0
Pod documented
246 6 $self->{acl}->{aliases}->{$alias_name} = $aliased;
247 }
248  
249 sub remove_alias {
250 2 ▶ my ($self, $alias_name) = @_;
Subroutine remove_alias: called CC=1 SCAR=0.0
Pod documented
251 2 delete $self->{acl}->{aliases}->{$alias_name};
252 }
253  
254 sub alias {
255 4 ▶ my ($self, $alias_name) = @_;
Subroutine alias: called CC=2 SCAR=6.9
Pod documented
256 4 ▶ if (exists ($self->{acl}->{aliases}->{$alias_name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"acl"}{"aliases"}{$alias_name}) 3 1 4
257 3 return $self->{acl}->{aliases}->{$alias_name};
258 }
259 1 return undef;
260 }
261  
262 sub aliases {
263 10 ▶ my ($self) = @_;
Subroutine aliases: called CC=2 SCAR=6.9
Pod documented
264 # give em something if we got nothing!
265 10 ▶ unless (ref($self->{acl}->{aliases}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"acl"}{"aliases"} eq "HASH") 1 9 10
266 1 $self->{acl}->{aliases} = {};
267 }
268 10 return $self->{acl}->{aliases};
269 }
270  
271 sub add_resource {
272 28 ▶ my ($self, $resource_name, @access) = @_;
Subroutine add_resource: called CC=4 SCAR=14.5
Pod documented
273 28 ▶ if ($resource_name eq "name") {
Branch: 1 branch
BranchTrueFalseTotal
if ($resource_name eq "name") 1 27 28
274 1 $resource_name = shift(@access);
275 }
276
277 28 ▶ if ($self->resource($resource_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->resource($resource_name)) { } 0 28 28
elsif (not $resource_name =~ m[^(?:\S+\:)?/.*$]) { } 0 28 28
278 0 die "Can't add new resource $resource_name: resource already exists!\n";
279 } elsif ($resource_name !~ /^(?:\S+\:)?\/.*$/) { # Thanks Matt
280 0 die "Invalid resource format in $resource_name! (format 'repo:/path')!\n";
281 } else {
282 28 my $resource = SVN::Access::Resource->new(
283 name => $resource_name,
284 authorized => \@access,
285 );
286 28 push(@{$self->{acl}->{resources}}, $resource);
287 28 return $resource;
288 }
289 }
290  
291 sub remove_resource {
292 8 ▶ my ($self, $resource_name) = @_;
Subroutine remove_resource: called CC=4 SCAR=13.9
Pod documented
293 8 my @resources;
294 8 foreach my $resource ($self->resources) {
295 33 ▶ push(@resources, $resource) unless $resource->name eq $resource_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource->name eq $resource_name 25 8 33
296 }
297 8 ▶ $self->{acl}->{resources} = scalar(@resources) ? \@resources : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @resources ? : 7 1 8
298 }
299  
300 sub resources {
301 91 ▶ my ($self) = @_;
Subroutine resources: called CC=2 SCAR=6.9
Pod documented
302 91 ▶ if (ref($self->{acl}->{resources}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'acl'}{'resources'} eq 'ARRAY') { } 79 12 91
303 79 return (@{$self->{acl}->{resources}});
304 } else {
305 12 return (undef);
306 }
307 }
308  
309 sub resource {
310 71 ▶ my ($self, $resource_name) = @_;
Subroutine resource: called CC=4 SCAR=13.9
Pod documented
311 71 foreach my $resource ($self->resources) {
312 197 ▶ return $resource if defined($resource) && $resource->name eq $resource_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $resource and $resource->name eq $resource_name 43 154 197
Condition100%3/3
defined $resource and $resource->name eq $resource_name
!ll&&!rl&&r
914543
MC/DC100%2/2
defined $resource and $resource->name eq $resource_name
defined $resource $resource->name eq $resource_name
Truth table100%3/3
A and B
A= defined $resourceB= $resource->name eq $resource_name
ABresult
0X0
100
111
313 }
314 28 return undef;
315 }
316  
317 sub add_group {
318 18 ▶ my ($self, $group_name, @initial_members) = @_;
Subroutine add_group: called CC=3 SCAR=11.1
Pod documented
319  
320 # get rid of the @ symbol.
321 18 ▶ $group_name =~ s/\@//g unless $self->{pedantic};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pedantic"} 14 4 18
322  
323 18 ▶ if ($self->group($group_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group($group_name)) { } 0 18 18
324 0 die "Can't add new group $group_name: group already exists!\n";
325 } else {
326 18 my $group = SVN::Access::Group->new(
327 name => $group_name,
328 members => \@initial_members,
329 );
330 18 push(@{$self->{acl}->{groups}}, $group);
331 18 return $group;
332 }
333 }
334  
335 sub remove_group {
336 1 ▶ my ($self, $group_name) = @_;
Subroutine remove_group: called CC=4 SCAR=14.2
Pod documented
337 1 my @groups;
338  
339 # get rid of the @ symbol.
340 1 $group_name =~ s/\@//g;
341 1 foreach my $group ($self->groups) {
342 1 ▶ push(@groups, $group) unless $group->name eq $group_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $group->name eq $group_name 0 1 1
343 }
344  
345 1 ▶ $self->{acl}->{groups} = scalar(@groups) ? \@groups : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @groups ? : 0 1 1
346 }
347  
348 sub groups {
349 55 ▶ my ($self) = @_;
Subroutine groups: called CC=2 SCAR=6.9
Pod documented
350 55 ▶ if (ref($self->{acl}->{groups}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'acl'}{'groups'} eq 'ARRAY') { } 41 14 55
351 41 return (@{$self->{acl}->{groups}});
352 } else {
353 14 return (undef);
354 }
355 }
356  
357 sub group {
358 36 ▶ my ($self, $group_name) = @_;
Subroutine group: called CC=4 SCAR=13.9
Pod documented
359 36 foreach my $group ($self->groups) {
360 71 ▶ return $group if defined($group) && $group->name eq $group_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $group and $group->name eq $group_name 18 53 71
Condition100%3/3
defined $group and $group->name eq $group_name
!ll&&!rl&&r
84518
MC/DC100%2/2
defined $group and $group->name eq $group_name
defined $group $group->name eq $group_name
Truth table100%3/3
A and B
A= defined $groupB= $group->name eq $group_name
ABresult
0X0
100
111
361 }
362 18 return undef;
363 }
364  
365 sub resolve {
366 8 ▶ my $self = shift;
Subroutine resolve: called CC=6 SCAR=18.0
Pod documented
367 8 my @res;
368  
369 8 foreach my $e (@_) {
370 8 ▶ if ($e =~ /^\@(.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e =~ /^\@(.+)/) { } 4 4 8
elsif ($e =~ /^\&(.+)/) { } 1 3 4
371 4 ▶ push @res, map $self->resolve($_), $self->group($1)->members()
Branch: 1 branch
BranchTrueFalseTotal
if $self->group($1) 4 0 4
372 if $self->group($1);
373 } elsif ($e =~ /^\&(.+)/) {
374 1 ▶ push @res, map $self->resolve($_), $self->alias($1)
Branch: 1 branch
BranchTrueFalseTotal
if $self->alias($1) 1 0 1
375 if $self->alias($1);
376 } else {
377 3 push @res, $e;
378 }
379 }
380  
381 8 return @res;
382 }
383  
384 1 1;
385 __END__
↑ Summary Access/Group.pm »