RemoteUser::supportsClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Kaliop\IdentityManagementBundle\Security\User\Provider;
4
5
use Symfony\Component\Security\Core\User\UserProviderInterface;
6
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
7
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
use Kaliop\IdentityManagementBundle\Security\User\AMSUser as UserClass;
10
use Kaliop\IdentityManagementBundle\Security\User\RemoteUserProviderInterface;
11
use Kaliop\IdentityManagementBundle\Security\User\RemoteUser as KaliopRemoteUser;
12
use Kaliop\IdentityManagementBundle\Security\User\RemoteUserHandlerInterface;
13
use eZ\Publish\Core\MVC\Symfony\Security\User\APIUserProviderInterface;
14
use eZ\Publish\Core\MVC\Symfony\Security\User as eZMVCUser;
15
use Psr\Log\LoggerInterface;
16
17
class RemoteUser implements UserProviderInterface, RemoteUserProviderInterface
18
{
19
    protected $logger;
20
    protected $eZUserProvider;
21
    protected $handlerMap;
22
    protected $container;
23
24
    /**
25
     * @param APIUserProviderInterface $eZUserProvider the user provider to which we actually delegate finding eZ User
26
     * @param array $handlerMap
27
     */
28
    public function __construct(APIUserProviderInterface $eZUserProvider, array $handlerMap, $container)
29
    {
30
        $this->eZUserProvider = $eZUserProvider;
31
        $this->handlerMap = $handlerMap;
32
        $this->container = $container;
33
    }
34
35
    public function setLogger(LoggerInterface $logger)
36
    {
37
        $this->logger = $logger;
38
    }
39
40
    /**
41
     * @todo throw an exception ?
42
     * @param $username
43
     * @return UserInterface
44
     */
45
    public function loadUserByUsername($username)
46
    {
47
    }
48
49
    /**
50
     * This method is called *on every page after the user logged in*.
51
     * We do not want to call the remote ws on every page.
52
     * We 'might' check in the eZ db if the user is still there and/or enabled, BUT even that might be unnecessary, as
53
     * the remoteuser gets converted to an ezmvcuser by the listener, which means this is only called upon login?
54
     *
55
     * @param UserInterface $user
56
     * @return UserInterface
57
     */
58
    public function refreshUser(UserInterface $user)
59
    {
60
        if (!$user instanceof \Kaliop\IdentityManagementBundle\Security\User\RemoteUser) {
61
            throw new UnsupportedUserException(
62
                sprintf('Instances of "%s" are not supported.', get_class($user))
63
            );
64
        }
65
66
        return $user;
67
    }
68
69
    /**
70
     * Whether this provider supports the given user class.
71
     *
72
     * @param string $class
73
     *
74
     * @return bool
75
     */
76
    public function supportsClass($class)
77
    {
78
        $supportedClass = 'Kaliop\IdentityManagementBundle\Security\User\RemoteUser';
79
        return $class === $supportedClass || is_subclass_of($class, $supportedClass);
80
    }
81
82
    public function loadAPIUserByRemoteUser(KaliopRemoteUser $remoteUser)
83
    {
84
        $repoUser = null;
85
        $userHandler = $this->getHandler($remoteUser);
86
87
        // does eZ user exist? If not, create it, else update it
88
        // NB: it would be nice to be able to wrap these calls in a try/catch block to fix any error during ez user
89
        //     account creation/update, and simply disallow login.
90
        //     Unfortunately, it seems that if at this stage we return null, the Sf session will be set to a logged-in
91
        //     user, while eZP will think that it is an anon user. I tried to fix the Sf session so as to prevent the
92
        //     user from being logged in, without success.
93
        //     This forces the developer to do validation of the user profile data gotten from the remote service inside
94
        //     the client code, which is not as logical/clean...
95
        try {
96
            $repoUser = $userHandler->loadAPIUserByRemoteUser($remoteUser);
97
            if ($repoUser === false) {
98
                // we have to create an eZ MVC user out of an eZ Repo user
99
                $repoUser = $userHandler->createRepoUser($remoteUser);
100
            } else {
101
                $userHandler->updateRepoUser($remoteUser, $repoUser);
102
            }
103
104
            // In case any post-processing is needed, give the user-handler a chance to execute it without the need to
105
            // register further listeners
106
            if (is_callable(array($userHandler, 'onRemoteUserLogin'))) {
107
                $userHandler->onRemoteUserLogin($remoteUser, $repoUser);
0 ignored issues
show
Bug introduced by
The method onRemoteUserLogin() does not seem to exist on object<Kaliop\IdentityMa...teUserHandlerInterface>.

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...
108
            }
109
110
        } catch (\Exception $e) {
111
            if ($this->logger) $this->logger->error("Unexpected error while finding/creating/updating repo user from data gotten from remote service: " . $e->getMessage());
112
            throw $e;
113
        }
114
115
        return $repoUser;
116
    }
117
118
    /**
119
     * @param KaliopRemoteUser $remoteUser
120
     * @return RemoteUserHandlerInterface
121
     * @throws \Exception
122
     */
123 View Code Duplication
    protected function getHandler($remoteUser)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $class = get_class($remoteUser);
126
        if (!isset($this->handlerMap[$class])) {
127
            throw new \Exception("Can not load conversion handler for remote user of class $class");
128
        }
129
        return $this->container->get($this->handlerMap[$class]);
130
    }
131
132
    /**
133
     * A courtesy method, if some other service wants to retrieve a remote-user handler for a given php class.
134
     * Useful to retrieve the remote-user handler before the actual creation of the actual remote-user object, which
135
     * allows f.e. to put in the remote-user handler some validation code
136
     *
137
     * @param string $class a php class name
138
     * @return RemoteUserHandlerInterface
139
     * @throws \Exception
140
     */
141 View Code Duplication
    public function getHandlerForClass($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        if (!isset($this->handlerMap[$class])) {
144
            throw new \Exception("Can not load conversion handler for remote user of class $class");
145
        }
146
        return $this->container->get($this->handlerMap[$class]);
147
    }
148
}