3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
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
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.
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.
26 RT::GroupMember - a member of an RT Group
30 RT::GroupMember should never be called directly. It should ONLY
31 only be accessed through the helper functions in RT::Group;
33 If you're operating on an RT::GroupMember object yourself, you B<ARE>
34 doing something wrong.
46 ok (require RT::GroupMember);
54 no warnings qw(redefine);
55 use RT::CachedGroupMembers;
59 =head2 Create { Group => undef, Member => undef }
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.
65 Both Group and Member are expected to be RT::Principal objects
74 InsideTransaction => undef,
78 unless ($args{'Group'} &&
79 UNIVERSAL::isa($args{'Group'}, 'RT::Principal') &&
80 $args{'Group'}->Id ) {
82 $RT::Logger->warning("GroupMember::Create called with a bogus Group arg");
86 unless($args{'Group'}->IsGroup) {
87 $RT::Logger->warning("Someone tried to add a member to a user instead of a group");
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");
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();
103 $RT::Handle->BeginTransaction() unless ($args{'InsideTransaction'});
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
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");
116 elsif ( $args{'Member'}->Id == $args{'Group'}->Id) {
117 $RT::Logger->debug("Can't add a group to itself");
123 my $id = $self->SUPER::Create(
124 GroupId => $args{'Group'}->Id,
125 MemberId => $args{'Member'}->Id
129 $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
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'},
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 .
145 my $cgm = RT::CachedGroupMembers->new( $self->CurrentUser );
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 );
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;
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
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'});
172 unless ($cached_id) {
173 $RT::Handle->Rollback() unless ($args{'InsideTransaction'});
177 $RT::Handle->Commit() unless ($args{'InsideTransaction'});
186 =head2 _StashUser PRINCIPAL
188 Create { Group => undef, Member => undef }
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.
194 PRINCIPAL is expected to be an RT::Principal object for a user
196 This routine expects to be called inside a transaction by RT::User->Create
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();
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
218 my $id = $self->SUPER::Create(
219 GroupId => $args{'Group'}->Id,
220 MemberId => $args{'Member'}->Id,
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'},
235 unless ($cached_id) {
248 Takes no arguments. deletes the currently loaded member from the
251 Expects to be called _outside_ a transaction
259 $RT::Handle->BeginTransaction();
261 # Find all occurrences of this member as a member of this group
262 # in the cache and nuke them, recursively.
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
270 my $cached_submembers = RT::CachedGroupMembers->new( $self->CurrentUser );
272 $cached_submembers->Limit(
275 VALUE => $self->MemberObj->Id
278 $cached_submembers->Limit(
279 FIELD => 'ImmediateParentId',
281 VALUE => $self->GroupObj->Id
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();
291 while ( my $item_to_del = $cached_submembers->Next() ) {
292 my $del_err = $item_to_del->Delete();
294 $RT::Handle->Rollback();
295 $RT::Logger->warning("Couldn't delete cached group submember ".$item_to_del->Id);
300 my $err = $self->SUPER::Delete();
302 $RT::Logger->warning("Couldn't delete cached group submember ".$self->Id);
303 $RT::Handle->Rollback();
306 $RT::Handle->Commit();
317 Returns an RT::Principal object for the Principal specified by $self->PrincipalId
323 unless ( defined( $self->{'Member_obj'} ) ) {
324 $self->{'Member_obj'} = RT::Principal->new( $self->CurrentUser );
325 $self->{'Member_obj'}->Load( $self->MemberId );
327 return ( $self->{'Member_obj'} );
336 Returns an RT::Principal object for the Group specified in $self->GroupId
342 unless ( defined( $self->{'Group_obj'} ) ) {
343 $self->{'Group_obj'} = RT::Principal->new( $self->CurrentUser );
344 $self->{'Group_obj'}->Load( $self->GroupId );
346 return ( $self->{'Group_obj'} );