Class: CF::UAA::StubScim

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

Instance Method Summary collapse

Constructor Details

#initializeStubScim

Returns a new instance of StubScim.



190
# File 'lib/stub/scim.rb', line 190

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

Instance Method Details

#add(rtype, stuff) ⇒ Object

Raises:



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/stub/scim.rb', line 194

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 })
  add_user_groups(id, thing[:members])
  @things_by_id[id] = @things_by_name[name] = thing
  id
end

#add_member(gid, member) ⇒ Object



234
235
236
237
238
# File 'lib/stub/scim.rb', line 234

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



250
251
252
253
254
255
256
257
258
# File 'lib/stub/scim.rb', line 250

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

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



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/stub/scim.rb', line 270

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



260
261
262
263
# File 'lib/stub/scim.rb', line 260

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



265
266
267
268
# File 'lib/stub/scim.rb', line 265

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

#id(name, rtype) ⇒ Object



192
# File 'lib/stub/scim.rb', line 192

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

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



240
241
242
# File 'lib/stub/scim.rb', line 240

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



191
# File 'lib/stub/scim.rb', line 191

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

#set_hidden_attr(id, attr, value) ⇒ Object

Raises:

  • (NotFound)


244
245
246
247
248
# File 'lib/stub/scim.rb', line 244

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)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/stub/scim.rb', line 207

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