class KeycloakAdmin::UserClient

Public Class Methods

new(configuration, realm_client) click to toggle source
Calls superclass method KeycloakAdmin::Client::new
# File lib/keycloak-admin/client/user_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

Public Instance Methods

create!(username, email, password, email_verified, locale) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 9
def create!(username, email, password, email_verified, locale)
  user = save(build(username, email, password, email_verified, locale))
  search(user.email)&.first
end
delete(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 67
def delete(user_id)
  execute_http do
    RestClient::Resource.new(users_url(user_id), @configuration.rest_client_options).delete(headers)
  end
  true
end
execute_actions_email(user_id, actions=[], lifespan=nil) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 99
def execute_actions_email(user_id, actions=[], lifespan=nil)
  execute_http do
    lifespan_param = lifespan.nil? ? "" : "lifespan=#{lifespan.seconds}"
    RestClient.put("#{execute_actions_email_url(user_id)}?#{lifespan_param}", actions.to_json, headers)
  end
  user_id
end
execute_actions_email_url(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 163
def execute_actions_email_url(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  "#{users_url(user_id)}/execute-actions-email"
end
federated_identity_url(user_id, identity_provider) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 178
def federated_identity_url(user_id, identity_provider)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  raise ArgumentError.new("identity_provider must be defined") if identity_provider.nil?
  "#{users_url(user_id)}/federated-identity/#{identity_provider}"
end
forgot_password(user_id, lifespan=nil) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 95
def forgot_password(user_id, lifespan=nil)
  execute_actions_email(user_id, ["UPDATE_PASSWORD"], lifespan)
end
get(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 34
def get(user_id)
  response = execute_http do
    RestClient::Resource.new(users_url(user_id), @configuration.rest_client_options).get(headers)
  end
  UserRepresentation.from_hash(JSON.parse(response))
end
get_redirect_impersonation(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 122
def get_redirect_impersonation(user_id)
  ImpersonationRedirectionRepresentation.from_url(impersonation_url(user_id), headers)
end
groups(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 74
def groups(user_id)
  response = execute_http do
    RestClient::Resource.new(groups_url(user_id), @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |group_as_hash| GroupRepresentation.from_hash(group_as_hash) }
end
groups_url(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 168
def groups_url(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  "#{users_url(user_id)}/groups"
end
impersonate(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 107
def impersonate(user_id)
  impersonation = get_redirect_impersonation(user_id)
  response = execute_http do
    RestClient::Request.execute(
      @configuration.rest_client_options.merge(
        method: :post,
        url: impersonation.impersonation_url,
        payload: impersonation.body.to_json,
        headers: impersonation.headers
      )
    )
  end
  ImpersonationRepresentation.from_response(response, @configuration.server_domain)
end
impersonation_url(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 173
def impersonation_url(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  "#{users_url(user_id)}/impersonation"
end
list() click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 63
def list
  search(nil)
end
reset_password_url(user_id) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 158
def reset_password_url(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  "#{users_url(user_id)}/reset-password"
end
save(user_representation) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 14
def save(user_representation)
  execute_http do
    RestClient::Resource.new(users_url, @configuration.rest_client_options).post(
      user_representation.to_json, headers
    )
  end
  user_representation
end
update(user_id, user_representation_body) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 23
def update(user_id, user_representation_body)
  RestClient::Request.execute(
    @configuration.rest_client_options.merge(
      method: :put,
      url: users_url(user_id),
      payload: user_representation_body.to_json,
      headers: headers
    )
  )
end
update_password(user_id, new_password) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 81
def update_password(user_id, new_password)
  execute_http do
    RestClient::Request.execute(
      @configuration.rest_client_options.merge(
        method: :put,
        url: reset_password_url(user_id),
        payload: { type: 'password', value: new_password, temporary: false }.to_json,
        headers: headers
      )
    )
  end
  user_id
end
users_url(id=nil) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 150
def users_url(id=nil)
  if id
    "#{@realm_client.realm_admin_url}/users/#{id}"
  else
    "#{@realm_client.realm_admin_url}/users"
  end
end

Private Instance Methods

build(username, email, password, email_verified, locale) click to toggle source
# File lib/keycloak-admin/client/user_client.rb, line 186
def build(username, email, password, email_verified, locale)
  user                     = UserRepresentation.new
  user.email               = email
  user.username            = username
  user.email_verified      = email_verified
  user.enabled             = true
  user.attributes          = {}
  user.attributes[:locale] = locale if locale
  user.add_credential(CredentialRepresentation.from_password(password))
  user
end