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)


3
4
5
6
7
# File 'lib/keycloak-admin/client/group_client.rb', line 3

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



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/keycloak-admin/client/group_client.rb', line 99

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
    RestClient::Resource.new(url, @configuration.rest_client_options).post(
      create_payload([role_representation]), headers
    )
  end
  role_representation
end

#children(parent_id) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/keycloak-admin/client/group_client.rb', line 16

def children(parent_id)
  response = execute_http do
    url = "#{groups_url(parent_id)}/children"
    RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
end

#create!(name, path = nil, attributes = {}) ⇒ Object



43
44
45
46
# File 'lib/keycloak-admin/client/group_client.rb', line 43

def create!(name, path = nil, attributes = {})
  response = save(build(name, path, attributes))
  created_id(response)
end

#create_subgroup!(parent_id, name, attributes = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/keycloak-admin/client/group_client.rb', line 59

def create_subgroup!(parent_id, name, attributes = {})
  url = "#{groups_url(parent_id)}/children"
  response = execute_http do
    RestClient::Resource.new(url, @configuration.rest_client_options).post(
      create_payload(build(name, nil, attributes)), headers
    )
  end
  created_id(response)
end

#delete(group_id) ⇒ Object



69
70
71
72
73
74
# File 'lib/keycloak-admin/client/group_client.rb', line 69

def delete(group_id)
  execute_http do
    RestClient::Resource.new(groups_url(group_id), @configuration.rest_client_options).delete(headers)
  end
  true
end

#get(group_id) ⇒ Object



9
10
11
12
13
14
# File 'lib/keycloak-admin/client/group_client.rb', line 9

def get(group_id)
  response = execute_http do
    RestClient::Resource.new(groups_url(group_id), @configuration.rest_client_options).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



90
91
92
93
94
95
96
# File 'lib/keycloak-admin/client/group_client.rb', line 90

def get_realm_level_roles(group_id)
  url = "#{groups_url(group_id)}/role-mappings/realm"
  response = execute_http do
    RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
end

#groups_url(id = nil) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/keycloak-admin/client/group_client.rb', line 111

def groups_url(id=nil)
  if id
    "#{@realm_client.realm_admin_url}/groups/#{id}"
  else
    "#{@realm_client.realm_admin_url}/groups"
  end
end

#listObject



24
25
26
# File 'lib/keycloak-admin/client/group_client.rb', line 24

def list
  search(nil)
end

#members(group_id, first = 0, max = 100) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/keycloak-admin/client/group_client.rb', line 76

def members(group_id, first=0, max=100)
  url = "#{groups_url(group_id)}/members"
  query = {first: first.try(:to_i), max: max.try(:to_i)}.compact
  unless query.empty?
    query_string = query.to_a.map { |e| "#{e[0]}=#{e[1]}" }.join("&")
    url = "#{url}?#{query_string}"
  end
  response = execute_http do
    RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |user_as_hash| UserRepresentation.from_hash(user_as_hash) }
end

#save(group_representation) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/keycloak-admin/client/group_client.rb', line 48

def save(group_representation)
  execute_http do
    payload = create_payload(group_representation)
    if group_representation.id
      RestClient::Resource.new(groups_url(group_representation.id), @configuration.rest_client_options).put(payload, headers)
    else
      RestClient::Resource.new(groups_url, @configuration.rest_client_options).post(payload, headers)
    end
  end
end

#search(query) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/keycloak-admin/client/group_client.rb', line 28

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
    RestClient::Resource.new(groups_url, @configuration.rest_client_options).get(derived_headers)
  end
  JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
end