change_access.rb 4.7 KB
Newer Older
1 2 3
module Gitlab
  module Checks
    class ChangeAccess
M
Michael Kozono 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
      ERROR_MESSAGES = {
        push_code: 'You are not allowed to push code to this project.',
        delete_default_branch: 'The default branch of a project cannot be deleted.',
        force_push_protected_branch: 'You are not allowed to force push code to a protected branch on this project.',
        non_master_delete_protected_branch: 'You are not allowed to delete protected branches from this project. Only a project master or owner can delete a protected branch.',
        non_web_delete_protected_branch: 'You can only delete protected branches using the web interface.',
        merge_protected_branch: 'You are not allowed to merge code into protected branches on this project.',
        push_protected_branch: 'You are not allowed to push code to protected branches on this project.',
        change_existing_tags: 'You are not allowed to change existing tags on this project.',
        update_protected_tag: 'Protected tags cannot be updated.',
        delete_protected_tag: 'Protected tags cannot be deleted.',
        create_protected_tag: 'You are not allowed to create this tag as it is protected.'
      }.freeze

18
      attr_reader :user_access, :project, :skip_authorization, :protocol
19

20
      def initialize(
21
        change, user_access:, project:, skip_authorization: false,
22 23
        protocol:
      )
S
Scott Le 已提交
24 25
        @oldrev, @newrev, @ref = change.values_at(:oldrev, :newrev, :ref)
        @branch_name = Gitlab::Git.branch_name(@ref)
26
        @tag_name = Gitlab::Git.tag_name(@ref)
27 28
        @user_access = user_access
        @project = project
29
        @skip_authorization = skip_authorization
30
        @protocol = protocol
31 32 33
      end

      def exec
34 35
        return GitAccessStatus.new(true) if skip_authorization

36 37 38
        push_checks
        branch_checks
        tag_checks
39

40
        GitAccessStatus.new(true)
41 42 43 44
      end

      protected

45 46
      def push_checks
        if user_access.cannot_do_action?(:push_code)
47
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:push_code]
48 49 50 51
        end
      end

      def branch_checks
52
        return unless @branch_name
53 54

        if deletion? && @branch_name == project.default_branch
55
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:delete_default_branch]
56 57 58 59 60 61
        end

        protected_branch_checks
      end

      def protected_branch_checks
62
        return unless ProtectedBranch.protected?(project, @branch_name)
63

64
        if forced_push?
65
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:force_push_protected_branch]
66 67
        end

68 69 70 71 72 73 74 75 76
        if deletion?
          protected_branch_deletion_checks
        else
          protected_branch_push_checks
        end
      end

      def protected_branch_deletion_checks
        unless user_access.can_delete_branch?(@branch_name)
77
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:non_master_delete_protected_branch]
78 79 80
        end

        unless protocol == 'web'
81
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:non_web_delete_protected_branch]
82 83 84 85
        end
      end

      def protected_branch_push_checks
86
        if matching_merge_request?
87
          unless user_access.can_merge_to_branch?(@branch_name) || user_access.can_push_to_branch?(@branch_name)
88
            raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:merge_protected_branch]
89 90
          end
        else
91
          unless user_access.can_push_to_branch?(@branch_name)
92
            raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:push_protected_branch]
93 94 95 96 97
          end
        end
      end

      def tag_checks
98
        return unless @tag_name
99

100
        if tag_exists? && user_access.cannot_do_action?(:admin_project)
101
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:change_existing_tags]
102
        end
103 104 105 106 107

        protected_tag_checks
      end

      def protected_tag_checks
108 109
        return unless ProtectedTag.protected?(project, @tag_name)

110 111
        raise(GitAccess::UnauthorizedError, ERROR_MESSAGES[:update_protected_tag]) if update?
        raise(GitAccess::UnauthorizedError, ERROR_MESSAGES[:delete_protected_tag]) if deletion?
112

113
        unless user_access.can_create_tag?(@tag_name)
114
          raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:create_protected_tag]
115 116 117
        end
      end

118 119
      private

120 121
      def tag_exists?
        project.repository.tag_exists?(@tag_name)
122 123 124
      end

      def forced_push?
125
        Gitlab::Checks::ForcePush.force_push?(@project, @oldrev, @newrev)
126 127
      end

128 129 130 131 132
      def update?
        !Gitlab::Git.blank_ref?(@oldrev) && !deletion?
      end

      def deletion?
133 134 135
        Gitlab::Git.blank_ref?(@newrev)
      end

136 137 138 139 140 141
      def matching_merge_request?
        Checks::MatchingMergeRequest.new(@newrev, @branch_name, @project).match?
      end
    end
  end
end