metamaps--metamaps/app/policies/synapse_policy.rb

68 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
2016-02-13 09:28:21 +00:00
class SynapsePolicy < ApplicationPolicy
class Scope < Scope
def resolve
return scope.where(permission: %w(public commons)) unless user
2016-09-24 04:27:34 +00:00
scope.where(permission: %w(public commons))
.or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
2016-09-24 04:27:34 +00:00
.or(scope.where(user_id: user.id))
2016-02-13 09:28:21 +00:00
end
end
def index?
true # really only for the API. should be policy scoped!
end
2016-02-13 09:28:21 +00:00
def create?
if record.try(:topic1) && record.try(:topic2)
topic1_show? && topic2_show? && user.present?
else
# allows us to use policy(Synapse).create?
user.present?
end
2016-02-13 09:28:21 +00:00
end
def show?
topic1_show? && topic2_show? && synapse_show?
2016-02-13 09:28:21 +00:00
end
def update?
if !user.present?
false
elsif record.defer_to_map.present?
map_policy.update?
else
record.permission == 'commons' || record.user == user
end
2016-02-13 09:28:21 +00:00
end
def destroy?
record.mappings.empty? && (record.user == user || admin_override)
2016-02-13 09:28:21 +00:00
end
# Helpers
def map_policy
@map_policy ||= Pundit.policy(user, record.defer_to_map)
end
def topic1_show?
@topic1_policy ||= Pundit.policy(user, record.topic1)
@topic1_policy&.show?
end
def topic2_show?
@topic2_policy ||= Pundit.policy(user, record.topic2)
@topic2_policy&.show?
end
def synapse_show?
if record.defer_to_map.present?
map_policy&.show?
else
record.permission == 'commons' || record.permission == 'public' || record.user == user
end
end
2016-02-13 09:28:21 +00:00
end