1: <?php
2:
3: namespace Budovy;
4:
5: use Nette;
6: use Nette\Security as NS;
7:
8: 9: 10:
11: class UserManager extends Nette\Object implements NS\IAuthenticator {
12:
13:
14: private $userRepository;
15:
16: public function __construct(UserRepository $userRepository) {
17: $this->userRepository = $userRepository;
18: }
19:
20: 21: 22: 23: 24:
25: public function authenticate(array $credentials) {
26: list($username, $password) = $credentials;
27:
28: $row = $this->userRepository->findByName($username);
29:
30: if (!$row) {
31: throw new NS\AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
32: }
33:
34: if ($row->password !== self::calculateHash($password, $row->password)) {
35: throw new NS\AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
36: }
37:
38: $data = $row->toArray();
39:
40: unset($data['password']);
41:
42: return new NS\Identity($row->id, array($row->role), $data);
43: }
44:
45: 46: 47: 48: 49:
50: public static function calculateHash($password) {
51: return sha1($password);
52: }
53:
54: }