1 <?php
2
3 namespace PLus\Orders\Repositories\Security;
4
5 use Nette;
6 use PLus\Orders\Repositories\Entities;
7
8 9 10 11 12
13 class UserManager extends \Nette\Object implements Nette\Security\IAuthenticator {
14
15
16 private $userRepository;
17
18
19 private $roleRepository;
20
21 function __construct(Entities\UserRepository $userRepository, RoleRepository $roleRepository) {
22 $this->userRepository = $userRepository;
23 $this->roleRepository = $roleRepository;
24 }
25
26
27
28 public function authenticate(array $credentials) {
29 list($username, $password) = $credentials;
30
31
32 $row = $this->userRepository->findByName($username);
33
34
35
36
37
38 $arr=array();
39 $i=0;
40 $selection = $this->roleRepository->findRoleByName($username);
41 foreach ($selection as $select) {
42 $arr[$i] = $select->code;
43 $i++;
44 }
45
46 if (!$row) {
47 throw new Nette\Security\AuthenticationException("Uživatel '$username' nenalezen.", self::IDENTITY_NOT_FOUND);
48 }
49
50 if ($row->password !== self::calculateHash($password, $row->password)) {
51 throw new Nette\Security\AuthenticationException("Chybné heslo.", self::INVALID_CREDENTIAL);
52 }
53
54 $data = $row->toArray();
55
56 unset($data['password']);
57
58 return new Nette\Security\Identity($row->id, $arr, $data);
59
60 }
61
62 public static function calculateHash($password) {
63 return sha1($password);
64 }
65
66 }
67