gitlab-org--gitlab-foss/app/models/ability.rb

58 lines
1.3 KiB
Ruby
Raw Normal View History

2011-10-08 17:36:38 -04:00
class Ability
def self.allowed(object, subject)
case subject.class.name
when "Project" then project_abilities(object, subject)
2011-10-17 06:39:03 -04: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 17:36:38 -04:00
else []
end
end
def self.project_abilities(user, project)
rules = []
rules << [
:read_project,
:read_issue,
2011-10-16 17:07:10 -04:00
:read_snippet,
2011-10-08 17:36:38 -04:00
:read_team_member,
:read_note
2011-10-08 17:36:38 -04:00
] if project.readers.include?(user)
rules << [
:write_project,
:write_issue,
2011-10-16 17:07:10 -04:00
:write_snippet,
:write_note
2011-10-08 17:36:38 -04:00
] if project.writers.include?(user)
rules << [
:admin_project,
:admin_issue,
2011-10-16 17:07:10 -04:00
:admin_snippet,
2011-10-08 17:36:38 -04:00
:admin_team_member,
:admin_note
2011-10-08 17:36:38 -04:00
] if project.admins.include?(user)
rules.flatten
end
2011-10-17 06:39:03 -04:00
class << self
2011-10-17 06:39:03 -04:00
[:issue, :note, :snippet].each do |name|
define_method "#{name}_abilities" do |user, subject|
if subject.author == user
[
:"read_#{name}",
:"write_#{name}",
:"admin_#{name}"
]
else
subject.respond_to?(:project) ?
2011-10-17 06:39:03 -04:00
project_abilities(user, subject.project) : []
end
end
end
end
2011-10-08 17:36:38 -04:00
end