1
0

user_edit.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. * @var FormException $form_exception
  5. */
  6. Core::addScript("public/js/app-user-edit.js");
  7. $user = User::get($id);
  8. if(!$user) throw new Exception("User not found.");
  9. if($user->level > Core::$user->level) $form_exception->addError("You do not have permission to edit this user as their level is higher.");
  10. $page_title = "Edit User";
  11. $user_customer_link = UserCustomer::queryOne(["user_id" => $user->id]);
  12. if(!$user_customer_link) $user_customer_link = new UserCustomer;
  13. $customers = Customer::query([], ["sort" => "name"]);
  14. Core::addBreadcrumb("/account", "Account");
  15. Core::addBreadcrumb("/account/users", "Users");
  16. Core::addBreadcrumb("/account/users/$id", $user->getDisplayName());
  17. Core::addBreadcrumb("/account/users/$id/edit", $page_title);
  18. $submit = HTTP::value("submit");
  19. if($submit) {
  20. try {
  21. Core::validateCSRFToken($submit);
  22. $form_exception = new FormException;
  23. // Password
  24. $password = HTTP::value("password");
  25. $password_confirm = HTTP::value("password_confirm");
  26. if($password !== $password_confirm) $form_exception->addError("Passwords do not match.");
  27. if($password) {
  28. $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
  29. }
  30. $user->level = HTTP::value("level", "int");
  31. $user->email = HTTP::value("email", "email");
  32. $user->title = HTTP::value("title", "trim");
  33. $user->first_name = HTTP::value("first_name", "trim");
  34. $user->last_name = HTTP::value("last_name", "trim");
  35. $user->date_of_birth = HTTP::value("date_of_birth");
  36. $user->reported = HTTP::value("reported", "int");
  37. $user->reported_comment = HTTP::value("reported_comment", "trim");
  38. $user->phone = HTTP::value("phone", "trim");
  39. $user->gender = HTTP::value("gender", "trim");
  40. $user->activated = HTTP::value("activated", "int");
  41. $user->can_claim = HTTP::value("can_claim", "int");
  42. $user_customer_link->delete();
  43. $customer_id = HTTP::value("customer_id", "int");
  44. if($customer_id) {
  45. $customer = Customer::get($customer_id);
  46. if(!$customer) $form_exception->addError("Customer is invalid.");
  47. $user_customer_link = new UserCustomer;
  48. $user_customer_link->user_id = $user->id;
  49. $user_customer_link->customer_id = $customer->id;
  50. $user_customer_link->commit();
  51. }
  52. if($password && !User::validatePassword($password)) $form_exception->addError("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character.");
  53. if(!isset(User::LEVELS[$user->level])) $form_exception->addError("User level is invalid.");
  54. if($user->level > Core::$user->level) $form_exception->addError("You cannot elevate this user's level higher than your own.");
  55. if(empty($user->email)) $form_exception->addError("Email address is invalid.");
  56. if(empty($user->title)) $form_exception->addError("Title is required.");
  57. if(empty($user->first_name)) $form_exception->addError("First name is required.");
  58. if(empty($user->last_name)) $form_exception->addError("Last name is required.");
  59. if(empty($user->date_of_birth)) $form_exception->addError("Date of birth is required.");
  60. if(empty($user->phone)) $form_exception->addError("Mobile number is required.");
  61. if($form_exception->hasErrors()) throw $form_exception;
  62. $success = $user->commit();
  63. if(!$success) throw new Exception("User was not updated successfully. If the email was changed, another user may already be using that address.");
  64. Core::addAlert("success", "User updated successfully.");
  65. HTTP::redirect("/account/users/$id/edit");
  66. }
  67. catch(FormException $e) {
  68. $e->generateAlerts();
  69. }
  70. catch(Exception $e) {
  71. Core::addAlert("danger", $e->getMessage());
  72. }
  73. }
  74. View::includeOnce("header", compact("page_title"));
  75. View::include("user_edit_form", compact("user", "customers", "user_customer_link"));
  76. View::includeOnce("footer");