1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/user.rb

358 lines
10 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# User is a physical or moral person with its authentication parameters
2020-03-30 11:33:12 +02:00
# It is linked to the Profile model with hold information about this person (like address, name, etc.)
2020-03-25 10:16:47 +01:00
class User < ApplicationRecord
2015-05-05 03:10:25 +02:00
include NotifyWith::NotificationReceiver
include NotifyWith::NotificationAttachedObject
include SingleSignOnConcern
2015-05-05 03:10:25 +02:00
# Include default devise modules. Others available are:
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,
:confirmable
2020-05-19 11:45:51 +02:00
rolify
2015-05-05 03:10:25 +02:00
extend FriendlyId
friendly_id :username, use: :slugged
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
has_one :invoicing_profile, dependent: :nullify
accepts_nested_attributes_for :invoicing_profile
2019-06-03 17:25:04 +02:00
has_one :statistic_profile, dependent: :nullify
accepts_nested_attributes_for :statistic_profile
2015-05-05 03:10:25 +02:00
has_many :project_users, dependent: :destroy
has_many :projects, through: :project_users
belongs_to :group
2016-03-23 18:39:41 +01:00
has_many :users_credits, dependent: :destroy
has_many :credits, through: :users_credits
has_many :training_credits, through: :users_credits, source: :training_credit
has_many :machine_credits, through: :users_credits, source: :machine_credit
has_many :user_tags, dependent: :destroy
has_many :tags, through: :user_tags
accepts_nested_attributes_for :tags, allow_destroy: true
has_many :exports, dependent: :destroy
2019-09-25 16:37:42 +02:00
has_many :imports, dependent: :nullify
has_one :payment_gateway_object, as: :item
has_many :accounting_periods, foreign_key: 'closed_by', dependent: :nullify
has_many :proof_of_identity_files, dependent: :destroy
has_many :proof_of_identity_refusals, dependent: :destroy
2016-03-23 18:39:41 +01:00
# fix for create admin user
before_save do
email&.downcase!
2016-03-23 18:39:41 +01:00
end
2015-05-05 03:10:25 +02:00
before_create :assign_default_role
after_commit :create_gateway_customer, on: [:create]
2016-03-23 18:39:41 +01:00
after_commit :notify_admin_when_user_is_created, on: :create
after_create :init_dependencies
after_update :update_invoicing_profile, if: :invoicing_data_was_modified?
after_update :update_statistic_profile, if: :statistic_data_was_modified?
before_destroy :remove_orphan_drafts
2015-05-05 03:10:25 +02:00
attr_accessor :cgu
2016-03-23 18:39:41 +01:00
delegate :first_name, to: :profile
delegate :last_name, to: :profile
delegate :subscriptions, to: :statistic_profile
delegate :reservations, to: :statistic_profile
delegate :trainings, to: :statistic_profile
2019-06-06 16:34:53 +02:00
delegate :my_projects, to: :statistic_profile
2021-06-21 17:39:48 +02:00
delegate :prepaid_packs, to: :statistic_profile
delegate :wallet, to: :invoicing_profile
delegate :wallet_transactions, to: :invoicing_profile
delegate :invoices, to: :invoicing_profile
2015-05-05 03:10:25 +02:00
validate :cgu_must_accept, if: :new_record?
validates :username, presence: true, uniqueness: true, length: { maximum: 30 }
validate :password_complexity
2015-05-05 03:10:25 +02:00
2016-03-23 18:39:41 +01:00
scope :active, -> { where(is_active: true) }
scope :without_subscription, -> { includes(statistic_profile: [:subscriptions]).where(subscriptions: { statistic_profile_id: nil }) }
scope :with_subscription, -> { joins(statistic_profile: [:subscriptions]) }
scope :not_confirmed, -> { where(confirmed_at: nil) }
2020-01-07 10:44:29 +01:00
scope :inactive_for_3_years, -> { where('users.last_sign_in_at < ?', 3.years.ago) }
2016-03-23 18:39:41 +01:00
def to_json(*)
ApplicationController.new.view_context.render(
partial: 'api/members/member',
locals: { member: self },
formats: [:json],
handlers: [:jbuilder]
)
2015-05-05 03:10:25 +02:00
end
def self.admins
User.with_role(:admin)
end
def self.members
User.with_role(:member)
end
def self.partners
User.with_role(:partner)
end
def self.managers
User.with_role(:manager)
end
2020-04-29 15:34:30 +02:00
def self.admins_and_managers
User.with_any_role(:admin, :manager)
end
def self.online_payers
User.with_any_role(:manager, :member)
end
def self.adminsys
return unless Rails.application.secrets.adminsys_email.present?
User.find_by('lower(email) = ?', Rails.application.secrets.adminsys_email&.downcase)
end
def training_machine?(machine)
return true if admin? || manager?
trainings.map(&:machines).flatten.uniq.include?(machine)
2016-03-23 18:39:41 +01:00
end
2021-06-29 11:14:36 +02:00
def packs?(item)
2021-06-25 17:24:34 +02:00
return true if admin?
2021-06-29 11:14:36 +02:00
PrepaidPackService.user_packs(self, item).count.positive?
2021-06-25 17:24:34 +02:00
end
2021-06-15 17:34:12 +02:00
def next_training_reservation_by_machine(machine)
reservations.where(reservable_type: 'Training', reservable_id: machine.trainings.map(&:id))
.includes(:slots)
.where('slots.start_at>= ?', DateTime.current)
.order('slots.start_at': :asc)
.references(:slots)
.limit(1)
.first
2016-03-23 18:39:41 +01:00
end
def subscribed_plan
return nil if subscription.nil? || subscription.expired_at < DateTime.current
2016-03-23 18:39:41 +01:00
subscription.plan
end
def subscription
subscriptions.order(:created_at).last
2016-03-23 18:39:41 +01:00
end
def admin?
2015-05-05 03:10:25 +02:00
has_role? :admin
end
def member?
2015-05-05 03:10:25 +02:00
has_role? :member
end
def manager?
has_role? :manager
end
def partner?
has_role? :partner
end
2021-06-29 11:14:36 +02:00
def privileged?
admin? || manager?
end
def role
if admin?
'admin'
elsif manager?
'manager'
2020-05-04 18:32:25 +02:00
elsif member?
'member'
else
'other'
end
end
2015-05-05 03:10:25 +02:00
def all_projects
my_projects.to_a.concat projects
end
def generate_subscription_invoice(operator_profile_id)
return unless subscription
subscription.generate_and_save_invoice(operator_profile_id)
2016-03-23 18:39:41 +01:00
end
def active_for_authentication?
super && is_active?
2016-03-23 18:39:41 +01:00
end
def need_completion?
statistic_profile.gender.nil? || profile.first_name.blank? || profile.last_name.blank? || username.blank? ||
email.blank? || encrypted_password.blank? || group_id.nil? || statistic_profile.birthday.blank? ||
2021-03-22 18:02:56 +01:00
(Setting.get('phone_required') && profile.phone.blank?) ||
(Setting.get('address_required') && invoicing_profile.address&.address&.blank?)
2016-03-23 18:39:41 +01:00
end
def self.mapping
# we protect some fields as they are designed to be managed by the system and must not be updated externally
blacklist = %w[id encrypted_password reset_password_token reset_password_sent_at remember_created_at
sign_in_count current_sign_in_at last_sign_in_at current_sign_in_ip last_sign_in_ip confirmation_token
confirmed_at confirmation_sent_at unconfirmed_email failed_attempts unlock_token locked_at created_at
updated_at slug provider auth_token merged_at]
User.columns_hash
.map { |k, v| [k, v.type.to_s] }
.delete_if { |col| blacklist.include?(col[0]) }
end
2020-05-19 11:45:51 +02:00
# will update the statistic_profile after a group switch or a role update
def update_statistic_profile
raise NoProfileError if statistic_profile.nil? || statistic_profile.id.nil?
statistic_profile.update_attributes(
group_id: group_id,
role_id: roles.first.id
)
end
2021-04-13 17:16:05 +02:00
def organization?
!invoicing_profile.organization.nil?
end
def notify_group_changed(ex_group, user_invalidated)
meta_data = { ex_group_name: ex_group.name, user_invalidated: user_invalidated }
NotificationCenter.call type: :notify_admin_user_group_changed,
receiver: User.admins_and_managers,
attached_object: self,
meta_data: meta_data
NotificationCenter.call type: :notify_user_user_group_changed,
receiver: self,
attached_object: self,
meta_data: meta_data
end
2016-03-23 18:39:41 +01:00
protected
2020-04-28 12:48:03 +02:00
# remove projects drafts that are not linked to another user
def remove_orphan_drafts
orphans = my_projects
.joins('LEFT JOIN project_users ON projects.id = project_users.project_id')
.where('project_users.project_id IS NULL')
.where(state: 'draft')
orphans.map(&:destroy!)
end
2016-03-23 18:39:41 +01:00
def confirmation_required?
Setting.get('confirmation_required') ? super : false
2016-03-23 18:39:41 +01:00
end
2015-05-05 03:10:25 +02:00
private
2015-05-05 03:10:25 +02:00
def assign_default_role
add_role(:member) if roles.blank?
2015-05-05 03:10:25 +02:00
end
2016-03-23 18:39:41 +01:00
def cached_has_role?(role)
roles = Rails.cache.fetch(
roles_for: { object_id: object_id },
expires_in: 1.day,
race_condition_ttl: 2.seconds
) { roles.map(&:name) }
2016-03-23 18:39:41 +01:00
roles.include?(role.to_s)
end
2015-05-05 03:10:25 +02:00
def cgu_must_accept
errors.add(:cgu, I18n.t('activerecord.errors.messages.empty')) if cgu == '0'
end
def create_gateway_customer
PaymentGatewayService.new.create_user(id)
2016-03-23 18:39:41 +01:00
end
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end
2015-05-05 03:10:25 +02:00
def notify_admin_when_user_is_created
if need_completion? && !provider.nil?
2016-03-23 18:39:41 +01:00
NotificationCenter.call type: 'notify_admin_when_user_is_imported',
receiver: User.admins,
attached_object: self
else
NotificationCenter.call type: 'notify_admin_when_user_is_created',
2020-04-29 15:34:30 +02:00
receiver: User.admins_and_managers,
2016-03-23 18:39:41 +01:00
attached_object: self
end
end
2019-06-03 17:25:04 +02:00
def invoicing_data_was_modified?
saved_change_to_email?
2019-06-03 17:25:04 +02:00
end
def statistic_data_was_modified?
saved_change_to_group_id?
end
def init_dependencies
if invoicing_profile.nil?
ip = InvoicingProfile.create!(
user: self,
email: email,
first_name: first_name,
last_name: last_name
)
else
update_invoicing_profile
end
if wallet.nil?
ip ||= invoicing_profile
Wallet.create!(
invoicing_profile: ip
)
end
if statistic_profile.nil?
StatisticProfile.create!(
user: self,
group_id: group_id,
role_id: roles.first.id
)
else
update_statistic_profile
end
2019-06-03 17:25:04 +02:00
end
def update_invoicing_profile
raise NoProfileError if invoicing_profile.nil?
invoicing_profile.update_attributes(
email: email,
first_name: first_name,
last_name: last_name
)
end
def password_complexity
return if password.blank? || SecurePassword.is_secured?(password)
errors.add I18n.t("app.public.common.password_is_too_weak"), I18n.t("app.public.common.password_is_too_weak_explanations")
end
2015-05-05 03:10:25 +02:00
end