Last active
August 29, 2015 14:27
-
-
Save sdm7g/93b9e072cffd398484ca to your computer and use it in GitHub Desktop.
bash script to add users to ArchivesSpace repository permissions group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add users to group | |
# | |
# Usage: script $REPO_ID $GROUP_CODE [ list of user-names ] | |
# | |
ASHOST=${ASHOST:-localhost} | |
BASE="http://$ASHOST:8089" | |
USER=admin | |
PWD=admin | |
# | |
REPO=$1 ; shift; | |
GROUP=$1 ; shift; | |
# | |
# | |
SESSION=$(curl -s -F password="$PWD" "$BASE/users/$USER/login" | sed 's|.*"session":"\([a-z0-9]*\)".*|\1|g') | |
# | |
if [ -z "$REPO" ] | |
then | |
echo 'Usage adduserstogroup $REPO_ID [$GROUP_CODE [ list of usernames ]]' | |
exit | |
fi | |
# get repository name & code: | |
THISREPO=( $(curl -H "X-ArchivesSpace-Session: $SESSION" -s $BASE/repositories/$REPO | jq '.uri, .repo_code, .name' )) | |
echo ${THISREPO[*]} | |
# | |
if [ -z "$GROUP" ] # if no group | |
then # display group codes for this repo | |
echo "Groups for $THISREPO:" | |
curl -H "X-ArchivesSpace-Session: $SESSION" -s $BASE/repositories/$REPO/groups | | |
jq 'reduce .[] as $x ( {}; . + { ($x.group_code): $x.description } )' | |
exit | |
fi | |
# find the URI for that group_code | |
expr=".[] | select(.group_code == \"$GROUP\" ) | .uri" | |
GROUP_URI=$( curl -H "X-ArchivesSpace-Session: $SESSION" -s $BASE/repositories/$REPO/groups | jq "$expr" | tr -d '"') | |
# capture and display JSON repr. of group to be updated | |
JSON=$( curl -H "X-ArchivesSpace-Session: $SESSION" -s ${BASE}$GROUP_URI ) | |
echo $JSON | jq . | |
if [ -n "$1" ] # if userlist | |
then | |
# | |
ULIST=$( for U in $* ; do echo -n "\"$U\", " ; done ) | |
echo "Add Users: [ ${ULIST:0:${#ULIST}-2} ] to group ${GROUP_URI} [Y/N]? " | |
read CONTINUE | |
if [ -n "$CONTINUE" -a "${CONTINUE:0:1}" = "Y" ] | |
then | |
# update JSON with merged .member_usernames | |
NEWJSON=$( echo $JSON | jq ".member_usernames = (( .member_usernames + [ ${ULIST:0:${#ULIST}-2} ] ) | unique )" ) | |
curl -H "X-ArchivesSpace-Session: $SESSION" -d "$NEWJSON" -s ${BASE}$GROUP_URI # POST-IT, returns status | |
curl -H "X-ArchivesSpace-Session: $SESSION" -s ${BASE}$GROUP_URI | jq .member_usernames # and echo updated results | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment