Passed
Pull Request — master (#602)
by Ross
01:35
created

ChangePasswordController::change()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 1
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->currentUser = $currentUser;
30
      $this->translator = $translator;
31
      $this->viewRenderer = $viewRenderer->withControllerName('changepassword');
32
    }
33
    
34
    public function change(
35
      AuthService $authService,
36
      Identity $identity,
37
      IdentityRepository $identityRepository,
38
      ServerRequestInterface $request,
39
      FormHydrator $formHydrator,
40
      ChangePasswordForm $changePasswordForm
41
    ): ResponseInterface {
42
      // permit an authenticated user with permission editPost (i.e. not a guest) only and null !== current user
43
      if (!$authService->isGuest()) {
44
        // see demo/blog/resources/rbac  
45
        if ($this->currentUser->can('editPost',[])) {
46
          // readonly the login detail on the change form
47
          $identity_id = $this->currentUser->getIdentity()->getId();
48
          if (null!==$identity_id) {
49
            $identity = $identityRepository->findIdentity($identity_id);
50
            if (null!==$identity) {
51
              // Identity and User are in a HasOne relationship so no null value
52
              $login = $identity->getUser()?->getLogin();
53
              if ($request->getMethod() === Method::POST
54
                && $formHydrator->populate($changePasswordForm, $request->getParsedBody())
55
                && $changePasswordForm->change() 
56
              ) {
57
                // Identity implements CookieLoginIdentityInterface: ensure the regeneration of the cookie auth key by means of $authService->logout();
58
                // @see vendor\yiisoft\user\src\Login\Cookie\CookieLoginIdentityInterface 
59
60
                // Specific note: "Make sure to invalidate earlier issued keys when you implement force user logout,
61
                // PASSWORD CHANGE and other scenarios, that require forceful access revocation for old sessions.
62
                // The authService logout function will regenerate the auth key here => overwriting any auth key
63
                $authService->logout();
64
                return $this->redirectToMain();
65
              }
66
              return $this->viewRenderer->render('change', ['formModel' => $changePasswordForm, 'login' => $login]);
67
            } // identity
68
          } // identity_id 
69
        } // current user
70
      } // auth service  
71
      return $this->redirectToMain();
72
    } // reset
73
        
74
    private function redirectToMain(): ResponseInterface
75
    {
76
      return $this->webService->getRedirectResponse('site/index');
77
    }
78
}
79