This commit was generated by cvs2svn to compensate for changes in r2523,
[freeside.git] / rt / lib / RT / ACL.pm
1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/ACL.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2 # Distributed under the terms of the GNU GPL
3 # Copyright (c) 2000 Jesse Vincent <jesse@fsck.com>
4
5 =head1 NAME
6
7   RT::ACL - collection of RT ACE objects
8
9 =head1 SYNOPSIS
10
11   use RT::ACL;
12 my $ACL = new RT::ACL($CurrentUser);
13
14 =head1 DESCRIPTION
15
16
17 =head1 METHODS
18
19 =begin testing
20
21 ok(require RT::TestHarness);
22 ok(require RT::ACL);
23
24 =end testing
25
26 =cut
27
28 package RT::ACL;
29 use RT::EasySearch;
30 use RT::ACE;
31 @ISA= qw(RT::EasySearch);
32
33 # {{{ sub _Init 
34 sub _Init  {
35   my $self = shift;
36   $self->{'table'} = "ACL";
37   $self->{'primary_key'} = "id";
38   return ( $self->SUPER::_Init(@_));
39   
40 }
41 # }}}
42
43 # {{{ sub NewItem 
44 sub NewItem  {
45   my $self = shift;
46   return(RT::ACE->new($self->CurrentUser));
47 }
48 # }}}
49
50 =head2 Next
51
52 Hand out the next ACE that was found
53
54 =cut
55
56 # {{{ sub Next 
57 sub Next {
58     my $self = shift;
59     
60     my $ACE = $self->SUPER::Next();
61     if ((defined($ACE)) and (ref($ACE))) {
62         
63         if ( $ACE->CurrentUserHasRight('ShowACL') or
64              $ACE->CurrentUserHasRight('ModifyACL')
65            ) {
66             return($ACE);
67         }
68         
69         #If the user doesn't have the right to show this ACE
70         else {  
71             return($self->Next());
72         }
73     }
74     #if there never was any ACE
75     else {
76         return(undef);
77     }   
78     
79 }
80
81 # }}}
82
83
84 =head1 Limit the ACL to a specific scope
85
86 There are two real scopes right now:
87
88 =item Queue is for rights that apply to a single queue
89
90 =item System is for rights that apply to the System (rights that aren't queue related)
91
92
93 =head2 LimitToQueue
94
95 Takes a single queueid as its argument.
96
97 Limit the ACL to just a given queue when supplied with an integer queue id.
98
99 =cut
100
101 sub LimitToQueue {
102     my $self = shift;
103     my $queue = shift;
104     
105     
106     
107     $self->Limit( FIELD =>'RightScope',
108                   ENTRYAGGREGATOR => 'OR',
109                   VALUE => 'Queue');
110     $self->Limit( FIELD =>'RightScope',
111                   ENTRYAGGREGATOR => 'OR',
112                 VALUE => 'Ticket');
113     
114     $self->Limit(ENTRYAGGREGATOR => 'OR',
115                  FIELD => 'RightAppliesTo',
116                  VALUE => $queue );
117   
118 }
119
120
121 =head2 LimitToSystem()
122
123 Limit the ACL to system rights
124
125 =cut 
126
127 sub LimitToSystem {
128   my $self = shift;
129   
130   $self->Limit( FIELD =>'RightScope',
131                 VALUE => 'System');
132 }
133
134
135 =head2 LimitRightTo
136
137 Takes a single RightName as its only argument.
138 Limits the search to the right $right.
139 $right is a right listed in perldoc RT::ACE
140
141 =cut
142
143 sub LimitRightTo {
144   my $self = shift;
145   my $right = shift;
146   
147   $self->Limit(ENTRYAGGREGATOR => 'OR',
148                FIELD => 'RightName',
149                VALUE => $right );
150   
151 }
152
153 =head1 Limit to a specifc set of principals
154
155 =head2 LimitPrincipalToUser
156
157 Takes a single userid as its only argument.
158 Limit the ACL to a just a specific user.
159
160 =cut
161
162 sub LimitPrincipalToUser {
163   my $self = shift;
164   my $user = shift;
165   
166   $self->Limit(ENTRYAGGREGATOR => 'OR',
167                FIELD => 'PrincipalType',
168                VALUE => 'User' );
169   
170   $self->Limit(ENTRYAGGREGATOR => 'OR',
171                FIELD => 'PrincipalId',
172                VALUE => $user );
173   
174 }
175
176
177 =head2 LimitPrincipalToGroup
178
179 Takes a single group as its only argument.
180 Limit the ACL to just a specific group.
181
182 =cut
183   
184 sub LimitPrincipalToGroup {
185   my $self = shift;
186   my $group = shift;
187   
188   $self->Limit(ENTRYAGGREGATOR => 'OR',
189                FIELD => 'PrincipalType',
190                VALUE => 'Group' );
191
192   $self->Limit(ENTRYAGGREGATOR => 'OR',
193                FIELD => 'PrincipalId',
194                VALUE => $group );
195
196 }
197
198 =head2 LimitPrincipalToType($type)
199
200 Takes a single argument, $type.
201 Limit the ACL to just a specific principal type
202
203 $type is one of:
204   TicketOwner
205   TicketRequestor
206   TicketCc
207   TicketAdminCc
208   Everyone
209   User
210   Group
211
212 =cut
213
214 sub LimitPrincipalToType {
215   my $self=shift;
216   my $type=shift;  
217   $self->Limit(ENTRYAGGREGATOR => 'OR',
218                 FIELD => 'PrincipalType',
219                 VALUE => $type );
220 }
221
222
223 =head2 LimitPrincipalToId 
224
225 Takes a single argument, the numeric Id of the principal to limit this ACL to. Repeated calls to this 
226 function will broaden the scope of the search to include all principals listed.
227
228 =cut
229
230 sub LimitPrincipalToId {
231     my $self = shift;
232     my $id = shift;
233
234     if ($id =~ /^\d+$/) {
235         $self->Limit(ENTRYAGGREGATOR => 'OR',
236                      FIELD => 'PrincipalId',
237                      VALUE => $id );
238     }
239     else {
240         $RT::Logger->warn($self."->LimitPrincipalToId called with '$id' as an id");
241         return undef;
242     }
243 }
244
245
246 #wrap around _DoSearch  so that we can build the hash of returned
247 #values 
248 sub _DoSearch {
249     my $self = shift;
250    # $RT::Logger->debug("Now in ".$self."->_DoSearch");
251     my $return = $self->SUPER::_DoSearch(@_);
252   #  $RT::Logger->debug("In $self ->_DoSearch. return from SUPER::_DoSearch was $return\n");
253     $self->_BuildHash();
254     return ($return);
255 }
256
257
258 #Build a hash of this ACL's entries.
259 sub _BuildHash {
260     my $self = shift;
261
262     while (my $entry = $self->Next) {
263        my $hashkey = $entry->RightScope . "-" .
264                              $entry->RightAppliesTo . "-" . 
265                              $entry->RightName . "-" .
266                              $entry->PrincipalId . "-" .
267                              $entry->PrincipalType;
268
269         $self->{'as_hash'}->{"$hashkey"} =1;
270
271     }
272 }
273
274
275 # {{{ HasEntry
276
277 =head2 HasEntry
278
279 =cut
280
281 sub HasEntry {
282
283     my $self = shift;
284     my %args = ( RightScope => undef,
285                  RightAppliesTo => undef,
286                  RightName => undef,
287                  PrincipalId => undef,
288                  PrincipalType => undef,
289                  @_ );
290
291     #if we haven't done the search yet, do it now.
292     $self->_DoSearch();
293
294     if ($self->{'as_hash'}->{ $args{'RightScope'} . "-" .
295                               $args{'RightAppliesTo'} . "-" . 
296                               $args{'RightName'} . "-" .
297                               $args{'PrincipalId'} . "-" .
298                               $args{'PrincipalType'}
299                             } == 1) {
300         return(1);
301     }
302     else {
303         return(undef);
304     }
305 }
306
307 # }}}
308 1;