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



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

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

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



28
29
30
31
# File 'lib/keycloak-admin/client/group_client.rb', line 28

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

#create_subgroup!(parent_id, name) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/keycloak-admin/client/group_client.rb', line 41

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

#get_realm_level_roles(group_id) ⇒ Object

Gets all realm-level roles for a group



65
66
67
68
69
70
71
# File 'lib/keycloak-admin/client/group_client.rb', line 65

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



86
87
88
89
90
91
92
# File 'lib/keycloak-admin/client/group_client.rb', line 86

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

#listObject



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

def list
  search(nil)
end

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



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/keycloak-admin/client/group_client.rb', line 51

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



33
34
35
36
37
38
39
# File 'lib/keycloak-admin/client/group_client.rb', line 33

def save(group_representation)
  execute_http do
    RestClient::Resource.new(groups_url, @configuration.rest_client_options).post(
      create_payload(group_representation), headers
    )
  end
end

#search(query) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/keycloak-admin/client/group_client.rb', line 13

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