- Add complete Auth module with login, logout, lock screen functionality - Fix 401 error on error monitoring page by adding /error-monitoring/ to public patterns - Improve lock screen with proper form handling and unlock functionality - Add RoleModel and role management views - Add BaseModel for common model functionality - Update authentication system and middleware - Add troubleshooting documentation - Update assets and build tools
127 lines
3.1 KiB
PHP
127 lines
3.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Users\Models;
|
|
|
|
use App\Core\BaseModel;
|
|
|
|
class RoleModel extends BaseModel
|
|
{
|
|
protected string $table = 'roles';
|
|
protected string $primaryKey = 'id';
|
|
|
|
protected array $fillable = [
|
|
'name',
|
|
'description',
|
|
'permissions',
|
|
'is_active'
|
|
];
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct(\App\Core\DatabaseManager::getInstance());
|
|
}
|
|
|
|
/**
|
|
* Get all roles with user count
|
|
*/
|
|
public function getAllWithUserCount(): array
|
|
{
|
|
$query = "
|
|
SELECT
|
|
r.*,
|
|
COUNT(DISTINCT u.id) as users_count
|
|
FROM {$this->table} r
|
|
LEFT JOIN users u ON u.role = r.name AND u.is_active = 1
|
|
WHERE r.is_active = 1
|
|
GROUP BY r.id
|
|
ORDER BY r.name ASC
|
|
";
|
|
|
|
return $this->db->fetchAll($query);
|
|
}
|
|
|
|
/**
|
|
* Get role with users
|
|
*/
|
|
public function getRoleWithUsers(int $id): ?array
|
|
{
|
|
$role = $this->getById($id);
|
|
if (!$role) {
|
|
return null;
|
|
}
|
|
|
|
// Get users with this role
|
|
$users = $this->db->fetchAll(
|
|
"SELECT id, username, email, created_at
|
|
FROM users
|
|
WHERE role = ? AND is_active = 1
|
|
LIMIT 100",
|
|
[$role['name']]
|
|
);
|
|
|
|
$role['users'] = $users;
|
|
$role['users_count'] = count($users);
|
|
|
|
// Parse permissions JSON
|
|
if (!empty($role['permissions'])) {
|
|
$role['permissions'] = json_decode($role['permissions'], true) ?: [];
|
|
} else {
|
|
$role['permissions'] = [];
|
|
}
|
|
|
|
return $role;
|
|
}
|
|
|
|
/**
|
|
* Create role with permissions
|
|
*/
|
|
public function createRole(array $data): int
|
|
{
|
|
// Convert permissions array to JSON
|
|
if (isset($data['permissions']) && is_array($data['permissions'])) {
|
|
$data['permissions'] = json_encode($data['permissions']);
|
|
}
|
|
|
|
return $this->create($data);
|
|
}
|
|
|
|
/**
|
|
* Update role with permissions
|
|
*/
|
|
public function updateRole(int $id, array $data): bool
|
|
{
|
|
// Convert permissions array to JSON
|
|
if (isset($data['permissions']) && is_array($data['permissions'])) {
|
|
$data['permissions'] = json_encode($data['permissions']);
|
|
}
|
|
|
|
return $this->update($id, $data);
|
|
}
|
|
|
|
/**
|
|
* Delete role (soft delete)
|
|
*/
|
|
public function deleteRole(int $id): bool
|
|
{
|
|
return $this->update($id, ['is_active' => 0]);
|
|
}
|
|
|
|
/**
|
|
* Check if role name exists
|
|
*/
|
|
public function roleNameExists(string $name, ?int $excludeId = null): bool
|
|
{
|
|
$query = "SELECT COUNT(*) as count FROM {$this->table} WHERE name = ? AND is_active = 1";
|
|
$params = [$name];
|
|
|
|
if ($excludeId) {
|
|
$query .= " AND id != ?";
|
|
$params[] = $excludeId;
|
|
}
|
|
|
|
$result = $this->db->fetch($query, $params);
|
|
return ($result['count'] ?? 0) > 0;
|
|
}
|
|
}
|