Class: KeycloakAdmin::OrganizationClient

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

Returns a new instance of OrganizationClient.

Raises:

  • (ArgumentError)


4
5
6
7
8
# File 'lib/keycloak-admin/client/organization_client.rb', line 4

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_identity_provider(organization_id, identity_provider_alias) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
# File 'lib/keycloak-admin/client/organization_client.rb', line 78

def add_identity_provider(organization_id, identity_provider_alias)
  raise ArgumentError.new("identity_provider_alias must be defined") if identity_provider_alias.nil?
  execute_http do
    resource(identity_providers_url(organization_id)).post(identity_provider_alias, headers)
  end
  true
end

#add_member(organization_id, user_id) ⇒ Object

Raises:

  • (ArgumentError)


126
127
128
129
130
131
132
# File 'lib/keycloak-admin/client/organization_client.rb', line 126

def add_member(organization_id, user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  execute_http do
    resource(members_url(organization_id)).post(user_id, headers)
  end
  true
end

#associated_with_member(member_id, brief_representation = true) ⇒ Object



148
149
150
151
152
153
# File 'lib/keycloak-admin/client/organization_client.rb', line 148

def associated_with_member(member_id, brief_representation=true)
  response = execute_http do
    resource(associated_with_member_url(member_id, brief_representation)).get(headers)
  end
  JSON.parse(response).map { |organization_as_hash| OrganizationRepresentation.from_hash(organization_as_hash) }
end

#associated_with_member_url(member_id, brief_representation = true) ⇒ Object



190
191
192
# File 'lib/keycloak-admin/client/organization_client.rb', line 190

def associated_with_member_url(member_id, brief_representation=true)
  "#{organizations_url}/members/#{encode_segment(member_id)}/organizations?#{build_query(briefRepresentation: brief_representation)}"
end

#build(name, alias_name, enabled, description, redirect_url = nil, domains = [], attributes = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/keycloak-admin/client/organization_client.rb', line 226

def build(name, alias_name, enabled, description, redirect_url=nil, domains=[], attributes={})
  unless domains.is_a?(Array)
    raise ArgumentError.new("domains must be an Array, got #{domains.class}")
  end

  unless domains.all? { |domain| domain.is_a?(KeycloakAdmin::OrganizationDomainRepresentation) }
    raise ArgumentError.new("All items in domains must be of type OrganizationDomainRepresentation")
  end

  organization              = OrganizationRepresentation.new
  organization.name         = name
  organization.alias        = alias_name
  organization.enabled      = enabled
  organization.description  = description
  organization.redirect_url = redirect_url
  organization.domains      = domains
  organization.attributes   = attributes
  organization
end

#count(exact = nil, query = nil, search = nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/keycloak-admin/client/organization_client.rb', line 18

def count(exact=nil, query=nil, search=nil)
  response = execute_http do
    resource(count_url(exact, query, search)).get(headers)
  end
  response.to_i
end

#count_url(exact, query, search) ⇒ Object



173
174
175
176
# File 'lib/keycloak-admin/client/organization_client.rb', line 173

def count_url(exact, query, search)
  query_parameters = build_query({exact: exact, q: query, search: search}.compact)
  "#{organizations_url}/count?#{query_parameters}"
end

#create!(name, alias_name, enabled, description, redirect_url = nil, domains = [], attributes = {}) ⇒ Object



42
43
44
# File 'lib/keycloak-admin/client/organization_client.rb', line 42

def create!(name, alias_name, enabled, description, redirect_url=nil, domains=[], attributes={})
  save(build(name, alias_name, enabled, description, redirect_url, domains, attributes))
end

#delete(organization_id) ⇒ Object



25
26
27
28
29
30
# File 'lib/keycloak-admin/client/organization_client.rb', line 25

def delete(organization_id)
  execute_http do
    resource(organization_url(organization_id)).delete(headers)
  end
  true
end

#delete_identity_provider(organization_id, identity_provider_alias) ⇒ Object



86
87
88
89
90
91
# File 'lib/keycloak-admin/client/organization_client.rb', line 86

def delete_identity_provider(organization_id, identity_provider_alias)
  execute_http do
    resource(identity_provider_url(organization_id, identity_provider_alias)).delete(headers)
  end
  true
end

#delete_member(organization_id, member_id) ⇒ Object



134
135
136
137
138
139
# File 'lib/keycloak-admin/client/organization_client.rb', line 134

def delete_member(organization_id, member_id)
  execute_http do
    resource(member_url(organization_id, member_id)).delete(headers)
  end
  true
end

#get(organization_id) ⇒ Object



56
57
58
59
60
61
# File 'lib/keycloak-admin/client/organization_client.rb', line 56

def get(organization_id)
  response = execute_http do
    resource(organization_url(organization_id)).get(headers)
  end
  OrganizationRepresentation.from_hash(JSON.parse(response))
end

#get_identity_provider(organization_id, identity_provider_alias) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
# File 'lib/keycloak-admin/client/organization_client.rb', line 70

def get_identity_provider(organization_id, identity_provider_alias)
  raise ArgumentError.new("identity_provider_alias must be defined") if identity_provider_alias.nil?
  response = execute_http do
    resource(identity_provider_url(organization_id, identity_provider_alias)).get(headers)
  end
  IdentityProviderRepresentation.from_hash(JSON.parse(response))
end

#get_member(organization_id, member_id) ⇒ Object



141
142
143
144
145
146
# File 'lib/keycloak-admin/client/organization_client.rb', line 141

def get_member(organization_id, member_id)
  response = execute_http do
    resource(member_url(organization_id, member_id)).get(headers)
  end
  MemberRepresentation.from_hash(JSON.parse(response))
end

#identity_provider_url(organization_id, identity_provider_alias) ⇒ Object

Raises:

  • (ArgumentError)


168
169
170
171
# File 'lib/keycloak-admin/client/organization_client.rb', line 168

def identity_provider_url(organization_id, identity_provider_alias)
  raise ArgumentError.new("identity_provider_alias must be defined") if identity_provider_alias.nil?
  "#{identity_providers_url(organization_id)}/#{encode_segment(identity_provider_alias)}"
end

#identity_providers(organization_id) ⇒ Object



63
64
65
66
67
68
# File 'lib/keycloak-admin/client/organization_client.rb', line 63

def identity_providers(organization_id)
  response = execute_http do
    resource(identity_providers_url(organization_id)).get(headers)
  end
  JSON.parse(response).map { |idp_as_hash| IdentityProviderRepresentation.from_hash(idp_as_hash) }
end

#identity_providers_url(organization_id) ⇒ Object



164
165
166
# File 'lib/keycloak-admin/client/organization_client.rb', line 164

def identity_providers_url(organization_id)
  "#{organization_url(organization_id)}/identity-providers"
end

#invite_existing_user(organization_id, user_id) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
# File 'lib/keycloak-admin/client/organization_client.rb', line 107

def invite_existing_user(organization_id, user_id)
  raise ArgumentError.new("user_id must be defined") if user_id.nil?
  execute_http do
    resource(invite_existing_user_url(organization_id)).post({id: user_id}, headers.merge(content_type: "application/x-www-form-urlencoded"))
  end
  true
end

#invite_existing_user_url(organization_id) ⇒ Object



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

def invite_existing_user_url(organization_id)
  "#{organization_url(organization_id)}/members/invite-existing-user"
end

#invite_user(organization_id, email, first_name, last_name) ⇒ Object



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

def invite_user(organization_id, email, first_name, last_name)
  execute_http do
    resource(invite_user_url(organization_id)).post({
      email: email,
      firstName: first_name,
      lastName: last_name
    }, headers.merge(content_type: "application/x-www-form-urlencoded"))
  end
  true
end

#invite_user_url(organization_id) ⇒ Object



207
208
209
# File 'lib/keycloak-admin/client/organization_client.rb', line 207

def invite_user_url(organization_id)
  "#{organization_url(organization_id)}/members/invite-user"
end

#list(brief_representation = true, exact = nil, first = nil, max = nil, query = nil, search = nil) ⇒ Object

This endpoint does not return members



11
12
13
14
15
16
# File 'lib/keycloak-admin/client/organization_client.rb', line 11

def list(brief_representation=true, exact=nil, first=nil, max=nil, query=nil, search=nil)
  response = execute_http do
    resource(organizations_url_with_parameters(brief_representation, exact, first, max, query, search)).get(headers)
  end
  JSON.parse(response).map { |organization_as_hash| OrganizationRepresentation.from_hash(organization_as_hash) }
end

#member_url(organization_id, member_id) ⇒ Object

Raises:

  • (ArgumentError)


198
199
200
201
# File 'lib/keycloak-admin/client/organization_client.rb', line 198

def member_url(organization_id, member_id)
  raise ArgumentError.new("member_id must be defined") if member_id.nil?
  "#{organization_url(organization_id)}/members/#{encode_segment(member_id)}"
end

#members(organization_id, exact = nil, first = nil, max = nil, membership_type = nil, search = nil) ⇒ Object



100
101
102
103
104
105
# File 'lib/keycloak-admin/client/organization_client.rb', line 100

def members(organization_id, exact=nil, first=nil, max=nil, membership_type=nil, search=nil)
  response = execute_http do
    resource(members_url_with_query_parameters(organization_id, exact, first, max, membership_type, search)).get(headers)
  end
  JSON.parse(response).map { |member_as_hash| MemberRepresentation.from_hash(member_as_hash) }
end

#members_count(organization_id) ⇒ Object



93
94
95
96
97
98
# File 'lib/keycloak-admin/client/organization_client.rb', line 93

def members_count(organization_id)
  response = execute_http do
    resource(members_count_url(organization_id)).get(headers)
  end
  response.to_i
end

#members_count_url(organization_id) ⇒ Object



194
195
196
# File 'lib/keycloak-admin/client/organization_client.rb', line 194

def members_count_url(organization_id)
  "#{organization_url(organization_id)}/members/count"
end

#members_url(organization_id) ⇒ Object



211
212
213
# File 'lib/keycloak-admin/client/organization_client.rb', line 211

def members_url(organization_id)
  "#{organization_url(organization_id)}/members"
end

#members_url_with_query_parameters(organization_id, exact, first, max, membership_type, search) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/keycloak-admin/client/organization_client.rb', line 215

def members_url_with_query_parameters(organization_id, exact, first, max, membership_type, search)
  query_parameters = build_query({
    exact: exact,
    first: first,
    max: max,
    membershipType: membership_type,
    search: search
  }.compact)
  "#{organization_url(organization_id)}/members?#{query_parameters}"
end

#organization_url(organization_id) ⇒ Object

Raises:

  • (ArgumentError)


159
160
161
162
# File 'lib/keycloak-admin/client/organization_client.rb', line 159

def organization_url(organization_id)
  raise ArgumentError.new("organization_id must be defined") if organization_id.nil?
  "#{organizations_url}/#{encode_segment(organization_id)}"
end

#organizations_urlObject



155
156
157
# File 'lib/keycloak-admin/client/organization_client.rb', line 155

def organizations_url
  "#{@realm_client.realm_admin_url}/organizations"
end

#organizations_url_with_parameters(brief_representation, exact, first, max, query, search) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/keycloak-admin/client/organization_client.rb', line 178

def organizations_url_with_parameters(brief_representation, exact, first, max, query, search)
  query_parameters = build_query({
    briefRepresentation: brief_representation,
    exact: exact,
    first: first,
    max: max,
    q: query,
    search: search
  }.compact)
  "#{organizations_url}?#{query_parameters}"
end

#save(organization_representation) ⇒ Object

This operation does not associate members and identity providers



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

def save(organization_representation)
  execute_http do
    resource(organizations_url).post(
      create_payload(organization_representation), headers
    )
  end
  true
end

#update(organization_representation) ⇒ Object



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

def update(organization_representation)
  execute_http do
    resource(organization_url(organization_representation.id)).put(
      create_payload(organization_representation), headers
    )
  end

  get(organization_representation.id)
end