Module: Keycloak::API::RoleResources

Extended by:
ActiveSupport::Concern
Includes:
Concerns::APIUtil
Included in:
Client
Defined in:
lib/keycloak/api/role_resources.rb

Instance Method Summary collapse

Methods included from Concerns::APIUtil

#admin_realm_url, #delete, #get, #post, #put, #realm_url

Instance Method Details

#add_role_mapping(user_id, role_mappings) ⇒ Object

Only role id and role name are required in role_mappings

Parameters:



48
49
50
51
# File 'lib/keycloak/api/role_resources.rb', line 48

def add_role_mapping(user_id, role_mappings)
  url = admin_realm_url + "/users/#{user_id}/role-mappings/realm"
  post(url, role_mappings.to_json, headers: {content_type: :json})
end

#create_or_find_role(role_rep) ⇒ Keycloak::Model::RoleRepresentation

Returns role representation.

Parameters:

Returns:



30
31
32
33
34
# File 'lib/keycloak/api/role_resources.rb', line 30

def create_or_find_role(role_rep)
  create_role(role_rep) && find_role_by_name(role_rep.name)
rescue RestClient::Conflict
  find_role_by_name(role_rep.name)
end

#create_role(role_rep) ⇒ Object

Parameters:



8
9
10
11
# File 'lib/keycloak/api/role_resources.rb', line 8

def create_role(role_rep)
  url = admin_realm_url + "/roles"
  post(url, role_rep.to_json, headers: {content_type: :json})
end

#find_role_by_name(name) ⇒ Keycloak::Model::RoleRepresentation

Returns role representation.

Parameters:

  • name (String)

    name of role

Returns:



21
22
23
24
25
26
# File 'lib/keycloak/api/role_resources.rb', line 21

def find_role_by_name(name)
  url = admin_realm_url + "/roles/#{name}"
  Model::RoleRepresentation.new JSON.parse(get(url).body)
rescue RestClient::NotFound
  nil
end

#find_user_realm_roles(user_id) ⇒ Array<Keycloak::Model::RoleRepresentation>

Returns an array of role representations.

Parameters:

  • user_id (String)

    user id

Returns:



38
39
40
41
42
# File 'lib/keycloak/api/role_resources.rb', line 38

def find_user_realm_roles(user_id)
  JSON.parse(get("#{user_resources_url}/#{user_id}/role-mappings/realm")).map do |role|
    Model::RoleRepresentation.new role
  end
end

#realm_rolesArray<Keycloak::Model::RoleRepresentation>

Returns role representations.

Returns:



14
15
16
17
# File 'lib/keycloak/api/role_resources.rb', line 14

def realm_roles
  url = admin_realm_url + "/roles"
  JSON.parse(get(url)).map { |role| Model::RoleRepresentation.new role }
end

#remove_role_mapping(user_id, role_mappings) ⇒ Object

Only role id and role name are required in role_mappings

Parameters:



57
58
59
60
# File 'lib/keycloak/api/role_resources.rb', line 57

def remove_role_mapping(user_id, role_mappings)
  url = admin_realm_url + "/users/#{user_id}/role-mappings/realm"
  delete(url, payload: role_mappings.to_json, headers: {content_type: :json})
end