-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepository.php
More file actions
156 lines (128 loc) · 3.91 KB
/
Copy pathUserRepository.php
File metadata and controls
156 lines (128 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace App\Repositories;
class UserRepository extends BaseRepository {
// Đã sửa thành $table (bỏ chữ s thừa) và nằm ở vị trí chuẩn của class
protected string $table = 'users';
/**
* Find user by email
*/
public function findByEmail(string $email): ?array
{
return $this->findOne(['email' => $email]);
}
/**
* Find user by student code
*/
public function findByStudentCode(string $studentCode): ?array
{
return $this->findOne(['student_code' => $studentCode]);
}
/**
* Check if email exists
*/
public function emailExists(string $email, ?int $excludeId = null): bool
{
$sql = "SELECT COUNT(*) as total FROM `{$this->table}` WHERE `email` = :email";
$params = ['email' => $email];
if ($excludeId) {
$sql .= " AND `id` != :exclude_id";
$params['exclude_id'] = $excludeId;
}
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
$result = $stmt->fetch();
return $result['total'] > 0;
}
/**
* Get users by role
*/
public function findByRole(string $role): array
{
return $this->findAll(['role' => $role, 'is_active' => 1], 'full_name ASC');
}
/**
* Get all students
*/
public function findAllStudents(): array
{
return $this->findByRole('student');
}
/**
* Get all lecturers
*/
public function findAllLecturers(): array
{
return $this->findByRole('lecturer');
}
/**
* Create user with hashed password
*/
public function createUser(array $data): int
{
if (isset($data['password'])) {
$data['password'] = password_hash($data['password'], PASSWORD_BCRYPT, [
'cost' => 10
]);
}
return $this->create($data);
}
/**
* Update user password
*/
public function updatePassword(int $userId, string $newPassword): bool
{
$hashedPassword = password_hash($newPassword, PASSWORD_BCRYPT, [
'cost' => 10
]);
return $this->update($userId, ['password' => $hashedPassword]);
}
/**
* Verify user password
*/
public function verifyPassword(string $email, string $password): ?array
{
$user = $this->findByEmail($email);
if (!$user || !$user['is_active']) {
return null;
}
if (password_verify($password, $user['password'])) {
return $user;
}
return null;
}
/**
* Activate/Deactivate user
*/
public function setActive(int $userId, bool $isActive): bool
{
return $this->update($userId, ['is_active' => $isActive ? 1 : 0]);
}
/**
* Search users
*/
public function search(string $keyword, ?string $role = null): array
{
$sql = "SELECT * FROM `{$this->table}` WHERE
(`full_name` LIKE :keyword OR `email` LIKE :keyword OR `student_code` LIKE :keyword)";
$params = ['keyword' => "%$keyword%"];
if ($role) {
$sql .= " AND `role` = :role";
$params['role'] = $role;
}
$sql .= " ORDER BY `full_name` ASC";
return $this->query($sql, $params);
}
/**
* Get user statistics
*/
public function getStatistics(): array
{
$sql = "SELECT
`role`,
COUNT(*) as total,
SUM(CASE WHEN `is_active` = 1 THEN 1 ELSE 0 END) as active
FROM `{$this->table}`
GROUP BY `role`";
return $this->query($sql);
}
}