Class: KeycloakAdmin::GroupClient
- Inherits:
-
Client
- Object
- Client
- KeycloakAdmin::GroupClient
show all
- Defined in:
- lib/keycloak-admin/client/group_client.rb
Instance Method Summary
collapse
-
#add_realm_level_role_name!(group_id, role_name) ⇒ Object
Adds a realm-level role to a group via the role name.
-
#children(parent_id) ⇒ Object
-
#create!(name, path = nil, attributes = {}) ⇒ Object
-
#create_subgroup!(parent_id, name, attributes = {}) ⇒ Object
-
#delete(group_id) ⇒ Object
-
#get(group_id) ⇒ Object
-
#get_realm_level_roles(group_id) ⇒ Object
Gets all realm-level roles for a group.
-
#groups_url(id = nil) ⇒ Object
-
#initialize(configuration, realm_client) ⇒ GroupClient
constructor
A new instance of GroupClient.
-
#list(first: nil, max: nil) ⇒ Object
Unlike the users endpoint, this one applies no default cap: a bare list already returns every group.
-
#members(group_id, first = 0, max = 100) ⇒ Object
-
#remove_realm_level_role_name!(group_id, role_name) ⇒ Object
Remove a realm-level role from a group by the role name.
-
#save(group_representation) ⇒ Object
-
#search(query) ⇒ Object
Methods inherited from Client
#create_payload, #created_id, #current_token, #execute_http, #headers, #server_url
Constructor Details
#initialize(configuration, realm_client) ⇒ GroupClient
Returns a new instance of GroupClient.
4
5
6
7
8
|
# File 'lib/keycloak-admin/client/group_client.rb', line 4
def initialize(configuration, realm_client)
super(configuration)
raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
@realm_client = realm_client
end
|
Instance Method Details
#add_realm_level_role_name!(group_id, role_name) ⇒ Object
Adds a realm-level role to a group via the role name
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/keycloak-admin/client/group_client.rb', line 105
def add_realm_level_role_name!(group_id, role_name)
role_representation = RoleClient.new(@configuration, @realm_client).get(role_name)
url = "#{groups_url(group_id)}/role-mappings/realm"
response = execute_http do
resource(url).post(
create_payload([role_representation]),
)
end
role_representation
end
|
#children(parent_id) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/keycloak-admin/client/group_client.rb', line 17
def children(parent_id)
response = execute_http do
url = "#{groups_url(parent_id)}/children"
resource(url).get()
end
JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
end
|
#create!(name, path = nil, attributes = {}) ⇒ Object
52
53
54
55
|
# File 'lib/keycloak-admin/client/group_client.rb', line 52
def create!(name, path = nil, attributes = {})
response = save(build(name, path, attributes))
created_id(response)
end
|
#create_subgroup!(parent_id, name, attributes = {}) ⇒ Object
68
69
70
71
72
73
74
75
76
|
# File 'lib/keycloak-admin/client/group_client.rb', line 68
def create_subgroup!(parent_id, name, attributes = {})
url = "#{groups_url(parent_id)}/children"
response = execute_http do
resource(url).post(
create_payload(build(name, nil, attributes)),
)
end
created_id(response)
end
|
#delete(group_id) ⇒ Object
78
79
80
81
82
83
|
# File 'lib/keycloak-admin/client/group_client.rb', line 78
def delete(group_id)
execute_http do
resource(groups_url(group_id)).delete()
end
true
end
|
#get(group_id) ⇒ Object
10
11
12
13
14
15
|
# File 'lib/keycloak-admin/client/group_client.rb', line 10
def get(group_id)
response = execute_http do
resource(groups_url(group_id)).get()
end
GroupRepresentation.from_hash(JSON.parse(response))
end
|
#get_realm_level_roles(group_id) ⇒ Object
Gets all realm-level roles for a group
96
97
98
99
100
101
102
|
# File 'lib/keycloak-admin/client/group_client.rb', line 96
def get_realm_level_roles(group_id)
url = "#{groups_url(group_id)}/role-mappings/realm"
response = execute_http do
resource(url).get()
end
JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
end
|
#groups_url(id = nil) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/keycloak-admin/client/group_client.rb', line 135
def groups_url(id=nil)
if id
"#{@realm_client.realm_admin_url}/groups/#{encode_segment(id)}"
else
"#{@realm_client.realm_admin_url}/groups"
end
end
|
#list(first: nil, max: nil) ⇒ Object
Unlike the users endpoint, this one applies no default cap: a bare list already returns
every group. Passing neither bound sends no query parameter at all, exactly as before.
Keycloak 19 only paginates here when both bounds are present - a lone max is ignored and
the whole list comes back. Sending first=0 alongside it makes the call behave the same on
every supported version, and is a no-op from Keycloak 23 on.
31
32
33
34
35
|
# File 'lib/keycloak-admin/client/group_client.rb', line 31
def list(first: nil, max: nil)
first = 0 if first.nil? && !max.nil?
= {first: first, max: max}.compact
search(.empty? ? nil : )
end
|
#members(group_id, first = 0, max = 100) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/keycloak-admin/client/group_client.rb', line 85
def members(group_id, first=0, max=100)
url = "#{groups_url(group_id)}/members"
query = {first: first&.to_i, max: max&.to_i}.compact
url = "#{url}?#{build_query(query)}" unless query.empty?
response = execute_http do
resource(url).get()
end
JSON.parse(response).map { |user_as_hash| UserRepresentation.from_hash(user_as_hash) }
end
|
#remove_realm_level_role_name!(group_id, role_name) ⇒ Object
Remove a realm-level role from a group by the role name
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/keycloak-admin/client/group_client.rb', line 118
def remove_realm_level_role_name!(group_id, role_name)
role_representation = RoleClient.new(@configuration, @realm_client).get(role_name)
url = "#{groups_url(group_id)}/role-mappings/realm"
execute_http do
Resource.execute(
connection_options.merge(
url: url,
method: :delete,
payload: create_payload([role_representation]),
headers: ,
logger: @configuration.logger
)
)
end
true
end
|
#save(group_representation) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/keycloak-admin/client/group_client.rb', line 57
def save(group_representation)
execute_http do
payload = create_payload(group_representation)
if group_representation.id
resource(groups_url(group_representation.id)).put(payload, )
else
resource(groups_url).post(payload, )
end
end
end
|
#search(query) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/keycloak-admin/client/group_client.rb', line 37
def search(query)
= case query
when String
.merge({params: { search: query }})
when Hash
.merge({params: query })
else
end
response = execute_http do
resource(groups_url).get()
end
JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
end
|