2011-10-08 21:36:38 +00:00
|
|
|
class Ability
|
2012-10-09 00:10:04 +00:00
|
|
|
class << self
|
2013-01-25 09:30:49 +00:00
|
|
|
def allowed(user, subject)
|
2013-09-24 12:58:39 +00:00
|
|
|
return not_auth_abilities(user, subject) if user.nil?
|
2013-01-25 09:30:49 +00:00
|
|
|
return [] unless user.kind_of?(User)
|
2013-08-27 09:41:49 +00:00
|
|
|
return [] if user.blocked?
|
2013-01-25 09:30:49 +00:00
|
|
|
|
2012-10-09 00:10:04 +00:00
|
|
|
case subject.class.name
|
2013-01-25 09:30:49 +00:00
|
|
|
when "Project" then project_abilities(user, subject)
|
|
|
|
when "Issue" then issue_abilities(user, subject)
|
|
|
|
when "Note" then note_abilities(user, subject)
|
2013-03-24 18:31:14 +00:00
|
|
|
when "ProjectSnippet" then project_snippet_abilities(user, subject)
|
2013-03-24 22:17:03 +00:00
|
|
|
when "PersonalSnippet" then personal_snippet_abilities(user, subject)
|
2013-01-25 09:30:49 +00:00
|
|
|
when "MergeRequest" then merge_request_abilities(user, subject)
|
2013-06-21 19:44:40 +00:00
|
|
|
when "Group" then group_abilities(user, subject)
|
|
|
|
when "Namespace" then namespace_abilities(user, subject)
|
2015-03-13 15:16:51 +00:00
|
|
|
when "GroupMember" then group_member_abilities(user, subject)
|
2012-10-09 00:10:04 +00:00
|
|
|
else []
|
2013-01-25 09:30:49 +00:00
|
|
|
end.concat(global_abilities(user))
|
|
|
|
end
|
|
|
|
|
2013-09-24 12:58:39 +00:00
|
|
|
# List of possible abilities
|
|
|
|
# for non-authenticated user
|
|
|
|
def not_auth_abilities(user, subject)
|
|
|
|
project = if subject.kind_of?(Project)
|
|
|
|
subject
|
|
|
|
elsif subject.respond_to?(:project)
|
|
|
|
subject.project
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2013-11-06 15:13:21 +00:00
|
|
|
if project && project.public?
|
2015-07-19 21:21:33 +00:00
|
|
|
rules = [
|
2013-09-24 19:13:28 +00:00
|
|
|
:read_project,
|
|
|
|
:read_wiki,
|
|
|
|
:read_issue,
|
2015-07-19 18:58:59 +00:00
|
|
|
:read_label,
|
2013-09-24 19:13:28 +00:00
|
|
|
:read_milestone,
|
|
|
|
:read_project_snippet,
|
2015-03-13 15:22:03 +00:00
|
|
|
:read_project_member,
|
2013-09-24 19:13:28 +00:00
|
|
|
:read_merge_request,
|
|
|
|
:read_note,
|
|
|
|
:download_code
|
|
|
|
]
|
2015-07-19 21:21:33 +00:00
|
|
|
|
|
|
|
rules - project_disabled_features_rules(project)
|
2013-09-24 12:58:39 +00:00
|
|
|
else
|
2014-02-13 20:45:51 +00:00
|
|
|
group = if subject.kind_of?(Group)
|
|
|
|
subject
|
|
|
|
elsif subject.respond_to?(:group)
|
|
|
|
subject.group
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2014-02-25 12:36:36 +00:00
|
|
|
|
2014-06-05 17:37:35 +00:00
|
|
|
if group && group.public_profile?
|
2014-02-13 20:45:51 +00:00
|
|
|
[:read_group]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2013-09-24 12:58:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 09:30:49 +00:00
|
|
|
def global_abilities(user)
|
|
|
|
rules = []
|
|
|
|
rules << :create_group if user.can_create_group
|
|
|
|
rules
|
2011-10-08 21:36:38 +00:00
|
|
|
end
|
|
|
|
|
2012-10-09 00:10:04 +00:00
|
|
|
def project_abilities(user, project)
|
|
|
|
rules = []
|
2014-06-14 10:05:25 +00:00
|
|
|
key = "/user/#{user.id}/project/#{project.id}"
|
2015-06-26 16:15:36 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
RequestStore.store[key] ||= begin
|
|
|
|
team = project.team
|
2011-10-08 21:36:38 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
# Rules based on role in project
|
|
|
|
if team.master?(user)
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*project_master_rules)
|
2013-01-03 19:09:18 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
elsif team.developer?(user)
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*project_dev_rules)
|
2012-11-29 04:29:11 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
elsif team.reporter?(user)
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*project_report_rules)
|
2012-11-29 04:29:11 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
elsif team.guest?(user)
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*project_guest_rules)
|
2014-06-14 10:05:25 +00:00
|
|
|
end
|
2012-11-29 04:29:11 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
if project.public? || project.internal?
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*public_project_rules)
|
2014-06-14 10:05:25 +00:00
|
|
|
end
|
2012-11-29 04:29:11 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
if project.owner == user || user.admin?
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*project_admin_rules)
|
2014-06-14 10:05:25 +00:00
|
|
|
end
|
2013-06-06 11:16:08 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
if project.group && project.group.has_owner?(user)
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*project_admin_rules)
|
2014-06-14 10:05:25 +00:00
|
|
|
end
|
2012-11-29 04:29:11 +00:00
|
|
|
|
2014-06-14 10:05:25 +00:00
|
|
|
if project.archived?
|
|
|
|
rules -= project_archived_rules
|
|
|
|
end
|
2013-06-17 11:17:32 +00:00
|
|
|
|
2015-07-19 21:21:33 +00:00
|
|
|
rules - project_disabled_features_rules(project)
|
2013-11-29 16:10:59 +00:00
|
|
|
end
|
2012-11-29 04:29:11 +00:00
|
|
|
end
|
|
|
|
|
2013-06-06 11:16:08 +00:00
|
|
|
def public_project_rules
|
2013-09-24 12:58:39 +00:00
|
|
|
project_guest_rules + [
|
2013-06-06 11:16:08 +00:00
|
|
|
:download_code,
|
2013-11-06 15:13:21 +00:00
|
|
|
:fork_project
|
2013-06-06 11:16:08 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2012-11-29 04:29:11 +00:00
|
|
|
def project_guest_rules
|
|
|
|
[
|
2012-10-09 00:10:04 +00:00
|
|
|
:read_project,
|
|
|
|
:read_wiki,
|
|
|
|
:read_issue,
|
2015-06-17 14:33:51 +00:00
|
|
|
:read_label,
|
2012-10-09 00:10:04 +00:00
|
|
|
:read_milestone,
|
2013-03-25 07:20:14 +00:00
|
|
|
:read_project_snippet,
|
2015-03-13 15:22:03 +00:00
|
|
|
:read_project_member,
|
2012-10-09 00:10:04 +00:00
|
|
|
:read_merge_request,
|
|
|
|
:read_note,
|
2015-06-26 13:55:56 +00:00
|
|
|
:create_project,
|
|
|
|
:create_issue,
|
|
|
|
:create_note
|
2012-11-29 04:29:11 +00:00
|
|
|
]
|
|
|
|
end
|
2012-02-20 18:16:55 +00:00
|
|
|
|
2012-11-29 04:29:11 +00:00
|
|
|
def project_report_rules
|
|
|
|
project_guest_rules + [
|
2015-10-06 10:01:16 +00:00
|
|
|
:create_commit_status,
|
|
|
|
:read_commit_statuses,
|
2012-10-09 00:10:04 +00:00
|
|
|
:download_code,
|
2013-06-04 15:50:42 +00:00
|
|
|
:fork_project,
|
2015-06-30 11:33:02 +00:00
|
|
|
:create_project_snippet,
|
|
|
|
:update_issue,
|
|
|
|
:admin_issue,
|
2015-07-23 18:48:55 +00:00
|
|
|
:admin_label
|
2012-11-29 04:29:11 +00:00
|
|
|
]
|
|
|
|
end
|
2012-02-20 18:16:55 +00:00
|
|
|
|
2012-11-29 04:29:11 +00:00
|
|
|
def project_dev_rules
|
|
|
|
project_report_rules + [
|
2015-07-23 18:48:55 +00:00
|
|
|
:admin_merge_request,
|
2015-06-26 13:55:56 +00:00
|
|
|
:create_merge_request,
|
|
|
|
:create_wiki,
|
2015-09-09 14:36:01 +00:00
|
|
|
:manage_builds,
|
2012-10-21 09:12:14 +00:00
|
|
|
:push_code
|
2012-11-29 04:29:11 +00:00
|
|
|
]
|
|
|
|
end
|
2012-10-21 09:12:14 +00:00
|
|
|
|
2013-11-29 16:10:59 +00:00
|
|
|
def project_archived_rules
|
|
|
|
[
|
2015-06-26 13:55:56 +00:00
|
|
|
:create_merge_request,
|
2013-11-29 16:10:59 +00:00
|
|
|
:push_code,
|
|
|
|
:push_code_to_protected_branches,
|
2015-06-26 13:55:56 +00:00
|
|
|
:update_merge_request,
|
2013-11-29 16:10:59 +00:00
|
|
|
:admin_merge_request
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2012-11-29 04:29:11 +00:00
|
|
|
def project_master_rules
|
|
|
|
project_dev_rules + [
|
|
|
|
:push_code_to_protected_branches,
|
2015-06-26 13:55:56 +00:00
|
|
|
:update_project_snippet,
|
|
|
|
:update_merge_request,
|
2012-10-09 00:10:04 +00:00
|
|
|
:admin_milestone,
|
2013-03-25 07:20:14 +00:00
|
|
|
:admin_project_snippet,
|
2015-03-13 15:22:03 +00:00
|
|
|
:admin_project_member,
|
2012-10-09 00:10:04 +00:00
|
|
|
:admin_merge_request,
|
|
|
|
:admin_note,
|
2012-12-04 20:06:55 +00:00
|
|
|
:admin_wiki,
|
|
|
|
:admin_project
|
2012-11-29 04:29:11 +00:00
|
|
|
]
|
|
|
|
end
|
2011-10-08 21:36:38 +00:00
|
|
|
|
2012-11-29 04:29:11 +00:00
|
|
|
def project_admin_rules
|
|
|
|
project_master_rules + [
|
2012-12-04 20:06:55 +00:00
|
|
|
:change_namespace,
|
2013-11-06 15:13:21 +00:00
|
|
|
:change_visibility_level,
|
2012-12-04 20:48:24 +00:00
|
|
|
:rename_project,
|
2013-11-29 16:10:59 +00:00
|
|
|
:remove_project,
|
|
|
|
:archive_project
|
2012-11-29 04:29:11 +00:00
|
|
|
]
|
2012-10-09 00:10:04 +00:00
|
|
|
end
|
2011-10-17 10:39:03 +00:00
|
|
|
|
2015-07-19 21:21:33 +00:00
|
|
|
def project_disabled_features_rules(project)
|
|
|
|
rules = []
|
|
|
|
|
|
|
|
unless project.issues_enabled
|
|
|
|
rules += named_abilities('issue')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless project.merge_requests_enabled
|
|
|
|
rules += named_abilities('merge_request')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless project.issues_enabled or project.merge_requests_enabled
|
|
|
|
rules += named_abilities('label')
|
|
|
|
rules += named_abilities('milestone')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless project.snippets_enabled
|
|
|
|
rules += named_abilities('project_snippet')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless project.wiki_enabled
|
|
|
|
rules += named_abilities('wiki')
|
|
|
|
end
|
|
|
|
|
|
|
|
rules
|
|
|
|
end
|
|
|
|
|
2014-09-25 22:07:40 +00:00
|
|
|
def group_abilities(user, group)
|
2012-11-24 20:00:30 +00:00
|
|
|
rules = []
|
|
|
|
|
2014-02-25 17:15:08 +00:00
|
|
|
if user.admin? || group.users.include?(user) || ProjectsFinder.new.execute(user, group: group).any?
|
2013-09-11 18:00:16 +00:00
|
|
|
rules << :read_group
|
|
|
|
end
|
|
|
|
|
2014-05-28 16:03:01 +00:00
|
|
|
# Only group masters and group owners can create new projects in group
|
|
|
|
if group.has_master?(user) || group.has_owner?(user) || user.admin?
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*[
|
2014-05-28 16:03:01 +00:00
|
|
|
:create_projects,
|
2014-10-09 07:47:47 +00:00
|
|
|
])
|
2014-05-28 16:03:01 +00:00
|
|
|
end
|
|
|
|
|
2015-04-10 12:39:10 +00:00
|
|
|
# Only group owner and administrators can admin group
|
2013-09-26 11:49:22 +00:00
|
|
|
if group.has_owner?(user) || user.admin?
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*[
|
2015-04-10 12:39:10 +00:00
|
|
|
:admin_group,
|
2015-07-28 14:32:15 +00:00
|
|
|
:admin_namespace,
|
|
|
|
:admin_group_member
|
2014-10-09 07:47:47 +00:00
|
|
|
])
|
2013-01-02 16:57:02 +00:00
|
|
|
end
|
2012-11-24 20:00:30 +00:00
|
|
|
|
|
|
|
rules.flatten
|
|
|
|
end
|
|
|
|
|
2014-09-25 22:07:40 +00:00
|
|
|
def namespace_abilities(user, namespace)
|
2013-06-21 19:44:40 +00:00
|
|
|
rules = []
|
|
|
|
|
2015-04-10 12:39:10 +00:00
|
|
|
# Only namespace owner and administrators can admin it
|
2013-06-21 19:44:40 +00:00
|
|
|
if namespace.owner == user || user.admin?
|
2014-10-09 07:47:47 +00:00
|
|
|
rules.push(*[
|
2014-05-28 16:03:01 +00:00
|
|
|
:create_projects,
|
2015-04-10 12:39:10 +00:00
|
|
|
:admin_namespace
|
2014-10-09 07:47:47 +00:00
|
|
|
])
|
2013-06-21 19:44:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rules.flatten
|
|
|
|
end
|
|
|
|
|
2015-06-26 16:15:36 +00:00
|
|
|
|
|
|
|
[:issue, :merge_request].each do |name|
|
2011-10-17 10:39:03 +00:00
|
|
|
define_method "#{name}_abilities" do |user, subject|
|
2015-06-26 16:15:36 +00:00
|
|
|
rules = []
|
|
|
|
|
|
|
|
if subject.author == user || (subject.respond_to?(:assignee) && subject.assignee == user)
|
|
|
|
rules += [
|
2011-10-17 10:39:03 +00:00
|
|
|
:"read_#{name}",
|
2015-06-26 15:18:40 +00:00
|
|
|
:"update_#{name}",
|
2011-10-17 10:39:03 +00:00
|
|
|
]
|
2015-06-26 16:15:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rules += project_abilities(user, subject.project)
|
|
|
|
rules
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[:note, :project_snippet, :personal_snippet].each do |name|
|
|
|
|
define_method "#{name}_abilities" do |user, subject|
|
|
|
|
rules = []
|
|
|
|
|
|
|
|
if subject.author == user
|
|
|
|
rules += [
|
2012-02-21 22:31:18 +00:00
|
|
|
:"read_#{name}",
|
2015-06-26 15:18:40 +00:00
|
|
|
:"update_#{name}",
|
2015-06-26 16:15:36 +00:00
|
|
|
:"admin_#{name}"
|
2012-02-21 22:31:18 +00:00
|
|
|
]
|
2011-10-17 10:39:03 +00:00
|
|
|
end
|
2015-06-26 16:15:36 +00:00
|
|
|
|
|
|
|
if subject.respond_to?(:project) && subject.project
|
|
|
|
rules += project_abilities(user, subject.project)
|
|
|
|
end
|
|
|
|
|
|
|
|
rules
|
2011-10-17 10:39:03 +00:00
|
|
|
end
|
|
|
|
end
|
2014-02-07 16:59:55 +00:00
|
|
|
|
2015-03-13 15:16:51 +00:00
|
|
|
def group_member_abilities(user, subject)
|
2014-02-07 16:59:55 +00:00
|
|
|
rules = []
|
|
|
|
target_user = subject.user
|
|
|
|
group = subject.group
|
2015-07-28 14:32:15 +00:00
|
|
|
can_manage = group_abilities(user, group).include?(:admin_group_member)
|
2015-06-26 16:15:36 +00:00
|
|
|
|
2014-02-07 16:59:55 +00:00
|
|
|
if can_manage && (user != target_user)
|
2015-06-26 13:55:56 +00:00
|
|
|
rules << :update_group_member
|
2015-03-13 15:16:51 +00:00
|
|
|
rules << :destroy_group_member
|
2014-02-07 16:59:55 +00:00
|
|
|
end
|
2015-06-26 16:15:36 +00:00
|
|
|
|
2014-02-07 16:59:55 +00:00
|
|
|
if !group.last_owner?(user) && (can_manage || (user == target_user))
|
2015-03-13 15:16:51 +00:00
|
|
|
rules << :destroy_group_member
|
2014-02-07 16:59:55 +00:00
|
|
|
end
|
2015-06-26 16:15:36 +00:00
|
|
|
|
2014-02-07 16:59:55 +00:00
|
|
|
rules
|
|
|
|
end
|
2014-10-19 08:50:23 +00:00
|
|
|
|
|
|
|
def abilities
|
|
|
|
@abilities ||= begin
|
|
|
|
abilities = Six.new
|
|
|
|
abilities << self
|
|
|
|
abilities
|
|
|
|
end
|
|
|
|
end
|
2015-05-22 10:17:37 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def named_abilities(name)
|
|
|
|
[
|
|
|
|
:"read_#{name}",
|
2015-06-26 15:18:40 +00:00
|
|
|
:"create_#{name}",
|
|
|
|
:"update_#{name}",
|
2015-05-22 10:17:37 +00:00
|
|
|
:"admin_#{name}"
|
|
|
|
]
|
|
|
|
end
|
2011-10-17 10:39:03 +00:00
|
|
|
end
|
2011-10-08 21:36:38 +00:00
|
|
|
end
|