summaryrefslogtreecommitdiff
path: root/lib/constraints/admin_constrainer.rb
blob: 59c855a1b73489f7f8f6f869a1c9f541f639eefa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# frozen_string_literal: true

module Constraints
  class AdminConstrainer
    def matches?(request)
      if Feature.enabled?(:user_mode_in_session)
        admin_mode_enabled?(request)
      else
        user_is_admin?(request)
      end
    end

    private

    def user_is_admin?(request)
      request.env['warden'].authenticate? && request.env['warden'].user.admin?
    end

    def admin_mode_enabled?(request)
      Gitlab::Session.with_session(request.session) do
        request.env['warden'].authenticate? && Gitlab::Auth::CurrentUserMode.new(request.env['warden'].user).admin_mode?
      end
    end
  end
end