import rt 3.6.4
[freeside.git] / rt / lib / RT / GroupMember_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::GroupMember - a member of an RT Group
51
52 =head1 SYNOPSIS
53
54 RT::GroupMember should never be called directly. It should ONLY
55 only be accessed through the helper functions in RT::Group;
56
57 If you're operating on an RT::GroupMember object yourself, you B<ARE>
58 doing something wrong.
59
60 =head1 DESCRIPTION
61
62
63
64
65 =head1 METHODS
66
67
68 =begin testing
69
70 ok (require RT::GroupMember);
71
72 =end testing
73
74
75 =cut
76
77
78 package RT::GroupMember;
79
80 use strict;
81 no warnings qw(redefine);
82 use RT::CachedGroupMembers;
83
84 # {{{ sub Create
85
86 =head2 Create { Group => undef, Member => undef }
87
88 Add a Principal to the group Group.
89 if the Principal is a group, automatically inserts all
90 members of the principal into the cached members table recursively down.
91
92 Both Group and Member are expected to be RT::Principal objects
93
94 =cut
95
96 sub Create {
97     my $self = shift;
98     my %args = (
99         Group  => undef,
100         Member => undef,
101         InsideTransaction => undef,
102         @_
103     );
104
105     unless ($args{'Group'} &&
106             UNIVERSAL::isa($args{'Group'}, 'RT::Principal') &&
107             $args{'Group'}->Id ) {
108
109         $RT::Logger->warning("GroupMember::Create called with a bogus Group arg");
110         return (undef);
111     }
112
113     unless($args{'Group'}->IsGroup) {
114         $RT::Logger->warning("Someone tried to add a member to a user instead of a group");
115         return (undef);
116     }
117
118     unless ($args{'Member'} && 
119             UNIVERSAL::isa($args{'Member'}, 'RT::Principal') &&
120             $args{'Member'}->Id) {
121         $RT::Logger->warning("GroupMember::Create called with a bogus Principal arg");
122         return (undef);
123     }
124
125
126     #Clear the key cache. TODO someday we may want to just clear a little bit of the keycache space. 
127     # TODO what about the groups key cache?
128     RT::Principal->InvalidateACLCache();
129
130     $RT::Handle->BeginTransaction() unless ($args{'InsideTransaction'});
131
132     # We really need to make sure we don't add any members to this group
133     # that contain the group itself. that would, um, suck. 
134     # (and recurse infinitely)  Later, we can add code to check this in the 
135     # cache and bail so we can support cycling directed graphs
136
137     if ($args{'Member'}->IsGroup) {
138         my $member_object = $args{'Member'}->Object;
139         if ($member_object->HasMemberRecursively($args{'Group'})) {
140             $RT::Logger->debug("Adding that group would create a loop");
141             return(undef);
142         }
143         elsif ( $args{'Member'}->Id == $args{'Group'}->Id) {
144             $RT::Logger->debug("Can't add a group to itself");
145             return(undef);
146         }
147     }
148
149
150     my $id = $self->SUPER::Create(
151         GroupId  => $args{'Group'}->Id,
152         MemberId => $args{'Member'}->Id
153     );
154
155     unless ($id) {
156         $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
157         return (undef);
158     }
159
160     my $cached_member = RT::CachedGroupMember->new( $self->CurrentUser );
161     my $cached_id     = $cached_member->Create(
162         Member          => $args{'Member'},
163         Group           => $args{'Group'},
164         ImmediateParent => $args{'Group'},
165         Via             => '0'
166     );
167
168
169     #When adding a member to a group, we need to go back
170     #and popuplate the CachedGroupMembers of all the groups that group is part of .
171
172     my $cgm = RT::CachedGroupMembers->new( $self->CurrentUser );
173
174     # find things which have the current group as a member. 
175     # $group is an RT::Principal for the group.
176     $cgm->LimitToGroupsWithMember( $args{'Group'}->Id );
177
178     while ( my $parent_member = $cgm->Next ) {
179         my $parent_id = $parent_member->MemberId;
180         my $via       = $parent_member->Id;
181         my $group_id  = $parent_member->GroupId;
182
183           my $other_cached_member =
184           RT::CachedGroupMember->new( $self->CurrentUser );
185         my $other_cached_id = $other_cached_member->Create(
186             Member          => $args{'Member'},
187                       Group => $parent_member->GroupObj,
188             ImmediateParent => $parent_member->MemberObj,
189             Via             => $parent_member->Id
190         );
191         unless ($other_cached_id) {
192             $RT::Logger->err( "Couldn't add " . $args{'Member'}
193                   . " as a submember of a supergroup" );
194             $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
195             return (undef);
196         }
197     } 
198
199     unless ($cached_id) {
200         $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
201         return (undef);
202     }
203
204     $RT::Handle->Commit() unless ($args{'InsideTransaction'});
205
206     return ($id);
207 }
208
209 # }}}
210
211 # {{{ sub _StashUser
212
213 =head2 _StashUser PRINCIPAL
214
215 Create { Group => undef, Member => undef }
216
217 Creates an entry in the groupmembers table, which lists a user
218 as a member of himself. This makes ACL checks a whole bunch easier.
219 This happens once on user create and never ever gets yanked out.
220
221 PRINCIPAL is expected to be an RT::Principal object for a user
222
223 This routine expects to be called inside a transaction by RT::User->Create
224
225 =cut
226
227 sub _StashUser {
228     my $self = shift;
229     my %args = (
230         Group  => undef,
231         Member => undef,
232         @_
233     );
234
235     #Clear the key cache. TODO someday we may want to just clear a little bit of the keycache space. 
236     # TODO what about the groups key cache?
237     RT::Principal->InvalidateACLCache();
238
239
240     # We really need to make sure we don't add any members to this group
241     # that contain the group itself. that would, um, suck. 
242     # (and recurse infinitely)  Later, we can add code to check this in the 
243     # cache and bail so we can support cycling directed graphs
244
245     my $id = $self->SUPER::Create(
246         GroupId  => $args{'Group'}->Id,
247         MemberId => $args{'Member'}->Id,
248     );
249
250     unless ($id) {
251         return (undef);
252     }
253
254     my $cached_member = RT::CachedGroupMember->new( $self->CurrentUser );
255     my $cached_id     = $cached_member->Create(
256         Member          => $args{'Member'},
257         Group           => $args{'Group'},
258         ImmediateParent => $args{'Group'},
259         Via             => '0'
260     );
261
262     unless ($cached_id) {
263         return (undef);
264     }
265
266     return ($id);
267 }
268
269 # }}}
270
271 # {{{ sub Delete
272
273 =head2 Delete
274
275 Takes no arguments. deletes the currently loaded member from the 
276 group in question.
277
278 Expects to be called _outside_ a transaction
279
280 =cut
281
282 sub Delete {
283     my $self = shift;
284
285
286     $RT::Handle->BeginTransaction();
287
288     # Find all occurrences of this member as a member of this group
289     # in the cache and nuke them, recursively.
290
291     # The following code will delete all Cached Group members
292     # where this member's group is _not_ the primary group 
293     # (Ie if we're deleting C as a member of B, and B happens to be 
294     # a member of A, will delete C as a member of A without touching
295     # C as a member of B
296
297     my $cached_submembers = RT::CachedGroupMembers->new( $self->CurrentUser );
298
299     $cached_submembers->Limit(
300         FIELD    => 'MemberId',
301         OPERATOR => '=',
302         VALUE    => $self->MemberObj->Id
303     );
304
305     $cached_submembers->Limit(
306         FIELD    => 'ImmediateParentId',
307         OPERATOR => '=',
308         VALUE    => $self->GroupObj->Id
309     );
310
311
312
313
314
315     while ( my $item_to_del = $cached_submembers->Next() ) {
316         my $del_err = $item_to_del->Delete();
317         unless ($del_err) {
318             $RT::Handle->Rollback();
319             $RT::Logger->warning("Couldn't delete cached group submember ".$item_to_del->Id);
320             return (undef);
321         }
322     }
323
324     my ($err, $msg) = $self->SUPER::Delete();
325     unless ($err) {
326             $RT::Logger->warning("Couldn't delete cached group submember ".$self->Id);
327         $RT::Handle->Rollback();
328         return (undef);
329     }
330
331     # Since this deletion may have changed the former member's
332     # delegation rights, we need to ensure that no invalid delegations
333     # remain.
334     $err = $self->MemberObj->_CleanupInvalidDelegations(InsideTransaction => 1);
335     unless ($err) {
336         $RT::Logger->warning("Unable to revoke delegated rights for principal ".$self->Id);
337         $RT::Handle->Rollback();
338         return (undef);
339     }
340
341     #Clear the key cache. TODO someday we may want to just clear a little bit of the keycache space. 
342     # TODO what about the groups key cache?
343     RT::Principal->InvalidateACLCache();
344
345     $RT::Handle->Commit();
346     return ($err);
347
348 }
349
350 # }}}
351
352 # {{{ sub MemberObj
353
354 =head2 MemberObj
355
356 Returns an RT::Principal object for the Principal specified by $self->PrincipalId
357
358 =cut
359
360 sub MemberObj {
361     my $self = shift;
362     unless ( defined( $self->{'Member_obj'} ) ) {
363         $self->{'Member_obj'} = RT::Principal->new( $self->CurrentUser );
364         $self->{'Member_obj'}->Load( $self->MemberId ) if ($self->MemberId);
365     }
366     return ( $self->{'Member_obj'} );
367 }
368
369 # }}}
370
371 # {{{ sub GroupObj
372
373 =head2 GroupObj
374
375 Returns an RT::Principal object for the Group specified in $self->GroupId
376
377 =cut
378
379 sub GroupObj {
380     my $self = shift;
381     unless ( defined( $self->{'Group_obj'} ) ) {
382         $self->{'Group_obj'} = RT::Principal->new( $self->CurrentUser );
383         $self->{'Group_obj'}->Load( $self->GroupId );
384     }
385     return ( $self->{'Group_obj'} );
386 }
387
388 # }}}
389
390 1;