Created
March 5, 2014 10:42
-
-
Save matej21/9364959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyAuthenticator extends Nette\Object implements IAuthenticator | |
{ | |
/** @var array */ | |
private $userlist; | |
/** | |
* @param array list of pairs username => password | |
*/ | |
public function __construct(array $userlist) | |
{ | |
$this->userlist = $userlist; | |
} | |
/** | |
* Performs an authentication against e.g. database. | |
* and returns IIdentity on success or throws AuthenticationException | |
* @return IIdentity | |
* @throws AuthenticationException | |
*/ | |
public function authenticate(array $credentials) | |
{ | |
list($username, $password) = $credentials; | |
foreach ($this->userlist as $name => $data) { | |
$pass = is_array($data) ? $data['password'] : $data; | |
$roles = (is_array($data) && isset($data['roles'])) ? (array) $data['roles'] : array(); | |
if (strcasecmp($name, $username) === 0) { | |
if ((string) $pass === (string) $password) { | |
return new Identity($name, $roles); | |
} else { | |
throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL); | |
} | |
} | |
} | |
throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment