<commit_before>#!/usr/bin/env bash

set -eu

token() {
    curl --silent \
	 --location \
	 --request POST \
	 --data "grant_type=password" \
	 --data "client_id=admin-cli" \
	 --data "username=admin" \
	 --data "password=password" \
	 --url "http://auth.lumen.local:8080/auth/realms/master/protocol/openid-connect/token" \
	| jq -M -r '.access_token'
}

group_id() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/groups" \
	| jq -M -r '.[0] | recurse(.subGroups[]) | select(.path == "/akvo/lumen/t1/admin") | .id'
}

user_id() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/users" \
	| jq -M -r ".[] | select( .email == \"${1}\" ) | .id"
}

assign_group() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --request PUT \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/users/${1}/groups/${2}"
}

new_user() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --request POST \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --data @/tmp/new-user.json \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/users"
}

# main

cat <<EOF > /tmp/new-user.json
{
    "access": {
        "manage": true,
        "impersonate": true,
        "mapRoles": true,
        "view": true,
        "manageGroupMembership": true
    },
    "notBefore": 0,
    "requiredActions": [],
    "disableableCredentialTypes": [
        "password"
    ],
    "email": "xxxxx",
    "lastName": "Admin",
    "firstName": "Akvo",
    "emailVerified": true,
    "totp": false,
    "enabled": true,
    "username": "local-admin"
}
EOF

email="${1}"
sed -i "s/xxxxx/${email}/" /tmp/new-user.json
new_user
uid=$(user_id "${email}")
gid=$(group_id)
assign_group "${uid}" "${gid}"
<commit_msg>[#1098] Use -i to support macOS<commit_after>#!/usr/bin/env bash

set -eu

token() {
    curl --silent \
	 --location \
	 --request POST \
	 --data "grant_type=password" \
	 --data "client_id=admin-cli" \
	 --data "username=admin" \
	 --data "password=password" \
	 --url "http://auth.lumen.local:8080/auth/realms/master/protocol/openid-connect/token" \
	| jq -M -r '.access_token'
}

group_id() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/groups" \
	| jq -M -r '.[0] | recurse(.subGroups[]) | select(.path == "/akvo/lumen/t1/admin") | .id'
}

user_id() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/users" \
	| jq -M -r ".[] | select( .email == \"${1}\" ) | .id"
}

assign_group() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --request PUT \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/users/${1}/groups/${2}"
}

new_user() {
    bearer=$(token)
    curl --silent \
	 --location \
	 --request POST \
	 --header "Authorization: Bearer ${bearer}" \
	 --header "Content-Type: application/json" \
	 --data @/tmp/new-user.json \
	 --url "http://auth.lumen.local:8080/auth/admin/realms/akvo/users"
}

# main

cat <<EOF > /tmp/new-user.json
{
    "access": {
        "manage": true,
        "impersonate": true,
        "mapRoles": true,
        "view": true,
        "manageGroupMembership": true
    },
    "notBefore": 0,
    "requiredActions": [],
    "disableableCredentialTypes": [
        "password"
    ],
    "email": "xxxxx",
    "lastName": "Admin",
    "firstName": "Akvo",
    "emailVerified": true,
    "totp": false,
    "enabled": true,
    "username": "local-admin"
}
EOF

email="${1}"
sed -i".bak" -e "s/xxxxx/${email}/" /tmp/new-user.json
new_user
uid=$(user_id "${email}")
gid=$(group_id)
assign_group "${uid}" "${gid}"

{'fraction_non_alphanumeric': 0.21410521479439468, 'fraction_numerical': 0.013324144268320699, 'mean_word_length': 2.6043046357615895, 'pattern_counts': {'":': 32, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1667019', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1592, 'n_words': 302}