Projekt

Obecné

Profil

Stáhnout (1.48 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3
namespace App\Model;
4

    
5
use App\Model\Repository\UserRepository;
6
use App\Model\Repository\UserRoleRepository;
7
use Nette\Security\AuthenticationException;
8
use Nette\Security\IAuthenticator;
9
use Nette\Security\Identity;
10

    
11
class Authenticator implements IAuthenticator
12
{
13

    
14
    /** @var UserRepository */
15
    private $userRepository;
16
    /** @var UserRoleRepository */
17
    private $userRoleRepository;
18

    
19
    public function __construct(UserRepository $userRepository, UserRoleRepository $userRoleRepository)
20
    {
21

    
22
        $this->userRepository = $userRepository;
23
        $this->userRoleRepository = $userRoleRepository;
24
    }
25

    
26
    /**
27
     * Autentikuje uživatele
28
     *
29
     * @param array $credentials
30
     * @return Identity
31
     * @throws AuthenticationException
32
     */
33
    function authenticate(array $credentials)
34
    {
35
        list($login, $password) = $credentials;
36

    
37
        $row = $this->userRepository->findByLogin($login);
38
        if(!$row){
39
            throw new AuthenticationException('Uživatel nebyl nalezen.');
40
        }
41

    
42
        $hash = md5($password);
43

    
44
        if ($hash !== $row->{UserRepository::COLUMN_PASSWORD})
45
        {
46
            throw new AuthenticationException('Nesprávné heslo.');
47
        }
48

    
49
        $roles = $row->related(UserRoleRepository::TABLE_NAME, UserRoleRepository::COLUMN_USER_ID)
50
            ->fetchField(UserRoleRepository::COLUMN_ROLE_ID);
51

    
52
        return new UserIdentity($row->{UserRepository::COLUMN_ID}, $row->{UserRepository::COLUMN_USERNAME}, $roles);
53
    }
54
}
(1-1/2)