Module: Rhoconnect::Condition::AdminRequired

Included in:
Server
Defined in:
lib/rhoconnect/condition/admin_required.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
# File 'lib/rhoconnect/condition/admin_required.rb', line 4

def self.extended(base)
  base.include_admin_required_condition
end

Instance Method Details

#include_admin_required_conditionObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rhoconnect/condition/admin_required.rb', line 8

def include_admin_required_condition
  set(:admin_required) do |value|
    condition do
      if value
        begin
          api_token = ApiToken.load(env[API_TOKEN_HEADER])
          api_token = ApiToken.load(params[:api_token]) unless api_token
          raise Rhoconnect::ApiException.new(422, "No API token provided") unless api_token
          raise Rhoconnect::ApiException.new(422, "Invalid/missing API user") unless api_token.user and api_token.user.admin == 1
          env[Rhoconnect::CURRENT_USER] = api_token.user
        rescue Rhoconnect::ApiException => ae
          throw :halt, [422, {'Content-Type' => 'text/plain'}, ae.message]
        end
      end
    end
  end
end