Overview
TheUsuarioController manages user accounts for dental clinic staff within the DentControl SaaS platform. This controller is exclusively for superadmin use and handles CRUD operations for creating, updating, and managing users with roles of dentista (dentist) and asistente (assistant). It includes comprehensive validation, password security rules, and status management.
Location: app/Http/Controllers/Admin/UsuarioController.php
Namespace: App\Http\Controllers\Admin
Methods
index()
Displays a list of all users with their associated clinic information. Route:GET /usuarios
Route Name: usuarios.index
Middleware:
auth- User must be authenticatedcan:admin-only- User must have superadmin role
admin.usuarios.index Blade view with:
Collection of all users with eager-loaded clinic relationships
Collection of active clinics (
estatus = 'activo') for the creation form- Eager loads
clinicarelationship to avoid N+1 queries - Only displays active clinics in the selection dropdown
store()
Creates a new user with comprehensive validation and automatic password hashing. Route:POST /usuarios
Route Name: usuarios.store
Middleware:
authcan:admin-only
ID of the clinic this user belongs to (must exist in
clinica table)First name (3-255 chars, letters and spaces only, includes Spanish characters)
Paternal last name (3-255 chars, letters and spaces only)
Maternal last name (3-255 chars, letters and spaces only)
Username (4-20 chars, alphanumeric only, unique)
Password with security requirements:
- Minimum 8 characters
- At least one letter
- Mixed case (uppercase and lowercase)
- At least one number
User role:
dentista or asistenteProfessional license number (7-10 digits, required if
rol = 'dentista')nombre.regex: “El nombre solo puede contener letras y espacios.”apellido_paterno.regex: “El apellido paterno solo puede contener letras y espacios.”nombre.min: “El nombre debe tener al menos 3 letras.”password.min: “La contraseña debe tener al menos 8 caracteres.”password.letters: “La contraseña debe incluir al menos una letra.”password.mixed_case: “La contraseña debe tener mayúsculas y minúsculas.”password.numbers: “La contraseña debe incluir al menos un número.”
usuarios.index with success message: “Usuario creado con éxito.”
Password Handling:
- Password is automatically hashed via model cast (
'hashed'cast onpasswordattribute) - Plain text password is never stored
edit()
Retrieves a specific user’s data for editing (AJAX endpoint). Route:GET /usuarios/{id}/edit
Route Name: usuarios.edit
Middleware:
authcan:admin-only
User ID (
id_usuario)- 404 Not Found if user doesn’t exist
update()
Updates an existing user with validation and optional password change. Route:PUT /usuarios/{id}
Route Name: usuarios.update
Middleware:
authcan:admin-only
User ID (
id_usuario)Clinic ID
First name
Paternal last name
Maternal last name
Username (unique, excluding current user)
New password (only validated if provided). Same security requirements as store.
User role:
superadmin, dentista, or asistente- If user being edited has
rol = 'superadmin':- Role is forced to remain
'superadmin'(cannot be downgraded) - This prevents accidental lockout of superadmin accounts
- Role is forced to remain
- If
passwordfield is empty/not provided:- Password validation is skipped
- Password is not updated (kept unchanged)
- If
passwordfield has a value:- Full password validation applies
- Password is hashed and updated
usuarios.index with success message: “Usuario actualizado correctamente.”
Example:
toggleStatus()
Toggles a user’s status between active and inactive, with superadmin protection. Route:PATCH /usuarios/{id}/toggle
Route Name: usuarios.toggle
Middleware:
authcan:admin-only
User ID (
id_usuario)- If
estatus = 'activo'→ changes to'baja' - If
estatus = 'baja'→ changes to'activo'
- Users with
rol = 'admin'cannot be suspended - Returns error message: “El Superadministrador no puede ser suspendido.”
'activo': “El usuario ha sido reactivado.”'baja': “El acceso de ha sido suspendido.”
- When a user is set to
'baja', they cannot log in - The
AuthControllerchecks user status during login
Summary
TheUsuarioController provides 5 methods for complete user management:
- index() - List all users with clinic relationships
- store() - Create new user with role and password validation
- edit() - Get user data (JSON for AJAX)
- update() - Update user with optional password change
- toggleStatus() - Activate/suspend user account
- Strong password requirements (8+ chars, mixed case, numbers)
- Role-based user creation (dentista, asistente)
- Superadmin protection (cannot be edited or suspended)
- Professional license validation for dentists
- Automatic password hashing via model casts
- Spanish character support in names
- Username uniqueness validation
- Status toggling with login impact
- Optional password updates (leave blank to keep existing)
- Only active clinics selectable for new users
- Password never returned in JSON responses
- Automatic password hashing
- Superadmin role immutability
- Suspended users cannot log in
- Username and clinic foreign key validation