|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Auth\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use App\Auth\AuthService; |
|
8
|
|
|
use App\Auth\Identity; |
|
9
|
|
|
use App\Auth\IdentityRepository; |
|
10
|
|
|
use App\Auth\Form\ChangePasswordForm; |
|
11
|
|
|
use App\Service\WebControllerService; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
|
|
|
|
|
14
|
|
|
use Yiisoft\FormModel\FormHydrator; |
|
|
|
|
|
|
15
|
|
|
use Yiisoft\Http\Method; |
|
|
|
|
|
|
16
|
|
|
use Yiisoft\Translator\TranslatorInterface as Translator; |
|
|
|
|
|
|
17
|
|
|
use Yiisoft\User\CurrentUser; |
|
|
|
|
|
|
18
|
|
|
use Yiisoft\Yii\View\ViewRenderer; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
final class ChangePasswordController |
|
21
|
|
|
{ |
|
22
|
|
|
public function __construct( |
|
23
|
|
|
private Translator $translator, |
|
24
|
|
|
private CurrentUser $currentUser, |
|
25
|
|
|
private WebControllerService $webService, |
|
26
|
|
|
private ViewRenderer $viewRenderer, |
|
27
|
|
|
) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('changepassword'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function change( |
|
33
|
|
|
AuthService $authService, |
|
34
|
|
|
Identity $identity, |
|
35
|
|
|
IdentityRepository $identityRepository, |
|
36
|
|
|
ServerRequestInterface $request, |
|
37
|
|
|
FormHydrator $formHydrator, |
|
38
|
|
|
ChangePasswordForm $changePasswordForm |
|
39
|
|
|
): ResponseInterface { |
|
40
|
|
|
// permit an authenticated user with permission editPost (i.e. not a guest) only and null !== current user |
|
41
|
|
|
if (!$authService->isGuest()) { |
|
42
|
|
|
// see demo/blog/resources/rbac |
|
43
|
|
|
if ($this->currentUser->can('editPost',[])) { |
|
44
|
|
|
// readonly the login detail on the change form |
|
45
|
|
|
$identity_id = $this->currentUser->getIdentity()->getId(); |
|
46
|
|
|
if (null!==$identity_id) { |
|
47
|
|
|
$identity = $identityRepository->findIdentity($identity_id); |
|
48
|
|
|
if (null!==$identity) { |
|
49
|
|
|
// Identity and User are in a HasOne relationship so no null value |
|
50
|
|
|
$login = $identity->getUser()?->getLogin(); |
|
51
|
|
|
if ($request->getMethod() === Method::POST |
|
52
|
|
|
&& $formHydrator->populate($changePasswordForm, $request->getParsedBody()) |
|
53
|
|
|
&& $changePasswordForm->change() |
|
54
|
|
|
) { |
|
55
|
|
|
// Identity implements CookieLoginIdentityInterface: ensure the regeneration of the cookie auth key by means of $authService->logout(); |
|
56
|
|
|
// @see vendor\yiisoft\user\src\Login\Cookie\CookieLoginIdentityInterface |
|
57
|
|
|
|
|
58
|
|
|
// Specific note: "Make sure to invalidate earlier issued keys when you implement force user logout, |
|
59
|
|
|
// PASSWORD CHANGE and other scenarios, that require forceful access revocation for old sessions. |
|
60
|
|
|
// The authService logout function will regenerate the auth key here => overwriting any auth key |
|
61
|
|
|
$authService->logout(); |
|
62
|
|
|
return $this->redirectToMain(); |
|
63
|
|
|
} |
|
64
|
|
|
return $this->viewRenderer->render('change', ['formModel' => $changePasswordForm, 'login' => $login]); |
|
65
|
|
|
} // identity |
|
66
|
|
|
} // identity_id |
|
67
|
|
|
} // current user |
|
68
|
|
|
} // auth service |
|
69
|
|
|
return $this->redirectToMain(); |
|
70
|
|
|
} // reset |
|
71
|
|
|
|
|
72
|
|
|
private function redirectToMain(): ResponseInterface |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->webService->getRedirectResponse('site/index'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths