Module: Keycloak::Admin

Included in:
Internal
Defined in:
lib/keycloak.rb

Overview

Os recursos desse module (admin) serão utilizadas apenas por usuários que possuem as roles do client realm-management

Class Method Summary collapse

Class Method Details

.add_client_level_roles_to_user(id, client, role_representation, access_token = nil) ⇒ Object


496
497
498
# File 'lib/keycloak.rb', line 496

def self.add_client_level_roles_to_user(id, client, role_representation, access_token = nil)
  generic_post("users/#{id}/role-mappings/clients/#{client}", nil, role_representation, access_token)
end

.count_users(access_token = nil) ⇒ Object


438
439
440
# File 'lib/keycloak.rb', line 438

def self.count_users(access_token = nil)
  generic_get("users/count/", nil, access_token)
end

.create_user(user_representation, access_token = nil) ⇒ Object


434
435
436
# File 'lib/keycloak.rb', line 434

def self.create_user(user_representation, access_token = nil)
  generic_post("users/", nil, user_representation, access_token)
end

.delete_client_level_roles_from_user(id, client, role_representation, access_token = nil) ⇒ Object


500
501
502
# File 'lib/keycloak.rb', line 500

def self.delete_client_level_roles_from_user(id, client, role_representation, access_token = nil)
  generic_delete("users/#{id}/role-mappings/clients/#{client}", nil, role_representation, access_token)
end

.delete_user(id, access_token = nil) ⇒ Object


450
451
452
# File 'lib/keycloak.rb', line 450

def self.delete_user(id, access_token = nil)
  generic_delete("users/#{id}", nil, nil, access_token)
end

.generic_delete(service, query_parameters = nil, body_parameter = nil, access_token = nil) ⇒ Object


577
578
579
# File 'lib/keycloak.rb', line 577

def self.generic_delete(service, query_parameters = nil, body_parameter = nil, access_token = nil)
  Keycloak.generic_request(effective_access_token(access_token), full_url(service), query_parameters, body_parameter, 'DELETE')
end

.generic_get(service, query_parameters = nil, access_token = nil) ⇒ Object

Generics methods


565
566
567
# File 'lib/keycloak.rb', line 565

def self.generic_get(service, query_parameters = nil, access_token = nil)
  Keycloak.generic_request(effective_access_token(access_token), full_url(service), query_parameters, nil, 'GET')
end

.generic_post(service, query_parameters, body_parameter, access_token = nil) ⇒ Object


569
570
571
# File 'lib/keycloak.rb', line 569

def self.generic_post(service, query_parameters, body_parameter, access_token = nil)
  Keycloak.generic_request(effective_access_token(access_token), full_url(service), query_parameters, body_parameter, 'POST')
end

.generic_put(service, query_parameters, body_parameter, access_token = nil) ⇒ Object


573
574
575
# File 'lib/keycloak.rb', line 573

def self.generic_put(service, query_parameters, body_parameter, access_token = nil)
  Keycloak.generic_request(effective_access_token(access_token), full_url(service), query_parameters, body_parameter, 'PUT')
end

.get_all_roles_client(id, access_token = nil) ⇒ Object


484
485
486
# File 'lib/keycloak.rb', line 484

def self.get_all_roles_client(id, access_token = nil)
  generic_get("clients/#{id}/roles", nil, access_token)
end

.get_client_level_role_for_user_and_app(id, client, access_token = nil) ⇒ Object


504
505
506
# File 'lib/keycloak.rb', line 504

def self.get_client_level_role_for_user_and_app(id, client, access_token = nil)
  generic_get("users/#{id}/role-mappings/clients/#{client}", nil, access_token)
end

.get_clients(query_parameters = nil, access_token = nil) ⇒ Object


480
481
482
# File 'lib/keycloak.rb', line 480

def self.get_clients(query_parameters = nil, access_token = nil)
  generic_get("clients/", query_parameters, access_token)
end

.get_effective_client_level_role_composite_user(id, client, access_token = nil) ⇒ Object


559
560
561
# File 'lib/keycloak.rb', line 559

def self.get_effective_client_level_role_composite_user(id, client, access_token = nil)
  generic_get("users/#{id}/role-mappings/clients/#{client}/composite", nil, access_token)
end

.get_groups(query_parameters = nil, access_token = nil) ⇒ Object


468
469
470
# File 'lib/keycloak.rb', line 468

def self.get_groups(query_parameters = nil, access_token = nil)
  generic_get("groups/", query_parameters, access_token)
end

.get_groups_by_role_name(role_name, query_parameters = nil, access_token = nil) ⇒ Object


476
477
478
# File 'lib/keycloak.rb', line 476

