bakaphp /
phalcon-api
| 1 | <?php |
||||||
| 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(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 44 | |||||||
| 45 | //if you are not a admin you cant see all the users |
||||||
| 46 | if (!$this->userData->hasRole('Default.Admins')) {
|
||||||
|
0 ignored issues
–
show
The property
userData does not exist on Gewaer\Api\Controllers\UsersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 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 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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()], |
||||||
|
0 ignored issues
–
show
The property
userData does not exist on Gewaer\Api\Controllers\UsersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
The type
Gewaer\Api\Controllers\QueryParser was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 83 | } |
||||||
| 84 | |||||||
| 85 | if ($user) {
|
||||||
| 86 | return $this->response($user); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 87 | } else {
|
||||||
| 88 | throw new Exception('Record not found');
|
||||||
|
0 ignored issues
–
show
|
|||||||
| 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())) {
|
||||||
|
0 ignored issues
–
show
The property
userData does not exist on Gewaer\Api\Controllers\UsersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 103 | $request = $this->request->getPut(); |
||||||
|
0 ignored issues
–
show
The method
getPut() does not exist on Phalcon\Http\RequestInterface. Did you maybe mean getPost()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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 |
||||||
|
0 ignored issues
–
show
The parameter
$id is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 144 | {
|
||||||
| 145 | //get the notification array |
||||||
| 146 | //delete the current ones |
||||||
| 147 | //iterate and save into users |
||||||
| 148 | } |
||||||
|
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return Phalcon\Http\Response. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||||||
| 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 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..