eZRemoteUserLoginUser::_loginUser()   A
last analyzed

Complexity

Conditions 2
Paths 8

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
rs 9.312
cc 2
nc 8
nop 3
1
<?php
2
3
use Kaliop\IdentityManagementBundle\Security\Authentication\Provider\RemoteUserAuthenticationProvider;
4
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
5
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
6
7
class eZRemoteUserLoginUser extends eZUser
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
10
    /**
11
     * We need to override this because parent call uses 'self' instead of 'static'
12
     * @param string $login
13
     * @param string $password
14
     * @param bool $authenticationMatch
15
     * @return bool|mixed
16
     */
17
    static function loginUser( $login, $password, $authenticationMatch = false )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    {
19
        $user = self::_loginUser( $login, $password, $authenticationMatch );
20
21
        if ( is_object( $user ) )
22
        {
23
            self::loginSucceeded( $user );
24
            return $user;
25
        }
26
        else
27
        {
28
            self::loginFailed( $user, $login );
29
            return false;
30
        }
31
    }
32
33
    protected static function _loginUser( $login, $password, $authenticationMatch = false )
34
    {
35
        $fwName = eZINI::instance('identitymanagement.ini')->variable('GeneralSettings', 'FirewallName');
36
37
        $container = ezpKernel::instance()->getServiceContainer();
38
39
        // nb: this string is related to the name of the firewall!
40
        /** @var RemoteUserAuthenticationProvider $remoteUserAuthProvider */
41
        $remoteUserAuthProvider = $container->get('security.authentication.provider.remoteuser.'.$fwName);
42
        $token = new UsernamePasswordToken($login, $password, $fwName, array('ROLE_USER'));
43
44
        try {
45
            // get the authorized token, which contains the remoteUser
46
            $authToken = $remoteUserAuthProvider->authenticate($token);
47
            // convert the remoteUser into an eZP user (this creates the user in the db if needed)
48
            $request = $container->get('request');
49
            $event = new InteractiveLoginEvent($request, $authToken);
50
            $container->get("event_dispatcher")->dispatch("security.interactive_login", $event);
51
52
            // now get back the eZP user for the eZ4 stack
53
            /** @var eZ\Publish\Core\Repository\Values\User\User $user */
54
            $user = $container->get('security.token_storage')->getToken()->getUser()->getAPIUser();
55
56
            // and set back an anon token for Sf, as after the redirect, that's what the eZ\Bundle\EzPublishLegacyBundle\EventListener\RequestListener expects
57
            $container->get('security.token_storage')->setToken(null);
58
59
            /// @todo shall we check isenabled ?
60
61
            return self::fetch($user->id);
62
63
        } catch(\Exception $e) {
64
            /// @todo make it easier to tell apart system error from user errors such as bad password...
65
66
            eZDebug::writeError($e->getMessage(), __METHOD__ );
67
68
            return false;
69
        }
70
    }
71
72
}
73