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
|
2015-03-18 22:02:38 +00:00
|
|
|
alias_method :remove_user?, :index?
|
|
|
|
alias_method :preview?, :index?
|
2015-03-14 22:10:04 +00:00
|
|
|
|
|
|
|
def show?
|
2015-03-18 22:02:38 +00:00
|
|
|
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
|
2015-03-18 22:02:38 +00:00
|
|
|
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?
|
2015-03-17 22:33:16 +00:00
|
|
|
!user.guest? && (!record.try(:owner) || policy(record.owner).write?)
|
2015-03-17 00:55:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update?
|
2015-03-18 22:02:38 +00:00
|
|
|
owner? || local_admin?
|
2015-03-14 22:10:04 +00:00
|
|
|
end
|
2015-03-18 22:02:38 +00:00
|
|
|
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
|
2015-03-17 22:33:16 +00:00
|
|
|
|
|
|
|
def run_mass_import?
|
|
|
|
return false unless policy(record.owner).write?
|
|
|
|
repo = Repository.find(record.add_to_repository_id)
|
|
|
|
repo.platform.main? && policy(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?
|
2015-03-18 22:02:38 +00:00
|
|
|
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
|
|
|
|
|
2015-03-17 22:33:16 +00:00
|
|
|
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
|
|
|
|
|
2015-03-12 22:43:13 +00:00
|
|
|
end
|