Skip to content

Commit 529964f

Browse files
authored
[dbAuth] add pkName to Columnnames if not specified inside 'dbAuth.returnedColumns'
If you try to change the password and $pkName isn't contained inside 'dbAuth.returnedColumns', dbAuth won't know the id column and returns error: <b>Warning</b>: Undefined array key "id" in <b>C:\XAMPP\htdocs\api.php</b> on line <b>7978</b><br /> {"code":9999,"message":"Tqdev\\PhpCrudApi\\Database\\GenericDB::updateSingle(): Argument #3 ($id) must be of type string, null given, called in C:\\XAMPP\\htdocs\\api.php on line 7978"}
1 parent a8d7605 commit 529964f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
112112
if (strlen($newPassword) < $passwordLength) {
113113
return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
114114
}
115-
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
115+
$userColumns = $columnNames;
116+
if(!in_array($pkName, $columnNames)){
117+
array_push($userColumns, $pkName);
118+
}
119+
$users = $this->db->selectAll($table, $userColumns, $condition, $columnOrdering, 0, 1);
116120
foreach ($users as $user) {
117121
if (password_verify($password, $user[$passwordColumnName]) == 1) {
118122
if (!headers_sent()) {
@@ -121,6 +125,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
121125
$data = [$passwordColumnName => password_hash($newPassword, PASSWORD_DEFAULT)];
122126
$this->db->updateSingle($table, $data, $user[$pkName]);
123127
unset($user[$passwordColumnName]);
128+
if(!in_array($pkName, $columnNames)){
129+
unset($user[$pkName]);
130+
}
124131
return $this->responder->success($user);
125132
}
126133
}

0 commit comments

Comments
 (0)