2011-10-08 23:36:38 +02:00
|
|
|
class Ability
|
|
|
|
def self.allowed(object, subject)
|
|
|
|
case subject.class.name
|
|
|
|
when "Project" then project_abilities(object, subject)
|
2011-10-17 12:39:03 +02:00
|
|
|
when "Issue" then issue_abilities(object, subject)
|
|
|
|
when "Note" then note_abilities(object, subject)
|
|
|
|
when "Snippet" then snippet_abilities(object, subject)
|
2011-10-08 23:36:38 +02:00
|
|
|
else []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.project_abilities(user, project)
|
|
|
|
rules = []
|
|
|
|
|
|
|
|
rules << [
|
|
|
|
:read_project,
|
|
|
|
:read_issue,
|
2011-10-16 23:07:10 +02:00
|
|
|
:read_snippet,
|
2011-10-08 23:36:38 +02:00
|
|
|
:read_team_member,
|
2011-11-28 08:39:43 +01:00
|
|
|
:read_merge_request,
|
2011-10-26 15:46:25 +02:00
|
|
|
:read_note
|
2011-12-15 22:57:46 +01:00
|
|
|
] if project.allow_read_for?(user)
|
2011-10-08 23:36:38 +02:00
|
|
|
|
|
|
|
rules << [
|
|
|
|
:write_project,
|
|
|
|
:write_issue,
|
2011-10-16 23:07:10 +02:00
|
|
|
:write_snippet,
|
2011-11-28 08:39:43 +01:00
|
|
|
:write_merge_request,
|
2011-10-26 15:46:25 +02:00
|
|
|
:write_note
|
2011-12-15 22:57:46 +01:00
|
|
|
] if project.allow_write_for?(user)
|
2011-10-08 23:36:38 +02:00
|
|
|
|
|
|
|
rules << [
|
2011-12-15 22:57:46 +01:00
|
|
|
:modify_issue,
|
|
|
|
:modify_snippet,
|
2011-10-08 23:36:38 +02:00
|
|
|
:admin_project,
|
|
|
|
:admin_issue,
|
2011-10-16 23:07:10 +02:00
|
|
|
:admin_snippet,
|
2011-10-08 23:36:38 +02:00
|
|
|
:admin_team_member,
|
2011-11-28 08:39:43 +01:00
|
|
|
:admin_merge_request,
|
2011-10-26 15:46:25 +02:00
|
|
|
:admin_note
|
2011-12-15 22:57:46 +01:00
|
|
|
] if project.allow_admin_for?(user)
|
2011-10-08 23:36:38 +02:00
|
|
|
|
|
|
|
rules.flatten
|
|
|
|
end
|
2011-10-17 12:39:03 +02:00
|
|
|
|
2011-10-26 15:46:25 +02:00
|
|
|
class << self
|
2011-11-28 08:39:43 +01:00
|
|
|
[:issue, :note, :snippet, :merge_request].each do |name|
|
2011-10-17 12:39:03 +02:00
|
|
|
define_method "#{name}_abilities" do |user, subject|
|
|
|
|
if subject.author == user
|
|
|
|
[
|
|
|
|
:"read_#{name}",
|
|
|
|
:"write_#{name}",
|
2011-12-15 22:57:46 +01:00
|
|
|
:"modify_#{name}",
|
2011-10-17 12:39:03 +02:00
|
|
|
:"admin_#{name}"
|
|
|
|
]
|
|
|
|
else
|
2011-10-26 15:46:25 +02:00
|
|
|
subject.respond_to?(:project) ?
|
2011-10-17 12:39:03 +02:00
|
|
|
project_abilities(user, subject.project) : []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-08 23:36:38 +02:00
|
|
|
end
|