Class: CrazyTrain::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/crazy_train/application_controller.rb

Direct Known Subclasses

ResourcesController, TablesController

Instance Method Summary collapse

Instance Method Details

#jwt_payloadObject



41
42
43
44
45
# File 'app/controllers/crazy_train/application_controller.rb', line 41

def jwt_payload
  CrazyTrain::JWT.decode(jwt_token, CrazyTrain.config.secret).first
rescue StandardError
  nil
end

#jwt_tokenObject



35
36
37
38
39
# File 'app/controllers/crazy_train/application_controller.rb', line 35

def jwt_token
  request.headers['Authorization'].split.last
rescue StandardError
  nil
end

#setup_roleObject



23
24
25
# File 'app/controllers/crazy_train/application_controller.rb', line 23

def setup_role
  switch_role(@role)
end

#switch_role(role) ⇒ Object



31
32
33
# File 'app/controllers/crazy_train/application_controller.rb', line 31

def switch_role(role)
  ActiveRecord::Base.connection.execute("SET ROLE #{role};")
end

#teardown_roleObject



27
28
29
# File 'app/controllers/crazy_train/application_controller.rb', line 27

def teardown_role
  switch_role(@default_role)
end

#verify_tokenObject



11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/crazy_train/application_controller.rb', line 11

def verify_token
  @default_role = CrazyTrain.current_role
  @role = if jwt_token && jwt_payload
            jwt_payload['role'] || CrazyTrain.config.authenticated_role
          else
            CrazyTrain.config.unauthorized_role
          end

  payload_string = JSON.generate(jwt_payload)
  CrazyTrain.setup_jwt_claims!(payload_string)
end