From b15debd3c29d794b35b93478873b5253c6cc976c Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 15 Mar 2017 15:26:53 +0200 Subject: [PATCH] user errors response --- app/library/App/Controllers/UserController.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/library/App/Controllers/UserController.php b/app/library/App/Controllers/UserController.php index 47794a6..f234111 100755 --- a/app/library/App/Controllers/UserController.php +++ b/app/library/App/Controllers/UserController.php @@ -4,6 +4,8 @@ namespace App\Controllers; use App\Model\Project; use App\Model\User; +use PhalconApi\Constants\ErrorCodes; +use PhalconApi\Exception; use PhalconRest\Mvc\Controllers\CrudResourceController; class UserController extends CrudResourceController @@ -55,7 +57,6 @@ class UserController extends CrudResourceController if (!$data || count($data) == 0) { return $this->onNoDataProvided(); } - if (!$this->postDataValid($data, false)) { return $this->onDataInvalid($data); } @@ -107,4 +108,27 @@ class UserController extends CrudResourceController } } -} + protected function postDataValid($data, $isUpdate) + { + $input_name = $data['username']; + if (!empty($input_name)) + { + + $same_user = User::find(["username = '".$input_name."'"]); + if (isset($same_user[0]) && !empty($same_user[0]->username)) + { + return false; + } + + } + + return true; + } + + protected function onDataInvalid($data) + { + $msg = 'Post-data is invalid, trying to use non-unique value of `username`'; + throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['data' => $data]); + } + +} \ No newline at end of file -- libgit2 0.21.4