Skip to content
Snippets Groups Projects
ability.rb 1.61 KiB
Newer Older
  • Learn to ignore specific revisions
  • gitlabhq's avatar
    gitlabhq committed
    class Ability
      def self.allowed(object, subject)
        case subject.class.name
        when "Project" then project_abilities(object, subject)
    
    gitlabhq's avatar
    gitlabhq committed
        when "Issue" then issue_abilities(object, subject)
        when "Note" then note_abilities(object, subject)
        when "Snippet" then snippet_abilities(object, subject)
    
    Valery Sizov's avatar
    Valery Sizov committed
        when "Wiki" then wiki_abilities(object, subject)
    
    gitlabhq's avatar
    gitlabhq committed
        else []
        end
      end
    
      def self.project_abilities(user, project)
        rules = []
    
        rules << [
          :read_project,
          :read_issue,
    
    gitlabhq's avatar
    gitlabhq committed
          :read_snippet,
    
    gitlabhq's avatar
    gitlabhq committed
          :read_team_member,
    
          :read_merge_request,
    
    Nihad Abbasov's avatar
    Nihad Abbasov committed
          :read_note
    
        ] if project.allow_read_for?(user)
    
    gitlabhq's avatar
    gitlabhq committed
    
        rules << [
          :write_project,
          :write_issue,
    
    gitlabhq's avatar
    gitlabhq committed
          :write_snippet,
    
          :write_merge_request,
    
    Valery Sizov's avatar
    Valery Sizov committed
          :write_note,
          :write_wiki
    
        ] if project.allow_write_for?(user)
    
    gitlabhq's avatar
    gitlabhq committed
    
        rules << [
    
          :modify_issue,
          :modify_snippet,
    
    Valery Sizov's avatar
    Valery Sizov committed
          :modify_wiki,
    
    gitlabhq's avatar
    gitlabhq committed
          :admin_project,
          :admin_issue,
    
    gitlabhq's avatar
    gitlabhq committed
          :admin_snippet,
    
    gitlabhq's avatar
    gitlabhq committed
          :admin_team_member,
    
          :admin_merge_request,
    
    Nihad Abbasov's avatar
    Nihad Abbasov committed
          :admin_note
    
        ] if project.allow_admin_for?(user)
    
    gitlabhq's avatar
    gitlabhq committed
    
    
        rules << [
          :download_code,
        ] if project.allow_pull_for?(user)
    
    
    gitlabhq's avatar
    gitlabhq committed
        rules.flatten
      end
    
    gitlabhq's avatar
    gitlabhq committed
    
    
    Nihad Abbasov's avatar
    Nihad Abbasov committed
      class << self
    
    Valery Sizov's avatar
    Valery Sizov committed
        [:issue, :note, :snippet, :merge_request, :wiki].each do |name|
    
    gitlabhq's avatar
    gitlabhq committed
          define_method "#{name}_abilities" do |user, subject|
            if subject.author == user
              [
                :"read_#{name}",
                :"write_#{name}",
    
                :"modify_#{name}",
    
    gitlabhq's avatar
    gitlabhq committed
                :"admin_#{name}"
              ]
            else
    
    Nihad Abbasov's avatar
    Nihad Abbasov committed
              subject.respond_to?(:project) ?
    
    gitlabhq's avatar
    gitlabhq committed
                project_abilities(user, subject.project) : []
            end
          end
        end
      end
    
    gitlabhq's avatar
    gitlabhq committed
    end