1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Security;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Trivial implementation of IAuthenticator.
20: *
21: * @author David Grudl
22: */
23: class SimpleAuthenticator extends Nette\Object implements IAuthenticator
24: {
25: /** @var array */
26: private $userlist;
27:
28:
29: /**
30: * @param array list of usernames and passwords
31: */
32: public function __construct(array $userlist)
33: {
34: $this->userlist = $userlist;
35: }
36:
37:
38:
39: /**
40: * Performs an authentication against e.g. database.
41: * and returns IIdentity on success or throws AuthenticationException
42: * @param array
43: * @return IIdentity
44: * @throws AuthenticationException
45: */
46: public function authenticate(array $credentials)
47: {
48: $username = $credentials[self::USERNAME];
49: foreach ($this->userlist as $name => $pass) {
50: if (strcasecmp($name, $credentials[self::USERNAME]) === 0) {
51: if (strcasecmp($pass, $credentials[self::PASSWORD]) === 0) {
52: // matched!
53: return new Identity($name);
54: }
55:
56: throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
57: }
58: }
59:
60: throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
61: }
62:
63: }
64: