UserController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace App\Controllers;
  3. use App\Database\DB;
  4. use App\Exceptions\NotFoundException;
  5. use App\Exceptions\UnauthorizedException;
  6. use App\Traits\SingletonController;
  7. use App\Web\Log;
  8. use App\Web\Session;
  9. use Flight;
  10. class UserController extends Controller
  11. {
  12. use SingletonController;
  13. const PER_PAGE = 15;
  14. public function index($page = 1): void
  15. {
  16. $this->checkAdmin();
  17. $page = max(0, --$page);
  18. $users = DB::query('SELECT * FROM `users` LIMIT ? OFFSET ?', [self::PER_PAGE, $page * self::PER_PAGE])->fetchAll();
  19. $pages = DB::query('SELECT COUNT(*) AS `count` FROM `users`')->fetch()->count / self::PER_PAGE;
  20. Flight::render('user/index.twig', [
  21. 'users' => $users,
  22. 'next' => $page < floor($pages),
  23. 'previous' => $page >= 1,
  24. 'current_page' => ++$page,
  25. ]);
  26. }
  27. public function create(): void
  28. {
  29. $this->checkAdmin();
  30. Flight::render('user/create.twig');
  31. }
  32. public function store(): void
  33. {
  34. $this->checkAdmin();
  35. $form = Flight::request()->data;
  36. if (!isset($form->email) || empty($form->email)) {
  37. Session::alert('The email is required.', 'danger');
  38. Flight::redirectBack();
  39. return;
  40. }
  41. if (!isset($form->username) || empty($form->username)) {
  42. Session::alert('The username is required.', 'danger');
  43. Flight::redirectBack();
  44. return;
  45. }
  46. if (DB::query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ?', $form->username)->fetch()->count > 0) {
  47. Session::alert('The username already taken.', 'danger');
  48. Flight::redirectBack();
  49. return;
  50. }
  51. if (!isset($form->password) || empty($form->password)) {
  52. Session::alert('The password is required.', 'danger');
  53. Flight::redirectBack();
  54. return;
  55. }
  56. do {
  57. $userCode = substr(md5(microtime()), rand(0, 26), 5);
  58. } while (DB::query('SELECT COUNT(*) AS `count` FROM `users` WHERE `user_code` = ?', $userCode)->fetch()->count > 0);
  59. $token = $this->generateNewToken();
  60. DB::query('INSERT INTO `users`(`email`, `username`, `password`, `is_admin`, `active`, `user_code`, `token`) VALUES (?, ?, ?, ?, ?, ?, ?)', [
  61. $form->email,
  62. $form->username,
  63. password_hash($form->password, PASSWORD_DEFAULT),
  64. isset($form->is_admin),
  65. isset($form->is_active),
  66. $userCode,
  67. $token
  68. ]);
  69. Session::alert("User '$form->username' created!", 'success');
  70. Log::info('User ' . Session::get('username') . ' created a new user.', [array_diff($form->getData(), ['password'])]);
  71. Flight::redirect('/users');
  72. }
  73. public function edit($id): void
  74. {
  75. $this->checkAdmin();
  76. $user = DB::query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  77. if (!$user) {
  78. Flight::error(new NotFoundException());
  79. return;
  80. }
  81. Flight::render('user/edit.twig', [
  82. 'user' => $user
  83. ]);
  84. }
  85. public function update($id): void
  86. {
  87. $this->checkAdmin();
  88. $form = Flight::request()->data;
  89. $user = DB::query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  90. if (!$user) {
  91. Flight::error(new NotFoundException());
  92. return;
  93. }
  94. if (!isset($form->email) || empty($form->email)) {
  95. Session::alert('The email is required.', 'danger');
  96. Flight::redirectBack();
  97. return;
  98. }
  99. if (!isset($form->username) || empty($form->username)) {
  100. Session::alert('The username is required.', 'danger');
  101. Flight::redirectBack();
  102. return;
  103. }
  104. if (DB::query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ? AND `username` <> ?', [$form->username, $user->username])->fetch()->count > 0) {
  105. Session::alert('The username already taken.', 'danger');
  106. Flight::redirectBack();
  107. return;
  108. }
  109. if (isset($form->password) && !empty($form->password)) {
  110. DB::query('UPDATE `users` SET `email`=?, `username`=?, `password`=?, `is_admin`=?, `active`=? WHERE `id` = ?', [
  111. $form->email,
  112. $form->username,
  113. password_hash($form->password, PASSWORD_DEFAULT),
  114. isset($form->is_admin),
  115. isset($form->is_active),
  116. $user->id
  117. ]);
  118. } else {
  119. DB::query('UPDATE `users` SET `email`=?, `username`=?, `is_admin`=?, `active`=? WHERE `id` = ?', [
  120. $form->email,
  121. $form->username,
  122. isset($form->is_admin),
  123. isset($form->is_active),
  124. $user->id
  125. ]);
  126. }
  127. Session::alert("User '$form->username' updated!", 'success');
  128. Log::info('User ' . Session::get('username') . " updated $user->id.", [$user, array_diff($form->getData(), ['password'])]);
  129. Flight::redirect('/users');
  130. }
  131. public function delete($id): void
  132. {
  133. $this->checkAdmin();
  134. $user = DB::query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  135. if (!$user) {
  136. Flight::error(new NotFoundException());
  137. return;
  138. }
  139. if ($user->id === Session::get('user_id')) {
  140. Session::alert('You cannot delete yourself.', 'danger');
  141. Flight::redirectBack();
  142. return;
  143. }
  144. DB::query('DELETE FROM `users` WHERE `id` = ?', $user->id);
  145. Session::alert('User deleted.', 'success');
  146. Log::info('User ' . Session::get('username') . " deleted $user->id.");
  147. Flight::redirect('/users');
  148. }
  149. public function profile(): void
  150. {
  151. $this->checkLogin();
  152. $user = DB::query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', Session::get('user_id'))->fetch();
  153. if (!$user) {
  154. Flight::error(new NotFoundException());
  155. return;
  156. }
  157. if ($user->id !== Session::get('user_id') && !Session::get('admin', false)) {
  158. Flight::error(new UnauthorizedException());
  159. return;
  160. }
  161. Flight::render('user/profile.twig', [
  162. 'user' => $user
  163. ]);
  164. }
  165. public function profileEdit($id): void
  166. {
  167. $this->checkLogin();
  168. $form = Flight::request()->data;
  169. $user = DB::query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  170. if (!$user) {
  171. Flight::error(new NotFoundException());
  172. return;
  173. }
  174. if ($user->id !== Session::get('user_id') && !Session::get('admin', false)) {
  175. Flight::error(new UnauthorizedException());
  176. return;
  177. }
  178. if (!isset($form->email) || empty($form->email)) {
  179. Session::alert('The email is required.', 'danger');
  180. Flight::redirectBack();
  181. return;
  182. }
  183. if (isset($form->password) && !empty($form->password)) {
  184. DB::query('UPDATE `users` SET `email`=?, `password`=? WHERE `id` = ?', [
  185. $form->email,
  186. password_hash($form->password, PASSWORD_DEFAULT),
  187. $user->id
  188. ]);
  189. } else {
  190. DB::query('UPDATE `users` SET `email`=? WHERE `id` = ?', [
  191. $form->email,
  192. $user->id
  193. ]);
  194. }
  195. Session::alert('Profile updated successfully!', 'success');
  196. Log::info('User ' . Session::get('username') . " updated profile of $user->id.");
  197. Flight::redirectBack();
  198. }
  199. public function refreshToken($id): void
  200. {
  201. $this->checkLogin();
  202. $user = DB::query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  203. if (!$user) {
  204. Flight::halt(404);
  205. return;
  206. }
  207. if ($user->id !== Session::get('user_id') && !Session::get('admin', false)) {
  208. Flight::halt(403);
  209. return;
  210. }
  211. $token = $this->generateNewToken();
  212. DB::query('UPDATE `users` SET `token`=? WHERE `id` = ?', [
  213. $token,
  214. $user->id
  215. ]);
  216. Log::info('User ' . Session::get('username') . " refreshed token of user $user->id.");
  217. echo $token;
  218. }
  219. protected function generateNewToken(): string
  220. {
  221. do {
  222. $token = 'token_' . md5(uniqid('', true));
  223. } while (DB::query('SELECT COUNT(*) AS `count` FROM `users` WHERE `token` = ?', $token)->fetch()->count > 0);
  224. return $token;
  225. }
  226. }