Upgrade rails to 7.2.2 so that we can proceed with the rails 8 upgrade afterwards # Changelog - `.circleci/config.yml` — align CI DB setup with GitHub Actions (`db:create` + `db:schema:load`) to avoid trigger-dependent prep steps. - `.rubocop.yml` — add `rubocop-rspec_rails` and disable new cops that don't match existing spec style. - `AGENTS.md` — document that specs should run without `.env` (rename temporarily when present). - `Gemfile` — upgrade to Rails 7.2, switch Azure storage gem, pin `commonmarker`, bump `sidekiq-cron`, add `rubocop-rspec_rails`, and relax some gem pins. - `Gemfile.lock` — dependency lockfile updates from the Rails 7.2 and gem changes. - `app/controllers/api/v1/accounts/integrations/linear_controller.rb` — stringify params before passing to the Linear service to keep key types stable. - `app/controllers/super_admin/instance_statuses_controller.rb` — use `MigrationContext` API for migration status in Rails 7.2. - `app/models/installation_config.rb` — add commentary on YAML serialization and future JSONB migration (no behavior change). - `app/models/integrations/hook.rb` — ensure hook type is set on create only and guard against missing app. - `app/models/user.rb` — update enum syntax for Rails 7.2 deprecation, serialize OTP backup codes with JSON, and use Ruby `alias`. - `app/services/crm/leadsquared/setup_service.rb` — stringify hook settings keys before merge to keep JSON shape consistent. - `app/services/macros/execution_service.rb` — remove macro-specific assignee activity workaround; rely on standard assignment handlers. - `config/application.rb` — load Rails 7.2 defaults. - `config/storage.yml` — update Azure Active Storage service name to `AzureBlob`. - `db/migrate/20230515051424_update_article_image_keys.rb` — use credentials `secret_key_base` with fallback to legacy secrets. - `docker/Dockerfile` — add `yaml-dev` and `pkgconf` packages for native extensions (Ruby 3.4 / psych). - `lib/seeders/reports/message_creator.rb` — add parentheses for clarity in range calculation. - `package.json` — pin Vite version and bump `vite-plugin-ruby`. - `pnpm-lock.yaml` — lockfile changes from JS dependency updates. - `spec/builders/v2/report_builder_spec.rb` — disable transactional fixtures; truncate tables per example via Rails `truncate_tables` so after_commit callbacks run with clean isolation; keep builder spec metadata minimal. - `spec/builders/v2/reports/label_summary_builder_spec.rb` — disable transactional fixtures + truncate tables via Rails `truncate_tables`; revert to real `resolved!`/`open!`/`resolved!` flow for multiple resolution events; align date range to `Time.zone` to avoid offset gaps; keep builder spec metadata minimal. - `spec/controllers/api/v1/accounts/macros_controller_spec.rb` — assert `assignee_id` instead of activity message to avoid transaction-timing flakes. - `spec/services/telegram/incoming_message_service_spec.rb` — reference the contact tied to the created conversation instead of `Contact.all.first` to avoid order-dependent failures when other specs leave data behind. - `spec/mailers/administrator_notifications/shared/smtp_config_shared.rb` — use `with_modified_env` instead of stubbing mailer internals. - `spec/services/account/sign_up_email_validation_service_spec.rb` — compare error `class.name` for parallel/reload-safe assertions.
204 lines
6.9 KiB
Ruby
204 lines
6.9 KiB
Ruby
# == Schema Information
|
|
#
|
|
# Table name: users
|
|
#
|
|
# id :integer not null, primary key
|
|
# availability :integer default("online")
|
|
# confirmation_sent_at :datetime
|
|
# confirmation_token :string
|
|
# confirmed_at :datetime
|
|
# consumed_timestep :integer
|
|
# current_sign_in_at :datetime
|
|
# current_sign_in_ip :string
|
|
# custom_attributes :jsonb
|
|
# display_name :string
|
|
# email :string
|
|
# encrypted_password :string default(""), not null
|
|
# last_sign_in_at :datetime
|
|
# last_sign_in_ip :string
|
|
# message_signature :text
|
|
# name :string not null
|
|
# otp_backup_codes :text
|
|
# otp_required_for_login :boolean default(FALSE)
|
|
# otp_secret :string
|
|
# provider :string default("email"), not null
|
|
# pubsub_token :string
|
|
# remember_created_at :datetime
|
|
# reset_password_sent_at :datetime
|
|
# reset_password_token :string
|
|
# sign_in_count :integer default(0), not null
|
|
# tokens :json
|
|
# type :string
|
|
# ui_settings :jsonb
|
|
# uid :string default(""), not null
|
|
# unconfirmed_email :string
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_users_on_email (email)
|
|
# index_users_on_otp_required_for_login (otp_required_for_login)
|
|
# index_users_on_otp_secret (otp_secret) UNIQUE
|
|
# index_users_on_pubsub_token (pubsub_token) UNIQUE
|
|
# index_users_on_reset_password_token (reset_password_token) UNIQUE
|
|
# index_users_on_uid_and_provider (uid,provider) UNIQUE
|
|
#
|
|
|
|
class User < ApplicationRecord
|
|
include AccessTokenable
|
|
include Avatarable
|
|
# Include default devise modules.
|
|
include DeviseTokenAuth::Concerns::User
|
|
include Pubsubable
|
|
include Rails.application.routes.url_helpers
|
|
include Reportable
|
|
include SsoAuthenticatable
|
|
include UserAttributeHelpers
|
|
|
|
devise :database_authenticatable,
|
|
:registerable,
|
|
:recoverable,
|
|
:rememberable,
|
|
:trackable,
|
|
:validatable,
|
|
:confirmable,
|
|
:password_has_required_content,
|
|
:two_factor_authenticatable,
|
|
:omniauthable, omniauth_providers: [:google_oauth2, :saml]
|
|
|
|
# TODO: remove in a future version once online status is moved to account users
|
|
# remove the column availability from users
|
|
enum :availability, { online: 0, offline: 1, busy: 2 }
|
|
|
|
# The validation below has been commented out as it does not
|
|
# work because :validatable in devise overrides this.
|
|
# validates_uniqueness_of :email, scope: :account_id
|
|
|
|
validates :email, presence: true
|
|
|
|
serialize :otp_backup_codes, coder: JSON, type: Array
|
|
|
|
# Encrypt sensitive MFA fields
|
|
encrypts :otp_secret, deterministic: true
|
|
encrypts :otp_backup_codes
|
|
|
|
has_many :account_users, dependent: :destroy_async
|
|
has_many :accounts, through: :account_users
|
|
accepts_nested_attributes_for :account_users
|
|
|
|
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify, inverse_of: :assignee
|
|
alias conversations assigned_conversations
|
|
has_many :csat_survey_responses, foreign_key: 'assigned_agent_id', dependent: :nullify, inverse_of: :assigned_agent
|
|
has_many :reviewed_csat_survey_responses, foreign_key: 'review_notes_updated_by_id', class_name: 'CsatSurveyResponse',
|
|
dependent: :nullify, inverse_of: :review_notes_updated_by
|
|
has_many :conversation_participants, dependent: :destroy_async
|
|
has_many :participating_conversations, through: :conversation_participants, source: :conversation
|
|
|
|
has_many :inbox_members, dependent: :destroy_async
|
|
has_many :inboxes, through: :inbox_members, source: :inbox
|
|
has_many :messages, as: :sender, dependent: :nullify
|
|
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', source: :inviter, dependent: :nullify
|
|
|
|
has_many :custom_filters, dependent: :destroy_async
|
|
has_many :dashboard_apps, dependent: :nullify
|
|
has_many :mentions, dependent: :destroy_async
|
|
has_many :notes, dependent: :nullify
|
|
has_many :notification_settings, dependent: :destroy_async
|
|
has_many :notification_subscriptions, dependent: :destroy_async
|
|
has_many :notifications, dependent: :destroy_async
|
|
has_many :team_members, dependent: :destroy_async
|
|
has_many :teams, through: :team_members
|
|
has_many :articles, foreign_key: 'author_id', dependent: :nullify, inverse_of: :author
|
|
# rubocop:disable Rails/HasManyOrHasOneDependent
|
|
# we are handling this in `remove_macros` callback
|
|
has_many :macros, foreign_key: 'created_by_id', inverse_of: :created_by
|
|
# rubocop:enable Rails/HasManyOrHasOneDependent
|
|
|
|
before_validation :set_password_and_uid, on: :create
|
|
after_destroy :remove_macros
|
|
|
|
scope :order_by_full_name, -> { order('lower(name) ASC') }
|
|
|
|
before_validation do
|
|
self.email = email.try(:downcase)
|
|
end
|
|
|
|
def send_devise_notification(notification, *)
|
|
devise_mailer.with(account: Current.account).send(notification, self, *).deliver_later
|
|
end
|
|
|
|
def set_password_and_uid
|
|
self.uid = email
|
|
end
|
|
|
|
def assigned_inboxes
|
|
administrator? ? Current.account.inboxes : inboxes.where(account_id: Current.account.id)
|
|
end
|
|
|
|
def serializable_hash(options = nil)
|
|
super(options).merge(confirmed: confirmed?)
|
|
end
|
|
|
|
def push_event_data
|
|
{
|
|
id: id,
|
|
name: name,
|
|
available_name: available_name,
|
|
avatar_url: avatar_url,
|
|
type: 'user',
|
|
availability_status: availability_status,
|
|
thumbnail: avatar_url
|
|
}
|
|
end
|
|
|
|
def webhook_data
|
|
{
|
|
id: id,
|
|
name: name,
|
|
email: email,
|
|
type: 'user'
|
|
}
|
|
end
|
|
|
|
# https://github.com/lynndylanhurley/devise_token_auth/blob/6d7780ee0b9750687e7e2871b9a1c6368f2085a9/app/models/devise_token_auth/concerns/user.rb#L45
|
|
# Since this method is overriden in devise_token_auth it breaks the email reconfirmation flow.
|
|
def will_save_change_to_email?
|
|
mutations_from_database.changed?('email')
|
|
end
|
|
|
|
def self.from_email(email)
|
|
find_by(email: email&.downcase)
|
|
end
|
|
|
|
# 2FA/MFA Methods
|
|
# Delegated to Mfa::ManagementService for better separation of concerns
|
|
def mfa_service
|
|
@mfa_service ||= Mfa::ManagementService.new(user: self)
|
|
end
|
|
|
|
delegate :two_factor_provisioning_uri, to: :mfa_service
|
|
delegate :backup_codes_generated?, to: :mfa_service
|
|
delegate :enable_two_factor!, to: :mfa_service
|
|
delegate :disable_two_factor!, to: :mfa_service
|
|
delegate :generate_backup_codes!, to: :mfa_service
|
|
delegate :validate_backup_code!, to: :mfa_service
|
|
|
|
def mfa_enabled?
|
|
otp_required_for_login?
|
|
end
|
|
|
|
def mfa_feature_available?
|
|
Chatwoot.mfa_enabled?
|
|
end
|
|
|
|
private
|
|
|
|
def remove_macros
|
|
macros.personal.destroy_all
|
|
end
|
|
end
|
|
|
|
User.include_mod_with('Audit::User')
|
|
User.include_mod_with('Concerns::User')
|