#! /usr/bin/perl =comment Example script illustrating how a super user could use P4-Perl to create a new user, change the email address and set a password. Sample invocation: mk_user new_user_name password $Id: //guest/jason_gibson/misc/mk_user.pl#1 $ $DateTime: 2010/07/28 11:52:07 $ $Author: jason_gibson $ $Change: 7721 $ =cut =comment Copyright (c) 2010, Perforce Software, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =cut =comment User contributed content on the Perforce Public Depot is not supported by Perforce, although it may be supported by its author. This applies to all contributions even those submitted by Perforce employees. =cut use warnings; use strict; use Carp; use P4; sub p4_emsg { return join '', map { $_->GetText() } $_[ 0 ]->Errors, $_[ 0 ]->Warnings } sub p4_errs { return $_[ 0 ]->WarningCount() + $_[ 0 ]->ErrorCount() } sub check { croak p4_emsg $_[ 0 ] if p4_errs $_[ 0 ] } sub im_not_super { return ! grep { $_->{ perm } eq 'super' } @{ $_[ 0 ]->Run( 'protects' ) } } sub user_exists { return @{ $_[ 0 ]->Run( 'users', $_[ 1 ] ) } } sub get_user { return @{ $_[ 0 ]->Run( 'user', '-f', '-o', $_[ 1 ] ) }[ 0 ] } sub save_user { my ( $p4, $user_spec ) = @_; $p4->SetInput( $p4->FormatUser( $user_spec ) ); $p4->Run( 'user', '-f', '-i' ); } sub change_pass { my ( $p4, $user, $pass ) = @_; $p4->SetInput( $pass ); $p4->Run( 'password', $user ); } sub rm_user { $_[ 0 ]->Run( 'user', '-f', '-d', $_[ 1 ] ) } ################################################################################ die "Need both a user and a password.\n" if @ARGV != 2; my $new_user = $ARGV[ 0 ]; my $new_pass = $ARGV[ 1 ]; my $email_suffix = '@example.com'; my $p4 = new P4; $p4->Connect or die p4_emsg $p4; die "You're not a super user.\n" if im_not_super $p4; check $p4; die "User '$new_user' already exists.\n" if user_exists $p4, $new_user; my $user_spec = get_user $p4, $new_user; $user_spec->_Email( $new_user . $email_suffix ); save_user $p4, $user_spec; check $p4; change_pass $p4, $new_user, $new_pass; if( p4_errs $p4 ) { print STDERR p4_emsg( $p4 ) . "\n"; rm_user $p4, $new_user; check $p4; exit 1; } $p4->Disconnect(); exit 0;