Skip to content

Commit

Permalink
Merge pull request #397 from projecthydra/clear_cache
Browse files Browse the repository at this point in the history
Clear cache when removing permissions
  • Loading branch information
jcoyne authored Jan 24, 2017
2 parents 88ce319 + 06a1bfc commit e2b5edb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,10 @@ def search_by_type_and_mode(type, mode)

# @param [RDF::URI] mode One of the permissions modes, e.g. ACL.Write, ACL.Read, etc.
# @yieldparam [Array<ActiveFedora::Base>] agent the agent type assertions
# @return [Array<Permission>] list of permissions where the mode is as selected, the block evaluates to true and the target is not marked for delete
# @return [Array<Permission>] list of permissions where the mode is as selected, the block evaluates to true
def search_by_mode(mode)
permissions.to_a.select do |p|
yield(p.agent) && !p.marked_for_destruction? && p.mode.first.rdf_subject == mode
yield(p.agent) && p.mode.first.rdf_subject == mode
end
end

Expand Down
14 changes: 13 additions & 1 deletion hydra-access-controls/app/models/hydra/access_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,42 @@ def permissions=(records)

def permissions_attributes=(attribute_list)
raise ArgumentError unless attribute_list.is_a? Array
any_destroyed = false
attribute_list.each do |attributes|
if attributes.key?(:id)
obj = relationship.find(attributes[:id])
if has_destroy_flag?(attributes)
obj.destroy
any_destroyed = true
else
obj.update(attributes.except(:id, '_destroy'))
end
else
relationship.create(attributes)
end
end
# Poison the cache
relationship.reset if any_destroyed
end

def relationship
@relationship ||= CollectionRelationship.new(self, :contains)
end

# This is like a has_many :through relationship
class CollectionRelationship
def initialize(owner, reflection)
@owner = owner
@relationship = @owner.send(reflection)
end

# The graph stored in @owner is now stale, so reload it and clear all caches
def reset
@owner.reload
@relationship.proxy_association.reload
self
end

delegate :to_a, :to_ary, :map, :delete, :first, :last, :size, :count, :[],
:==, :detect, :empty?, :each, :any?, :all?, :include?, :destroy_all,
to: :@relationship
Expand All @@ -52,7 +64,7 @@ def initialize(owner, reflection)
# delegate find.
def find(id)
return to_a.find { |record| record.id == id } if @relationship.loaded?

unless id.start_with?(@owner.id)
raise ArgumentError, "requested ACL (#{id}) is not a member of #{@owner.id}"
end
Expand Down
3 changes: 2 additions & 1 deletion hydra-access-controls/spec/unit/permissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Foo < ActiveFedora::Base
it "removes permissions on existing users" do
indexed_result = ActiveFedora::SolrService.query("id:#{subject.id}").first['edit_access_person_ssim']
expect(indexed_result).to eq ['jcoyne']
expect(subject.permissions.map(&:to_hash)).to eq [{ name: "jcoyne", type: "person", access: "edit" }]
expect(reloaded).to eq [{ name: "jcoyne", type: "person", access: "edit" }]
end
end
Expand Down Expand Up @@ -188,7 +189,7 @@ class Foo < ActiveFedora::Base
subject.update permissions_attributes: [
{ id: permissions_id, type: "group", access: "read", name: "group1", _destroy: '1' },
{ type: "group", access: "edit", name: "group2" },
{ type: "person", access: "read", name: "joebob" }
{ type: "person", access: "read", name: "joebob" }
]
end

Expand Down

0 comments on commit e2b5edb

Please sign in to comment.