Failed Conditions
Push — issue#777 ( 19a7c1 )
by Guilherme
08:25
created

UserProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsClass() 0 7 2
B createUser() 0 24 1
A refreshUser() 0 8 2
A loadUserByUsername() 0 13 2
A __construct() 0 7 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Security\OIDC;
12
13
use Doctrine\ORM\EntityManager;
14
use LoginCidadao\CoreBundle\Model\PersonInterface;
15
use OAuth2\ServerBundle\Entity\User;
16
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
17
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
18
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
19
use Symfony\Component\Security\Core\User\UserProviderInterface;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
use OAuth2\ServerBundle\User\OAuth2UserProvider;
22
23
class UserProvider extends OAuth2UserProvider implements UserProviderInterface
24
{
25
    private $em;
26
    private $encoderFactory;
27
28
    public function __construct(
29
        EntityManager $entityManager,
30
        EncoderFactoryInterface $encoderFactory
31
    ) {
32
        parent::__construct($entityManager, $encoderFactory);
33
        $this->em = $entityManager;
34
        $this->encoderFactory = $encoderFactory;
35
    }
36
37
    /**
38
     * Loads the user for the given username.
39
     *
40
     * This method must throw UsernameNotFoundException if the user is not
41
     * found.
42
     *
43
     * @param string $username The username
44
     *
45
     * @return UserInterface
46
     *
47
     * @see UsernameNotFoundException
48
     *
49
     * @throws UsernameNotFoundException if the user is not found
50
     *
51
     */
52
    public function loadUserByUsername($username)
53
    {
54
        /** @var PersonInterface|null $user */
55
        $user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')
56
            ->find($username);
57
58
        if (!$user) {
59
            throw new UsernameNotFoundException(sprintf(
60
                'Username "%s" not found.', $username
61
            ));
62
        }
63
64
        return $user;
65
    }
66
67
    /**
68
     * Refreshes the user for the account interface.
69
     *
70
     * It is up to the implementation to decide if the user data should be
71
     * totally reloaded (e.g. from the database), or if the UserInterface
72
     * object can just be merged into some internal array of users / identity
73
     * map.
74
     * @param UserInterface $user
75
     *
76
     * @return UserInterface
77
     *
78
     * @throws UnsupportedUserException if the account is not supported
79
     */
80
    public function refreshUser(UserInterface $user)
81
    {
82
        if (!$user instanceof PersonInterface) {
83
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.',
84
                get_class($user)));
85
        }
86
87
        return $this->loadUserByUsername($user->getUsername());
88
    }
89
90
    /**
91
     * Whether this provider supports the given user class
92
     *
93
     * @param string $class
94
     *
95
     * @return Boolean
96
     */
97
    public function supportsClass($class)
98
    {
99
        if ($class == 'Person') {
100
            return true;
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Creates a new user
108
     *
109
     * @param string $username
110
     *
111
     * @param string $password
112
     *
113
     * @param array $roles
114
     *
115
     * @param array $scopes
116
     *
117
     * @return UserInterface
118
     */
119
    public function createUser(
120
        $username,
121
        $password,
122
        array $roles = array(),
123
        array $scopes = array()
124
    ) {
125
        /** @var User $user */
126
        $user = parent::createUser($username, $password, $roles, $scopes);
127
128
        // Generate password
129
        $salt = $this->generateSalt();
130
        $password = $this->encoderFactory->getEncoder($user)
131
            ->encodePassword($password, $salt);
132
133
        $user->setSalt($salt);
134
        $user->setPassword($password);
135
        $user->setUsername($username);
136
        $user->setRoles($roles);
137
        $user->setScopes($scopes);
138
139
        $this->em->persist($user);
140
        $this->em->flush();
141
142
        return $user;
143
    }
144
}
145