#!/bin/bash # ========== # $File: //guest/amo/scripts/triggers/validJobUser.sh $ # $Revision: #2 $ # $Change: 26333 $ # # $Author: amo $ # $DateTimeTZ: 2020/02/20 03:16:55 -0800 PST $ # ---------- # Purpose: # script to check if the username passed to this script exists in the database # # Example trigger entry: # validJobUser form-save job "%//scripts/triggers/validJobUser.sh% admin %serverport% myWorkspace %formfile%" # # ---------- # This is an example script, and is not supported by Perforce Software. # While it does some basic sanity checks (confirming that 'p4' is available, and that there's a valid # login ticket), it would benefit from more robust exception-handling, some more thorough testing. # # NB, originally, the user was grabbed from the 'p4 job' command using the command below. However, # brand-new jobs are named 'new' and this 'p4 job' command doesn't retrieve anything. # Had to switch to the 'formfile' approach to account for this. # -> user=`$p4ztag -F %Assignee% job -o $jobname` # ========== # name the logfile based on the script's name # log="$(basename $0).log" # this works if the script is not in the depot, otherwise it picks the tmp filename. this=validJobUser.sh log=validJobUser.log # create a $(timestamp function timestamp(){ date +"%Y-%m-%d-%T" } # initial log output # printf "%s %s: starting... (actual script: %s)\n" $(timestamp) $this $0 >> $log # the parameters we're expecting (note - not checked/validated) adminuser=$1 port=$2 client=$3 jobname=$4 # actually formfile - we're having to parse the tmp file. # messages usage="USAGE: $0 adminuser port client job-formname" nocli="ERROR: 'p4' client not found" nocon="ERROR: 'p4' not logged in or no connection available" unknown="ERROR: Trying to assign job to unknown user:" # checking parameters provided if [ "$#" -ne 4 ]; then printf "%s\n" "$usage" >> $log; exit 4; fi # check we have 'p4' available p4cli=`which p4` if [ "$?" -ne 0 ]; then printf "%s\n%s\n" "$usage" "$nocli" >> $log; exit 2; fi # 'p4' exists; create the 'connection string' (tagged mode) p4ztag="$p4cli -u $adminuser -p $port -c $client -ztag" # test for a current login ticket $p4ztag login -s > /dev/null 2>&1 if [ $? -ne 0 ]; then printf "%s\n%s\n" "$usage" "$nocon" >> $log; exit 3; fi # get 'Assignee' from job formfile user=`grep ^Assignee $jobname | sed -e "s/Assignee:\s//g" 2>&1` usery="$user found in user list - accepted job." usern="$user not found in user list - rejecting." # count the number of times 'Assignee' user appears in 'p4 users'. Would be 1 or 0, realistically. ucount=`$p4ztag -F %User% users $user 2>/dev/null | wc -l` # if the count is greater than 0, then we've found a user that matches, so we accept this. # otherwise, we can't accept this name for 'Assignee' and the trigger fails. if [ "$ucount" -gt "0" ]; then # user found: write message to log file, accept job printf "%s %s %s\n" $(timestamp) "$this" "$usery" >> $log exit 0 else # user not found: write message to log file, then to user, then fail printf "%s %s %s\n" $(timestamp) "$this" "$usern" >> $log printf "%s %s\n" "$unknown" "$usern" exit 1 fi
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 26333 | Adam Morriss | Updated the comments in the script. | ||
#1 | 26332 | Adam Morriss |
A form-save trigger script for 'job' forms. This script looks for a field 'Assignee' in the form file being passed to the server. This field is expected to contain a username, and the script is checking if the name entered here is a known user. This is done by running "p4 users [username]", and counting the number of records returned - either 1 or 0, in theory. Jobs are rejected if the user does not exist, otherwise the trigger passes and all is well. This is an example script. It is not supported. It includes some basic sanity checks, but would benefit from more robust exception-handling and testing. Currently, it does little more than checking that 'p4' is available and that there's a valid login ticket. |