ability.rb 1.9 KB
Newer Older
G
gitlabhq 已提交
1 2 3 4
class Ability
  def self.allowed(object, subject)
    case subject.class.name
    when "Project" then project_abilities(object, subject)
G
gitlabhq 已提交
5 6 7
    when "Issue" then issue_abilities(object, subject)
    when "Note" then note_abilities(object, subject)
    when "Snippet" then snippet_abilities(object, subject)
8
    when "MergeRequest" then merge_request_abilities(object, subject)
G
gitlabhq 已提交
9 10 11 12 13 14 15 16 17
    else []
    end
  end

  def self.project_abilities(user, project)
    rules = []

    rules << [
      :read_project,
D
Dmitriy Zaporozhets 已提交
18
      :read_wiki,
G
gitlabhq 已提交
19
      :read_issue,
D
Dmitriy Zaporozhets 已提交
20
      :read_milestone,
G
gitlabhq 已提交
21
      :read_snippet,
G
gitlabhq 已提交
22
      :read_team_member,
D
Dmitriy Zaporozhets 已提交
23
      :read_merge_request,
D
Dmitriy Zaporozhets 已提交
24
      :read_note,
G
gitlabhq 已提交
25 26
      :write_project,
      :write_issue,
D
Dmitriy Zaporozhets 已提交
27 28 29 30 31
      :write_note
    ] if project.guest_access_for?(user)

    rules << [
      :download_code,
32 33
      :write_merge_request,
      :write_snippet
D
Dmitriy Zaporozhets 已提交
34 35 36
    ] if project.report_access_for?(user)

    rules << [
V
Valery Sizov 已提交
37
      :write_wiki
D
Dmitriy Zaporozhets 已提交
38
    ] if project.dev_access_for?(user)
G
gitlabhq 已提交
39 40

    rules << [
D
Dmitriy Zaporozhets 已提交
41 42
      :modify_issue,
      :modify_snippet,
43
      :modify_merge_request,
G
gitlabhq 已提交
44 45
      :admin_project,
      :admin_issue,
D
Dmitriy Zaporozhets 已提交
46
      :admin_milestone,
G
gitlabhq 已提交
47
      :admin_snippet,
G
gitlabhq 已提交
48
      :admin_team_member,
D
Dmitriy Zaporozhets 已提交
49
      :admin_merge_request,
D
Dmitriy Zaporozhets 已提交
50
      :admin_note,
D
Dmitriy Zaporozhets 已提交
51
      :accept_mr,
D
Dmitriy Zaporozhets 已提交
52
      :admin_wiki
53
    ] if project.master_access_for?(user) || project.owner == user
G
gitlabhq 已提交
54 55 56

    rules.flatten
  end
G
gitlabhq 已提交
57

N
Nihad Abbasov 已提交
58
  class << self
D
Dmitriy Zaporozhets 已提交
59
    [:issue, :note, :snippet, :merge_request].each do |name|
G
gitlabhq 已提交
60 61 62 63 64
      define_method "#{name}_abilities" do |user, subject|
        if subject.author == user
          [
            :"read_#{name}",
            :"write_#{name}",
D
Dmitriy Zaporozhets 已提交
65
            :"modify_#{name}",
G
gitlabhq 已提交
66 67
            :"admin_#{name}"
          ]
68 69 70 71 72 73
        elsif subject.respond_to?(:assignee) && subject.assignee == user
          [
            :"read_#{name}",
            :"write_#{name}",
            :"modify_#{name}",
          ]
G
gitlabhq 已提交
74
        else
N
Nihad Abbasov 已提交
75
          subject.respond_to?(:project) ?
G
gitlabhq 已提交
76 77 78 79 80
            project_abilities(user, subject.project) : []
        end
      end
    end
  end
G
gitlabhq 已提交
81
end