Class: ForestLiana::ApplicationController

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#route_not_found

Class Method Details

.papertrail?Boolean



12
13
14
# File 'app/controllers/forest_liana/application_controller.rb', line 12

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

Instance Method Details

#authenticate_user_from_jwtObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/forest_liana/application_controller.rb', line 74

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 = ForestLiana::Token::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)

      # NOTICE: Automatically logs out the users that use tokens having an old data format.
      if @jwt_decoded_token['data']
        raise ForestLiana::Errors::HTTP401Error.new("Your token format is invalid, please login again.")
      end

      @rendering_id = @jwt_decoded_token['rendering_id']
    else
      head :unauthorized
    end
  rescue JWT::ExpiredSignature, JWT::VerificationError
    render json: { error: 'expired_token' }, status: :unauthorized,
      serializer: nil
  rescue
    head :unauthorized
  end
end

#deactivate_count_responseObject



109
110
111
# File 'app/controllers/forest_liana/application_controller.rb', line 109

def deactivate_count_response
  render serializer: nil, json: { meta: { count: 'deactivated'} }
end

#forest_userObject

NOTICE: Helper method for Smart Routes logic based on current user info.



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

def forest_user
  @jwt_decoded_token
end

#get_collectionObject

Raises:

  • (NotImplementedError)


70
71
72
# File 'app/controllers/forest_liana/application_controller.rb', line 70

def get_collection
  raise NotImplementedError, "#{self.class} must implement #get_collection"
end

#internal_server_errorObject



105
106
107
# File 'app/controllers/forest_liana/application_controller.rb', line 105

def internal_server_error
  head :internal_server_error
end

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



41
42
43
44
45
46
47
48
# File 'app/controllers/forest_liana/application_controller.rb', line 41

def serialize_model(record, options = {})
  options[:is_collection] = false
  options[:context] = { unoptimized: true }.merge(options[:context] || {})

  json = ForestAdmin::JSONAPI::Serializer.serialize(record, options)

  force_utf8_encoding(json)
end

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/forest_liana/application_controller.rb', line 50

def serialize_models(records, options = {}, fields_searched = [])
  options[:is_collection] = true
  if options[:params] && options[:params][:fields].nil?
    options[:context] = { unoptimized: true }.merge(options[:context] || {})
  end

  json = ForestAdmin::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