Class: KeycloakAdmin::UserClient

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

Returns a new instance of UserClient.

Raises:

  • (ArgumentError)


3
4
5
6
7
# 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

Instance Method Details

#add_client_roles_on_user(user_id, client_id, role_representations) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/keycloak-admin/client/user_client.rb', line 57

def add_client_roles_on_user(user_id, client_id, role_representations)
  execute_http do
    RestClient::Resource.new(user_client_role_mappings_url(user_id, client_id), @configuration.rest_client_options).post(
      create_payload(role_representations), headers
    )
  end
end

#add_group(user_id, group_id) ⇒ Object



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

def add_group(user_id, group_id)
  RestClient::Request.execute(
    @configuration.rest_client_options.merge(
      method: :put,
      url: "#{users_url(user_id)}/groups/#{group_id}",
      payload: create_payload({}),
      headers: headers
    )
  )
end

#create!(username, email, password, email_verified, locale, attributes = {}) ⇒ Object



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

def create!(username, email, password, email_verified, locale, attributes={})
  user = save(build(username, email, password, email_verified, locale, attributes))
  search(user.email)&.first
end

#delete(user_id) ⇒ Object



98
99
100
101
102
103
# File 'lib/keycloak-admin/client/user_client.rb', line 98

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, redirect_uri = nil, client_id = nil) ⇒ Object

Raises:

  • (ArgumentError)


130
131
132
133
134
135
136
137
138
139
# File 'lib/keycloak-admin/client/user_client.rb', line 130

def execute_actions_email(user_id, actions=[], lifespan=nil, redirect_uri=nil, client_id=nil)
  raise ArgumentError.new("client_id must be defined") if client_id.nil? && !redirect_uri.nil?
  execute_http do
    lifespan_param = lifespan.nil? ? "" : "&lifespan=#{lifespan.seconds}"
    redirect_uri_param = redirect_uri.nil? ? "" : "&redirect_uri=#{redirect_uri}"
    client_id_param = client_id.nil? ? "" : "client_id=#{client_id}"
    RestClient.put("#{execute_actions_email_url(user_id)}?#{client_id_param}#{redirect_uri_param}#{lifespan_param}", create_payload(actions), headers)
  end
  user_id
end

#execute_actions_email_url(user_id) ⇒ Object

Raises:

  • (ArgumentError)


225
226
227
228
# File 'lib/keycloak-admin/client/user_client.rb', line 225

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) ⇒ Object

Raises:

  • (ArgumentError)


240
241
242
243
244
# File 'lib/keycloak-admin/client/user_client.rb', line 240

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) ⇒ Object



126
127
128
# File 'lib/keycloak-admin/client/user_client.rb', line 126

def forgot_password(user_id, lifespan=nil)
  execute_actions_email(user_id, ["UPDATE_PASSWORD"], lifespan)
end

#get(user_id) ⇒ Object



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

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) ⇒ Object



180
181
182
# File 'lib/keycloak-admin/client/user_client.rb', line 180

def get_redirect_impersonation(user_id)
  ImpersonationRedirectionRepresentation.from_url(impersonation_url(user_id), headers)
end

#groups(user_id) ⇒ Object



105
106
107
108
109
110
# File 'lib/keycloak-admin/client/user_client.rb', line 105

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) ⇒ Object

Raises:

  • (ArgumentError)


230
231
232
233
# File 'lib/keycloak-admin/client/user_client.rb', line 230

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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/keycloak-admin/client/user_client.rb', line 141

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) ⇒ Object

Raises:

  • (ArgumentError)


235
236
237
238
# File 'lib/keycloak-admin/client/user_client.rb', line 235

def impersonation_url(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  "#{users_url(user_id)}/impersonation"
end


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/keycloak-admin/client/user_client.rb', line 184

def link_idp(user_id, idp_id, idp_user_id, idp_username)
  fed_id_rep                   = FederatedIdentityRepresentation.new
  fed_id_rep.user_id           = idp_user_id
  fed_id_rep.user_name         = idp_username
  fed_id_rep.identity_provider = idp_id

  execute_http do
    RestClient::Request.execute(
      @configuration.rest_client_options.merge(
        method: :post,
        url: federated_identity_url(user_id, idp_id),
        payload: fed_id_rep.to_json,
        headers: headers
      )
    )
  end
end

#listObject



94
95
96
# File 'lib/keycloak-admin/client/user_client.rb', line 94

def list
  search(nil)
end

#logout(user_id) ⇒ Object

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/keycloak-admin/client/user_client.rb', line 165

def logout(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?

  execute_http do
    RestClient::Request.execute(
      @configuration.rest_client_options.merge(
        method: :post,
        url: logout_url(user_id),
        headers: headers
      )
    )
  end
  true
end

#logout_url(user_id) ⇒ Object

Raises:

  • (ArgumentError)


246
247
248
249
250
# File 'lib/keycloak-admin/client/user_client.rb', line 246

def logout_url(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?

  "#{users_url(user_id)}/logout"
end

#remove_group(user_id, group_id) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/keycloak-admin/client/user_client.rb', line 47

def remove_group(user_id, group_id)
  RestClient::Request.execute(
    @configuration.rest_client_options.merge(
      method: :delete,
      url: "#{users_url(user_id)}/groups/#{group_id}",
      headers: headers
    )
  )
end

#reset_password_url(user_id) ⇒ Object

Raises:

  • (ArgumentError)


220
221
222
223
# File 'lib/keycloak-admin/client/user_client.rb', line 220

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) ⇒ Object



14
15
16
17
18
19
20
21
# 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(
      create_payload(user_representation), headers
    )
  end
  user_representation
end

#search(query) ⇒ Object

Query can be a string or a hash.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/keycloak-admin/client/user_client.rb', line 78

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(users_url, @configuration.rest_client_options).get(derived_headers)
  end
  JSON.parse(response).map { |user_as_hash| UserRepresentation.from_hash(user_as_hash) }
end

#sessions(user_id) ⇒ Object

Raises:

  • (ArgumentError)


156
157
158
159
160
161
162
163
# File 'lib/keycloak-admin/client/user_client.rb', line 156

def sessions(user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?

  response = execute_http do
    RestClient::Resource.new("#{users_url(user_id)}/sessions", @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |session_as_hash| SessionRepresentation.from_hash(session_as_hash) }
end


202
203
204
205
206
# File 'lib/keycloak-admin/client/user_client.rb', line 202

def unlink_idp(user_id, idp_id)
  execute_http do
    RestClient::Resource.new(federated_identity_url(user_id, idp_id), @configuration.rest_client_options).delete(headers)
  end
end

#update(user_id, user_representation_body) ⇒ Object

pay attention that, since Keycloak 24.0.4, partial updates of attributes are not authorized anymore

Raises:

  • (ArgumentError)


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

def update(user_id, user_representation_body)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  RestClient::Request.execute(
    @configuration.rest_client_options.merge(
      method: :put,
      url: users_url(user_id),
      payload: create_payload(user_representation_body),
      headers: headers
    )
  )
end

#update_password(user_id, new_password) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/keycloak-admin/client/user_client.rb', line 112

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

#user_client_role_mappings_url(user_id, client_id) ⇒ Object



216
217
218
# File 'lib/keycloak-admin/client/user_client.rb', line 216

def user_client_role_mappings_url(user_id, client_id)
  "#{users_url(user_id)}/role-mappings/clients/#{client_id}"
end

#users_url(id = nil) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/keycloak-admin/client/user_client.rb', line 208

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