|
| 1 | +module KeycloakAdmin |
| 2 | + class OrganizationClient < Client |
| 3 | + def initialize(configuration, realm_client) |
| 4 | + super(configuration) |
| 5 | + raise ArgumentError.new("realm must be defined") unless realm_client.name_defined? |
| 6 | + @realm_client = realm_client |
| 7 | + end |
| 8 | + |
| 9 | + # This endpoint does not return members |
| 10 | + def list(brief_representation=true, exact=nil, first=nil, max=nil, query=nil, search=nil) |
| 11 | + response = execute_http do |
| 12 | + RestClient::Resource.new(organizations_url_with_parameters(brief_representation, exact, first, max, query, search), @configuration.rest_client_options).get(headers) |
| 13 | + end |
| 14 | + JSON.parse(response).map { |organization_as_hash| OrganizationRepresentation.from_hash(organization_as_hash) } |
| 15 | + end |
| 16 | + |
| 17 | + def count(exact=nil, query=nil, search=nil) |
| 18 | + response = execute_http do |
| 19 | + RestClient::Resource.new(count_url(exact, query, search), @configuration.rest_client_options).get(headers) |
| 20 | + end |
| 21 | + response.to_i |
| 22 | + end |
| 23 | + |
| 24 | + def delete(organization_id) |
| 25 | + execute_http do |
| 26 | + RestClient::Resource.new(organization_url(organization_id), @configuration.rest_client_options).delete(headers) |
| 27 | + end |
| 28 | + true |
| 29 | + end |
| 30 | + |
| 31 | + def update(organization_representation) |
| 32 | + execute_http do |
| 33 | + RestClient::Resource.new(organization_url(organization_representation.id), @configuration.rest_client_options).put( |
| 34 | + create_payload(organization_representation), headers |
| 35 | + ) |
| 36 | + end |
| 37 | + |
| 38 | + get(organization_representation.id) |
| 39 | + end |
| 40 | + |
| 41 | + def create!(name, alias_name, enabled, description, redirect_url=nil, domains=[], attributes={}) |
| 42 | + save(build(name, alias_name, enabled, description, redirect_url, domains, attributes)) |
| 43 | + end |
| 44 | + |
| 45 | + # This operation does not associate members and identity providers |
| 46 | + def save(organization_representation) |
| 47 | + execute_http do |
| 48 | + RestClient::Resource.new(organizations_url, @configuration.rest_client_options).post( |
| 49 | + create_payload(organization_representation), headers |
| 50 | + ) |
| 51 | + end |
| 52 | + true |
| 53 | + end |
| 54 | + |
| 55 | + def get(organization_id) |
| 56 | + response = execute_http do |
| 57 | + RestClient::Resource.new(organization_url(organization_id), @configuration.rest_client_options).get(headers) |
| 58 | + end |
| 59 | + OrganizationRepresentation.from_hash(JSON.parse(response)) |
| 60 | + end |
| 61 | + |
| 62 | + def identity_providers(organization_id) |
| 63 | + response = execute_http do |
| 64 | + RestClient::Resource.new(identity_providers_url(organization_id), @configuration.rest_client_options).get(headers) |
| 65 | + end |
| 66 | + JSON.parse(response).map { |idp_as_hash| IdentityProviderRepresentation.from_hash(idp_as_hash) } |
| 67 | + end |
| 68 | + |
| 69 | + def get_identity_provider(organization_id, identity_provider_alias) |
| 70 | + raise ArgumentError.new("identity_provider_alias must be defined") if identity_provider_alias.nil? |
| 71 | + response = execute_http do |
| 72 | + RestClient::Resource.new("#{identity_providers_url(organization_id)}/#{identity_provider_alias}", @configuration.rest_client_options).get(headers) |
| 73 | + end |
| 74 | + IdentityProviderRepresentation.from_hash(JSON.parse(response)) |
| 75 | + end |
| 76 | + |
| 77 | + def add_identity_provider(organization_id, identity_provider_alias) |
| 78 | + raise ArgumentError.new("identity_provider_alias must be defined") if identity_provider_alias.nil? |
| 79 | + execute_http do |
| 80 | + RestClient::Resource.new(identity_providers_url(organization_id), @configuration.rest_client_options).post(identity_provider_alias, headers) |
| 81 | + end |
| 82 | + true |
| 83 | + end |
| 84 | + |
| 85 | + def delete_identity_provider(organization_id, identity_provider_alias) |
| 86 | + execute_http do |
| 87 | + RestClient::Resource.new(identity_provider_url(organization_id, identity_provider_alias), @configuration.rest_client_options).delete(headers) |
| 88 | + end |
| 89 | + true |
| 90 | + end |
| 91 | + |
| 92 | + def members_count(organization_id) |
| 93 | + response = execute_http do |
| 94 | + RestClient::Resource.new(members_count_url(organization_id), @configuration.rest_client_options).get(headers) |
| 95 | + end |
| 96 | + response.to_i |
| 97 | + end |
| 98 | + |
| 99 | + def members(organization_id, exact=nil, first=nil, max=nil, membership_type=nil, search=nil) |
| 100 | + response = execute_http do |
| 101 | + RestClient::Resource.new(members_url_with_query_parameters(organization_id, exact, first, max, membership_type, search), @configuration.rest_client_options).get(headers) |
| 102 | + end |
| 103 | + JSON.parse(response).map { |member_as_hash| MemberRepresentation.from_hash(member_as_hash) } |
| 104 | + end |
| 105 | + |
| 106 | + def invite_existing_user(organization_id, user_id) |
| 107 | + raise ArgumentError.new("user_id must be defined") if user_id.nil? |
| 108 | + execute_http do |
| 109 | + RestClient::Resource.new(invite_existing_user_url(organization_id), @configuration.rest_client_options).post({id: user_id}, headers.merge(content_type: "application/x-www-form-urlencoded")) |
| 110 | + end |
| 111 | + true |
| 112 | + end |
| 113 | + |
| 114 | + def invite_user(organization_id, email, first_name, last_name) |
| 115 | + execute_http do |
| 116 | + RestClient::Resource.new(invite_user_url(organization_id), @configuration.rest_client_options).post({ |
| 117 | + email: email, |
| 118 | + firstName: first_name, |
| 119 | + lastName: last_name |
| 120 | + }, headers.merge(content_type: "application/x-www-form-urlencoded")) |
| 121 | + end |
| 122 | + true |
| 123 | + end |
| 124 | + |
| 125 | + def add_member(organization_id, user_id) |
| 126 | + raise ArgumentError.new("user_id must be defined") if user_id.nil? |
| 127 | + execute_http do |
| 128 | + RestClient::Resource.new(members_url(organization_id), @configuration.rest_client_options).post(user_id, headers) |
| 129 | + end |
| 130 | + true |
| 131 | + end |
| 132 | + |
| 133 | + def delete_member(organization_id, member_id) |
| 134 | + execute_http do |
| 135 | + RestClient::Resource.new(member_url(organization_id, member_id), @configuration.rest_client_options).delete(headers) |
| 136 | + end |
| 137 | + true |
| 138 | + end |
| 139 | + |
| 140 | + def get_member(organization_id, member_id) |
| 141 | + response = execute_http do |
| 142 | + RestClient::Resource.new(member_url(organization_id, member_id), @configuration.rest_client_options).get(headers) |
| 143 | + end |
| 144 | + MemberRepresentation.from_hash(JSON.parse(response)) |
| 145 | + end |
| 146 | + |
| 147 | + def associated_with_member(member_id, brief_representation=true) |
| 148 | + response = execute_http do |
| 149 | + RestClient::Resource.new(associated_with_member_url(member_id, brief_representation), @configuration.rest_client_options).get(headers) |
| 150 | + end |
| 151 | + JSON.parse(response).map { |organization_as_hash| OrganizationRepresentation.from_hash(organization_as_hash) } |
| 152 | + end |
| 153 | + |
| 154 | + def organizations_url |
| 155 | + "#{@realm_client.realm_admin_url}/organizations" |
| 156 | + end |
| 157 | + |
| 158 | + def organization_url(organization_id) |
| 159 | + raise ArgumentError.new("organization_id must be defined") if organization_id.nil? |
| 160 | + "#{organizations_url}/#{organization_id}" |
| 161 | + end |
| 162 | + |
| 163 | + def identity_providers_url(organization_id) |
| 164 | + "#{organization_url(organization_id)}/identity-providers" |
| 165 | + end |
| 166 | + |
| 167 | + def identity_provider_url(organization_id, identity_provider_alias) |
| 168 | + raise ArgumentError.new("identity_provider_alias must be defined") if identity_provider_alias.nil? |
| 169 | + "#{identity_providers_url(organization_id)}/#{identity_provider_alias}" |
| 170 | + end |
| 171 | + |
| 172 | + def count_url(exact, query, search) |
| 173 | + query_parameters = {exact: exact, q: query, search: search}.compact.to_a.map { |e| "#{e[0]}=#{e[1]}" }.join("&") |
| 174 | + "#{organizations_url}/count?#{query_parameters}" |
| 175 | + end |
| 176 | + |
| 177 | + def organizations_url_with_parameters(brief_representation, exact, first, max, query, search) |
| 178 | + query_parameters = { |
| 179 | + briefRepresentation: brief_representation, |
| 180 | + exact: exact, |
| 181 | + first: first, |
| 182 | + max: max, |
| 183 | + q: query, |
| 184 | + search: search |
| 185 | + }.compact.to_a.map { |e| "#{e[0]}=#{e[1]}" }.join("&") |
| 186 | + "#{organizations_url}?#{query_parameters}" |
| 187 | + end |
| 188 | + |
| 189 | + def associated_with_member_url(member_id, brief_representation=true) |
| 190 | + "#{organizations_url}/members/#{member_id}/organizations?briefRepresentation=#{brief_representation}" |
| 191 | + end |
| 192 | + |
| 193 | + def members_count_url(organization_id) |
| 194 | + "#{organization_url(organization_id)}/members/count" |
| 195 | + end |
| 196 | + |
| 197 | + def member_url(organization_id, member_id) |
| 198 | + raise ArgumentError.new("member_id must be defined") if member_id.nil? |
| 199 | + "#{organization_url(organization_id)}/members/#{member_id}" |
| 200 | + end |
| 201 | + |
| 202 | + def invite_existing_user_url(organization_id) |
| 203 | + "#{organization_url(organization_id)}/members/invite-existing-user" |
| 204 | + end |
| 205 | + |
| 206 | + def invite_user_url(organization_id) |
| 207 | + "#{organization_url(organization_id)}/members/invite-user" |
| 208 | + end |
| 209 | + |
| 210 | + def members_url(organization_id) |
| 211 | + "#{organization_url(organization_id)}/members" |
| 212 | + end |
| 213 | + |
| 214 | + def members_url_with_query_parameters(organization_id, exact, first, max, membership_type, search) |
| 215 | + query_parameters = { |
| 216 | + exact: exact, |
| 217 | + first: first, |
| 218 | + max: max, |
| 219 | + membershipType: membership_type, |
| 220 | + search: search |
| 221 | + }.compact.to_a.map { |e| "#{e[0]}=#{e[1]}" }.join("&") |
| 222 | + "#{organization_url(organization_id)}/members?#{query_parameters}" |
| 223 | + end |
| 224 | + |
| 225 | + def build(name, alias_name, enabled, description, redirect_url=nil, domains=[], attributes={}) |
| 226 | + unless domains.is_a?(Array) |
| 227 | + raise ArgumentError.new("domains must be an Array, got #{new_domains.class}") |
| 228 | + end |
| 229 | + |
| 230 | + unless domains.all? { |domain| domain.is_a?(KeycloakAdmin::OrganizationDomainRepresentation) } |
| 231 | + raise ArgumentError.new("All items in domains must be of type OrganizationDomainRepresentation") |
| 232 | + end |
| 233 | + |
| 234 | + organization = OrganizationRepresentation.new |
| 235 | + organization.name = name |
| 236 | + organization.alias = alias_name |
| 237 | + organization.enabled = enabled |
| 238 | + organization.description = description |
| 239 | + organization.redirect_url = redirect_url |
| 240 | + organization.domains = domains |
| 241 | + organization.attributes = attributes |
| 242 | + organization |
| 243 | + end |
| 244 | + end |
| 245 | +end |
0 commit comments