1 | package LATMOS::Accounts::Acls::Acl; |
---|
2 | use strict; |
---|
3 | use warnings; |
---|
4 | |
---|
5 | =head1 NAME |
---|
6 | |
---|
7 | LATMOS::Accounts::Acls::Acl - Single acl entry |
---|
8 | |
---|
9 | =head1 DESCRIPTION |
---|
10 | |
---|
11 | Manage a single acl entry |
---|
12 | |
---|
13 | =head1 FUNCTIONS |
---|
14 | |
---|
15 | =head2 new ($objdotatt, $list) |
---|
16 | |
---|
17 | Create new sub acl where C<$objdotatt> is object in form C<Object.Attribute> and |
---|
18 | C<$list> the list of permission. |
---|
19 | |
---|
20 | =cut |
---|
21 | |
---|
22 | sub new { |
---|
23 | my ($class, $objdotatt, $list) = @_; |
---|
24 | my ($obj, $attr) = $objdotatt =~ /^([^.]+)\.(.*)/ or return; |
---|
25 | my @attrs = ($attr =~ /^\{(.*)\}$/) |
---|
26 | ? (split(/\s*,\s*/, $1)) |
---|
27 | : ($attr); |
---|
28 | @attrs or return; |
---|
29 | my $acl = { |
---|
30 | obj => lc($obj), |
---|
31 | attr => [ map { lc } @attrs ], |
---|
32 | users => [], # user->{r} = 1 |
---|
33 | }; |
---|
34 | bless($acl, $class); |
---|
35 | foreach my $k (@{ $list || [] }) { |
---|
36 | $acl->add_perm($k) or return; |
---|
37 | } |
---|
38 | $acl |
---|
39 | } |
---|
40 | |
---|
41 | =head2 add_perm($perm) |
---|
42 | |
---|
43 | Add a permission to this sub acl |
---|
44 | |
---|
45 | =cut |
---|
46 | |
---|
47 | sub add_perm { |
---|
48 | my ($self, $perm) = @_; |
---|
49 | my ($username, $perms) = $perm =~ /^\s*(\S*):\s*(.*)$/; |
---|
50 | $username && $perms or return; |
---|
51 | my $user = { user => $username }; |
---|
52 | foreach (split(/\s*,\s*/, $perms)) { |
---|
53 | /^read$/i and do { $user->{r} = 1; next; }; |
---|
54 | /^write$/i and do { $user->{w} = 1; next; }; |
---|
55 | /^deny$/i and do { |
---|
56 | # reseting... |
---|
57 | $user->{r} = 0; |
---|
58 | $user->{w} = 0; |
---|
59 | last; # we end here, life is hard |
---|
60 | }; |
---|
61 | return; |
---|
62 | } |
---|
63 | push(@{$self->{users}}, $user); |
---|
64 | 1 |
---|
65 | } |
---|
66 | |
---|
67 | =head2 match($obj, $attr, $perm, $who, $groups) |
---|
68 | |
---|
69 | Return true is this sub acl apply to C<$obj>/C<$attr> for C<$perm> by user |
---|
70 | C<$who> in groups C<$groups>. |
---|
71 | |
---|
72 | =cut |
---|
73 | |
---|
74 | sub match { |
---|
75 | my ($self, $obj, $attr, $perm, $who, $groups) = @_; |
---|
76 | my $objtype = ref $obj ? lc($obj->type) : $obj; |
---|
77 | $attr = lc($attr); |
---|
78 | |
---|
79 | # Does this ACL series concern this object: |
---|
80 | if (!($self->{obj} eq '*' || $self->{obj} eq $objtype)) { |
---|
81 | return |
---|
82 | } |
---|
83 | # Does this ACL series concern this attribute: |
---|
84 | grep { $_ eq '*' || $_ eq $attr } @{$self->{attr}} or return; |
---|
85 | |
---|
86 | # Foreach user, testing if this permission match: |
---|
87 | foreach my $u (@{ $self->{users} }) { |
---|
88 | # Obj have attr eq login user |
---|
89 | if (substr($u->{user}, 0, 1) eq '$') { # check attr content |
---|
90 | if (ref $obj) { |
---|
91 | my $attribute = substr($u->{user}, 1); |
---|
92 | my $val = $obj->_get_c_field($attribute) or return; |
---|
93 | my @vals = ref $val ? (@{ $val }) : ($val); |
---|
94 | return $u->{$perm} if (defined($u->{$perm}) && grep { $_ eq $who } @vals); |
---|
95 | } |
---|
96 | # user is in group |
---|
97 | } elsif (substr($u->{user}, 0, 1) eq '%') { # group |
---|
98 | my $group = substr($u->{user}, 1); |
---|
99 | return $u->{$perm} if (defined($u->{$perm}) && grep { $group eq $_ } grep { $_ } @{$groups ||[]}); |
---|
100 | # any user |
---|
101 | } elsif ($u->{user} eq '*' || $u->{user} eq $who) { |
---|
102 | return $u->{$perm} if (defined($u->{$perm})); |
---|
103 | # any authenticated user |
---|
104 | } elsif (lc($u->{user}) eq '@authenticated' && $who) { |
---|
105 | return $u->{$perm} if (defined($u->{$perm})); |
---|
106 | # not login |
---|
107 | } elsif (lc($u->{user}) eq '@anonymous' && $who eq "") { |
---|
108 | return $u->{$perm} if (defined($u->{$perm})); |
---|
109 | } |
---|
110 | } |
---|
111 | return; |
---|
112 | } |
---|
113 | |
---|
114 | =head2 dump |
---|
115 | |
---|
116 | Return a textual dump of the sub acl |
---|
117 | |
---|
118 | =cut |
---|
119 | |
---|
120 | sub dump { |
---|
121 | my ($self) = @_; |
---|
122 | my $dump = sprintf("%s.{%s}\n", $self->{obj}, join(', ', @{$self->{attr}})); |
---|
123 | foreach my $u (@{ $self->{users} }) { |
---|
124 | $dump .= sprintf("\t%s: %s\n", |
---|
125 | $u->{user}, |
---|
126 | join(', ', ($u->{r} ? 'read' : ()), ($u->{w} ? 'write' : ())) || |
---|
127 | 'deny'); |
---|
128 | } |
---|
129 | $dump |
---|
130 | } |
---|
131 | |
---|
132 | 1; |
---|
133 | |
---|
134 | __END__ |
---|
135 | |
---|
136 | =head1 SEE ALSO |
---|
137 | |
---|
138 | L<LATMOS::Accounts::Acls> |
---|
139 | |
---|
140 | =head1 AUTHOR |
---|
141 | |
---|
142 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
143 | |
---|
144 | =head1 COPYRIGHT AND LICENSE |
---|
145 | |
---|
146 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
147 | |
---|
148 | This library is free software; you can redistribute it and/or modify |
---|
149 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
150 | at your option, any later version of Perl 5 you may have available. |
---|
151 | |
---|
152 | =cut |
---|