-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathuaa-util
executable file
·145 lines (121 loc) · 3.48 KB
/
uaa-util
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
set -euo pipefail
usage_and_exit() {
local script_name=$(basename ${BASH_SOURCE[0]})
cat <<EOF
Usage: $script_name <command> [options]
Examples:
$script_name login
$script_name compare-groups <user1> <user2>
$script_name copy-groups <source_user> <target_user>
EOF
exit 1
}
login() {
local server=
read -r -p "UAA server: " server
uaac target "$server"
local username=
read -r -p "Username: " username
local password=
read -r -s -p "Password: " password
uaac token client get "$username" -s "$password"
}
get_user_groups() {
local username="${1:?username is required}"
local output=
if ! output=$(uaac curl "/Users?filter=userName+eq+%22$username%22&startIndex=1" --bodyonly); then
echo "Error searching for user: $username"
echo "$output" >&2
exit 1
fi
echo "$output" | jq -r '.resources[].groups[].display'
}
compare_groups() {
local user1="${1:?user1 is required}"
local user2="${2:?user2 is required}"
# Get the groups for each user
local groups_user1=$(get_user_groups $user1)
local groups_user2=$(get_user_groups $user2)
# Find common groups
local common_groups=""
for group in $groups_user1; do
if [[ $groups_user2 =~ (^|[[:space:]])$group($|[[:space:]]) ]]; then
common_groups+="$group "
fi
done
# Find unique groups for user1
local unique_groups_user1=""
for group in $groups_user1; do
if [[ ! $groups_user2 =~ (^|[[:space:]])$group($|[[:space:]]) ]]; then
unique_groups_user1+="$group "
fi
done
# Find unique groups for user2
local unique_groups_user2=""
for group in $groups_user2; do
if [[ ! $groups_user1 =~ (^|[[:space:]])$group($|[[:space:]]) ]]; then
unique_groups_user2+="$group "
fi
done
# Output the results
echo "Common groups:"
for group in $common_groups; do
echo " $group"
done
echo ""
echo "Unique groups for $user1:"
for group in $unique_groups_user1; do
echo " $group"
done
echo ""
echo "Unique groups for $user2:"
for group in $unique_groups_user2; do
echo " $group"
done
echo ""
}
copy_groups() {
local source_user="${1:?source user is required}"
local target_user="${2:?target user is required}"
# Get the groups for each user
local groups_source_user=$(get_user_groups $source_user)
local groups_target_user=$(get_user_groups $target_user)
# Find unique groups for source user
local unique_groups_source_user=""
for group in $groups_source_user; do
if [[ ! $groups_target_user =~ (^|[[:space:]])$group($|[[:space:]]) ]]; then
unique_groups_source_user+="$group "
fi
done
# Check if there are any unique groups
if [[ -z $unique_groups_source_user ]]; then
echo "No unique groups to copy to $target_user"
exit 0
fi
# Add target user to each unique group of source user
for group in $unique_groups_source_user; do
echo "Adding $target_user to $group"
uaac member add "$group" "$target_user"
done
echo "Done."
}
main() {
local subcommand="${1:-}"
shift || true
case "$subcommand" in
login)
login
;;
compare-groups)
compare_groups "$@"
;;
copy-groups)
copy_groups "$@"
;;
*)
usage_and_exit
;;
esac
}
main "$@"