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