Overview
Grupo Mecsa CMS implements comprehensive security measures to protect sensitive data and ensure only authorized staff can access administrative functions.Security Architecture
The CMS uses a multi-layered security approach:- Session-based Authentication - Secure token storage in PHP sessions
- Role-based Access Control (RBAC) - Fine-grained permissions by user role
- Supabase Row Level Security - Database-level access control
- Security Audit Logging - Track unauthorized access attempts
Access Control
Security Check Implementation
Every protected page must include
config/security_check.php to enforce authentication and authorizationconfig/security_check.php
The security check runs on every page load and performs:
- Session validation
- Token verification
- Role authorization
- Access denial for non-staff users
Usage in Pages
Include at the top of every protected PHP file:Staff Roles
The system recognizes four staff roles:| Role | Variable | Permissions |
|---|---|---|
| Admin | $isAdmin | Full system access, user management |
| Comercial | $isComercial | Client and project management |
| Proyecto | $isProyecto | Project management and tracking |
| RRHH | $isRRHH | Employee and department management |
Role Detection Logic
Session Management
Session Initialization
Required Session Variables
| Variable | Type | Description |
|---|---|---|
$_SESSION['token'] | string | Supabase authentication JWT token |
$_SESSION['user'] | array | User profile with role flags |
$_SESSION['uid'] | string | Unique user identifier |
Token Validation
Session Expiration
Users are redirected with specific error codes:session_expired- No valid token foundunauthorized_staff_only- Valid user but no staff roleunauthorized- Generic access denial
API Key Protection
Development vs Production Keys
Development Environment
Store keys inlocal.supabase.php (add to .gitignore):
local.supabase.php
Production Environment
Use environment variables:.htaccess:
Key Types and Security Levels
Understand the difference between key types to use them appropriately
Anon/Public Key ($supabase_key)
- Security Level: Standard
- RLS: Enforced (respects Row Level Security policies)
- Usage: Normal CRUD operations, user-level authentication
- Exposure: Can be exposed in client-side code
Service Role Key ($supabase_service_role)
- Security Level: Administrative
- RLS: Bypassed (full database access)
- Usage: User management, password resets, admin operations
- Exposure: Must NEVER be exposed to clients
Security Audit Logging
Audit Log Location
Unauthorized access attempts are logged to:Log Format
- Timestamp
- Event type (CMS_BLOCK)
- Page that was blocked
- User ID (if available)
Log Implementation
Best Practices
1. Credential Management
2. Session Security
3. Password Security
- Enforce strong password requirements
- Use Supabase’s built-in password hashing
- Implement password reset flows with time-limited tokens
- Support multi-factor authentication (MFA)
4. Database Security
Leverage Supabase Row Level Security (RLS) for defense in depth
5. Input Validation
6. CSRF Protection
Implement CSRF tokens for state-changing operations:Monitoring and Maintenance
Regular Tasks
Security Incident Response
If you detect unauthorized access:-
Immediate Actions:
- Revoke compromised API keys in Supabase
- Force logout all sessions
- Change admin passwords
-
Investigation:
- Review audit logs
- Check database access logs in Supabase
- Identify the attack vector
-
Remediation:
- Patch the vulnerability
- Update security policies
- Notify affected users if necessary
Security Checklist
- All sensitive files added to
.gitignore - Production API keys stored in environment variables
- HTTPS enabled on production server
- Secure session settings configured
-
security_check.phpincluded on all protected pages - Row Level Security policies configured in Supabase
- Regular audit log reviews scheduled
- Strong password policy enforced
- CSRF protection implemented on forms
- Input validation on all user inputs
- Error messages don’t reveal sensitive information
- Security headers configured (HSTS, CSP, X-Frame-Options)
Next Steps
Supabase Configuration
Configure database connection and credentials
Deployment
Deploy securely to production