-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.php
More file actions
229 lines (197 loc) · 6.54 KB
/
Copy pathAuthController.php
File metadata and controls
229 lines (197 loc) · 6.54 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace App\Controllers;
use App\Core\Session;
use App\Repositories\UserRepository;
/**
* Authentication Controller
*
* Xử lý đăng nhập, đăng xuất, đăng ký
*
* @author Lê Văn Thuận (22070984)
*/
class AuthController
{
private UserRepository $userRepo;
public function __construct()
{
$this->userRepo = new UserRepository();
}
/**
* Show login page
*/
public function showLogin(): void
{
if (Session::isLoggedIn()) {
$this->redirectToDashboard();
return;
}
require VIEW_PATH . '/auth/login.php';
}
/**
* Process login
*/
public function login(): void
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ' . BASE_URL . 'login');
exit;
}
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$remember = isset($_POST['remember']);
// Validate input
if (empty($email) || empty($password)) {
Session::flash('error', 'Vui lòng nhập đầy đủ email và mật khẩu');
header('Location: ' . BASE_URL . 'login');
exit;
}
// Verify credentials
$user = $this->userRepo->verifyPassword($email, $password);
if (!$user) {
Session::flash('error', 'Email hoặc mật khẩu không đúng');
header('Location: ' . BASE_URL . 'login');
exit;
}
// Set session
Session::setUser($user);
// Remember me (optional)
if ($remember) {
// Implement remember me logic here
}
Session::flash('success', 'Đăng nhập thành công!');
$this->redirectToDashboard();
}
/**
* Show registration page
*/
public function showRegister(): void
{
if (Session::isLoggedIn()) {
$this->redirectToDashboard();
return;
}
require VIEW_PATH . '/auth/register.php';
}
/**
* Process registration
*/
public function register(): void
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ' . BASE_URL . 'register');
exit;
}
// Get form data
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$passwordConfirm = $_POST['password_confirm'] ?? '';
$fullName = trim($_POST['full_name'] ?? '');
$role = $_POST['role'] ?? 'student';
$studentCode = trim($_POST['student_code'] ?? '');
// Validate input
$errors = [];
if (empty($email)) {
$errors[] = 'Email không được để trống';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email không hợp lệ';
} elseif (!preg_match('/@(ischool\.vnu\.edu\.vn|vnu\.edu\.vn)$/', $email)) {
$errors[] = 'Email phải thuộc domain @ischool.vnu.edu.vn hoặc @vnu.edu.vn';
} elseif ($this->userRepo->emailExists($email)) {
$errors[] = 'Email đã được sử dụng';
}
if (empty($fullName)) {
$errors[] = 'Họ tên không được để trống';
}
if (empty($password)) {
$errors[] = 'Mật khẩu không được để trống';
} elseif (strlen($password) < 6) {
$errors[] = 'Mật khẩu phải có ít nhất 6 ký tự';
} elseif ($password !== $passwordConfirm) {
$errors[] = 'Mật khẩu xác nhận không khớp';
}
if ($role === 'student' && empty($studentCode)) {
$errors[] = 'Mã sinh viên không được để trống';
}
if (!empty($errors)) {
Session::flash('error', implode('<br>', $errors));
header('Location: ' . BASE_URL . 'register');
exit;
}
// Create user
try {
$userData = [
'email' => $email,
'password' => $password,
'full_name' => $fullName,
'role' => $role,
'student_code' => $role === 'student' ? $studentCode : null,
'is_active' => 1
];
$userId = $this->userRepo->createUser($userData);
if ($userId) {
Session::flash('success', 'Đăng ký thành công! Vui lòng đăng nhập.');
header('Location: ' . BASE_URL . 'login');
exit;
}
} catch (\Exception $e) {
Session::flash('error', 'Đã xảy ra lỗi khi đăng ký. Vui lòng thử lại.');
header('Location: ' . BASE_URL . 'register');
exit;
}
}
/**
* Logout
*/
public function logout(): void
{
Session::clearUser();
Session::flash('success', 'Đăng xuất thành công');
header('Location: ' . BASE_URL . 'login');
exit;
}
/**
* Redirect to appropriate dashboard based on role
*/
private function redirectToDashboard(): void
{
$role = Session::getUserRole();
switch ($role) {
case ROLE_ADMIN:
header('Location: ' . BASE_URL . 'admin/dashboard');
break;
case ROLE_LECTURER:
header('Location: ' . BASE_URL . 'lecturer/dashboard');
break;
case ROLE_STUDENT:
header('Location: ' . BASE_URL . 'student/dashboard');
break;
default:
header('Location: ' . BASE_URL);
break;
}
exit;
}
/**
* Check if user is authenticated (middleware)
*/
public static function requireAuth(): void
{
if (!Session::isLoggedIn()) {
Session::flash('error', 'Vui lòng đăng nhập để tiếp tục');
header('Location: ' . BASE_URL . 'login');
exit;
}
}
/**
* Check if user has required role (middleware)
*/
public static function requireRole(string $role): void
{
self::requireAuth();
if (!Session::hasRole($role)) {
Session::flash('error', 'Bạn không có quyền truy cập trang này');
header('Location: ' . BASE_URL);
exit;
}
}
}