Commit b15debd3c29d794b35b93478873b5253c6cc976c

Authored by Alex Savenko
1 parent c4e1ecec

user errors response

Showing 1 changed file with 26 additions and 2 deletions   Show diff stats
app/library/App/Controllers/UserController.php
... ... @@ -4,6 +4,8 @@ namespace App\Controllers;
4 4  
5 5 use App\Model\Project;
6 6 use App\Model\User;
  7 +use PhalconApi\Constants\ErrorCodes;
  8 +use PhalconApi\Exception;
7 9 use PhalconRest\Mvc\Controllers\CrudResourceController;
8 10  
9 11 class UserController extends CrudResourceController
... ... @@ -55,7 +57,6 @@ class UserController extends CrudResourceController
55 57 if (!$data || count($data) == 0) {
56 58 return $this->onNoDataProvided();
57 59 }
58   -
59 60 if (!$this->postDataValid($data, false)) {
60 61 return $this->onDataInvalid($data);
61 62 }
... ... @@ -107,4 +108,27 @@ class UserController extends CrudResourceController
107 108 }
108 109 }
109 110  
110   -}
  111 + protected function postDataValid($data, $isUpdate)
  112 + {
  113 + $input_name = $data['username'];
  114 + if (!empty($input_name))
  115 + {
  116 +
  117 + $same_user = User::find(["username = '".$input_name."'"]);
  118 + if (isset($same_user[0]) && !empty($same_user[0]->username))
  119 + {
  120 + return false;
  121 + }
  122 +
  123 + }
  124 +
  125 + return true;
  126 + }
  127 +
  128 + protected function onDataInvalid($data)
  129 + {
  130 + $msg = 'Post-data is invalid, trying to use non-unique value of `username`';
  131 + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['data' => $data]);
  132 + }
  133 +
  134 +}
111 135 \ No newline at end of file
... ...