EditAccountAction.class.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. class EditAccountAction implements IAction {
  3. public function execute() {
  4. if(!Authorize::isLoggedIn() || !Authorize::hasPermission()) {
  5. return array("error" => "Access not authorized");
  6. }
  7. $clientId = $_POST['client_id'] ?? $_GET['client_id'] ?? "";
  8. $subdomain = $_POST['subdomain'] ?? $_GET['subdomain'] ?? "";
  9. $displayName = $_POST['display_name'] ?? $_GET['display_name'] ?? "";
  10. //TODO: scrub inputs
  11. if(empty($clientId) || empty($subdomain) || empty($displayName)) {
  12. return array("error" => "One or more required fields missing: client_id, subdomain, display_name");
  13. }
  14. $db = SqliteDatabase::getSingleton();
  15. $sql = "UPDATE accounts SET subdomain = :subdomain, display_name = :display_name WHERE rowid = :client_id";
  16. $preparedQuery = $db->prepare($sql);
  17. $preparedQuery->bindValue(':subdomain', $subdomain);
  18. $preparedQuery->bindValue(':display_name', $displayName);
  19. $preparedQuery->bindValue(':client_id', $clientId);
  20. try {
  21. $result = $preparedQuery->execute();
  22. $loggerData = array();
  23. $loggerData['display_name'] = $_SESSION['display_name'];
  24. $loggerData['user_id'] = $_SESSION['user_id'];
  25. $loggerData['client_id'] = $clientId;
  26. $loggerData['account_display_name'] = $displayName;
  27. $loggerData['subdomain'] = $subdomain;
  28. SecurityLogger::action("edit-account", $loggerData, time());
  29. return array("status" => "success", "message" => "Updated account '" . $subdomain . "' (".$clientId.").");
  30. }
  31. catch(Exception $e) {
  32. return array("error" => "Error when updating account '" . $subdomain. "' (".$clientId."): invalid fields?" , "exception" => $e->getMessage());
  33. }
  34. }
  35. }