def self.get_groups_by_role_name(role_name, query_parameters = nil, access_token = nil)
  generic_get("roles/#{role_name}/groups", query_parameters, access_token)
end

.get_role_mappings(id, access_token = nil) ⇒ Object


464
465
466
# File 'lib/keycloak.rb', line 464

def self.get_role_mappings(id, access_token = nil)
  generic_get("users/#{id}/role-mappings", nil, access_token)
end

.get_roles_client_by_name(id, role_name, access_token = nil) ⇒ Object


488
489
490
# File 'lib/keycloak.rb', line 488

def self.get_roles_client_by_name(id, role_name, access_token = nil)
  generic_get("clients/#{id}/roles/#{role_name}", nil, access_token)
end

.get_user(id, access_token = nil) ⇒ Object


442
443
444
# File 'lib/keycloak.rb', line 442

def self.get_user(id, access_token = nil)
  generic_get("users/#{id}", nil, access_token)
end

.get_users(query_parameters = nil, access_token = nil) ⇒ Object


426
427
428
# File 'lib/keycloak.rb', line 426

def self.get_users(query_parameters = nil, access_token = nil)
  generic_get("users/", query_parameters, access_token)
end

.get_users_by_group(id, query_parameters = nil, access_token = nil) ⇒ Object


430
431
432
# File 'lib/keycloak.rb', line 430

def self.get_users_by_group(id, query_parameters = nil, access_token = nil)
  generic_get("groups/#{id}/members", query_parameters, access_token)
end

.get_users_by_role_name(role_name, query_parameters = nil, access_token = nil) ⇒ Object


472
473
474
# File 'lib/keycloak.rb', line 472

def self.get_users_by_role_name(role_name, query_parameters = nil, access_token = nil)
  generic_get("roles/#{role_name}/users", query_parameters, access_token)
end

.get_users_client_by_role_name(id, role_name, access_token = nil) ⇒ Object


492
493
494
# File 'lib/keycloak.rb', line 492

def self.get_users_client_by_role_name(id, role_name, access_token = nil)
  generic_get("clients/#{id}/roles/#{role_name}/users", nil, access_token)
end

.reset_password(id, credential_representation, access_token = nil) ⇒ Object


555
556
557
# File 'lib/keycloak.rb', line 555

def self.reset_password(id, credential_representation, access_token = nil)
  generic_put("users/#{id}/reset-password", nil, credential_representation, access_token)
end

454
455
456
457
# File 'lib/keycloak.rb', line 454

def self.revoke_consent_user(id, client_id = nil, access_token = nil)
  client_id = Keycloak::Client.client_id if isempty?(client_id)
  generic_delete("users/#{id}/consents/#{client_id}", nil, nil, access_token)
end

.update_account_email(id, actions, redirect_uri = '', client_id = nil, access_token = nil) ⇒ Object


459
460
461
462
# File 'lib/keycloak.rb', line 459

def self.(id, actions, redirect_uri = '', client_id = nil, access_token = nil)
  client_id = Keycloak::Client.client_id if isempty?(client_id)
  generic_put("users/#{id}/execute-actions-email", { redirect_uri: redirect_uri, client_id: client_id }, actions, access_token)
end

.update_effective_user_roles(id, client_id, roles_names, access_token = nil) ⇒ Object


508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/keycloak.rb', line 508

def self.update_effective_user_roles(id, client_id, roles_names, access_token = nil)
  client = JSON get_clients({ clientId: client_id }, access_token)

  user_roles = JSON get_client_level_role_for_user_and_app(id, client[0]['id'], access_token)

  roles = Array.new
  # Include new role
  roles_names.each do |r|
    if r && !r.empty?
      found = false
      user_roles.each do |ur|
        found = ur['name'] == r
        break if found
        found = false
      end
      if !found
        role = JSON get_roles_client_by_name(client[0]['id'], r, access_token)
        roles.push(role)
      end
    end
  end

  garbage_roles = Array.new
  # Exclude old role
  user_roles.each do |ur|
    found = false
    roles_names.each do |r|
      if r && !r.empty?
        found = ur['name'] == r
        break if found
        found = false
      end
    end
    if !found
      garbage_roles.push(ur)
    end
  end

  if garbage_roles.count > 0
    delete_client_level_roles_from_user(id, client[0]['id'], garbage_roles, access_token)
  end

  if roles.count > 0
    add_client_level_roles_to_user(id, client[0]['id'], roles, access_token)
  end
end

.update_user(id, user_representation, access_token = nil) ⇒ Object


446
447
448
# File 'lib/keycloak.rb', line 446

def self.update_user(id, user_representation, access_token = nil)
  generic_put("users/#{id}", nil, user_representation, access_token)
end