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.



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

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

Instance Method Details

#add(rtype, stuff) ⇒ Object

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/stub/scim.rb', line 182

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



222
223
224
225
226
# File 'lib/stub/scim.rb', line 222

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

#find(rtype, start = 0, count = nil, filter_string = nil, attrs = nil) ⇒ Object



253
254
255
256
257
258
259
260
261
# File 'lib/stub/scim.rb', line 253

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

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



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

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



248
249
250
251
# File 'lib/stub/scim.rb', line 248

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

#id(name, rtype) ⇒ Object



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

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

#name(id, rtype = nil) ⇒ Object



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

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

#remove(id, rtype = nil) ⇒ Object



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

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

#set_hidden_attr(id, attr, value) ⇒ Object

Raises:

  • (NotFound)


228
229
230
231
232
# File 'lib/stub/scim.rb', line 228

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)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/stub/scim.rb', line 195

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
    remove_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