Class: CF::UAA::StubScim

Inherits:
Object
  • Object
show all
Defined in:
lib/uaa/stub/scim.rb

Overview

StubScim is the in-memory database of the stubbed out UAA server. Although called StubScim it manages ALL of the objects of the server; users, groups, clients, zones, providers, etc

Instance Method Summary collapse

Constructor Details

#initializeStubScim

Returns a new instance of StubScim.



197
198
199
# File 'lib/uaa/stub/scim.rb', line 197

def initialize
  @things_by_id, @things_by_name, @clients_metadata = {}, {}, {}
end

Instance Method Details

#add(rtype, stuff) ⇒ Object

Raises:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/uaa/stub/scim.rb', line 204

def add(rtype, stuff)
  unless stuff.is_a?(Hash) && (name = stuff[NAME_ATTR[rtype].to_s])
    raise SchemaViolation, "new #{rtype} has no name #{NAME_ATTR[rtype]}"
  end
  raise AlreadyExists if @things_by_name.key?(name = rtype.to_s + name.downcase)
  enforce_schema(rtype, stuff)
  thing = input(stuff).merge!(rtype: rtype, id: (id = SecureRandom.uuid),
      meta: { created: Time.now.iso8601, last_modified: Time.now.iso8601, version: 1})
  if rtype == :client
    @clients_metadata[stuff['client_id']] = {:createdby => CREATOR}
  end
  add_user_groups(id, thing[:members])
  @things_by_id[id] = @things_by_name[name] = thing
  id
end

#add_group_mapping(external_group, group_id, group_name, origin) ⇒ Object



328
329
330
331
332
333
334
# File 'lib/uaa/stub/scim.rb', line 328

def add_group_mapping(external_group, group_id, group_name, origin)
  group = group_id ? ref_by_id(group_id, :group) : ref_by_name(group_name, :group)
  return unless group
  (group[:external_groups] ||= Hash.new)
  group[:external_groups][external_group] = origin
  group
end

#add_member(gid, member) ⇒ Object



276
277
278
279
280
# File 'lib/uaa/stub/scim.rb', line 276

def add_member(gid, member)
  return unless g = ref_by_id(gid, :group)
  (g[:members] ||= Set.new) << member
  add_user_groups(gid, Set[member])
end

#delete(id, rtype = nil) ⇒ Object



292
293
294
295
296
297
298
299
300
# File 'lib/uaa/stub/scim.rb', line 292

def delete(id, rtype = nil)
  return unless thing = ref_by_id(id, rtype)
  rtype = thing[:rtype]
  delete_user_groups(id, thing[:members])
  @things_by_id.delete(id)
  thing = @things_by_name.delete(rtype.to_s + thing[NAME_ATTR[rtype]].downcase)
  delete_references(id)
  remove_attrs(output(thing))
end

#delete_group_mapping(group_id, external_group, origin) ⇒ Object

Raises:

  • (NotFound)


336
337
338
339
340
# File 'lib/uaa/stub/scim.rb', line 336

def delete_group_mapping(group_id, external_group, origin)
  raise NotFound unless group = ref_by_id(group_id, :group)
  raise NotFound unless group[:external_groups] && group[:external_groups].include?(external_group)
  group[:external_groups][external_group].delete(origin)
end

#find(rtype, opts = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/uaa/stub/scim.rb', line 316

def find(rtype, opts = {})
  filter, total, start = ScimFilter.new(opts[:filter]), 0, (opts[:start] || 0)
  count, attrs, acl, acl_id = opts[:count], opts[:attrs], opts[:acl], opts[:acl_id]
  objs = @things_by_id.each_with_object([]) { |(k, v), o|
    next unless rtype == v[:rtype] && filter.match?(v)
    next if acl && acl_id && !is_member(v[:id], acl_id, acl)
    o << output(v, attrs) if total >= start && (count.nil? || o.length < count)
    total += 1
  }
  [objs, total]
end

#get(id, rtype = nil, *attrs) ⇒ Object



302
303
304
305
# File 'lib/uaa/stub/scim.rb', line 302

def get(id, rtype = nil, *attrs)
  return unless thing = ref_by_id(id, rtype)
  output(thing, attrs)
end

#get_by_name(name, rtype, *attrs) ⇒ Object



311
312
313
314
# File 'lib/uaa/stub/scim.rb', line 311

def get_by_name(name, rtype, *attrs)
  return unless thing = ref_by_name(name, rtype)
  output(thing, attrs)
end

#get_client_meta(client_id) ⇒ Object



307
308
309
# File 'lib/uaa/stub/scim.rb', line 307

