aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/application_controller.rb
blob: 9a83394e2a363b03ec4295a5a982fc9b7548e459 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class ApplicationController < ActionController::Base
  include PaperTrailSupport
  include Pundit
  include FeatureChecker

  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

  # TODO : Delete hack to authorize Cross Request for js and json get request from javascript
  protect_from_forgery unless: -> { request.get? && (request.format.json? || request.format.js?) }
  before_action :authenticate_user!
  before_action :set_locale


  # Load helpers in rails engine
  helper LanguageEngine::Engine.helpers

  def set_locale
    # I18n.locale = session[:language] || I18n.default_locale
    # For testing different locales w/o restarting the server
    I18n.locale = (params['lang'] || session[:language] || I18n.default_locale).to_sym
    logger.info "locale set to #{I18n.locale.inspect}"
  end

  def pundit_user
    UserContext.new(current_user, referential: @referential)
  end

  protected

  def user_not_authorized
    render 'errors/forbidden', status: 403
  end

  def current_organisation
    current_user.organisation if current_user
  end
  helper_method :current_organisation

  def collection_name
    self.class.name.split("::").last.gsub('Controller', '').underscore
  end

  def decorated_collection
    if instance_variable_defined?("@#{collection_name}")
      instance_variable_get("@#{collection_name}")
    else
      nil
    end
  end
  helper_method :decorated_collection

  def begin_of_association_chain
    current_organisation
  end

  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    new_user_session_path
  end
end