Class: ErpUser

Inherits:
ApplicationRecord show all
Includes:
ErpUserProcedures, HasPermissions
Defined in:
app/models/erp_user.rb

Overview

Represents an ERP user in the system.

Constant Summary collapse

MAX_IDLE_TIME =
15.minutes

Constants included from ErpUserProcedures

ErpUserProcedures::HEADERS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasPermissions

#active_permission_groups, #associate_permission_groups, #has_permission?, #has_permission_group?, #has_permissions?, #permission_codes, #update_assigned_permission_groups, #validate_permission_groups_presence

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Class Method Details

.adminObject



12
13
14
15
16
17
# File 'app/models/erp_user.rb', line 12

def admin
  find_by!(
    system_record: true,
    email: 'admin@namtek.ca'
  )
end

Instance Method Details

#allowed?(controller, action) ⇒ Boolean

Checks if the user is allowed to access a specific controller and action.

Parameters:

  • controller (String)

    the name of the controller

  • action (String)

    the name of the action

Returns:

  • (Boolean)

    true if the user is allowed, false otherwise



258
259
260
# File 'app/models/erp_user.rb', line 258

def allowed?(controller, action)
  navigation_configuration.allows?(controller, action)
end

#attribution_attributes(new_record: true, modification_date: :modification_date, modified_by: :modified_by, creation_date: :creation_date, created_by: :created_by) ⇒ Hash

Returns the attribution attributes for the user.

Examples:

user.attribution_attributes(new_record: true)

Parameters:

  • new_record (Boolean) (defaults to: true)

    whether the record is new

  • modification_date (Symbol) (defaults to: :modification_date)

    the attribute for the modification date

  • modified_by (Symbol) (defaults to: :modified_by)

    the attribute for the modified by

  • creation_date (Symbol) (defaults to: :creation_date)

    the attribute for the creation date

  • created_by (Symbol) (defaults to: :created_by)

    the attribute for the created by

Returns:

  • (Hash)

    the attribution attributes



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/erp_user.rb', line 120

def attribution_attributes(
  new_record: true,
  modification_date: :modification_date,
  modified_by: :modified_by,
  creation_date: :creation_date,
  created_by: :created_by
)
  date = Time.now
  if new_record
    {
      creation_date => date,
      created_by => truncated_email,
      modification_date => date,
      modified_by => truncated_email
    }
  else
    {
      modification_date => date,
      modified_by => truncated_email
    }
  end
end

#create_login_activity(ip_address:, host_name:, status: 'I', remote_identity: '', remote_port: '', machine_id: '', session_id: 0, iis_session_id: '', memo: '') ⇒ void

This method returns an undefined value.

Creates a login activity for the user.

Examples:

user.(ip_address: '192.168.1.1', host_name: 'localhost')

Parameters:

  • ip_address (String)

    the IP address of the user

  • host_name (String)

    the host name of the user

  • status (String) (defaults to: 'I')

    the status of the login activity (default: ‘I’)

  • remote_identity (String) (defaults to: '')

    the remote identity of the user (default: ”)

  • remote_port (String) (defaults to: '')

    the remote port of the user (default: ”)

  • machine_id (String) (defaults to: '')

    the machine ID of the user (default: ”)

  • session_id (Integer) (defaults to: 0)

    the session ID of the user (default: 0)

  • iis_session_id (String) (defaults to: '')

    the IIS session ID of the user (default: ”)

  • memo (String) (defaults to: '')

    the memo for the login activity (default: ”)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/models/erp_user.rb', line 200

def (
  ip_address:, host_name:, status: 'I',
  remote_identity: '',
  remote_port: '',
  machine_id: '',
  session_id: 0,
  iis_session_id: '',
  memo: ''
)
  logout_acitvities
  .create!(
    user_code: user_code || '',
    date_login: Time.current,
    status: status,
    ip_address: ip_address,
    host_name: host_name,
    remote_identity: remote_identity,
    remote_port: remote_port,
    machine_id: machine_id,
    session_id: session_id,
    iis_session_id: iis_session_id,
    memo: memo
  )
end

#invite(by:) ⇒ void

This method returns an undefined value.

Sends an invitation email to the user.

Parameters:

  • by (ErpUser)

    the user sending the invitation



242
243
244
# File 'app/models/erp_user.rb', line 242

def invite(by:)
  ErpUserMailer.invitation(user: self, by: by).deliver_now
end

#invite_from_onboardingvoid

This method returns an undefined value.

Sends an onboarding invitation email to the user.



249
250
251
# File 'app/models/erp_user.rb', line 249

