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:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/uaa/stub/scim.rb', line 207

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
  name = append_origin_to_username(name, rtype, stuff['origin'])
  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



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

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



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

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

#append_origin_to_username(name, rtype, origin) ⇒ Object



324
325
326
327
328
329
330
# File 'lib/uaa/stub/scim.rb', line 324

def append_origin_to_username(name, rtype, origin)
  if rtype == :user
    origin =  origin || 'uaa'
    name = "#{name}_#{origin}"
  end
  name
end

#delete(id, rtype = nil) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/uaa/stub/scim.rb', line 297

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

#delete_group_mapping(group_id, external_group, origin) ⇒ Object

Raises:

  • (NotFound)


352
353
354
355
356
# File 'lib/uaa/stub/scim.rb', line 352

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



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/uaa/stub/scim.rb', line 332

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



309
310
311
312
# File 'lib/uaa/stub/scim.rb', line 309

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



318
319
320
321
322
# File 'lib/uaa/stub/scim.rb', line 318

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

#get_client_meta(client_id) ⇒ Object



314
315
316
# File 'lib/uaa/stub/scim.rb', line 314

def get_client_meta(client_id)
  @clients_metadata[client_id]
end

#get_group_mappingsObject



358
359
360
361
362
363
364
365
366
367
368
# File 'lib/uaa/stub/scim.rb', line 358

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
203
204
205
# File 'lib/uaa/stub/scim.rb', line 202

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

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



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

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)


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/uaa/stub/scim.rb', line 252

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)


291
292
293
294
295
# File 'lib/uaa/stub/scim.rb', line 291

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)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/uaa/stub/scim.rb', line 224

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
      name = append_origin_to_username(newname, rtype, stuff['origin'])
      raise AlreadyExists if @things_by_name.key?(name)
      @things_by_name.delete(oldname)
      @things_by_name[name] = 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