1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
50 RT::GroupMember - a member of an RT Group
54 RT::GroupMember should never be called directly. It should ONLY
55 only be accessed through the helper functions in RT::Group;
57 If you're operating on an RT::GroupMember object yourself, you B<ARE>
58 doing something wrong.
70 ok (require RT::GroupMember);
78 package RT::GroupMember;
81 no warnings qw(redefine);
82 use RT::CachedGroupMembers;
86 =head2 Create { Group => undef, Member => undef }
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.
92 Both Group and Member are expected to be RT::Principal objects
101 InsideTransaction => undef,
105 unless ($args{'Group'} &&
106 UNIVERSAL::isa($args{'Group'}, 'RT::Principal') &&
107 $args{'Group'}->Id ) {
109 $RT::Logger->warning("GroupMember::Create called with a bogus Group arg");
113 unless($args{'Group'}->IsGroup) {
114 $RT::Logger->warning("Someone tried to add a member to a user instead of a group");
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");
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();
130 $RT::Handle->BeginTransaction() unless ($args{'InsideTransaction'});
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
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");
143 elsif ( $args{'Member'}->Id == $args{'Group'}->Id) {
144 $RT::Logger->debug("Can't add a group to itself");
150 my $id = $self->SUPER::Create(
151 GroupId => $args{'Group'}->Id,
152 MemberId => $args{'Member'}->Id
156 $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
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'},
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 .
172 my $cgm = RT::CachedGroupMembers->new( $self->CurrentUser );
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 );
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;
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
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'});
199 unless ($cached_id) {
200 $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
204 $RT::Handle->Commit() unless ($args{'InsideTransaction'});
213 =head2 _StashUser PRINCIPAL
215 Create { Group => undef, Member => undef }
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.
221 PRINCIPAL is expected to be an RT::Principal object for a user
223 This routine expects to be called inside a transaction by RT::User->Create
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();
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
245 my $id = $self->SUPER::Create(
246 GroupId => $args{'Group'}->Id,
247 MemberId => $args{'Member'}->Id,
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'},
262 unless ($cached_id) {
275 Takes no arguments. deletes the currently loaded member from the
278 Expects to be called _outside_ a transaction
286 $RT::Handle->BeginTransaction();
288 # Find all occurrences of this member as a member of this group
289 # in the cache and nuke them, recursively.
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
297 my $cached_submembers = RT::CachedGroupMembers->new( $self->CurrentUser );
299 $cached_submembers->Limit(
302 VALUE => $self->MemberObj->Id
305 $cached_submembers->Limit(
306 FIELD => 'ImmediateParentId',
308 VALUE => $self->GroupObj->Id
315 while ( my $item_to_del = $cached_submembers->Next() ) {
316 my $del_err = $item_to_del->Delete();
318 $RT::Handle->Rollback();
319 $RT::Logger->warning("Couldn't delete cached group submember ".$item_to_del->Id);
324 my ($err, $msg) = $self->SUPER::Delete();
326 $RT::Logger->warning("Couldn't delete cached group submember ".$self->Id);
327 $RT::Handle->Rollback();
331 # Since this deletion may have changed the former member's
332 # delegation rights, we need to ensure that no invalid delegations
334 $err = $self->MemberObj->_CleanupInvalidDelegations(InsideTransaction => 1);
336 $RT::Logger->warning("Unable to revoke delegated rights for principal ".$self->Id);
337 $RT::Handle->Rollback();
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();
345 $RT::Handle->Commit();
356 Returns an RT::Principal object for the Principal specified by $self->PrincipalId
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);
366 return ( $self->{'Member_obj'} );
375 Returns an RT::Principal object for the Group specified in $self->GroupId
381 unless ( defined( $self->{'Group_obj'} ) ) {
382 $self->{'Group_obj'} = RT::Principal->new( $self->CurrentUser );
383 $self->{'Group_obj'}->Load( $self->GroupId );
385 return ( $self->{'Group_obj'} );