1
|
<?php
|
2
|
|
3
|
namespace App\Model;
|
4
|
|
5
|
use App\Model\Repository\RoleRepository;
|
6
|
use App\Model\Repository\UserRepository;
|
7
|
use App\Model\Repository\UserRoleRepository;
|
8
|
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
|
list($login, $password) = $credentials;
|
37
|
|
38
|
$row = $this->userRepository->findByLogin($login);
|
39
|
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
|
$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
|
|
56
|
return new Identity($row->{UserRepository::COLUMN_ID}, $roles, $row);
|
57
|
}
|
58
|
}
|