Class: KeycloakAdmin::RoleClient

Inherits:
Client
  • Object
show all
Defined in:
lib/keycloak-admin/client/role_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) ⇒ RoleClient

Returns a new instance of RoleClient.

Raises:

  • (ArgumentError)


3
4
5
6
7
# File 'lib/keycloak-admin/client/role_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

#get(name) ⇒ Object

Returns the role representation for the specified role name



17
18
19
20
21
22
23
24
# File 'lib/keycloak-admin/client/role_client.rb', line 17

def get(name)
  # allows special characters in the name like space
  name = URI.encode_uri_component(name)
  response = execute_http do
    RestClient::Resource.new(role_name_url(name), @configuration.rest_client_options).get(headers)
  end
  RoleRepresentation.from_hash JSON.parse(response)
end

#listObject



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

def list
  response = execute_http do
    RestClient::Resource.new(roles_url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| RoleRepresentation.from_hash(role_as_hash) }
end

#list_groups(name) ⇒ Object

Lists all groups that have the specified role name assigned



27
28
29
30
31
32
33
34
# File 'lib/keycloak-admin/client/role_client.rb', line 27

def list_groups(name)
  # allows special characters in the name like space
  name = URI.encode_uri_component(name)
  response = execute_http do
    RestClient::Resource.new("#{role_name_url(name)}/groups", @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| GroupRepresentation.from_hash(role_as_hash) }
end

#role_id_url(id) ⇒ Object



51
52
53
# File 'lib/keycloak-admin/client/role_client.rb', line 51

def role_id_url(id)
  "#{@realm_client.realm_admin_url}/roles-by-id/#{id}"
end

#role_name_url(name) ⇒ Object



55
56
57
# File 'lib/keycloak-admin/client/role_client.rb', line 55

def role_name_url(name)
  "#{@realm_client.realm_admin_url}/roles/#{name}"
end

#roles_urlObject



47
48
49
# File 'lib/keycloak-admin/client/role_client.rb', line 47

def roles_url
  "#{@realm_client.realm_admin_url}/roles"
end

#save(role_representation) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/keycloak-admin/client/role_client.rb', line 36

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