Last active
November 7, 2024 20:43
-
-
Save ryanschuhler/35ef6993ee9d6d0415000d32e2e0604d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
API_KEY="" | |
API_SECRET="" | |
FILE_NAME="user_data" | |
LIFERAY_URL="" | |
PAGE_SIZE=1000 | |
get_auth_token() { | |
local token_response=$(curl -s -X POST \ | |
-H "Content-Type: application/x-www-form-urlencoded" \ | |
-d "grant_type=client_credentials&client_id=${API_KEY}&client_secret=${API_SECRET}" \ | |
"${LIFERAY_URL}/o/oauth2/token") | |
echo "$token_response" | jq -r '.access_token' | |
} | |
AUTH_TOKEN=$(get_auth_token) | |
API_URL="${LIFERAY_URL}/o/headless-admin-user/v1.0/user-accounts" | |
AUTH_HEADER="Authorization: Bearer ${AUTH_TOKEN}" | |
echo '"Name","ID","Email","Organizations","Accounts","Account Roles","Regular Roles","Last Login"' > "${FILE_NAME}.csv" | |
page=1 | |
last_page=0 | |
while true; do | |
paginated_api_url="${API_URL}?pageSize=${PAGE_SIZE}&page=${page}" | |
curl -s -H "${AUTH_HEADER}" "${paginated_api_url}" > "${FILE_NAME}_${page}.json" | |
if [[ $page -eq 1 ]]; then | |
last_page=$(jq -r '.lastPage' "${FILE_NAME}_${page}.json") | |
fi | |
echo "Processing page ${page} of ${last_page}" | |
jq -r '.items[] | [ | |
.name, | |
.id, | |
.emailAddress, | |
(.organizationBriefs | map(.name) | join("; ")), | |
(.accountBriefs | map(.name) | join("; ")), | |
(.accountBriefs | map(.roleBriefs | map(.name) | join(", ")) | join("; ")), | |
(.roleBriefs | map(.name) | join("; ")), | |
.lastLoginDate | |
] | @csv' "${FILE_NAME}_${page}.json" >> "${FILE_NAME}.csv" | |
if [[ "${page}" -eq "${last_page}" ]]; then | |
break | |
fi | |
page=$((page + 1)) | |
done | |
rm "${FILE_NAME}_"*.json | |
echo "User data extracted to ${FILE_NAME}.csv" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment