Passed
Pull Request — master (#602)
by Sergei
01:51
created

ChangePasswordController::change()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 2
b 0
f 0
nc 6
nop 6
dl 0
loc 38
rs 8.4444
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;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\FormModel\FormHydrator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\FormModel\FormHydrator 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\Http\Method;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Method 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Yiisoft\Translator\TranslatorInterface as Translator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Translator\TranslatorInterface 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Yiisoft\User\CurrentUser;
0 ignored issues
show
Bug introduced by
The type Yiisoft\User\CurrentUser 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Yiisoft\Yii\View\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\ViewRenderer 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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