user_edit.php 3.7 KB

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