lib/ddr/auth/roles/role_set.rb in ddr-models-1.13.1 vs lib/ddr/auth/roles/role_set.rb in ddr-models-1.13.2

- old
+ new

@@ -6,18 +6,29 @@ # # Wraps a set of Roles (ActiveTriples::Term) # class RoleSet < SimpleDelegator + def self.deserialize(serialized, fmt = :ruby) + if fmt == :json + deserialize JSON.parse(serialized) + else # :ruby + role_set = serialized.map do |role_data| + Role.build(role_data.with_indifferent_access) + end + new(role_set) + end + end + # Grants roles - i.e., adds them to the role set # Note that we reject roles that are included because # ActiveTriples::Term#<< does not support isomorphism. # https://github.com/ActiveTriples/ActiveTriples/issues/42 - # @example - default scope (:resource) - # grant type: :curator, person: "bob" + # @example - default scope ("resource") + # grant type: "Curator", agent: "bob" # @example - explicit scope - # grant type: :curator, person: "sue", scope: :policy + # grant type: "Curator", agent: "sue", scope: "policy" # @param roles [Array<Ddr::Auth::Roles::Role, Hash>] the roles to grant def grant(*roles) self << coerce(roles).reject { |r| include?(r) } end @@ -46,23 +57,36 @@ # Replace the current roles in the role set with new roles # @param roles [Array<Ddr::Auth::Roles::Role, Hash>] the roles to grant def replace(*roles) revoke_all - # XXX Not sure why we have to use __getobj__ here - __getobj__.set coerce(roles) + grant(*roles) end # Remove all roles from the role set def revoke_all - delete(*__getobj__) + each(&:destroy) + self end def to_a map.to_a end + def to_json + serialize(:json) + end + + def serialize(fmt = :ruby) + case fmt + when :json + serialize(:ruby).to_json + else # :ruby + to_a.map(&:to_h) + end + end + def where(criteria) query.where(criteria) end private @@ -76,10 +100,10 @@ when Array role.map { |r| coerce(r) } when Role role else - Ddr::Auth::Roles.build_role(role) + Role.build(role) end end end end