First Commit

This commit is contained in:
marco
2026-09-01 10:46:29 -03:00
commit bf96b77e9c
10000 changed files with 3508600 additions and 0 deletions
+526
View File
@@ -0,0 +1,526 @@
<?php
/**
* SysCMS - Content Management System
*
* Copyright (c) 2025 Marco Costa
* All rights reserved.
*
* Author: Marco Costa
* Email: contato@syspanel.com.br
* Project: SysCMS
*
* This software is licensed, not sold.
* Permission is granted to use this software
* subject to the terms of the SysCMS Commercial License.
*
* Redistribution, sublicensing, resale, or
* disclosure of this software, in whole or in part,
* is strictly prohibited without prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*/
namespace Modules\Auth\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class User extends Model
{
use SoftDeletes;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'users_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'users_name',
'users_email',
'users_password',
'users_role',
'users_bus_id',
'users_avatar',
'users_bio',
'users_phone',
'users_email_verified_at',
'users_last_login_at',
'users_status',
'users_preferences'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'users_password',
'remember_token'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'users_email_verified_at' => 'datetime',
'users_last_login_at' => 'datetime',
'users_preferences' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'users_email_verified_at',
'users_last_login_at',
'created_at',
'updated_at',
'deleted_at'
];
/**
* Boot the model
*/
protected static function boot()
{
parent::boot();
// Add company scope by default
static::addGlobalScope('company', function ($query) {
if (isset($_SESSION['user_idbus']) && $_SESSION['user_idbus'] !== null && $_SESSION['user_role'] !== 'super_admin') {
$query->where('users_bus_id', $_SESSION['user_idbus']);
}
});
}
/**
* Get the company that owns the user.
*/
public function company(): BelongsTo
{
return $this->belongsTo(\Modules\Company\Models\Company::class, 'users_bus_id', 'bus_id');
}
/**
* Get the permissions for the user.
*/
public function permissions(): HasMany
{
return $this->hasMany(\App\Models\UserPermission::class, 'user_permissions_user_id', 'users_id');
}
/**
* Get the activity logs for the user.
*/
public function activityLogs(): HasMany
{
return $this->hasMany(\App\Models\ActivityLog::class, 'activity_logs_user_id', 'users_id');
}
/**
* Check if user is super admin
*
* @return bool
*/
public function isSuperAdmin(): bool
{
return $this->users_role === 'super_admin';
}
/**
* Check if user is admin
*
* @return bool
*/
public function isAdmin(): bool
{
return $this->users_role === 'admin';
}
/**
* Check if user is member
*
* @return bool
*/
public function isMember(): bool
{
return $this->users_role === 'member';
}
/**
* Check if user is regular user
*
* @return bool
*/
public function isUser(): bool
{
return $this->users_role === 'user';
}
/**
* Check if user has specific permission
*
* @param string $permission
* @return bool
*/
public function hasPermission(string $permission): bool
{
// Super admin has all permissions
if ($this->isSuperAdmin()) {
return true;
}
// Admin role has full access within their company scope
if ($this->isAdmin()) {
return true;
}
// Check direct permissions
$hasDirectPermission = \App\Models\UserPermission::where('user_permissions_user_id', $this->users_id)
->where('user_permissions_permission', $permission)
->where('user_permissions_bus_id', $this->users_bus_id)
->where(function ($query) {
$query->whereNull('user_permissions_expires_at')
->orWhere('user_permissions_expires_at', '>', \Carbon\Carbon::now());
})
->exists();
if ($hasDirectPermission) {
return true;
}
// Check role permissions
return \App\Models\RolePermission::where('role_permissions_role', $this->users_role)
->where('role_permissions_bus_id', $this->users_bus_id)
->where('role_permissions_permission', $permission)
->exists();
}
/**
* Get all permissions for the user as an array of strings.
*
* @return array
*/
public function getPermissions(): array
{
// Se for Super Admin, retorna um token que indica todas as permissões
// (Ou poderíamos retornar todas as permissões do sistema se necessário)
if ($this->isSuperAdmin()) {
return ['*'];
}
// Se for Admin, retorna todas as permissões da empresa (ou um token)
if ($this->isAdmin()) {
return ['*.company'];
}
$directPermissions = \App\Models\UserPermission::where('user_permissions_user_id', $this->users_id)
->where('user_permissions_bus_id', $this->users_bus_id)
->where(function ($query) {
$query->whereNull('user_permissions_expires_at')
->orWhere('user_permissions_expires_at', '>', \Carbon\Carbon::now());
})
->pluck('user_permissions_permission')
->toArray();
$rolePermissions = \App\Models\RolePermission::where('role_permissions_role', $this->users_role)
->where('role_permissions_bus_id', $this->users_bus_id)
->pluck('role_permissions_permission')
->toArray();
return array_unique(array_merge($directPermissions, $rolePermissions));
}
/**
* Get the avatar URL attribute
*
* @return string
*/
public function getAvatarUrlAttribute(): string
{
if ($this->users_avatar) {
return url('storage/uploads/avatars/' . $this->users_avatar);
}
return asset_url('img/default-avatar.png');
}
/**
* Get display name
*
* @return string
*/
public function getDisplayNameAttribute(): string
{
return $this->users_name ?: $this->users_email;
}
/**
* Get id attribute alias
*
* @return int
*/
public function getIdAttribute(): int
{
return $this->users_id;
}
/**
* Hash the password before saving
*
* @param string $value
* @return void
*/
public function setUsersPasswordAttribute($value): void
{
$this->attributes['users_password'] = password_hash($value, PASSWORD_DEFAULT);
}
/**
* Alias for setting password via 'password' attribute
*
* @param string $value
* @return void
*/
public function setPasswordAttribute($value): void
{
$this->setUsersPasswordAttribute($value);
}
/**
* Get the password attribute for authentication
*
* @return string
*/
public function getUsersPasswordAttribute(): string
{
return $this->attributes['users_password'] ?? '';
}
/**
* Alias for getting password via 'password' attribute
*
* @return string
*/
public function getPasswordAttribute(): string
{
return $this->getUsersPasswordAttribute();
}
/**
* Get the email attribute
*
* @return string
*/
public function getEmailAttribute(): string
{
return $this->attributes['users_email'] ?? '';
}
/**
* Get the name attribute
*
* @return string
*/
public function getNameAttribute(): string
{
return $this->attributes['users_name'] ?? '';
}
/**
* Get the role attribute
*
* @return string
*/
public function getRoleAttribute(): string
{
return $this->attributes['users_role'] ?? '';
}
/**
* Get the status attribute
*
* @return string
*/
public function getStatusAttribute(): string
{
return $this->attributes['users_status'] ?? '';
}
/**
* Get the email_verified_at attribute
*
* @return string|null
*/
public function getEmailVerifiedAtAttribute(): ?string
{
return $this->attributes['users_email_verified_at'] ?? null;
}
/**
* Set the email_verified_at attribute
*
* @param string|null $value
* @return void
*/
public function setEmailVerifiedAtAttribute($value): void
{
$this->attributes['users_email_verified_at'] = $value;
}
/**
* Get the avatar attribute
*
* @return string|null
*/
public function getAvatarAttribute(): ?string
{
return $this->attributes['users_avatar'] ?? null;
}
/**
* Set the avatar attribute
*
* @param string|null $value
* @return void
*/
public function setAvatarAttribute($value): void
{
$this->attributes['users_avatar'] = $value;
}
/**
* Get the bio attribute
*
* @return string|null
*/
public function getBioAttribute(): ?string
{
return $this->attributes['users_bio'] ?? null;
}
/**
* Set the bio attribute
*
* @param string|null $value
* @return void
*/
public function setBioAttribute($value): void
{
$this->attributes['users_bio'] = $value;
}
/**
* Get the phone attribute
*
* @return string|null
*/
public function getPhoneAttribute(): ?string
{
return $this->attributes['users_phone'] ?? null;
}
/**
* Set the phone attribute
*
* @param string|null $value
* @return void
*/
public function setPhoneAttribute($value): void
{
$this->attributes['users_phone'] = $value;
}
/**
* Get the preferences attribute
*
* @return array|null
*/
public function getPreferencesAttribute(): ?array
{
$value = $this->attributes['users_preferences'] ?? null;
return $value ? json_decode($value, true) : null;
}
/**
* Set the preferences attribute
*
* @param array|null $value
* @return void
*/
public function setPreferencesAttribute($value): void
{
$this->attributes['users_preferences'] = is_array($value) ? json_encode($value) : $value;
}
/**
* Get users by company
*
* @param int $companyId
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getByCompany(int $companyId)
{
return self::where('users_bus_id', $companyId)->get();
}
/**
* Get active users only
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
{
return $query->where('users_status', 'active');
}
/**
* Get users by role
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $role
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRole($query, string $role)
{
return $query->where('users_role', $role);
}
/**
* Update last login timestamp
*
* @return bool
*/
public function updateLastLogin(): bool
{
return $this->update(['users_last_login_at' => \Carbon\Carbon::now()]);
}
}