[Rt-commit] rt branch, 4.0/initialdata-membership, created. rt-4.0.10-73-g5bc2656

Alex Vandiver alexmv at bestpractical.com
Fri Oct 11 19:50:03 EDT 2013


The branch, 4.0/initialdata-membership has been created
        at  5bc2656271b12a9b1215e9c6800505126b356557 (commit)

- Log -----------------------------------------------------------------
commit 5bc2656271b12a9b1215e9c6800505126b356557
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 2 16:35:19 2012 -0400

    Allow member addition in initialdata

diff --git a/docs/initialdata.pod b/docs/initialdata.pod
index 6445fb0..dd97806 100644
--- a/docs/initialdata.pod
+++ b/docs/initialdata.pod
@@ -81,6 +81,8 @@ For a full list of fields, read the documentation for L<RT::User/Create>.
         Domain      => 'UserDefined',
         Name        => 'Example Employees',
         Description => 'All of the employees of my company',
+        Members     => { Users =>  [ qw/ alexmv trs falcone / ],
+                         Groups => [ qw/ extras / ] },
     };
 
 Creates a new L<RT::Group> for each hashref.  In almost all cases you'll want
@@ -93,44 +95,6 @@ array ref.  Each value should be a user-defined group name or hashref to pass
 into L<< RT::Group->LoadByCols >>.  Each group found will have the new group
 added as a member.
 
-Unfortunately you can't specify the I<members> of a group at this time.  As a
-workaround, you can push a subref into C<@Final> which adds members to your new
-groups.  An example, using a convenience function to avoid repeating yourself:
-
-    push @Final, sub {
-        add_members('My New Group Name' => qw(trs alex ruslan));
-        add_members('My Second Group'   => qw(jesse kevin sunnavy jim));
-    };
-
-    sub add_members {
-        my $group_name = shift;
-        my @members    = @_;
-
-        my $group = RT::Group->new( RT->SystemUser );
-        $group->LoadUserDefinedGroup($group_name);
-
-        if ($group->id) {
-            for my $name (@members) {
-                my $member = RT::User->new( RT->SystemUser );
-                $member->LoadByCols( Name => $name );
-
-                unless ($member->Id) {
-                    RT->Logger->error("Unable to find user '$name'");
-                    next;
-                }
-
-                my ($ok, $msg) = $group->AddMember( $member->PrincipalObj->Id );
-                if ($ok) {
-                    RT->Logger->info("Added member $name to $group_name");
-                } else {
-                    RT->Logger->error("Unable to AddMember $name to $group_name: $msg");
-                }
-            }
-        } else {
-            RT->Logger->error("Unable to find group '$group_name'!");
-        }
-    }
-
 =head2 C<@Queues>
 
     push @Queues, {
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 0a1d61e..d08d972 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -758,9 +758,9 @@ sub InsertData {
     );
 
     # Slurp in stuff to insert from the datafile. Possible things to go in here:-
-    our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
+    our (@Groups, @Users, @Members, @ACL, @Queues, @ScripActions, @ScripConditions,
            @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
-    local (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
+    local (@Groups, @Users, @Members, @ACL, @Queues, @ScripActions, @ScripConditions,
            @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
 
     local $@;
@@ -781,6 +781,7 @@ sub InsertData {
         foreach my $item (@Groups) {
             my $new_entry = RT::Group->new( RT->SystemUser );
             my $member_of = delete $item->{'MemberOf'};
+            my $members = delete $item->{'Members'};
             my ( $return, $msg ) = $new_entry->_Create(%$item);
             unless ( $return ) {
                 $RT::Logger->error( $msg );
@@ -819,6 +820,12 @@ sub InsertData {
                     }
                 }
             }
+            push @Members, map { +{Group => $new_entry->id,
+                                   Class => "RT::User", Name => $_} }
+                @{ $members->{Users} || [] };
+            push @Members, map { +{Group => $new_entry->id,
+                                   Class => "RT::Group", Name => $_} }
+                @{ $members->{Groups} || [] };
         }
         $RT::Logger->debug("done.");
     }
@@ -838,6 +845,33 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
+    if ( @Members ) {
+        $RT::Logger->debug("Adding users and groups to groups...");
+        for my $item (@Members) {
+            my $group = RT::Group->new(RT->SystemUser);
+            $group->LoadUserDefinedGroup( delete $item->{Group} );
+            unless ($group->Id) {
+                RT->Logger->error("Unable to find group '$group' to add members to");
+                next;
+            }
+
+            my $class = delete $item->{Class} || 'RT::User';
+            my $member = $class->new( RT->SystemUser );
+            $item->{Domain} = 'UserDefined' if $member->isa("RT::Group");
+            $member->LoadByCols( %$item );
+            unless ($member->Id) {
+                RT->Logger->error("Unable to find $class '$name' to add to ".$group->Name);
+                next;
+            }
+
+            my ( $return, $msg) = $group->AddMember( $member->PrincipalObj->Id );
+            unless ( $return ) {
+                $RT::Logger->error( $msg );
+            } else {
+                $RT::Logger->debug( $return ."." );
+            }
+        }
+    }
     if ( @Queues ) {
         $RT::Logger->debug("Creating queues...");
         for my $item (@Queues) {

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


More information about the Rt-commit mailing list