bakaphp /
phalcon-api
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gewaer\Api\Controllers; |
||
| 6 | |||
| 7 | use Gewaer\Models\Users; |
||
| 8 | use Gewaer\Models\UserLinkedSources; |
||
| 9 | use Baka\Auth\Models\Sources; |
||
| 10 | use Phalcon\Http\Response; |
||
| 11 | use Phalcon\Validation; |
||
| 12 | use Phalcon\Validation\Validator\PresenceOf; |
||
| 13 | use Gewaer\Exception\BadRequestHttpException; |
||
| 14 | use Gewaer\Exception\UnprocessableEntityHttpException; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Users controller |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | class UsersController extends \Baka\Auth\UsersController |
||
| 21 | {
|
||
| 22 | /* |
||
| 23 | * fields we accept to create |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $createFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'family']; |
||
| 28 | |||
| 29 | /* |
||
| 30 | * fields we accept to create |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $updateFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company']; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * set objects |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | public function onConstruct() |
||
| 42 | {
|
||
| 43 | $this->model = new Users(); |
||
| 44 | |||
| 45 | //if you are not a admin you cant see all the users |
||
| 46 | if (!$this->userData->hasRole('Default.Admins')) {
|
||
| 47 | $this->additionalSearchFields = [ |
||
| 48 | ['id', ':', $this->userData->getId()], |
||
| 49 | ]; |
||
| 50 | } else {
|
||
| 51 | //admin get all the users for this company |
||
| 52 | $this->additionalSearchFields = [ |
||
| 53 | ['default_company', ':', $this->userData->default_company], |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get Uer |
||
| 60 | * |
||
| 61 | * @param mixed $id |
||
| 62 | * |
||
| 63 | * @method GET |
||
| 64 | * @url /v1/users/{id}
|
||
| 65 | * |
||
| 66 | * @return Phalcon\Http\Response |
||
| 67 | */ |
||
| 68 | public function getById($id) : Response |
||
| 69 | {
|
||
| 70 | //find the info |
||
| 71 | $user = $this->model->findFirst([ |
||
| 72 | 'id = ?0 AND is_deleted = 0', |
||
| 73 | 'bind' => [$this->userData->getId()], |
||
| 74 | ]); |
||
| 75 | |||
| 76 | $user->password = null; |
||
| 77 | |||
| 78 | //get relationship |
||
| 79 | if ($this->request->hasQuery('relationships')) {
|
||
| 80 | $relationships = $this->request->getQuery('relationships', 'string');
|
||
| 81 | |||
| 82 | $user = QueryParser::parseRelationShips($relationships, $user); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($user) {
|
||
| 86 | return $this->response($user); |
||
| 87 | } else {
|
||
| 88 | throw new Exception('Record not found');
|
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Update a User Info |
||
| 94 | * |
||
| 95 | * @method PUT |
||
| 96 | * @url /v1/users/{id}
|
||
| 97 | * |
||
| 98 | * @return Phalcon\Http\Response |
||
| 99 | */ |
||
| 100 | public function edit($id) : Response |
||
| 101 | {
|
||
| 102 | if ($user = $this->model->findFirst($this->userData->getId())) {
|
||
| 103 | $request = $this->request->getPut(); |
||
| 104 | |||
| 105 | if (empty($request)) {
|
||
| 106 | $request = $this->request->getJsonRawBody(true); |
||
| 107 | } |
||
| 108 | |||
| 109 | //clean pass |
||
| 110 | if (array_key_exists('password', $request) && !empty($request['password'])) {
|
||
| 111 | $user->password = Users::passwordHash($request['password']); |
||
| 112 | unset($request['password']); |
||
| 113 | } |
||
| 114 | |||
| 115 | //clean default company |
||
| 116 | if (array_key_exists('default_company', $request)) {
|
||
| 117 | //@todo check if I belong to this company |
||
| 118 | if ($company = Companies::findFirst($request['default_company'])) {
|
||
| 119 | $user->default_company = $company->getId(); |
||
| 120 | unset($request['default_company']); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | //update |
||
| 125 | if ($user->update($request, $this->updateFields)) {
|
||
| 126 | $user->password = null; |
||
| 127 | return $this->response($user); |
||
| 128 | } else {
|
||
| 129 | //didnt work |
||
| 130 | throw new Exception($user->getMessages()[0]); |
||
| 131 | } |
||
| 132 | } else {
|
||
| 133 | throw new Exception('Record not found');
|
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Add users notifications |
||
| 139 | * |
||
| 140 | * @param int $id |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function updateNotifications($id): Response |
||
| 144 | {
|
||
| 145 | //get the notification array |
||
| 146 | //delete the current ones |
||
| 147 | //iterate and save into users |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Associate a Device with the corrent loggedin user |
||
| 152 | * |
||
| 153 | * @url /users/{id}/device
|
||
| 154 | * @method POST |
||
| 155 | * @return Response |
||
| 156 | */ |
||
| 157 | public function devices(): Response |
||
| 158 | {
|
||
| 159 | //Ok let validate user password |
||
| 160 | $validation = new Validation(); |
||
| 161 | $validation->add('app', new PresenceOf(['message' => _('App name is required.')]));
|
||
| 162 | $validation->add('deviceId', new PresenceOf(['message' => _('device ID is required.')]));
|
||
| 163 | |||
| 164 | //validate this form for password |
||
| 165 | $messages = $validation->validate($this->request->getPost()); |
||
| 166 | if (count($messages)) {
|
||
| 167 | foreach ($messages as $message) {
|
||
| 168 | throw new BadRequestHttpException((string) $message); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $app = $this->request->getPost('app', 'string');
|
||
| 173 | $deviceId = $this->request->getPost('deviceId', 'string');
|
||
| 174 | |||
| 175 | //get the app source |
||
| 176 | if ($source = Sources::getByTitle($app)) {
|
||
| 177 | if (!$userSource = UserLinkedSources::findFirst(['conditions' => 'user_id = ?0 and source_user_id_text =?1', 'bind' => [$this->userData->getId(), $deviceId]])) {
|
||
| 178 | $userSource = new UserLinkedSources(); |
||
| 179 | $userSource->user_id = $this->userData->getId(); |
||
| 180 | $userSource->source_id = $source->source_id; |
||
| 181 | $userSource->source_user_id = $this->userData->getId(); |
||
| 182 | $userSource->source_user_id_text = $deviceId; |
||
| 183 | $userSource->source_username = $this->userData->displayname . ' ' . $app; |
||
| 184 | |||
| 185 | if (!$userSource->save()) {
|
||
| 186 | throw new UnprocessableEntityHttpException(current($userSource->getMessages())); |
||
| 187 | } |
||
| 188 | |||
| 189 | $msg = 'User Device Associated'; |
||
| 190 | } else {
|
||
| 191 | $msg = 'User Device Already Associated'; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | //clean password @todo move this to a better place |
||
| 196 | $this->userData->password = null; |
||
| 197 | |||
| 198 | return $this->response([ |
||
| 199 | 'msg' => $msg, |
||
| 200 | 'user' => $this->userData |
||
| 201 | ]); |
||
| 202 | } |
||
| 203 | } |
||
| 204 |