import rt 3.2.2
[freeside.git] / rt / lib / RT / ACL_Overlay.pm
1 # {{{ BEGIN BPS TAGGED BLOCK
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
35
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
44
45 # }}} END BPS TAGGED BLOCK
46 =head1 NAME
47
48   RT::ACL - collection of RT ACE objects
49
50 =head1 SYNOPSIS
51
52   use RT::ACL;
53 my $ACL = new RT::ACL($CurrentUser);
54
55 =head1 DESCRIPTION
56
57
58 =head1 METHODS
59
60 =begin testing
61
62 ok(require RT::ACL);
63
64 =end testing
65
66 =cut
67
68 use strict;
69 no warnings qw(redefine);
70
71
72 =head2 Next
73
74 Hand out the next ACE that was found
75
76 =cut
77
78
79 # {{{ LimitToObject 
80
81 =head2 LimitToObject $object
82
83 Limit the ACL to rights for the object $object. It needs to be an RT::Record class.
84
85 =cut
86
87 sub LimitToObject {
88     my $self = shift;
89     my $obj = shift;
90     unless (defined($obj) && ref($obj) && UNIVERSAL::can($obj, 'id')) {
91     return undef;
92     }
93     $self->Limit(FIELD => 'ObjectType', OPERATOR=> '=', VALUE => ref($obj), ENTRYAGGREGATOR => 'OR');
94     $self->Limit(FIELD => 'ObjectId', OPERATOR=> '=', VALUE => $obj->id, ENTRYAGGREGATOR => 'OR', QUOTEVALUE => 0);
95
96 }
97
98 # }}}
99
100 # {{{ LimitToPrincipal 
101
102 =head2 LimitToPrincipal { Type => undef, Id => undef, IncludeGroupMembership => undef }
103
104 Limit the ACL to the principal with PrincipalId Id and PrincipalType Type
105
106 Id is not optional.
107 Type is.
108
109 if IncludeGroupMembership => 1 is specified, ACEs which apply to the principal due to group membership will be included in the resultset.
110
111
112 =cut
113
114 sub LimitToPrincipal {
115     my $self = shift;
116     my %args = ( Type                               => undef,
117                  Id                                 => undef,
118                  IncludeGroupMembership => undef,
119                  @_ );
120     if ( $args{'IncludeGroupMembership'} ) {
121         my $cgm = $self->NewAlias('CachedGroupMembers');
122         $self->Join( ALIAS1 => 'main',
123                      FIELD1 => 'PrincipalId',
124                      ALIAS2 => $cgm,
125                      FIELD2 => 'GroupId' );
126         $self->Limit( ALIAS           => $cgm,
127                       FIELD           => 'MemberId',
128                       OPERATOR        => '=',
129                       VALUE           => $args{'Id'},
130                       ENTRYAGGREGATOR => 'OR' );
131     }
132     else {
133         if ( defined $args{'Type'} ) {
134             $self->Limit( FIELD           => 'PrincipalType',
135                           OPERATOR        => '=',
136                           VALUE           => $args{'Type'},
137                           ENTRYAGGREGATOR => 'OR' );
138         }
139     # if the principal id points to a user, we really want to point
140     # to their ACL equivalence group. The machinations we're going through
141     # lead me to start to suspect that we really want users and groups
142     # to just be the same table. or _maybe_ that we want an object db.
143     my $princ = RT::Principal->new($RT::SystemUser);
144     $princ->Load($args{'Id'});
145     if ($princ->PrincipalType eq 'User') {
146     my $group = RT::Group->new($RT::SystemUser);
147         $group->LoadACLEquivalenceGroup($princ);
148         $args{'Id'} = $group->PrincipalId;
149     }
150         $self->Limit( FIELD           => 'PrincipalId',
151                       OPERATOR        => '=',
152                       VALUE           => $args{'Id'},
153                       ENTRYAGGREGATOR => 'OR' );
154     }
155 }
156
157 # }}}
158
159
160
161 # {{{ ExcludeDelegatedRights
162
163 =head2 ExcludeDelegatedRights 
164
165 Don't list rights which have been delegated.
166
167 =cut
168
169 sub ExcludeDelegatedRights {
170     my $self = shift;
171     $self->DelegatedBy(Id => 0);
172     $self->DelegatedFrom(Id => 0);
173 }
174 # }}}
175
176 # {{{ DelegatedBy 
177
178 =head2 DelegatedBy { Id => undef }
179
180 Limit the ACL to rights delegated by the principal whose Principal Id is
181 B<Id>
182
183 Id is not optional.
184
185 =cut
186
187 sub DelegatedBy {
188     my $self = shift;
189     my %args = (
190         Id => undef,
191         @_
192     );
193     $self->Limit(
194         FIELD           => 'DelegatedBy',
195         OPERATOR        => '=',
196         VALUE           => $args{'Id'},
197         ENTRYAGGREGATOR => 'OR'
198     );
199
200 }
201
202 # }}}
203
204 # {{{ DelegatedFrom 
205
206 =head2 DelegatedFrom { Id => undef }
207
208 Limit the ACL to rights delegate from the ACE which has the Id specified 
209 by the Id parameter.
210
211 Id is not optional.
212
213 =cut
214
215 sub DelegatedFrom {
216     my $self = shift;
217     my %args = (
218                  Id => undef,
219                  @_);
220     $self->Limit(FIELD => 'DelegatedFrom', OPERATOR=> '=', VALUE => $args{'Id'}, ENTRYAGGREGATOR => 'OR');
221
222 }
223
224 # }}}
225
226
227 # {{{ sub Next 
228 sub Next {
229     my $self = shift;
230
231     my $ACE = $self->SUPER::Next();
232     if ( ( defined($ACE) ) and ( ref($ACE) ) ) {
233
234         if ( $self->CurrentUser->HasRight( Right  => 'ShowACL',
235                                            Object => $ACE->Object )
236              or $self->CurrentUser->HasRight( Right  => 'ModifyACL',
237                                               Object => $ACE->Object )
238           ) {
239             return ($ACE);
240         }
241
242         #If the user doesn't have the right to show this ACE
243         else {
244             return ( $self->Next() );
245         }
246     }
247
248     #if there never was any ACE
249     else {
250         return (undef);
251     }
252
253 }
254
255 # }}}
256
257
258
259 #wrap around _DoSearch  so that we can build the hash of returned
260 #values 
261 sub _DoSearch {
262     my $self = shift;
263    # $RT::Logger->debug("Now in ".$self."->_DoSearch");
264     my $return = $self->SUPER::_DoSearch(@_);
265   #  $RT::Logger->debug("In $self ->_DoSearch. return from SUPER::_DoSearch was $return\n");
266     $self->_BuildHash();
267     return ($return);
268 }
269
270
271 #Build a hash of this ACL's entries.
272 sub _BuildHash {
273     my $self = shift;
274
275     while (my $entry = $self->Next) {
276        my $hashkey = $entry->ObjectType . "-" .  $entry->ObjectId . "-" .  $entry->RightName . "-" .  $entry->PrincipalId . "-" .  $entry->PrincipalType;
277
278         $self->{'as_hash'}->{"$hashkey"} =1;
279
280     }
281 }
282
283
284 # {{{ HasEntry
285
286 =head2 HasEntry
287
288 =cut
289
290 sub HasEntry {
291
292     my $self = shift;
293     my %args = ( RightScope => undef,
294                  RightAppliesTo => undef,
295                  RightName => undef,
296                  PrincipalId => undef,
297                  PrincipalType => undef,
298                  @_ );
299
300     #if we haven't done the search yet, do it now.
301     $self->_DoSearch();
302
303     if ($self->{'as_hash'}->{ $args{'RightScope'} . "-" .
304                               $args{'RightAppliesTo'} . "-" . 
305                               $args{'RightName'} . "-" .
306                               $args{'PrincipalId'} . "-" .
307                               $args{'PrincipalType'}
308                             } == 1) {
309         return(1);
310     }
311     else {
312         return(undef);
313     }
314 }
315
316 # }}}
317 1;