Module: Keycloak::API::UserResources

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

Instance Method Summary collapse

Methods included from Concerns::APIUtil

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

Instance Method Details

#create_user(user_rep, roles = []) ⇒ String

Keycloak server doesn’t support assign roles at creating user, so we actually implement this in 2 steps.

Parameters:

Returns:

  • (String)

    id of the created user



17
18
19
20
21
22
23
# File 'lib/keycloak/api/user_resources.rb', line 17

def create_user(user_rep, roles = [])
  res = post(user_resources_url, user_rep.to_json, headers: {content_type: :json})
  id = res.headers[:location].split("/")[-1]
  return id if roles.empty?
  add_role_mapping(id, roles)
  id
end

#delete_user(id) ⇒ Object

Parameters:

  • id (String)

    user id



74
75
76
# File 'lib/keycloak/api/user_resources.rb', line 74

def delete_user(id)
  delete("#{user_resources_url}/#{id}", headers: {content_type: :json})
end

#find_user(id) ⇒ Keycloak::Model::UserRepresentation

Returns user representation.

Parameters:

  • id (String)

    user id

Returns:



27
28
29
# File 'lib/keycloak/api/user_resources.rb', line 27

def find_user(id)
  Model::UserRepresentation.new JSON.parse(get("#{user_resources_url}/#{id}"))
end

#find_user_by_client_role(id_of_client, role, params = {}) ⇒ Keycloak::Model::UserRepresentation

Returns user representation.

Parameters:

  • id_of_client (String)

    id of client (not client-id)

  • role (String)

    role name

  • params (Hash) (defaults to: {})

    extra params

Returns:



66
67
68
69
70
71
# File 'lib/keycloak/api/user_resources.rb', line 66

def find_user_by_client_role(id_of_client, role, params = {})
  url = "#{admin_realm_url}/clients/#{id_of_client}/roles/#{role}/users"
  Utils::RepresentationIterator.new(self, params) do
    JSON.parse(get(url, params: params)).map { |user| Model::UserRepresentation.new(user) }
  end
end

#find_user_by_role(role, params = {}) ⇒ Keycloak::Model::UserRepresentation

Returns user representation.

Parameters:

  • role (String)

    role name

  • params (Hash) (defaults to: {})

    extra params

Returns:



55
56
57
58
59
60
# File 'lib/keycloak/api/user_resources.rb', line 55

def find_user_by_role(role, params = {})
  url = "#{admin_realm_url}/roles/#{role}/users"
  Utils::RepresentationIterator.new(self, params) do
    JSON.parse(get(url, params: params)).map { |user| Model::UserRepresentation.new(user) }
  end
end

#find_user_by_username(username) ⇒ Keycloak::Model::UserRepresentation

Returns user representation.

Parameters:

  • username (String)

    username

Returns:



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

def find_user_by_username(username)
  find_users({username: username}).to_a[0]
end

#find_users(params = {}) ⇒ Keycloak::Utils::RepresentationIterator

Returns:



39
40
41
42
43
44
# File 'lib/keycloak/api/user_resources.rb', line 39

def find_users(params = {})
  Utils::RepresentationIterator.new(self, params) do
    res = JSON.parse(get(user_resources_url, params: params))
    res.map { |user| Model::UserRepresentation.new user }
  end
end

#update_user(id, user_rep) ⇒ Object

Parameters:

  • id (String)

    user id



32
33
34
# File 'lib/keycloak/api/user_resources.rb', line 32

def update_user(id, user_rep)
  put("#{user_resources_url}/#{id}", user_rep.to_json, headers: {content_type: :json})
end

#user_resources_urlObject



7
8
9
# File 'lib/keycloak/api/user_resources.rb', line 7

def user_resources_url
  admin_realm_url + "/users"
end