Module: AuthentificationHelper

Includes:
ApplicationHelper
Included in:
ApplicationCable::Channel, ApplicationController
Defined in:
app/helpers/authentification_helper.rb

Instance Method Summary collapse

Instance Method Details

#accessible_company_ids_from_tenant_and_user(user:, tenant:) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/helpers/authentification_helper.rb', line 37

def accessible_company_ids_from_tenant_and_user(
  user:,
  tenant:
)
  (
    user.system_record ? tenant.companies : user.companies
  ).where(active: true).pluck(:company_id)
end

#application_mode_from_hostname(referer_hostname:) ⇒ Object



20
21
22
23
24
# File 'app/helpers/authentification_helper.rb', line 20

def application_mode_from_hostname(
  referer_hostname:
)
  AppMode.from_referer_host(referer_hostname)
end

#current_company_id_from_accessible_company_ids(accessible_company_ids: [], company_id: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/authentification_helper.rb', line 46

def current_company_id_from_accessible_company_ids(
  accessible_company_ids: [],
  company_id: nil
)
  return nil if accessible_company_ids.blank?
  return accessible_company_ids.first if company_id.blank?

  if accessible_company_ids.include?(company_id)
    company_id
  else
    accessible_company_ids.first
  end
end

#current_tenant_from_user(user:, system_user_tenant_id:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/authentification_helper.rb', line 26

def current_tenant_from_user(
  user:,
  system_user_tenant_id:
)
  if user.system_record
    Tenant.find_by(tenant_id: system_user_tenant_id) || user.tenant
  else
    user.tenant
  end
end

#current_user_and_locale_from_token(authorization_header: nil, token: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/authentification_helper.rb', line 4

def current_user_and_locale_from_token(
  authorization_header: nil,
  token: nil
)
  token_from_header = token || authorization_header.split(' ').last
  decoded_token = Auth::Token.decode(token: token_from_header, purposes: [Auth::Token::PURPOSES[:access_ressources]])

  current_user = ErpUser.find(decoded_token[:user_id])
  current_user.revoked_tokens.exists?(token: token_from_header)
  # @current_user.not_idle!
  # current_user.update_last_api_call_at
  I18n.locale = current_user.locale

  current_user
end