fix: Add missing Auth class and UserModel methods
- Import Auth.php from old CRM to fix authentication - Add getAllForAdmin, getTotalForAdmin, findForAdmin methods to UserModel - Fix admin users API endpoints functionality - Resolve 'Erro ao carregar utilizadores' error
This commit is contained in:
1 parent
0d76c40980
commit
2cab67b1ab
1 file changed
+124
@@ -263,4 +263,128 @@ class UserModel
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all users for admin with pagination and search
|
||||
*/
|
||||
public function getAllForAdmin(string $search = '', string $sort = 'name', int $page = 1, int $perPage = 10): array
|
||||
{
|
||||
try {
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$sql = "SELECT
|
||||
u.id,
|
||||
u.username,
|
||||
u.first_name,
|
||||
u.last_name,
|
||||
u.email,
|
||||
u.phone,
|
||||
u.city,
|
||||
u.country,
|
||||
u.role,
|
||||
u.is_active,
|
||||
u.created_at,
|
||||
u.updated_at,
|
||||
u.last_login
|
||||
FROM users u
|
||||
WHERE u.id > 0";
|
||||
|
||||
$params = [];
|
||||
|
||||
if (!empty($search)) {
|
||||
$sql .= " AND (u.username LIKE ? OR u.email LIKE ? OR u.first_name LIKE ? OR u.last_name LIKE ?)";
|
||||
$searchTerm = '%' . $search . '%';
|
||||
$params[] = $searchTerm;
|
||||
$params[] = $searchTerm;
|
||||
$params[] = $searchTerm;
|
||||
$params[] = $searchTerm;
|
||||
}
|
||||
|
||||
// Add sorting
|
||||
$allowedSorts = ['name', 'email', 'role', 'created_at', 'last_login'];
|
||||
if (in_array($sort, $allowedSorts)) {
|
||||
if ($sort === 'name') {
|
||||
$sql .= " ORDER BY u.first_name, u.last_name";
|
||||
} else {
|
||||
$sql .= " ORDER BY u.{$sort}";
|
||||
}
|
||||
} else {
|
||||
$sql .= " ORDER BY u.created_at DESC";
|
||||
}
|
||||
|
||||
$sql .= " LIMIT ? OFFSET ?";
|
||||
$params[] = $perPage;
|
||||
$params[] = $offset;
|
||||
|
||||
return $this->db->fetchAll($sql, $params) ?: [];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total count of users for admin with search
|
||||
*/
|
||||
public function getTotalForAdmin(string $search = ''): int
|
||||
{
|
||||
try {
|
||||
$sql = "SELECT COUNT(*) as total FROM users u WHERE u.id > 0";
|
||||
$params = [];
|
||||
|
||||
if (!empty($search)) {
|
||||
$sql .= " AND (u.username LIKE ? OR u.email LIKE ? OR u.first_name LIKE ? OR u.last_name LIKE ?)";
|
||||
$searchTerm = '%' . $search . '%';
|
||||
$params[] = $searchTerm;
|
||||
$params[] = $searchTerm;
|
||||
$params[] = $searchTerm;
|
||||
$params[] = $searchTerm;
|
||||
}
|
||||
|
||||
$result = $this->db->fetch($sql, $params);
|
||||
return (int)($result['total'] ?? 0);
|
||||
} catch (\Exception $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find user for admin by ID
|
||||
*/
|
||||
public function findForAdmin(int $id): ?array
|
||||
{
|
||||
try {
|
||||
if ($id <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sql = "SELECT
|
||||
u.id,
|
||||
u.username,
|
||||
u.first_name,
|
||||
u.last_name,
|
||||
u.email,
|
||||
u.phone,
|
||||
u.city,
|
||||
u.country,
|
||||
u.zipcode,
|
||||
u.website,
|
||||
u.description,
|
||||
u.avatar,
|
||||
u.job_title,
|
||||
u.company_name,
|
||||
u.joining_date,
|
||||
u.role,
|
||||
u.is_active,
|
||||
u.created_at,
|
||||
u.updated_at,
|
||||
u.last_login
|
||||
FROM users u
|
||||
WHERE u.id = ?";
|
||||
|
||||
$result = $this->db->fetch($sql, [$id]);
|
||||
return $result ?: null;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user