dfioretti
6/7/2018 - 11:49 PM

Box CLI - Provision User Locked Root Bash Example

#!/bin/bash

# This is an example script for provisioning Box users with an admin owned
# user personal folder using the Box CLI.  The script takes in a CSV of new 
# user's names and email addresses and for each creates the user, creates 
# their personal folder, and collaborates the user into the folder as an 
# editor.

# Display script usage when called with empty parameters or -h|--help option 
function script_usage() {
    cat << EOF
    This is an example script for provisioning Box users with an admin owned
    user personal folder using the Box CLI.  The script takes in a CSV of new 
    user's names and email addresses and for each creates the user, creates 
    their personal folder, and collaborates the user into the folder as an 
    editor.

    Usage:
        -h|--help                  Displays this help
        -f|--file                  Path to CSV formatted as: FIRST_NAME LAST_NAME,EMAIL_ADDRESS
        -t|--token                 Box Access Token
        -p|--parent-id             Parent folder ID for user folder 
        -a|--as-user               User ID to execute script 
EOF
}

# Prossess the command line arguments and set them to the appropriate 
# variables for processsing.  If no arugments are supplied, print
# script usage.
function process_options() {
    if [ $# == 0 ] ; then
        script_usage
        exit 1;
    fi

    while [ -n "$1" ]
    do
        case "$1" in
            -h|--help) 
                script_usage
                exit 0
                ;;
            -f|--file)
                file="$2"
                shift
                ;;
            -t|--token)
                token="$2"
                shift
                ;;
            -p|--parent-id)
                parent="$2"
                shift
                ;;
            -a|--as-user)
                as_user="$2"
                shift
                ;;
            --) 
                shift
                break
                ;;
            *) echo "Option $1 not recognized";;
        esac
        shift
    done
}

# Provision a user with the Box CLI by processing the specified
# CSV file.  For each line within the CSV, first create the
# user's personal folder, then create the user, then collaborate
# the user into the folder as an editor.
function provision_users() {
    while IFS="," read -r col1 col2 
    do
        USER_NAME="$col1"
        EMAIL="$col2"
        echo "Provisioning: $USER_NAME $EMAIL"
        FOLDER_ID=$(box folders create $parent "$USER_NAME" --id-only --as-user $as_user --token $token)
        echo "Created Folder $FOLDER_ID"
        USER_ID=$(box users create "$USER_NAME" $EMAIL --id-only --as-user $as_user --token $token)
        echo "Created User $USER_ID"
        COLLABORATION=$(box collaborations add $FOLDER_ID folder --editor --user-id $USER_ID --as-user $as_user --token $token)
        echo "Collaborated User $COLLABORATION"
    done < "$file"
}
    
# Man control flow
function main() {
    process_options "$@"
    provision_users
}

# Run
main "$@"