ensure topics and synapses have their permission match the map they're deferring to

This commit is contained in:
Connor Turland 2016-12-01 23:03:34 +00:00
parent a5f793fe54
commit 3c67f68d3b
3 changed files with 13 additions and 8 deletions

View file

@ -32,6 +32,8 @@ class Map < ApplicationRecord
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :screenshot, content_type: /\Aimage\/.*\Z/
after_save :update_deferring_topics_and_synapses, if: :permission_changed?
def mappings
topicmappings.or(synapsemappings)
end
@ -131,4 +133,9 @@ class Map < ApplicationRecord
end
removed.compact
end
def update_deferring_topics_and_synapses
Topic.where(defer_to_map_id: id).update_all(permission: permission)
Synapse.where(defer_to_map_id: id).update_all(permission: permission)
end
end

View file

@ -2,11 +2,10 @@
class SynapsePolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
return scope.where(permission: visible) unless user
return scope.where(permission: %w(public commons)) unless user
scope.where(permission: visible)
.or(scope.where.not(defer_to_map_id: nil).where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
scope.where(permission: %w(public commons))
.or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
.or(scope.where(user_id: user.id))
end
end

View file

@ -2,11 +2,10 @@
class TopicPolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
return scope.where(permission: visible) unless user
return scope.where(permission: %w(public commons)) unless user
scope.where(permission: visible)
.or(scope.where.not(defer_to_map_id: nil).where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
scope.where(permission: %w(public commons))
.or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
.or(scope.where(user_id: user.id))
end
end