Projekt

Obecné

Profil

Stáhnout (1.48 KB) Statistiky
| Větev: | Tag: | Revize:
1 6b983650 Filip Jani
<?php
2
3 793a2c68 Filip Jani
namespace App\Model;
4 6b983650 Filip Jani
5 793a2c68 Filip Jani
use App\Model\Repository\UserRepository;
6
use App\Model\Repository\UserRoleRepository;
7 6b983650 Filip Jani
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 793a2c68 Filip Jani
        list($login, $password) = $credentials;
36 6b983650 Filip Jani
37 793a2c68 Filip Jani
        $row = $this->userRepository->findByLogin($login);
38 6b983650 Filip Jani
        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 793a2c68 Filip Jani
        return new UserIdentity($row->{UserRepository::COLUMN_ID}, $row->{UserRepository::COLUMN_USERNAME}, $roles);
53 6b983650 Filip Jani
    }
54
}