Projekt

Obecné

Profil

Stáhnout (1.63 KB) Statistiky
| Větev: | Tag: | Revize:
1 6b983650 Filip Jani
<?php
2
3 00f32800 Filip Jani
namespace App\Model;
4 6b983650 Filip Jani
5 3aa5f2d2 Filip Jani
use App\Model\Repository\RoleRepository;
6 00f32800 Filip Jani
use App\Model\Repository\UserRepository;
7
use App\Model\Repository\UserRoleRepository;
8 6b983650 Filip Jani
use Nette\Security\AuthenticationException;
9
use Nette\Security\IAuthenticator;
10
use Nette\Security\Identity;
11
12
class Authenticator implements IAuthenticator
13
{
14
15
    /** @var UserRepository */
16
    private $userRepository;
17
    /** @var UserRoleRepository */
18
    private $userRoleRepository;
19
20
    public function __construct(UserRepository $userRepository, UserRoleRepository $userRoleRepository)
21
    {
22
23
        $this->userRepository = $userRepository;
24
        $this->userRoleRepository = $userRoleRepository;
25
    }
26
27
    /**
28
     * Autentikuje uživatele
29
     *
30
     * @param array $credentials
31
     * @return Identity
32
     * @throws AuthenticationException
33
     */
34
    function authenticate(array $credentials)
35
    {
36 00f32800 Filip Jani
        list($login, $password) = $credentials;
37 6b983650 Filip Jani
38 00f32800 Filip Jani
        $row = $this->userRepository->findByLogin($login);
39 6b983650 Filip Jani
        if(!$row){
40
            throw new AuthenticationException('Uživatel nebyl nalezen.');
41
        }
42
43
        $hash = md5($password);
44
45
        if ($hash !== $row->{UserRepository::COLUMN_PASSWORD})
46
        {
47
            throw new AuthenticationException('Nesprávné heslo.');
48
        }
49
50 3aa5f2d2 Filip Jani
        $userRoles = $row->related(UserRoleRepository::TABLE_NAME, UserRoleRepository::COLUMN_USER_ID)->fetchAll();
51
        foreach ($userRoles as $userRole)
52
        {
53
            $roles[] = $userRole->ref(RoleRepository::TABLE_NAME, UserRoleRepository::COLUMN_ROLE_ID)->{RoleRepository::COLUMN_NAME};
54
        }
55 6b983650 Filip Jani
56 3aa5f2d2 Filip Jani
        return new Identity($row->{UserRepository::COLUMN_ID}, $roles, $row);
57 6b983650 Filip Jani
    }
58
}