rosa-build/app/policies/project_policy.rb

104 lines
3.0 KiB
Ruby
Raw Normal View History

2015-03-12 22:43:13 +00:00
class ProjectPolicy < ApplicationPolicy
2015-03-14 22:10:04 +00:00
def index?
2015-03-17 00:55:04 +00:00
!user.guest?
2015-03-14 22:10:04 +00:00
end
alias_method :remove_user?, :index?
alias_method :preview?, :index?
2015-03-14 22:10:04 +00:00
def show?
return true if record.public?
return true if record.owner == user
return true if record.owner.is_a?(Group) && user_group_ids.inclide?(record.owner_id)
local_reader?
2015-03-14 22:10:04 +00:00
end
alias_method :read?, :show?
alias_method :fork?, :show?
alias_method :archive?, :show?
alias_method :get_id?, :show?
alias_method :refs_list?, :show?
2015-03-14 22:10:04 +00:00
2015-03-17 00:55:04 +00:00
def create?
return false if user.guest?
!record.try(:owner) || owner_policy.write?
2015-03-17 00:55:04 +00:00
end
def update?
owner? || local_admin?
2015-03-14 22:10:04 +00:00
end
alias_method :alias?, :update?
alias_method :sections?, :update?
alias_method :manage_collaborators?, :update?
alias_method :autocomplete_maintainers?, :update?
alias_method :add_member?, :update?
alias_method :remove_member?, :update?
alias_method :remove_members?, :update?
alias_method :update_member?, :update?
alias_method :members?, :update?
alias_method :schedule?, :update?
2015-03-14 22:10:04 +00:00
2015-03-17 00:55:04 +00:00
def destroy?
owner? || record.owner.is_a?(Group) && record.owner.actors.exists?(actor_type: 'User', actor_id: user.id, role: 'admin')
2015-03-14 22:10:04 +00:00
end
2015-03-17 00:55:04 +00:00
def mass_import?
user.platforms.main.find{ |p| local_admin?(p) }.present?
2015-03-14 22:10:04 +00:00
end
def run_mass_import?
return false unless owner_policy.write?
repo = Repository.find(record.add_to_repository_id)
repo.platform.main? && PlatformPolicy.new(user, repo.platform).add_project?
end
2015-03-14 22:10:04 +00:00
2015-03-17 00:55:04 +00:00
# for grack
def write?
owner? || local_writer?
2015-03-17 00:55:04 +00:00
end
def possible_forks
true
2015-03-14 22:10:04 +00:00
end
class Scope < Scope
def membered
policy = Pundit.policy!(user, :project)
scope.where <<-SQL, { user_id: user.id, user_group_ids: policy.user_group_ids }
(
projects.owner_type = 'User' AND projects.owner_id = :user_id
) OR (
projects.owner_type = 'Group' AND projects.owner_id IN (:user_group_ids)
) OR (
projects.id = ANY (
ARRAY (
SELECT target_id
FROM relations
INNER JOIN projects ON projects.id = relations.target_id
WHERE relations.target_type = 'Project' AND
(
projects.owner_type = 'User' AND projects.owner_id != :user_id OR
projects.owner_type = 'Group' AND projects.owner_id NOT IN (:user_group_ids)
) AND (
relations.actor_type = 'User' AND relations.actor_id = :user_id OR
relations.actor_type = 'Group' AND relations.actor_id IN (:user_group_ids)
)
)
)
)
SQL
end
end
private
def owner_policy
if record.owner.is_a?(User)
UserPolicy.new(user, record.owner)
else
GroupPolicy.new(user, record.owner)
end
end
2015-03-12 22:43:13 +00:00
end