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