ability.rb 1.6 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)
V
Valery Sizov 已提交
8
    when "Wiki" then wiki_abilities(object, subject)
G
gitlabhq 已提交
9 10 11 12 13 14 15 16 17 18
    else []
    end
  end

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

    rules << [
      :read_project,
      :read_issue,
G
gitlabhq 已提交
19
      :read_snippet,
G
gitlabhq 已提交
20
      :read_team_member,
D
Dmitriy Zaporozhets 已提交
21
      :read_merge_request,
N
Nihad Abbasov 已提交
22
      :read_note
D
Dmitriy Zaporozhets 已提交
23
    ] if project.allow_read_for?(user)
G
gitlabhq 已提交
24 25 26 27

    rules << [
      :write_project,
      :write_issue,
G
gitlabhq 已提交
28
      :write_snippet,
D
Dmitriy Zaporozhets 已提交
29
      :write_merge_request,
V
Valery Sizov 已提交
30 31
      :write_note,
      :write_wiki
D
Dmitriy Zaporozhets 已提交
32
    ] if project.allow_write_for?(user)
G
gitlabhq 已提交
33 34

    rules << [
D
Dmitriy Zaporozhets 已提交
35 36
      :modify_issue,
      :modify_snippet,
V
Valery Sizov 已提交
37
      :modify_wiki,
G
gitlabhq 已提交
38 39
      :admin_project,
      :admin_issue,
G
gitlabhq 已提交
40
      :admin_snippet,
G
gitlabhq 已提交
41
      :admin_team_member,
D
Dmitriy Zaporozhets 已提交
42
      :admin_merge_request,
N
Nihad Abbasov 已提交
43
      :admin_note
D
Dmitriy Zaporozhets 已提交
44
    ] if project.allow_admin_for?(user)
G
gitlabhq 已提交
45

46 47 48 49
    rules << [
      :download_code,
    ] if project.allow_pull_for?(user)

G
gitlabhq 已提交
50 51
    rules.flatten
  end
G
gitlabhq 已提交
52

N
Nihad Abbasov 已提交
53
  class << self
V
Valery Sizov 已提交
54
    [:issue, :note, :snippet, :merge_request, :wiki].each do |name|
G
gitlabhq 已提交
55 56 57 58 59
      define_method "#{name}_abilities" do |user, subject|
        if subject.author == user
          [
            :"read_#{name}",
            :"write_#{name}",
D
Dmitriy Zaporozhets 已提交
60
            :"modify_#{name}",
G
gitlabhq 已提交
61 62 63
            :"admin_#{name}"
          ]
        else
N
Nihad Abbasov 已提交
64
          subject.respond_to?(:project) ?
G
gitlabhq 已提交
65 66 67 68 69
            project_abilities(user, subject.project) : []
        end
      end
    end
  end
G
gitlabhq 已提交
70
end