Class: ForestLiana::ApplicationController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/forest_liana/application_controller.rb

Constant Summary collapse

/forest_session_token=([^;]*)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.papertrail?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'app/controllers/forest_liana/application_controller.rb', line 8

def self.papertrail?
  Object.const_get('PaperTrail::Version').is_a?(Class) rescue false
end

Instance Method Details

#authenticate_user_from_jwtObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/forest_liana/application_controller.rb', line 59

def authenticate_user_from_jwt
  begin
    if request.headers
      if request.headers['Authorization']
        token = request.headers['Authorization'].split.second
      # NOTICE: Necessary for downloads authentication.
      elsif request.headers['cookie']
        match = REGEX_COOKIE_SESSION_TOKEN.match(request.headers['cookie'])
        token = match[1] if match && match[1]
      end

      @jwt_decoded_token = JWT.decode(token, ForestLiana.auth_secret, true,
        { algorithm: 'HS256' }).try(:first)
      @rendering_id = @jwt_decoded_token['data']['relationships']['renderings']['data'][0]['id']
    else
      head :unauthorized
    end
  rescue JWT::ExpiredSignature, JWT::VerificationError
    render json: { error: 'expired_token' }, status: :unauthorized,
      serializer: nil
  rescue
    head :unauthorized
  end
end

#forest_userObject



32
33
34
# File 'app/controllers/forest_liana/application_controller.rb', line 32

def forest_user
  @jwt_decoded_token
end

#get_smart_action_contextObject



84
85
86
87
88
89
90
91
# File 'app/controllers/forest_liana/application_controller.rb', line 84

def get_smart_action_context
  begin
    params[:data][:attributes].values[0].to_hash.symbolize_keys
  rescue => error
    FOREST_LOGGER.error "Smart Action context retrieval error: #{error}"
    {}
  end
end

#internal_server_errorObject



97
98
99
# File 'app/controllers/forest_liana/application_controller.rb', line 97

def internal_server_error
  head :internal_server_error
end

#route_not_foundObject



93
94
95
# File 'app/controllers/forest_liana/application_controller.rb', line 93

def route_not_found
  head :not_found
end

#serialize_model(record, options = {}) ⇒ Object



36
37
38
39
40
41
# File 'app/controllers/forest_liana/application_controller.rb', line 36

def serialize_model(record, options = {})
  options[:is_collection] = false
  json = JSONAPI::Serializer.serialize(record, options)

  force_utf8_encoding(json)
end

#serialize_models(records, options = {}, fields_searched = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/forest_liana/application_controller.rb', line 43

def serialize_models(records, options = {}, fields_searched = [])
  options[:is_collection] = true
  json = JSONAPI::Serializer.serialize(records, options)

  if options[:params] && options[:params][:search]
    # NOTICE: Add the Smart Fields with a 'String' type.
    fields_searched.concat(get_collection.string_smart_fields_names).uniq!
    json['meta'] = {
      decorators: ForestLiana::DecorationHelper
        .decorate_for_search(json, fields_searched, options[:params][:search])
    }
  end

  force_utf8_encoding(json)
end