2015-03-12 22:43:13 +00:00
|
|
|
class PlatformPolicy < ApplicationPolicy
|
|
|
|
|
|
|
|
def index?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def show?
|
|
|
|
return true unless record.hidden?
|
|
|
|
return true if record.owner == user
|
|
|
|
return true if owner?
|
|
|
|
end
|
|
|
|
|
|
|
|
def create?
|
|
|
|
is_admin?
|
|
|
|
end
|
|
|
|
|
2015-03-17 22:33:16 +00:00
|
|
|
def update?
|
2015-03-12 22:43:13 +00:00
|
|
|
owner? || local_admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def clone?
|
|
|
|
return false if record.personal?
|
|
|
|
owner? || local_admin?
|
|
|
|
end
|
|
|
|
|
2015-03-17 22:33:16 +00:00
|
|
|
def add_project?
|
|
|
|
owner? || local_admin?
|
|
|
|
end
|
|
|
|
|
2015-03-12 22:43:13 +00:00
|
|
|
class Scope < Scope
|
|
|
|
|
|
|
|
def related
|
2015-03-17 22:33:16 +00:00
|
|
|
policy = Pundit.policy!(user, :platform)
|
2015-03-14 22:10:04 +00:00
|
|
|
scope.where <<-SQL, { user_id: user.id, user_group_ids: policy.user_group_ids, platform_ids: related_platform_ids }
|
2015-03-12 22:43:13 +00:00
|
|
|
(
|
|
|
|
platforms.id IN (:platform_ids)
|
|
|
|
) OR (
|
|
|
|
platforms.owner_type = 'User' AND platforms.owner_id = :user_id
|
|
|
|
) OR (
|
|
|
|
platforms.owner_type = 'Group' AND platforms.owner_id IN (:user_group_ids)
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def related_platform_ids
|
2015-03-17 22:33:16 +00:00
|
|
|
Rails.cache.fetch(['PlatformPolicy::Scope#related_platform_ids', user]) do
|
2015-03-12 22:43:13 +00:00
|
|
|
user.repositories.pluck(:platform_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|