Completed
Push — feature/evo-2472-whoami ( c50209...9b4500 )
by Jan
30:40
created

loadUserByUsername()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * airlock authkey based consultant user provider
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Provider;
7
8
use Graviton\SecurityBundle\Entities\SecurityConsultant;
9
use Graviton\SecurityBundle\Entities\SecurityContract;
10
use GravitonDyn\ContractBundle\Document\Contract;
11
use \Graviton\RestBundle\Model\ModelInterface;
12
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
13
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
14
use Symfony\Component\Security\Core\User\UserInterface;
15
use Symfony\Component\Security\Core\User\UserProviderInterface;
16
17
/**
18
 * Class AirlockAuthenticationKeyUserProvider
19
 *
20
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
21
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
22
 * @link     http://swisscom.ch
23
 */
24
class AirlockAuthenticationKeyConsultantProvider implements UserProviderInterface
25
{
26
    /**
27
     * @var \Graviton\RestBundle\Model\ModelInterface
28
     */
29
    private $documentModel;
30
31
    /**
32
     * @param \Graviton\RestBundle\Model\ModelInterface $contract contract to use as documentModel
33
     */
34
    public function __construct(ModelInterface $contract)
35
    {
36
        $this->documentModel = $contract;
37
    }
38
39
    /**
40
     * Loads the user for the given username.
41
     *
42
     * This method must throw UsernameNotFoundException if the user is not
43
     * found.
44
     *
45
     * @param string $username the consultants username
46
     *
47
     * @return \Symfony\Component\Security\Core\User\UserInterface
48
     *
49
     * @see \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
50
     *
51
     * @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException if the user is not found
52
     */
53
    public function loadUserByUsername($username)
54
    {
55
        $user = $this->documentModel->getRepository()->findOneBy(array('username' => $username));
56
57
        if($user) {
58
           return new SecurityConsultant($user, array('ROLE_GRAVITON_USER'));
0 ignored issues
show
Documentation introduced by
array('ROLE_GRAVITON_USER') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<Sym...curity\Core\Role\Role>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        }
60
61
        throw new UsernameNotFoundException();
62
    }
63
64
    /**
65
     * Finds a contract based on the provided ApiKey.
66
     *
67
     * @param string $apiKey key from airlock
68
     *
69
     * @return string
70
     */
71 View Code Duplication
    public function getUsernameForApiKey($apiKey)
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...
72
    {
73
        $contractId = '';
74
        /** @var \GravitonDyn\ContractBundle\Document\Contract $contract */
75
        $contract = $this->documentModel->getRepository()->findOneBy(array('number' => $apiKey));
76
        if ($contract instanceof Contract) {
0 ignored issues
show
Bug introduced by
The class GravitonDyn\ContractBundle\Document\Contract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
            $contractId = $contract->getId();
78
        }
79
        return $contractId;
80
    }
81
82
    /**
83
     * Refreshes the user for the account interface.
84
     *
85
     * It is up to the implementation to decide if the user data should be
86
     * totally reloaded (e.g. from the database), or if the UserInterface
87
     * object can just be merged into some internal array of users / identity
88
     * map.
89
     *
90
     * @param \Symfony\Component\Security\Core\User\UserInterface $user user to refresh
91
     *
92
     * @return \Symfony\Component\Security\Core\User\UserInterface
93
     *
94
     * @throws \Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported
95
     */
96
    public function refreshUser(UserInterface $user)
97
    {
98
        // this is used for storing authentication in the session
99
        // but in this example, the token is sent in each request,
100
        // so authentication can be stateless. Throwing this exception
101
        // is proper to make things stateless
102
        throw new UnsupportedUserException();
103
    }
104
105
    /**
106
     * Whether this provider supports the given user class.
107
     *
108
     * @param string $class class to check for support
109
     *
110
     * @return bool
111
     */
112
    public function supportsClass($class)
113
    {
114
        return $class instanceof \Symfony\Component\Security\Core\User\UserInterface;
115
    }
116
}
117