Completed
Push — develop ( 77b219...ede896 )
by
unknown
15:55 queued 07:55
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
2
namespace Auth\Adapter;
3
4
use Zend\Authentication\Adapter\AbstractAdapter;
5
use Zend\Authentication\Result;
6
use Auth\Entity\Filter\CredentialFilter;
7
8
/**
9
 * This class allow to authenticate with a user/password account.
10
 *
11
 * @author Mathias Gelhausen <[email protected]>
12
 * @author Carsten Bleek     <[email protected]>
13
 */
14
class User extends AbstractAdapter
15
{
16
    /**
17
     * User entity repository
18
     *
19
     * @var \Core\Repository\RepositoryInterface
20
     */
21
    protected $repository;
22
23
    /**
24
     * Initial user.
25
     *
26
     * @var null|\Auth\Entity\UserInterface
27
     */
28
    protected $defaultUser;
29
30
    /**
31
     * Creates a new user authentication adapter
32
     *
33
     * @param \Core\Repository\RepositoryInterface $repository User entity repository
34
     * @param null|string $identity
35
     * @param null|string $credential
36
     */
37
    public function __construct($repository, $identity = null, $credential = null)
38
    {
39
        $this->repository = $repository;
40
        $this->setIdentity($identity);
41
        $this->setCredential($credential);
42
    }
43
44
    /**
45
     * Gets the user repository
46
     *
47
     * @return \Core\Repository\RepositoryInterface
48
     */
49
    public function getRepository()
50
    {
51
        return $this->repository;
52
    }
53
54
    /**
55
     * Sets default user login and password.
56
     *
57
     * If no password is provided,
58
     *
59
     * @param string $login
60
     * @param string $password
61
     * @param string $role (default='recruiter')
62
     *
63
     * @return self
64
     */
65
    public function setDefaultUser($login, $password, $role = \Auth\Entity\User::ROLE_RECRUITER)
66
    {
67
        $this->defaultUser = array($login, $password, $role);
68
        return $this;
69
    }
70
71
    /**
72
     * Performs an authentication attempt
73
     *
74
     * {@inheritDoc}
75
     *
76
     */
77
    public function authenticate()
78
    {
79
        /* @var $users \Auth\Repository\User */
80
        $identity    = $this->getIdentity();
81
        $users       = $this->getRepository();
82
        $user        = $users->findByLogin($identity, ['allowDeactivated' => true]);
83
        $filter      = new CredentialFilter();
84
        $credential  = $this->getCredential();
85
86
        
87
        if (!$user || $user->getCredential() != $filter->filter($credential)) {
88
            return new Result(Result::FAILURE_CREDENTIAL_INVALID, $identity, array('User not known or invalid credential'));
89
        }
90
        
91
        return new Result(Result::SUCCESS, $user->getId());
92
    }
93
}
94