1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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: * @package Nette\Security
11: */
12:
13:
14:
15: /**
16: * Trivial implementation of IAuthenticator.
17: *
18: * @author David Grudl
19: */
20: class SimpleAuthenticator extends Object implements IAuthenticator
21: {
22: /** @var array */
23: private $userlist;
24:
25:
26: /**
27: * @param array list of pairs username => password
28: */
29: public function __construct(array $userlist)
30: {
31: $this->userlist = $userlist;
32: }
33:
34:
35:
36: /**
37: * Performs an authentication against e.g. database.
38: * and returns IIdentity on success or throws AuthenticationException
39: * @param array
40: * @return IIdentity
41: * @throws AuthenticationException
42: */
43: public function authenticate(array $credentials)
44: {
45: list($username, $password) = $credentials;
46: foreach ($this->userlist as $name => $pass) {
47: if (strcasecmp($name, $username) === 0) {
48: if ($pass === $password) {
49: return new Identity($name);
50: } else {
51: throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
52: }
53: }
54: }
55: throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
56: }
57:
58: }
59: