Overview
FacturaScripts provides a comprehensive user management system with role-based access control (RBAC). Users can be assigned specific roles that define their permissions across different areas of the application.User Model
The User model is located atCore/Model/User.php and includes the following key properties:
User Properties
- nick: Username (3-50 alphanumeric characters, required)
- email: Email address (validated)
- password: Encrypted password (min 8 characters, must contain letters and numbers)
- admin: Boolean flag for administrator privileges
- enabled: Whether the user account is active
- level: User level (0-99, admins have level 99)
- homepage: Default page displayed after login
- langcode: User’s preferred language
- codalmacen: Default warehouse code
- codserie: Default document series
- codagente: Associated agent code
- idempresa: Associated company ID
Security Features
Password Requirements
Passwords must meet the following criteria (enforced inUser.php:328):
- Minimum 8 characters
- Must contain at least one number
- Must contain at least one letter
- Automatically hashed using PHP’s
PASSWORD_DEFAULTalgorithm
Two-Factor Authentication (2FA)
FacturaScripts supports TOTP-based two-factor authentication:- two_factor_enabled: Boolean to enable/disable 2FA
- two_factor_secret_key: Secret key for TOTP generation
enableTwoFactor(string $key = ''): Activates 2FA and returns secret keydisableTwoFactor(): Deactivates 2FAverifyTwoFactorCode(string $code): Validates TOTP codegetTwoFactorUrl(): Returns QR code URL for authenticator appsgetTwoFactorQR(): Returns QR code image
Session Management
User activity is tracked with:- lastactivity: Timestamp of last activity
- lastip: Last IP address (max 40 characters)
- lastbrowser: Last browser string (max 200 characters)
- logkey: 99-character random session key
UPDATE_ACTIVITY_PERIOD (3600 seconds = 1 hour).
User Permissions
Permission Levels
-
Admin Users (
admin = true):- Level automatically set to 99
- Full access to all pages except those with
only-owner-datarestriction - Can perform all operations
-
Regular Users (
admin = false):- Default level is 2 (
DEFAULT_LEVEL) - Permissions controlled by assigned roles
- Access determined by role configuration
- Default level is 2 (
Checking Permissions
Use thecan() method to verify user permissions:
access: Can view the pageupdate: Can modify datadelete: Can delete recordsexport: Can export dataimport: Can import dataonly-owner-data: Can only see own data
Role Management
Role Model
Roles are defined inCore/Model/Role.php with:
- codrole: Role code (1-20 alphanumeric characters)
- descripcion: Role description
Role Operations
Adding Roles to Users
User.php:113):
- Checks if user already has the role
- Validates the role exists
- Adds the role to the user
- Sets default homepage if user doesn’t have one
Removing Roles from Users
Getting User Roles
Role Access Control
TheRoleAccess model (Core/Model/RoleAccess.php) defines permissions for each page within a role:
- allowupdate: Can update records (default: true)
- allowdelete: Can delete records (default: true)
- allowexport: Can export data (default: true)
- allowimport: Can import data (default: true)
- onlyownerdata: Can only access own data (default: false)
Adding Pages to Roles
Removing Pages from Roles
User Creation
Default Admin Account
During installation, a default admin account is created with:- Username: From
initial_userconfig (default: ‘admin’) - Password: From
initial_passconfig (default: ‘admin’) - Email: From
initial_emailconfig - Admin privileges: Enabled
- Homepage: ‘Wizard’
- Level: 99
User.php:277 for installation details.
Creating New Users
When creating a new user:- Set required properties (nick, email, password)
- Set optional properties (codalmacen, idempresa, langcode)
- Call
save()
User Validation
Thetest() method (User.php:345) validates:
- Nick: Alphanumeric, 3-50 characters, supports
@,+,.,-,_ - Email: Valid email format
- Creation date: Auto-set if empty
- Last activity: Can be null
- Last browser: HTML-escaped, max 200 characters
- Last IP: HTML-escaped, max 40 characters
- Admin level: Auto-set to 99 if admin
- Password: Complexity requirements
- Agent: Validates associated agent exists
- Warehouse: Validates warehouse exists and belongs to user’s company
User Restrictions
Cannot Delete Last User
The system prevents deleting the last user (User.php:207):
Disabled Users
Disabled users (enabled = false) cannot:
- Access any pages
- Perform any operations
- Log in to the system
can() method (User.php:164).
Best Practices
- Always use roles: Assign permissions via roles rather than individual user settings
- Limit admin users: Only grant admin privileges when absolutely necessary
- Enable 2FA: Require two-factor authentication for sensitive accounts
- Regular audits: Review user activity via
lastactivity,lastip, andlastbrowser - Strong passwords: The 8-character minimum is enforced, but encourage longer passwords
- Disable unused accounts: Set
enabled = falseinstead of deleting users to preserve audit trails
Related Files
- User Model:
/Core/Model/User.php - Role Model:
/Core/Model/Role.php - RoleAccess Model:
/Core/Model/RoleAccess.php - RoleUser Model:
/Core/Model/RoleUser.php - TwoFactorManager:
/Core/Lib/TwoFactorManager.php - User Controller:
/Core/Controller/EditUser.php

