[rt-users] mass-creation of RT user accounts

Andy Harrison aharrison at gmail.com
Wed Dec 8 11:18:06 EST 2004


On Mon, 6 Dec 2004 15:33:20 -0800, Pei Ku <pku at autotradecenter.com> wrote:
> Hi,
> 
> Is there a quick way of creating, say, 100 user accounts in RT w/o entering them one by one via the web interface?


***I didn't test this at all.***  This is 10 minutes of cutting and
pasting with a few extra lines thrown in.

Keep passing stuff to CreateUser.  

You can probably drop the $ErrorTo and $entity part (or populate them
accordingly as seen in <RT_BASE>/lib/RT/Interface/Email.pm for an
example) and just do:
CreateUser( "username", "e-mail address", "name" );

Or loop through a list appropriately.

--------------
         #!/usr/local/bin/perl -w
         use strict;
         use lib "/path/to/rt/libraries/";

         use RT::Interface::CLI  qw(CleanEnv
                                  GetCurrentUser GetMessageContent loc);

         #Clean out all the nasties from the environment
         CleanEnv();

         #let's talk to RT'
         use RT;

         #Load RT's config file
         RT::LoadConfig();

         # Connect to the database. set up loggign
         RT::Init();

         #Load the systemuser
         my $NewUser = RT::User->new($RT::SystemUser);

         print loc('Hello!'); # Synonym of $CurrentUser->loc('Hello!');


# {{{ Create User
 
 sub CreateUser {
     my ($Username, $Address, $Name, $ErrorsTo, $entity) = @_;
     my $NewUser = RT::User->new($RT::SystemUser);
 
     my ($Val, $Message) = 
       $NewUser->Create(Name => ($Username || $Address),
                        EmailAddress => $Address,
                        RealName => $Name,
                        Password => undef,
                        Privileged => 0,
                        Comments => 'Autocreated on ticket submission'
                       );
     
     unless ($Val) {
         
         # Deal with the race condition of two account creations at once
         #
         if ($Username) {
             $NewUser->LoadByName($Username);
         }
         
         unless ($NewUser->Id) {
             $NewUser->LoadByEmail($Address);
         }
         
         unless ($NewUser->Id) {  
             MailError( To => $ErrorsTo,
                        Subject => "User could not be created",
                        Explanation => "User creation failed in
mailgateway: $Message",
                        MIMEObj => $entity,
                        LogLevel => 'crit'
                      );
         }
     }
 
     #Load the new user object
     my $CurrentUser = RT::CurrentUser->new();
     $CurrentUser->LoadByEmail($Address);
 
     unless ($CurrentUser->id) {
             $RT::Logger->warning("Couldn't load user '$Address'.". 
"giving up");
                 MailError( To => $ErrorsTo,
                            Subject => "User could not be loaded",
                            Explanation => "User  '$Address' could not
be loaded in the mail gateway",
                            MIMEObj => $entity,
                            LogLevel => 'crit'
                      );
     } else {
        my ($stat, $pass) = $CurrentUser->SetRandomPassword();
        if ( $stat ) {
            print "User: $CurrentUser->Name, " Password: ", $pass, "\n";
        } else {
             print "Unable to set password for user ", $CurrentUser->Name, "\n";
        }

}
 
     return $CurrentUser;
 }
 # }}}       


-- 
Andy Harrison



More information about the rt-users mailing list