List Info

Thread: Creating Users




Creating Users
user name
2006-03-08 13:32:44
Tony,

Thanks for the reply. My goal is to create a script for creating new users that automates all of the individual tasks that are starting to get tedious. This is also a good opportunity for me to get a handle on scripting in general with Perforce.

I come from an extensive background in ClearCase. Over the years I've developed many scripts used by both myself as an administrator and by my user base. They have been spoiled with all of the things they can do with my scripts. So, they expect the same as we transition over to Perforce.

Back to the script... I want to take a users full name and do the following:

1) Create a user based on the full name as lowercase <first initial&gt;<last name>.
2) Set the new users Email address to include our domain name.
3) Initialize user password. ( We use strong passwords. Eventual LDAP authentication. )
4) Add the user to the primary users group.
5) Add the user to any other groups as needed.

So, far I have code to perform the first two steps:

require P4;

my $p4 = new P4;
 
$p4->ParseForms();
$p4->Connect or die ( "Failed to connect to Perforce Server" );
 
newUsers = ( "Test User" ); # Will come from user prompting and/or file.
 
foreach $newUser ( newUsers )
{
 &nbsp;  createUser( $newUser );
}
 
sub getUsername {
 &nbsp;  my ( $fullName ) = _; # Assumes complete "Firstname Lastname"
 
 &nbsp;  $fullName =~ m/^\s*(\w)\w*\s+(\w+)\s*$/;
&nbsp; &nbsp; $userName = lc($1) . lc($2);
 &nbsp;  return $userName;
}
 
sub createUser {
 &nbsp;  my ( $fullName ) = _; # Assumes complete "Firstname Lastname"
 
 &nbsp;  $userName = getUsername( $fullName );
 
 &nbsp;  # Initiate new user creation
  ;  my $user = $p4->FetchUser( $userName );
 
 &nbsp;  # Modify user data
&nbsp; &nbsp; $user->{ "Email" } = "$userName\visiprise.com";
 ; &nbsp; $user->{ "FullName" } = "$fullName";
 
 &nbsp;  # Save new user
&nbsp; &nbsp; $p4->SetInput( $user );
 &nbsp;  $p4->Run("user", "-fi");
 
 &nbsp;  # Set Password
 
  ;  # Add to base user group.
 
  ;  # Add to other groups.
}

Any input on the remaining steps.
 
Thanks.....Scott...

-----Original Message-----
From: Tony Smith [
perforce.com">mailto:tonyperforce.com]
Sent: Wednesday, March 08, 2006 6:21 AM
To: p4perlperforce.com
Cc: Scott Lavender
Subject: Re: [p4perl] Creating Users



Hi Scott,

On Tuesday 07 March 2006 19:45, Scott Lavender wrote:
>; Thanks for the help David. It works great. But, I have a follow up
> question. After modifying the user hash, what would be the corresponding
> way to save/create the user? I though it would be:
>
> $p4->SaveUser( $user );
>
&gt; But, this does not seem to work.

If you're attempting to modify another user's details, you will need to invoke
the power of the '-f' flag. This should do the trick:

&nbsp;   ; &nbsp;  $p4->SaveUser( "-f", $user );

Note also that when things don't 'seem to work', it's worth printing the
output returned from the command and the contents of the $p4->Errors() and
$p4-&gt;Warnings() arrays. They'll yield valuable clues.

You can also use $p4->DebugLevel( 3 ) (in the latest builds of P4Perl) to see
way more information than you really want.

Tony
Creating Users
user name
2006-03-08 15:19:55
Hi Scott,

> Thanks for the reply. My goal is to create a script for
creating new users
> that automates all of the individual tasks that are
starting to get
> tedious. This is also a good opportunity for me to get
a handle on
> scripting in general with Perforce.

Indeed - sounds like a great place to start.

> I come from an extensive background in ClearCase. Over
the years I've
> developed many scripts used by both myself as an
administrator and by my
> user base. They have been spoiled with all of the
things they can do with
> my scripts. So, they expect the same as we transition
over to Perforce.

I doubt they'll be disappointed: anything you can do from
the command line, 
you can do in a script.

> Back to the script... I want to take a users full name
and do the
> following:
>
> 1) Create a user based on the full name as lowercase
<first initial><last
> name>. 2) Set the new users Email address to include
our domain name.
> 3) Initialize user password. ( We use strong passwords.
Eventual LDAP
> authentication. ) 4) Add the user to the primary users
group.
> 5) Add the user to any other groups as needed.
>
> Any input on the remaining steps.

Sure. On step 3, you'll need to use something like this:

	$p4->SetInput( [ $newpass, $newpass ] );
	$p4->Run( "passwd", $username );

This is because the dialog for a user who has an existing
password is a little 
different from setting an initial password (there's no
prompt for the 
existing password), and P4::Password() doesn't handle that.
It may do in 
future, but it doesn't now.

On steps 4 and 5, here's an example:

------------------------ cut
--------------------------------
#!/usr/bin/perl
use P4;

my $p4 = new P4;
$p4->ParseForms();
$p4->Connect() or die( "Can't connect to
Perforce" );

# Create the new user
my $user = $p4->FetchUser( 'TestUser' );
$user->{ 'Email' } = 'tonyperforce.com';
$user->{ 'FullName' } = 'Test User';
$p4->SaveUser( $user, "-f" );

# Add the user to the group, creating it if necessary
my $group = $p4->FetchGroup( 'somegroup' );
$group->{ 'Users' } = [] unless defined $group->{
'Users' };
push( {$group->{ 'Users' }}, $user->{ 'User' } );
$p4->SaveGroup( $group );
------------------------ cut
--------------------------------

Hope that helps!

Tony
_______________________________________________
p4perl mailing list
p4perlperforce.com

http://maillist.perforce.com/mailman/listinfo/p4perl
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )