Class: KeycloakAdmin::ClientAuthzPermissionClient

Inherits:
Client
  • Object
show all
Defined in:
lib/keycloak-admin/client/client_authz_permission_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, client_id, type, resource_id = nil) ⇒ ClientAuthzPermissionClient

Returns a new instance of ClientAuthzPermissionClient.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 3

def initialize(configuration, realm_client, client_id, type, resource_id = nil)
  super(configuration)
  raise ArgumentError.new("realm must be defined") unless realm_client.name_defined?
  raise ArgumentError.new("bad permission type") if !resource_id && !%i[resource scope].include?(type.to_sym)

  @realm_client = realm_client
  @client_id = client_id
  @type = type
  @resource_id = resource_id
end

Instance Method Details

#authz_permission_url(client_id, resource_id = nil, type = nil, id = nil) ⇒ Object



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

def authz_permission_url(client_id, resource_id = nil, type = nil, id = nil)
  if resource_id
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/resource/#{resource_id}/permissions"
  elsif id
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/permission/#{type}/#{id}"
  else
    "#{@realm_client.realm_admin_url}/clients/#{client_id}/authz/resource-server/permission/#{type}"
  end
end

#build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 66

def build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type)
  policy                   = ClientAuthzPermissionRepresentation.new
  policy.name              = name
  policy.description       = description
  policy.type              = @type
  policy.decision_strategy = decision_strategy
  policy.resource_type     = resource_type
  policy.resources         = resources
  policy.policies          = policies
  policy.scopes            = scopes
  policy.logic             = logic
  policy
end

#create!(name, description, decision_strategy, logic = "POSITIVE", resources = [], policies = [], scopes = [], resource_type = nil) ⇒ Object



29
30
31
32
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 29

def create!(name, description, decision_strategy,logic = "POSITIVE", resources = [], policies = [], scopes = [], resource_type = nil)
  response = save(build(name, description, decision_strategy, logic, resources, policies, scopes, resource_type))
  ClientAuthzPermissionRepresentation.from_hash(JSON.parse(response))
end

#delete(permission_id) ⇒ Object



14
15
16
17
18
19
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 14

def delete(permission_id)
  execute_http do
    RestClient::Resource.new(authz_permission_url(@client_id, nil, nil, permission_id), @configuration.rest_client_options).delete(headers)
  end
  true
end

#find_by(name, resource, scope = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 21

def find_by(name, resource, scope = nil)
  response = execute_http do
    url = "#{authz_permission_url(@client_id)}?name=#{name}&resource=#{resource}&type=#{@type}&scope=#{scope}&deep=true&first=0&max=100"
    RestClient::Resource.new(url, @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| ClientAuthzPermissionRepresentation.from_hash(role_as_hash) }
end

#get(permission_id) ⇒ Object



49
50
51
52
53
54
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 49

def get(permission_id)
  response = execute_http do
    RestClient::Resource.new(authz_permission_url(@client_id, nil, @type, permission_id), @configuration.rest_client_options).get(headers)
  end
  ClientAuthzPermissionRepresentation.from_hash(JSON.parse(response))
end

#listObject



42
43
44
45
46
47
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 42

def list
  response = execute_http do
    RestClient::Resource.new(authz_permission_url(@client_id, @resource_id), @configuration.rest_client_options).get(headers)
  end
  JSON.parse(response).map { |role_as_hash| ClientAuthzPermissionRepresentation.from_hash(role_as_hash) }
end

#save(permission_representation) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/keycloak-admin/client/client_authz_permission_client.rb', line 34

def save(permission_representation)
  execute_http do
    RestClient::Resource.new(authz_permission_url(@client_id, nil, permission_representation.type), @configuration.rest_client_options).post(
      create_payload(permission_representation), headers
    )
  end
end