import rt 3.6.6
[freeside.git] / rt / lib / RT / ACL_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46
47 # END BPS TAGGED BLOCK }}}
48 =head1 NAME
49
50   RT::ACL - collection of RT ACE objects
51
52 =head1 SYNOPSIS
53
54   use RT::ACL;
55 my $ACL = new RT::ACL($CurrentUser);
56
57 =head1 DESCRIPTION
58
59
60 =head1 METHODS
61
62 =begin testing
63
64 ok(require RT::ACL);
65
66 =end testing
67
68 =cut
69
70
71 package RT::ACL;
72
73 use strict;
74 no warnings qw(redefine);
75
76
77 =head2 Next
78
79 Hand out the next ACE that was found
80
81 =cut
82
83
84 # {{{ LimitToObject 
85
86 =head2 LimitToObject $object
87
88 Limit the ACL to rights for the object $object. It needs to be an RT::Record class.
89
90 =cut
91
92 sub LimitToObject {
93     my $self = shift;
94     my $obj  = shift;
95     unless ( defined($obj)
96         && ref($obj)
97         && UNIVERSAL::can( $obj, 'id' )
98         && $obj->id )
99     {
100         return undef;
101     }
102     $self->Limit(
103         FIELD           => 'ObjectType',
104         OPERATOR        => '=',
105         VALUE           => ref($obj),
106         ENTRYAGGREGATOR => 'OR'
107     );
108     $self->Limit(
109         FIELD           => 'ObjectId',
110         OPERATOR        => '=',
111         VALUE           => $obj->id,
112         ENTRYAGGREGATOR => 'OR',
113         QUOTEVALUE      => 0
114     );
115
116 }
117
118 # }}}
119
120 # {{{ LimitNotObject
121
122 =head2 LimitNotObject $object
123
124 Limit the ACL to rights NOT on the object $object.  $object needs to be
125 an RT::Record class.
126
127 =cut
128
129 sub LimitNotObject {
130     my $self = shift;
131     my $obj  = shift;
132     unless ( defined($obj)
133         && ref($obj)
134         && UNIVERSAL::can( $obj, 'id' )
135         && $obj->id )
136     {
137         return undef;
138     }
139     $self->Limit( FIELD => 'ObjectType',
140                   OPERATOR => '!=',
141                   VALUE => ref($obj),
142                   ENTRYAGGREGATOR => 'OR',
143                   SUBCLAUSE => $obj->id
144                 );
145     $self->Limit( FIELD => 'ObjectId',
146                   OPERATOR => '!=',
147                   VALUE => $obj->id,
148                   ENTRYAGGREGATOR => 'OR',
149                   QUOTEVALUE => 0,
150                   SUBCLAUSE => $obj->id
151                 );
152 }
153
154 # }}}
155
156 # {{{ LimitToPrincipal 
157
158 =head2 LimitToPrincipal { Type => undef, Id => undef, IncludeGroupMembership => undef }
159
160 Limit the ACL to the principal with PrincipalId Id and PrincipalType Type
161
162 Id is not optional.
163 Type is.
164
165 if IncludeGroupMembership => 1 is specified, ACEs which apply to the principal due to group membership will be included in the resultset.
166
167
168 =cut
169
170 sub LimitToPrincipal {
171     my $self = shift;
172     my %args = ( Type                               => undef,
173                  Id                                 => undef,
174                  IncludeGroupMembership => undef,
175                  @_ );
176     if ( $args{'IncludeGroupMembership'} ) {
177         my $cgm = $self->NewAlias('CachedGroupMembers');
178         $self->Join( ALIAS1 => 'main',
179                      FIELD1 => 'PrincipalId',
180                      ALIAS2 => $cgm,
181                      FIELD2 => 'GroupId' );
182         $self->Limit( ALIAS           => $cgm,
183                       FIELD           => 'MemberId',
184                       OPERATOR        => '=',
185                       VALUE           => $args{'Id'},
186                       ENTRYAGGREGATOR => 'OR' );
187     }
188     else {
189         if ( defined $args{'Type'} ) {
190             $self->Limit( FIELD           => 'PrincipalType',
191                           OPERATOR        => '=',
192                           VALUE           => $args{'Type'},
193                           ENTRYAGGREGATOR => 'OR' );
194         }
195     # if the principal id points to a user, we really want to point
196     # to their ACL equivalence group. The machinations we're going through
197     # lead me to start to suspect that we really want users and groups
198     # to just be the same table. or _maybe_ that we want an object db.
199     my $princ = RT::Principal->new($RT::SystemUser);
200     $princ->Load($args{'Id'});
201     if ($princ->PrincipalType eq 'User') {
202     my $group = RT::Group->new($RT::SystemUser);
203         $group->LoadACLEquivalenceGroup($princ);
204         $args{'Id'} = $group->PrincipalId;
205     }
206         $self->Limit( FIELD           => 'PrincipalId',
207                       OPERATOR        => '=',
208                       VALUE           => $args{'Id'},
209                       ENTRYAGGREGATOR => 'OR' );
210     }
211 }
212
213 # }}}
214
215
216
217 # {{{ ExcludeDelegatedRights
218
219 =head2 ExcludeDelegatedRights 
220
221 Don't list rights which have been delegated.
222
223 =cut
224
225 sub ExcludeDelegatedRights {
226     my $self = shift;
227     $self->DelegatedBy(Id => 0);
228     $self->DelegatedFrom(Id => 0);
229 }
230 # }}}
231
232 # {{{ DelegatedBy 
233
234 =head2 DelegatedBy { Id => undef }
235
236 Limit the ACL to rights delegated by the principal whose Principal Id is
237 B<Id>
238
239 Id is not optional.
240
241 =cut
242
243 sub DelegatedBy {
244     my $self = shift;
245     my %args = (
246         Id => undef,
247         @_
248     );
249     $self->Limit(
250         FIELD           => 'DelegatedBy',
251         OPERATOR        => '=',
252         VALUE           => $args{'Id'},
253         ENTRYAGGREGATOR => 'OR'
254     );
255
256 }
257
258 # }}}
259
260 # {{{ DelegatedFrom 
261
262 =head2 DelegatedFrom { Id => undef }
263
264 Limit the ACL to rights delegate from the ACE which has the Id specified 
265 by the Id parameter.
266
267 Id is not optional.
268
269 =cut
270
271 sub DelegatedFrom {
272     my $self = shift;
273     my %args = (
274                  Id => undef,
275                  @_);
276     $self->Limit(FIELD => 'DelegatedFrom', OPERATOR=> '=', VALUE => $args{'Id'}, ENTRYAGGREGATOR => 'OR');
277
278 }
279
280 # }}}
281
282
283 # {{{ sub Next 
284 sub Next {
285     my $self = shift;
286
287     my $ACE = $self->SUPER::Next();
288     if ( ( defined($ACE) ) and ( ref($ACE) ) ) {
289
290         if ( $self->CurrentUser->HasRight( Right  => 'ShowACL',
291                                            Object => $ACE->Object )
292              or $self->CurrentUser->HasRight( Right  => 'ModifyACL',
293                                               Object => $ACE->Object )
294           ) {
295             return ($ACE);
296         }
297
298         #If the user doesn't have the right to show this ACE
299         else {
300             return ( $self->Next() );
301         }
302     }
303
304     #if there never was any ACE
305     else {
306         return (undef);
307     }
308
309 }
310
311 # }}}
312
313
314
315 #wrap around _DoSearch  so that we can build the hash of returned
316 #values 
317 sub _DoSearch {
318     my $self = shift;
319    # $RT::Logger->debug("Now in ".$self."->_DoSearch");
320     my $return = $self->SUPER::_DoSearch(@_);
321   #  $RT::Logger->debug("In $self ->_DoSearch. return from SUPER::_DoSearch was $return\n");
322     $self->_BuildHash();
323     return ($return);
324 }
325
326
327 #Build a hash of this ACL's entries.
328 sub _BuildHash {
329     my $self = shift;
330
331     while (my $entry = $self->Next) {
332        my $hashkey = $entry->ObjectType . "-" .  $entry->ObjectId . "-" .  $entry->RightName . "-" .  $entry->PrincipalId . "-" .  $entry->PrincipalType;
333
334         $self->{'as_hash'}->{"$hashkey"} =1;
335
336     }
337 }
338
339
340 # {{{ HasEntry
341
342 =head2 HasEntry
343
344 =cut
345
346 sub HasEntry {
347
348     my $self = shift;
349     my %args = ( RightScope => undef,
350                  RightAppliesTo => undef,
351                  RightName => undef,
352                  PrincipalId => undef,
353                  PrincipalType => undef,
354                  @_ );
355
356     #if we haven't done the search yet, do it now.
357     $self->_DoSearch();
358
359     if ($self->{'as_hash'}->{ $args{'RightScope'} . "-" .
360                               $args{'RightAppliesTo'} . "-" . 
361                               $args{'RightName'} . "-" .
362                               $args{'PrincipalId'} . "-" .
363                               $args{'PrincipalType'}
364                             } == 1) {
365         return(1);
366     }
367     else {
368         return(undef);
369     }
370 }
371
372 # }}}
373 1;