Skip to content

Instantly share code, notes, and snippets.

@matej21
Created March 5, 2014 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matej21/9364959 to your computer and use it in GitHub Desktop.
Save matej21/9364959 to your computer and use it in GitHub Desktop.
<?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