1 # {{{ BEGIN BPS TAGGED BLOCK
5 # This software is Copyright (c) 1996-2004 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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
45 # }}} END BPS TAGGED BLOCK
48 RT::GroupMember - a member of an RT Group
52 RT::GroupMember should never be called directly. It should ONLY
53 only be accessed through the helper functions in RT::Group;
55 If you're operating on an RT::GroupMember object yourself, you B<ARE>
56 doing something wrong.
68 ok (require RT::GroupMember);
76 no warnings qw(redefine);
77 use RT::CachedGroupMembers;
81 =head2 Create { Group => undef, Member => undef }
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.
87 Both Group and Member are expected to be RT::Principal objects
96 InsideTransaction => undef,
100 unless ($args{'Group'} &&
101 UNIVERSAL::isa($args{'Group'}, 'RT::Principal') &&
102 $args{'Group'}->Id ) {
104 $RT::Logger->warning("GroupMember::Create called with a bogus Group arg");
108 unless($args{'Group'}->IsGroup) {
109 $RT::Logger->warning("Someone tried to add a member to a user instead of a group");
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");
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();
125 $RT::Handle->BeginTransaction() unless ($args{'InsideTransaction'});
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
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");
138 elsif ( $args{'Member'}->Id == $args{'Group'}->Id) {
139 $RT::Logger->debug("Can't add a group to itself");
145 my $id = $self->SUPER::Create(
146 GroupId => $args{'Group'}->Id,
147 MemberId => $args{'Member'}->Id
151 $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
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'},
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 .
167 my $cgm = RT::CachedGroupMembers->new( $self->CurrentUser );
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 );
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;
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
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'});
194 unless ($cached_id) {
195 $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
199 $RT::Handle->Commit() unless ($args{'InsideTransaction'});
208 =head2 _StashUser PRINCIPAL
210 Create { Group => undef, Member => undef }
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.
216 PRINCIPAL is expected to be an RT::Principal object for a user
218 This routine expects to be called inside a transaction by RT::User->Create
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();
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
240 my $id = $self->SUPER::Create(
241 GroupId => $args{'Group'}->Id,
242 MemberId => $args{'Member'}->Id,
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'},
257 unless ($cached_id) {
270 Takes no arguments. deletes the currently loaded member from the
273 Expects to be called _outside_ a transaction
281 $RT::Handle->BeginTransaction();
283 # Find all occurrences of this member as a member of this group
284 # in the cache and nuke them, recursively.
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
292 my $cached_submembers = RT::CachedGroupMembers->new( $self->CurrentUser );
294 $cached_submembers->Limit(
297 VALUE => $self->MemberObj->Id
300 $cached_submembers->Limit(
301 FIELD => 'ImmediateParentId',
303 VALUE => $self->GroupObj->Id
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();
313 while ( my $item_to_del = $cached_submembers->Next() ) {
314 my $del_err = $item_to_del->Delete();
316 $RT::Handle->Rollback();
317 $RT::Logger->warning("Couldn't delete cached group submember ".$item_to_del->Id);
322 my $err = $self->SUPER::Delete();
324 $RT::Logger->warning("Couldn't delete cached group submember ".$self->Id);
325 $RT::Handle->Rollback();
328 $RT::Handle->Commit();
339 Returns an RT::Principal object for the Principal specified by $self->PrincipalId
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);
349 return ( $self->{'Member_obj'} );
358 Returns an RT::Principal object for the Group specified in $self->GroupId
364 unless ( defined( $self->{'Group_obj'} ) ) {
365 $self->{'Group_obj'} = RT::Principal->new( $self->CurrentUser );
366 $self->{'Group_obj'}->Load( $self->GroupId );
368 return ( $self->{'Group_obj'} );