[Bps-public-commit] RT-Extension-MergeUsers branch, master, updated. 1.02_01-6-gf1c8dae

? sunnavy sunnavy at bestpractical.com
Wed Jan 15 10:13:52 EST 2020


The branch, master has been updated
       via  f1c8dae65da89e860ccb012ec3f445a4a2f167c2 (commit)
       via  0013bfee4b40ac8fb89c92a5fcdd625768e117fb (commit)
      from  9e301fd38ff12cd127e0c59d0e9f6081f002e1ef (commit)

Summary of changes:
 lib/RT/Extension/MergeUsers.pm | 126 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 126 insertions(+)

- Log -----------------------------------------------------------------
commit 0013bfee4b40ac8fb89c92a5fcdd625768e117fb
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Jan 11 01:43:10 2020 +0800

    Support to delete group members for merged users
    
    As RT::Group::UserMembersObj returns ids of main users(i.e. users that
    others are merged into), these main ids are passed on member delete on
    admin group members page, no matter if it's merged users or the main
    user that got added to the group.
    
    Thus we need to find out all the merged users in DeleteMember and delete
    them accordingly.

diff --git a/lib/RT/Extension/MergeUsers.pm b/lib/RT/Extension/MergeUsers.pm
index bfbe908..b2e67ab 100644
--- a/lib/RT/Extension/MergeUsers.pm
+++ b/lib/RT/Extension/MergeUsers.pm
@@ -457,6 +457,47 @@ sub SetDisabled {
     return ($ret, $msg);
 }
 
+{
+    package RT::Group;
+    my $orig_delete_member = \&RT::Group::DeleteMember;
+    *DeleteMember = sub {
+        my $self      = shift;
+        my $member_id = shift;
+
+        my $principal = RT::Principal->new( $self->CurrentUser );
+        $principal->Load($member_id);
+        if ( $principal->IsUser ) {
+
+            # Not call GetMergedUsers as we don't want to create the attribute here
+            my $merged_users = $principal->Object->FirstAttribute('MergedUsers');
+            if ( $merged_users && @{ $merged_users->Content } ) {
+                my $members = $self->MembersObj;
+                $members->Limit(
+                    FIELD    => 'MemberId',
+                    VALUE    => [ $member_id, @{ $merged_users->Content } ],
+                    OPERATOR => 'IN',
+                );
+
+                if ( $members->Count ) {
+                    my ( $ret, $msg );
+                    $RT::Handle->BeginTransaction;
+                    while ( my $member = $members->Next ) {
+                        ( $ret, $msg ) = $orig_delete_member->( $self, $member->MemberId, @_ );
+                        if ( !$ret ) {
+                            $RT::Handle->Rollback;
+                            return ( $ret, $msg );
+                        }
+                    }
+                    $RT::Handle->Commit;
+                    return ( $ret, $msg );
+
+                }
+            }
+        }
+        return $orig_delete_member->( $self, $member_id, @_ );
+    };
+}
+
 =head1 AUTHOR
 
 Best Practical Solutions, LLC E<lt>modules at bestpractical.comE<gt>

commit f1c8dae65da89e860ccb012ec3f445a4a2f167c2
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Jan 11 02:18:56 2020 +0800

    Check all the merged users for user's group memberships
    
    Thus we can have correct info on admin user membership page.

diff --git a/lib/RT/Extension/MergeUsers.pm b/lib/RT/Extension/MergeUsers.pm
index b2e67ab..3ed885b 100644
--- a/lib/RT/Extension/MergeUsers.pm
+++ b/lib/RT/Extension/MergeUsers.pm
@@ -496,6 +496,91 @@ sub SetDisabled {
         }
         return $orig_delete_member->( $self, $member_id, @_ );
     };
+
+}
+
+{
+    package RT::Groups;
+    sub WithMember {
+        my $self = shift;
+        my %args = ( PrincipalId => undef,
+                     Recursively => undef,
+                     @_);
+        my $members = $self->Join(
+            ALIAS1 => 'main', FIELD1 => 'id',
+            $args{'Recursively'}
+                ? (TABLE2 => 'CachedGroupMembers')
+                # (GroupId, MemberId) is unique in GM table
+                : (TABLE2 => 'GroupMembers', DISTINCT => 1)
+            ,
+            FIELD2 => 'GroupId',
+        );
+
+        my $principal = RT::Principal->new( $self->CurrentUser );
+        $principal->Load($args{'PrincipalId'});
+        my @ids = $args{'PrincipalId'};
+        if ( $principal->IsUser ) {
+
+            # Not call GetMergedUsers as we don't want to create the attribute here
+            my $merged_users = $principal->Object->FirstAttribute('MergedUsers');
+            push @ids, @{ $merged_users->Content } if $merged_users;
+        }
+
+        $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => 'IN', VALUE => \@ids);
+        $self->Limit(ALIAS => $members, FIELD => 'Disabled', VALUE => 0)
+            if $args{'Recursively'};
+
+        return $members;
+    }
+
+    sub WithoutMember {
+        my $self = shift;
+        my %args = (
+            PrincipalId => undef,
+            Recursively => undef,
+            @_
+        );
+
+        my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
+        my $members_alias = $self->Join(
+            TYPE   => 'LEFT',
+            FIELD1 => 'id',
+            TABLE2 => $members,
+            FIELD2 => 'GroupId',
+            DISTINCT => $members eq 'GroupMembers',
+        );
+
+        my $principal = RT::Principal->new( $self->CurrentUser );
+        $principal->Load($args{'PrincipalId'});
+        my @ids = $args{'PrincipalId'};
+        if ( $principal->IsUser ) {
+
+            # Not call GetMergedUsers as we don't want to create the attribute here
+            my $merged_users = $principal->Object->FirstAttribute('MergedUsers');
+            push @ids, @{ $merged_users->Content } if $merged_users;
+        }
+        $self->Limit(
+            LEFTJOIN => $members_alias,
+            ALIAS    => $members_alias,
+            FIELD    => 'MemberId',
+            OPERATOR => 'IN',
+            VALUE    => \@ids,
+        );
+
+        $self->Limit(
+            LEFTJOIN => $members_alias,
+            ALIAS    => $members_alias,
+            FIELD    => 'Disabled',
+            VALUE    => 0
+        ) if $args{'Recursively'};
+        $self->Limit(
+            ALIAS    => $members_alias,
+            FIELD    => 'MemberId',
+            OPERATOR => 'IS',
+            VALUE    => 'NULL',
+            QUOTEVALUE => 0,
+        );
+    }
 }
 
 =head1 AUTHOR

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list