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



526
527
528
# File 'lib/keycloak.rb', line 526

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



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

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

.create_user(user_representation, access_token = nil) ⇒ Object



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

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



530
531
532
# File 'lib/keycloak.rb', line 530

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



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

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



607
608
609
# File 'lib/keycloak.rb', line 607

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



595
596
597
# File 'lib/keycloak.rb', line 595

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



599
600
601
# File 'lib/keycloak.rb', line 599

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



603
604
605
# File 'lib/keycloak.rb', line 603

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



514
515
516
# File 'lib/keycloak.rb', line 514

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



534
535
536
# File 'lib/keycloak.rb', line 534

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



510
511
512
# File 'lib/keycloak.rb', line 510

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



589
590
591
# File 'lib/keycloak.rb', line 589

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



498
499
500
# File 'lib/keycloak.rb', line 498

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



506
507
508
# File 'lib/keycloak.rb', line 506

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



494
495
496
# File 'lib/keycloak.rb', line 494

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



518
519
520
# File 'lib/keycloak.rb', line 518

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



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

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



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

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



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

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



502
503
504
# File 'lib/keycloak.rb', line 502

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



522
523
524
# File 'lib/keycloak.rb', line 522

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



585
586
587
# File 'lib/keycloak.rb', line 585

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


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

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



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

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



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/keycloak.rb', line 538

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



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

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