[Rt-commit] rt branch, 4.0/initialdata-membership, created. rt-4.0.18rc1-25-g58b176d

Alex Vandiver alexmv at bestpractical.com
Fri Oct 11 22:29:51 EDT 2013


The branch, 4.0/initialdata-membership has been created
        at  58b176dbe790ee2026fbac2d86bb54cbbe3da324 (commit)

- Log -----------------------------------------------------------------
commit 44bda4ae3eb4854c65a9858e162df0d85312bfec
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 c649b62..1872dee 100644
--- a/docs/initialdata.pod
+++ b/docs/initialdata.pod
@@ -81,55 +81,19 @@ 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
 to follow the example above to create a group just as if you had done it from
 the admin interface.  B<Do not> omit the C<< Domain => 'UserDefined' >> line.
 
-Additionally, the C<MemberOf> field is specially handled to make it easier to
-add the new group to other groups.  C<MemberOf> may be a single value or an
-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'!");
-        }
-    }
+In addition to the C<Members> option shown above, which can take both
+users and groups, the C<MemberOf> field may be a single value or an
+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.
 
 =head2 C<@Queues>
 
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index ca6f2e4..c820eaa 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -765,9 +765,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 $@;
@@ -788,6 +788,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 );
@@ -826,6 +827,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.");
     }
@@ -845,6 +852,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 '".($item->{id} || $item->{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) {

commit 58b176dbe790ee2026fbac2d86bb54cbbe3da324
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Oct 11 22:29:35 2013 -0400

    Default the Domain to UserDefined, because that's what 99% will be

diff --git a/docs/initialdata.pod b/docs/initialdata.pod
index 1872dee..6f3b414 100644
--- a/docs/initialdata.pod
+++ b/docs/initialdata.pod
@@ -87,7 +87,7 @@ For a full list of fields, read the documentation for L<RT::User/Create>.
 
 Creates a new L<RT::Group> for each hashref.  In almost all cases you'll want
 to follow the example above to create a group just as if you had done it from
-the admin interface.  B<Do not> omit the C<< Domain => 'UserDefined' >> line.
+the admin interface.
 
 In addition to the C<Members> option shown above, which can take both
 users and groups, the C<MemberOf> field may be a single value or an
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index c820eaa..16a3d36 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -787,6 +787,7 @@ sub InsertData {
         $RT::Logger->debug("Creating groups...");
         foreach my $item (@Groups) {
             my $new_entry = RT::Group->new( RT->SystemUser );
+            $item->{Domain} ||= 'UserDefined';
             my $member_of = delete $item->{'MemberOf'};
             my $members = delete $item->{'Members'};
             my ( $return, $msg ) = $new_entry->_Create(%$item);

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


More information about the Rt-commit mailing list