import of rt 3.0.4
[freeside.git] / rt / lib / RT / GroupMember_Overlay.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 =head1 NAME
25
26   RT::GroupMember - a member of an RT Group
27
28 =head1 SYNOPSIS
29
30 RT::GroupMember should never be called directly. It should ONLY
31 only be accessed through the helper functions in RT::Group;
32
33 If you're operating on an RT::GroupMember object yourself, you B<ARE>
34 doing something wrong.
35
36 =head1 DESCRIPTION
37
38
39
40
41 =head1 METHODS
42
43
44 =begin testing
45
46 ok (require RT::GroupMember);
47
48 =end testing
49
50
51 =cut
52
53 use strict;
54 no warnings qw(redefine);
55 use RT::CachedGroupMembers;
56
57 # {{{ sub Create
58
59 =head2 Create { Group => undef, Member => undef }
60
61 Add a Principal to the group Group.
62 if the Principal is a group, automatically inserts all
63 members of the principal into the cached members table recursively down.
64
65 Both Group and Member are expected to be RT::Principal objects
66
67 =cut
68
69 sub Create {
70     my $self = shift;
71     my %args = (
72         Group  => undef,
73         Member => undef,
74         InsideTransaction => undef,
75         @_
76     );
77
78     unless ($args{'Group'} &&
79             UNIVERSAL::isa($args{'Group'}, 'RT::Principal') &&
80             $args{'Group'}->Id ) {
81
82         $RT::Logger->warning("GroupMember::Create called with a bogus Group arg");
83         return (undef);
84     }
85
86     unless($args{'Group'}->IsGroup) {
87         $RT::Logger->warning("Someone tried to add a member to a user instead of a group");
88         return (undef);
89     }
90
91     unless ($args{'Member'} && 
92             UNIVERSAL::isa($args{'Member'}, 'RT::Principal') &&
93             $args{'Member'}->Id) {
94         $RT::Logger->warning("GroupMember::Create called with a bogus Principal arg");
95         return (undef);
96     }
97
98
99     #Clear the key cache. TODO someday we may want to just clear a little bit of the keycache space. 
100     # TODO what about the groups key cache?
101     RT::Principal->_InvalidateACLCache();
102
103     $RT::Handle->BeginTransaction() unless ($args{'InsideTransaction'});
104
105     # We really need to make sure we don't add any members to this group
106     # that contain the group itself. that would, um, suck. 
107     # (and recurse infinitely)  Later, we can add code to check this in the 
108     # cache and bail so we can support cycling directed graphs
109
110     if ($args{'Member'}->IsGroup) {
111         my $member_object = $args{'Member'}->Object;
112         if ($member_object->HasMemberRecursively($args{'Group'})) {
113             $RT::Logger->debug("Adding that group would create a loop");
114             return(undef);
115         }
116         elsif ( $args{'Member'}->Id == $args{'Group'}->Id) {
117             $RT::Logger->debug("Can't add a group to itself");
118             return(undef);
119         }
120     }
121
122
123     my $id = $self->SUPER::Create(
124         GroupId  => $args{'Group'}->Id,
125         MemberId => $args{'Member'}->Id
126     );
127
128     unless ($id) {
129         $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
130         return (undef);
131     }
132
133     my $cached_member = RT::CachedGroupMember->new( $self->CurrentUser );
134     my $cached_id     = $cached_member->Create(
135         Member          => $args{'Member'},
136         Group           => $args{'Group'},
137         ImmediateParent => $args{'Group'},
138         Via             => '0'
139     );
140
141
142     #When adding a member to a group, we need to go back
143     #and popuplate the CachedGroupMembers of all the groups that group is part of .
144
145     my $cgm = RT::CachedGroupMembers->new( $self->CurrentUser );
146
147     # find things which have the current group as a member. 
148     # $group is an RT::Principal for the group.
149     $cgm->LimitToGroupsWithMember( $args{'Group'}->Id );
150
151     while ( my $parent_member = $cgm->Next ) {
152         my $parent_id = $parent_member->MemberId;
153         my $via       = $parent_member->Id;
154         my $group_id  = $parent_member->GroupId;
155
156           my $other_cached_member =
157           RT::CachedGroupMember->new( $self->CurrentUser );
158         my $other_cached_id = $other_cached_member->Create(
159             Member          => $args{'Member'},
160                       Group => $parent_member->GroupObj,
161             ImmediateParent => $parent_member->MemberObj,
162             Via             => $parent_member->Id
163         );
164         unless ($other_cached_id) {
165             $RT::Logger->err( "Couldn't add " . $args{'Member'}
166                   . " as a submember of a supergroup" );
167             $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
168             return (undef);
169         }
170     } 
171
172     unless ($cached_id) {
173         $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
174         return (undef);
175     }
176
177     $RT::Handle->Commit() unless ($args{'InsideTransaction'});
178
179     return ($id);
180 }
181
182 # }}}
183
184 # {{{ sub _StashUser
185
186 =head2 _StashUser PRINCIPAL
187
188 Create { Group => undef, Member => undef }
189
190 Creates an entry in the groupmembers table, which lists a user
191 as a member of himself. This makes ACL checks a whole bunch easier.
192 This happens once on user create and never ever gets yanked out.
193
194 PRINCIPAL is expected to be an RT::Principal object for a user
195
196 This routine expects to be called inside a transaction by RT::User->Create
197
198 =cut
199
200 sub _StashUser {
201     my $self = shift;
202     my %args = (
203         Group  => undef,
204         Member => undef,
205         @_
206     );
207
208     #Clear the key cache. TODO someday we may want to just clear a little bit of the keycache space. 
209     # TODO what about the groups key cache?
210     RT::Principal->_InvalidateACLCache();
211
212
213     # We really need to make sure we don't add any members to this group
214     # that contain the group itself. that would, um, suck. 
215     # (and recurse infinitely)  Later, we can add code to check this in the 
216     # cache and bail so we can support cycling directed graphs
217
218     my $id = $self->SUPER::Create(
219         GroupId  => $args{'Group'}->Id,
220         MemberId => $args{'Member'}->Id,
221     );
222
223     unless ($id) {
224         return (undef);
225     }
226
227     my $cached_member = RT::CachedGroupMember->new( $self->CurrentUser );
228     my $cached_id     = $cached_member->Create(
229         Member          => $args{'Member'},
230         Group           => $args{'Group'},
231         ImmediateParent => $args{'Group'},
232         Via             => '0'
233     );
234
235     unless ($cached_id) {
236         return (undef);
237     }
238
239     return ($id);
240 }
241
242 # }}}
243
244 # {{{ sub Delete
245
246 =head2 Delete
247
248 Takes no arguments. deletes the currently loaded member from the 
249 group in question.
250
251 Expects to be called _outside_ a transaction
252
253 =cut
254
255 sub Delete {
256     my $self = shift;
257
258
259     $RT::Handle->BeginTransaction();
260
261     # Find all occurrences of this member as a member of this group
262     # in the cache and nuke them, recursively.
263
264     # The following code will delete all Cached Group members
265     # where this member's group is _not_ the primary group 
266     # (Ie if we're deleting C as a member of B, and B happens to be 
267     # a member of A, will delete C as a member of A without touching
268     # C as a member of B
269
270     my $cached_submembers = RT::CachedGroupMembers->new( $self->CurrentUser );
271
272     $cached_submembers->Limit(
273         FIELD    => 'MemberId',
274         OPERATOR => '=',
275         VALUE    => $self->MemberObj->Id
276     );
277
278     $cached_submembers->Limit(
279         FIELD    => 'ImmediateParentId',
280         OPERATOR => '=',
281         VALUE    => $self->GroupObj->Id
282     );
283
284     #Clear the key cache. TODO someday we may want to just clear a little bit of the keycache space. 
285     # TODO what about the groups key cache?
286     RT::Principal->_InvalidateACLCache();
287
288
289
290
291     while ( my $item_to_del = $cached_submembers->Next() ) {
292         my $del_err = $item_to_del->Delete();
293         unless ($del_err) {
294             $RT::Handle->Rollback();
295             $RT::Logger->warning("Couldn't delete cached group submember ".$item_to_del->Id);
296             return (undef);
297         }
298     }
299
300     my $err = $self->SUPER::Delete();
301     unless ($err) {
302             $RT::Logger->warning("Couldn't delete cached group submember ".$self->Id);
303         $RT::Handle->Rollback();
304         return (undef);
305     }
306     $RT::Handle->Commit();
307     return ($err);
308
309 }
310
311 # }}}
312
313 # {{{ sub MemberObj
314
315 =head2 MemberObj
316
317 Returns an RT::Principal object for the Principal specified by $self->PrincipalId
318
319 =cut
320
321 sub MemberObj {
322     my $self = shift;
323     unless ( defined( $self->{'Member_obj'} ) ) {
324         $self->{'Member_obj'} = RT::Principal->new( $self->CurrentUser );
325         $self->{'Member_obj'}->Load( $self->MemberId );
326     }
327     return ( $self->{'Member_obj'} );
328 }
329
330 # }}}
331
332 # {{{ sub GroupObj
333
334 =head2 GroupObj
335
336 Returns an RT::Principal object for the Group specified in $self->GroupId
337
338 =cut
339
340 sub GroupObj {
341     my $self = shift;
342     unless ( defined( $self->{'Group_obj'} ) ) {
343         $self->{'Group_obj'} = RT::Principal->new( $self->CurrentUser );
344         $self->{'Group_obj'}->Load( $self->GroupId );
345     }
346     return ( $self->{'Group_obj'} );
347 }
348
349 # }}}
350
351 1;