<commit_before>#!/bin/sh

. "$(dirname "$(readlink -f "$0")")"/engine-prolog.sh

generatePgPass() {
	local password="${<PASSWORD>}"

	#
	# we need client side psql library
	# version as at least in rhel for 8.4
	# the password within pgpassfile is
	# not escaped.
	# the simplest way is to checkout psql
	# utility version.
	#
	if ! psql -V | grep -q ' 8\.'; then
		password="$(echo "${password}" | sed -e 's/\\/\\\\/g' -e 's/:/\\:/g')"
	fi

	export PGPASSFILE="${MYTEMP}/.pgpass"
	touch "${PGPASSFILE}" || die "Can't create ${PGPASSFILE}"
	chmod 0600 "${PGPASSFILE}" || die "Can't chmod ${PGPASSFILE}"

	cat > "${PGPASSFILE}" << __EOF__
${ENGINE_DB_HOST}:${ENGINE_DB_PORT}:${ENGINE_DB_DATABASE}:${ENGINE_DB_USER}:${password}
__EOF__
}

usage() {
	cat << __EOF__
Usage: $0 [OPTIONS]
Manage user roles.

    --command=command        Command.
        add                      Add role.
    --user-name=name         User name.
    --provider=name          Name of authorization provider instace.
    --provider-namespace=ns  Namespace within provider.
    --provider-id=id         Unique user id within provider.
    --role=role              Role name.

Interesting roles:

    SuperUser
        Role of administrator.
__EOF__
}

cleanup() {
	[ -n "${MYTEMP}" ] && rm -fr "${MYTEMP}" ]
}
trap cleanup 0

COMMAND=
USER_NAME=
PROVIDER=
PROVIDER_NAMESPACE=
PROVIDER_ID=
ROLE=

while [ -n "$1" ]; do
	x="$1"
	v="${x#*=}"
	shift
	case "${x}" in
		--command=*)
			COMMAND="${v}"
			case "${COMMAND}" in
				add) ;;
				*) die "Invalid command '${COMMAND}'" ;;
			esac
		;;
		--user-name=*)
			USER_NAME="${v}"
		;;
		--provider=*)
			PROVIDER="${v}"
		;;
		--provider-namespace=*)
			PROVIDER_NAMESPACE="${v}"
		;;
		--provider-id=*)
			PROVIDER_ID="${v}"
		;;
		--role=*)
			ROLE="${v}"
		;;
		--help)
			usage
			exit 0
		;;
		*)
			usage
			exit 1
		;;
	esac
done

[ -n "${COMMAND}" ] || die "Please specify command"
[ -n "${USER_NAME}" ] || die "Please specify user name"
[ -n "${PROVIDER}" ] || die "Please specify provider"
[ -n "${PROVIDER_NAMESPACE}" ] || die "Please specify provider namespace"
[ -n "${PROVIDER_ID}" ] || die "Please specify provider id"
[ -n "${ROLE}" ] || die "Please specify role"

MYTEMP="$(mktemp -d)"
generatePgPass
psql -h "${ENGINE_DB_HOST}" -p "${ENGINE_DB_PORT}" -U "${ENGINE_DB_USER}" -c "
	select attach_user_to_role(
		'${USER_NAME}',
		'${PROVIDER}',
		'${PROVIDER_NAMESPACE}',
		'${PROVIDER_ID}',
		'${ROLE}'
	);
" > /dev/null<commit_msg>aaa: attach_user_to_role: rename parameters to match product conventions<commit_after>#!/bin/sh

. "$(dirname "$(readlink -f "$0")")"/engine-prolog.sh

generatePgPass() {
	local password="${<PASSWORD>}"

	#
	# we need client side psql library
	# version as at least in rhel for 8.4
	# the password within pgpassfile is
	# not escaped.
	# the simplest way is to checkout psql
	# utility version.
	#
	if ! psql -V | grep -q ' 8\.'; then
		password="$(echo "${password}" | sed -e 's/\\/\\\\/g' -e 's/:/\\:/g')"
	fi

	export PGPASSFILE="${MYTEMP}/.pgpass"
	touch "${PGPASSFILE}" || die "Can't create ${PGPASSFILE}"
	chmod 0600 "${PGPASSFILE}" || die "Can't chmod ${PGPASSFILE}"

	cat > "${PGPASSFILE}" << __EOF__
${ENGINE_DB_HOST}:${ENGINE_DB_PORT}:${ENGINE_DB_DATABASE}:${ENGINE_DB_USER}:${password}
__EOF__
}

usage() {
	cat << __EOF__
Usage: $0 [OPTIONS]
Manage user roles.

    --command=command        Command.
        add                      Add role.
    --user-name=name         User name.
    --authz-name=name        Name of authorization provider instace.
    --principal-namespace=ns Namespace within provider.
    --principal-id=id        Unique user id within provider.
    --role=role              Role name.

Interesting roles:

    SuperUser
        Role of administrator.
__EOF__
}

cleanup() {
	[ -n "${MYTEMP}" ] && rm -fr "${MYTEMP}" ]
}
trap cleanup 0

COMMAND=
USER_NAME=
AUTHZ_NAME=
PRINCIPAL_NAMESPACE=
PRINCIPAL_ID=
ROLE=

while [ -n "$1" ]; do
	x="$1"
	v="${x#*=}"
	shift
	case "${x}" in
		--command=*)
			COMMAND="${v}"
			case "${COMMAND}" in
				add) ;;
				*) die "Invalid command '${COMMAND}'" ;;
			esac
		;;
		--user-name=*)
			USER_NAME="${v}"
		;;
		--authz-name=*)
			AUTHZ_NAME="${v}"
		;;
		--principal-namespace=*)
			PRINCIPAL_NAMESPACE="${v}"
		;;
		--principal-id=*)
			PRINCIPAL_ID="${v}"
		;;
		--role=*)
			ROLE="${v}"
		;;
		--help)
			usage
			exit 0
		;;
		*)
			usage
			exit 1
		;;
	esac
done

[ -n "${COMMAND}" ] || die "Please specify command"
[ -n "${USER_NAME}" ] || die "Please specify user name"
[ -n "${AUTHZ_NAME}" ] || die "Please specify provider"
[ -n "${PRINCIPAL_NAMESPACE}" ] || die "Please specify provider namespace"
[ -n "${PRINCIPAL_ID}" ] || die "Please specify provider id"
[ -n "${ROLE}" ] || die "Please specify role"

MYTEMP="$(mktemp -d)"
generatePgPass
psql -h "${ENGINE_DB_HOST}" -p "${ENGINE_DB_PORT}" -U "${ENGINE_DB_USER}" -c "
	select attach_user_to_role(
		'${USER_NAME}',
		'${AUTHZ_NAME}',
		'${PRINCIPAL_NAMESPACE}',
		'${PRINCIPAL_ID}',
		'${ROLE}'
	);
" > /dev/null
{'fraction_non_alphanumeric': 0.19565650527993625, 'fraction_numerical': 0.005578800557880056, 'mean_word_length': 3.0418679549114334, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '5267179', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1885, 'n_words': 464}