def get_client_meta(client_id)
  @clients_metadata[client_id]
end

#get_group_mappingsObject



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/uaa/stub/scim.rb', line 342

def get_group_mappings
  group_mappings = []
  @things_by_id.each do |id, thing|
    if thing[:rtype] == :group
      thing[:external_groups].each do |key, value|
        group_mappings << { groupid: thing[:id], displayname: thing[:displayname], externalgroup: key, origin: value }
      end if thing[:external_groups]
    end
  end unless @things_by_id.empty?
  group_mappings
end

#id(name, rtype) ⇒ Object



202
# File 'lib/uaa/stub/scim.rb', line 202

def id(name, rtype) (t = ref_by_name(name, rtype))? t[:id] : nil end

#is_member(gid, member, attr = :members) ⇒ Object



282
283
284
# File 'lib/uaa/stub/scim.rb', line 282

def is_member(gid, member, attr = :members)
  (g = ref_by_id(gid, :group)) && (a = g[attr]) && a.include?(member)
end

#name(id, rtype = nil) ⇒ Object



201
# File 'lib/uaa/stub/scim.rb', line 201

def name(id, rtype = nil) (t = ref_by_id(id, rtype))? t[NAME_ATTR[t[:rtype]]]: nil end

#patch(id, stuff, match_version = nil, match_type = nil) ⇒ Object

Raises:

  • (NotFound)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/uaa/stub/scim.rb', line 247

def patch(id, stuff, match_version = nil, match_type = nil)
  raise NotFound unless thing = ref_by_id(id, match_type)
  raise BadVersion if match_version && match_version != thing[:meta][:version]
  enforce_schema(rtype = thing[:rtype], remove_attrs(stuff, READ_ONLY_ATTRS))
  new_thing = input(stuff)
  if newname = new_thing[NAME_ATTR[rtype]]
    oldname = rtype.to_s + thing[NAME_ATTR[rtype]].downcase
    unless (newname = rtype.to_s + newname.downcase) == oldname
      raise AlreadyExists if @things_by_name.key?(newname)
      @things_by_name.delete(oldname)
      @things_by_name[newname] = thing
    end
  end
  if new_thing[:members] || thing[:members]
    old_members = thing[:members] || Set.new
    new_members = new_thing[:members] || Set.new
    delete_user_groups(id, old_members - new_members)
    add_user_groups(id, new_members - old_members)
  end
  READ_ONLY_ATTRS.each { |a| new_thing[a] = thing[a] if thing[a] }
  HIDDEN_ATTRS.each { |a| new_thing[a] = thing[a] if thing[a] }
  new_thing.each do |key, value|
    thing[key] = value
  end
  thing[:meta][:version] += 1
  thing[:meta][:lastmodified] == Time.now.iso8601
  id
end

#set_hidden_attr(id, attr, value) ⇒ Object

Raises:

  • (NotFound)


286
287
288
289
290
# File 'lib/uaa/stub/scim.rb', line 286

def set_hidden_attr(id, attr, value)
  raise NotFound unless thing = ref_by_id(id)
  raise ArgumentError unless HIDDEN_ATTRS.include?(attr)
  thing[attr] = value
end

#update(id, stuff, match_version = nil, match_type = nil) ⇒ Object

Raises:

  • (NotFound)


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/uaa/stub/scim.rb', line 220

def update(id, stuff, match_version = nil, match_type = nil)
  raise NotFound unless thing = ref_by_id(id, match_type)
  raise BadVersion if match_version && match_version != thing[:meta][:version]
  enforce_schema(rtype = thing[:rtype], remove_attrs(stuff, READ_ONLY_ATTRS))
  new_thing = input(stuff)
  if newname = new_thing[NAME_ATTR[rtype]]
    oldname = rtype.to_s + thing[NAME_ATTR[rtype]].downcase
    unless (newname = rtype.to_s + newname.downcase) == oldname
      raise AlreadyExists if @things_by_name.key?(newname)
      @things_by_name.delete(oldname)
      @things_by_name[newname] = thing
    end
  end
  if new_thing[:members] || thing[:members]
    old_members = thing[:members] || Set.new
    new_members = new_thing[:members] || Set.new
    delete_user_groups(id, old_members - new_members)
    add_user_groups(id, new_members - old_members)
  end
  READ_ONLY_ATTRS.each { |a| new_thing[a] = thing[a] if thing[a] }
  HIDDEN_ATTRS.each { |a| new_thing[a] = thing[a] if thing[a] }
  thing.replace new_thing
  thing[:meta][:version] += 1
  thing[:meta][:lastmodified] == Time.now.iso8601
  id
end