def invite_from_onboarding
  ErpUserMailer.onboarding_invitation(user: self).deliver_now
end

#language_descriptionString

Returns the description of the user’s language.

Returns:

  • (String)

    the description of the user’s language



83
84
85
# File 'app/models/erp_user.rb', line 83

def language_description
  Language.from_locale(locale).description
end

#localeString

Returns the locale for the user.

Returns:

  • (String)

    the locale for the user



74
75
76
77
78
# File 'app/models/erp_user.rb', line 74

def locale
  return Language.new(user_language).locale if user_language.present?

  companies&.first&.locale || 'fr'
end

#logout_acitvitiesvoid

This method returns an undefined value.

Logs out all active login activities for the user.



228
229
230
231
232
233
234
235
236
# File 'app/models/erp_user.rb', line 228

def logout_acitvities
  .where(
    date_logout: nil,
    status: 'I'
  ).update_all(
    date_logout: Time.current,
    status: 'O'
  )
end

#name_from_emailString

Extracts the name from the user’s email.

Returns:

  • (String)

    the name extracted from the email



67
68
69
# File 'app/models/erp_user.rb', line 67

def name_from_email
  email&.split('@')&.first
end

#not_idle!void

This method returns an undefined value.

Checks if the user is not idle.

Raises:

  • (RuntimeError)

    if the user is idle



98
99
100
101
# File 'app/models/erp_user.rb', line 98

def not_idle!
  return unless last_api_call_at
  raise 'Idle User' if last_api_call_at < MAX_IDLE_TIME.ago
end

#tenantTenant?

Returns the tenant associated with the user.

Returns:

  • (Tenant, nil)

    the tenant associated with the user, or nil if none exists



265
266
267
# File 'app/models/erp_user.rb', line 265

def tenant
  @tenant ||= companies.where.not(tenant_id: nil).first&.tenant
end

#tenant_idInteger?

Returns the tenant ID associated with the user.

Returns:

  • (Integer, nil)

    the tenant ID associated with the user, or nil if none exists



272
273
274
# File 'app/models/erp_user.rb', line 272

def tenant_id
  tenant&.tenant_id
end

#truncated_emailString

Returns the truncated email of the user.

Returns:

  • (String)

    the first 20 characters of the user’s email



106
107
108
# File 'app/models/erp_user.rb', line 106

def truncated_email
  email.first(20)
end

#update_assigned_companies(company_ids: [], accessible_companies: tenant.companies, forbid_modification: false) ⇒ void

This method returns an undefined value.

Updates the assigned companies for the user.

Examples:

user.update_assigned_companies(company_ids: [1, 2, 3])

Parameters:

  • company_ids (Array<Integer>) (defaults to: [])

    the IDs of the companies to assign

  • accessible_companies (ActiveRecord::Relation) (defaults to: tenant.companies)

    the accessible companies

  • forbid_modification (Boolean) (defaults to: false)

    whether to forbid modification



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/erp_user.rb', line 151

def update_assigned_companies(
  company_ids: [],
  accessible_companies: tenant.companies,
  forbid_modification: false
)
  ActiveRecord::Base.transaction do
    company_ids = company_ids.map(&:to_i)
    accessible_company_ids = accessible_companies.select(:company_id)

    unless (company_ids & accessible_company_ids.pluck(:company_id)) == company_ids
      errors.add(:companies, 'must be accessible')
      raise ActiveRecord::RecordInvalid.new(self)
    end

    current_company_ids = companies.select(:company_id).pluck(:company_id)
    to_add_company_ids = company_ids - current_company_ids
    to_remove_company_ids = current_company_ids - company_ids
    return unless to_add_company_ids.present? || to_remove_company_ids.present?

    if forbid_modification
      errors.add(:companies, 'cannot be changed')
      raise ActiveRecord::RecordInvalid.new(self)
    end

    to_add_company_ids.each do |company_id|
      company_assigned_users.find_or_create_by!(company_id: company_id, active: true)
    end

    to_remove_company_ids.each do |company_id|
      company_assigned_user = company_assigned_users.find_by(company_id: company_id)
      company_assigned_user.destroy if company_assigned_user
    end
  end
end

#update_last_api_call_atvoid

This method returns an undefined value.

Updates the last API call timestamp for the user.



90
91
92
# File 'app/models/erp_user.rb', line 90

def update_last_api_call_at
  update(last_api_call_at: Time.current)
end

#validate_companies_presenceObject



276
277
278
279
280
# File 'app/models/erp_user.rb', line 276

def validate_companies_presence
  return unless companies.empty?

  errors.add(:companies, 'must be present')
end