Class: KeycloakAdmin::GroupClient

Inherits:
Client
  • Object
show all
Defined in:
lib/keycloak-admin/client/group_client.rb

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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)
  # creates a full role-representation object needed by the keycloak api to work
  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]), headers
    )
  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(headers)
  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)), headers
    )
  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(headers)
  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(headers)
  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(headers)
  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?
  pagination = {first: first, max: max}.compact
  search(pagination.empty? ? nil : pagination)
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(headers)
  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: 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, headers)
    else
      resource(groups_url).post(payload, headers)
    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)
  derived_headers = case query
                    when String
                      headers.merge({params: { search: query }})
                    when Hash
                      headers.merge({params: query })
                    else
                      headers
                    end
  response = execute_http do
    resource(groups_url).get(derived_headers)
  end
  JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
end