Completed
Pull Request — master (#137)
by
unknown
11:35
created

ApiKeyUserProvider::supportsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Security;
4
5
use Symfony\Component\Security\Core\User\UserProviderInterface;
6
use Symfony\Component\Security\Core\User\User;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
9
use Doctrine\Common\Persistence\ManagerRegistry;
10
11
12
13
class ApiKeyUserProvider implements UserProviderInterface
14
{
15
16
    private $registry;
17
18
    public function __construct(ManagerRegistry $registry)
19
    {
20
        $this->registry = $registry;
21
    }
22
23
24
    public function getUsernameForApiKey($apiKey)
25
    {
26
        // Look up the username based on the token in the database, via
27
        // an API call, or do something entirely different
28
29
        $user=$this->registry->getManager()
30
            ->getRepository('AppBundle:Customer')
31
            ->findUsernameByApiKey($apiKey);
32
        if($user){
33
            $username=$user[0]['username'];
34
        }
35
        else $username=$user;
36
37
       return $username;
38
    }
39
40
    public function loadUserByUsername($username)
41
    {
42
        return new User(
43
            $username,
44
            null,
45
            // the roles for the user - you may choose to determine
46
            // these dynamically somehow based on the user
47
            array('ROLE_API')
48
        );
49
    }
50
51
    public function refreshUser(UserInterface $user)
52
    {
53
        // this is used for storing authentication in the session
54
        // but in this example, the token is sent in each request,
55
        // so authentication can be stateless. Throwing this exception
56
        // is proper to make things stateless
57
        throw new UnsupportedUserException();
58
    }
59
60
    public function supportsClass($class)
61
    {
62
        return User::class === $class;
63
    }
64
65
    public function createApiKeyUser(){
66
67
    }
68
}