Skip to main content

User Model

The UsuarioPOJO represents a user entity in the e-commerce system. It encapsulates user identification, username, and secure password management through value objects.

Model Structure

{
  "id": 12345,
  "usuario": "[email protected]",
  "contrasena": {
    "value": "SecurePass123"
  }
}

Fields

id
Long
required
Unique identifier for the user.
usuario
String
required
Username or email address for the user account.
contrasena
ContrasenaVO
required
Password value object with built-in validation and security measures.

Password Validation

The ContrasenaVO value object enforces password security through constructor validation. Any attempt to create a password that doesn’t meet requirements will throw an InvalidPasswordException.

Validation Rules

The password must satisfy ALL of the following conditions:
RuleRequirementException Message
Not EmptyPassword cannot be null or empty”La contraseña no puede estar vacía”
Minimum LengthAt least 8 characters”La contraseña debe tener al menos 8 caracteres”
Maximum LengthNo more than 64 characters”La contraseña no puede superar los 64 caracteres”
ComplexityAt least one letter AND one digit”La contraseña debe contener al menos una letra y un número”

Valid Password Examples

// Valid passwords
"Password1"
"mySecure123"
"Admin2024!"
"Test1234567890"

Invalid Password Examples

// Too short (< 8 characters)
"Pass1"  // InvalidPasswordException

// No digits
"PasswordOnly"  // InvalidPasswordException

// No letters
"12345678"  // InvalidPasswordException

// Empty
""  // InvalidPasswordException

// Too long (> 64 characters)
"ThisPasswordIsWayTooLongAndExceedsTheSixtyFourCharacterLimitSetByTheSystem123"  // InvalidPasswordException

Creating a User

{
  "usuario": "[email protected]",
  "contrasena": {
    "value": "StrongPass456"
  }
}

Updating a Password

The ContrasenaVO provides an actualizarContrasena() method that creates a new password value object:
ContrasenaVO currentPassword = usuario.getContrasena();
ContrasenaVO newPassword = currentPassword.actualizarContrasena("NewSecure789");
usuario.setContrasena(newPassword);
Password value objects are immutable. The actualizarContrasena() method returns a new ContrasenaVO instance rather than modifying the existing one.

Security Considerations

  • Passwords are masked in string representations (toString() returns "****")
  • Store password hashes in the database, never plain text values
  • The model enforces validation but does not handle hashing
  • Implement proper password hashing (BCrypt, Argon2) in your service layer

Source Code Reference

  • UsuarioPOJO: src/main/java/com/example/demo/usuario/domain/models/UsuarioPOJO.java:14
  • ContrasenaVO: src/main/java/com/example/demo/usuario/domain/vo/ContrasenaVO.java:8

Build docs developers (and LLMs) love