Class: Maquina::UsedPassword

Inherits:
ApplicationRecord show all
Defined in:
app/models/maquina/used_password.rb

Overview

A class representing the UsedPassword model in the Maquina module. Keeps track of previously used passwords for users to prevent password reuse.

Attributes

  • password_digest

    Encrypted password digest (required).

  • maquina_user_id

    Foreign key reference to the associated User.

Associations

  • user

    Belongs to a User.

Encryption

  • password_digest

    Value is encrypted at rest in the database.

Class Method Summary collapse

Methods inherited from ApplicationRecord

searchable?

Class Method Details

.store_password_digest(user_id, password_digest) ⇒ Object

Stores a new password digest for a user while maintaining password history limit. If password retention is disabled via configuration, no storage occurs.

Args:

  • user_id

    Integer representing the ID of the user

  • password_digest

    String of encrypted password digest to store

Returns:

  • nil

    If password retention is disabled (count is nil or 0)

  • Maquina::UsedPassword

    The newly created used password record

Example:

Maquina::UsedPassword.store_password_digest(1, "digest123")


40
41
42
43
44
45
# File 'app/models/maquina/used_password.rb', line 40

def self.store_password_digest(user_id, password_digest)
  return if Maquina.configuration.password_retain_count.blank? || Maquina.configuration.password_retain_count.zero?

  Maquina::UsedPassword.where(user: user_id).order(id: :desc).offset(2).delete_all
  Maquina::UsedPassword.create(maquina_user_id: user_id, password_digest: password_digest)
end