Class: ErpUser
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ErpUser
- 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
Class Method Summary collapse
Instance Method Summary collapse
-
#allowed?(controller, action) ⇒ Boolean
Checks if the user is allowed to access a specific controller and action.
-
#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.
-
#create_login_activity(ip_address:, host_name:, status: 'I', remote_identity: '', remote_port: '', machine_id: '', session_id: 0, iis_session_id: '', memo: '') ⇒ void
Creates a login activity for the user.
-
#invite(by:) ⇒ void
Sends an invitation email to the user.
-
#invite_from_onboarding ⇒ void
Sends an onboarding invitation email to the user.
-
#language_description ⇒ String
Returns the description of the user’s language.
-
#locale ⇒ String
Returns the locale for the user.
-
#logout_acitvities ⇒ void
Logs out all active login activities for the user.
-
#name_from_email ⇒ String
Extracts the name from the user’s email.
-
#not_idle! ⇒ void
Checks if the user is not idle.
-
#tenant ⇒ Tenant?
Returns the tenant associated with the user.
-
#tenant_id ⇒ Integer?
Returns the tenant ID associated with the user.
-
#truncated_email ⇒ String
Returns the truncated email of the user.
-
#update_assigned_companies(company_ids: [], accessible_companies: tenant.companies, forbid_modification: false) ⇒ void
Updates the assigned companies for the user.
-
#update_last_api_call_at ⇒ void
Updates the last API call timestamp for the user.
- #validate_companies_presence ⇒ Object
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
.admin ⇒ Object
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.
258 259 260 |
# File 'app/models/erp_user.rb', line 258 def allowed?(controller, action) .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.
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.
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 create_login_activity( ip_address:, host_name:, status: 'I', remote_identity: '', remote_port: '', machine_id: '', session_id: 0, iis_session_id: '', memo: '' ) logout_acitvities user_login_activities.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.
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_onboarding ⇒ void
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_description ⇒ String
Returns 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 |
#locale ⇒ String
Returns 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_acitvities ⇒ void
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 user_login_activities.where( date_logout: nil, status: 'I' ).update_all( date_logout: Time.current, status: 'O' ) end |
#name_from_email ⇒ String
Extracts the name from the user’s 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.
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 |
#tenant ⇒ Tenant?
Returns the tenant associated with the user.
265 266 267 |
# File 'app/models/erp_user.rb', line 265 def tenant @tenant ||= companies.where.not(tenant_id: nil).first&.tenant end |
#tenant_id ⇒ Integer?
Returns the tenant ID associated with the user.
272 273 274 |
# File 'app/models/erp_user.rb', line 272 def tenant_id tenant&.tenant_id end |
#truncated_email ⇒ String
Returns the truncated email of the user.
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.
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_at ⇒ void
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_presence ⇒ Object
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 |