Skip to content

Instantly share code, notes, and snippets.

@Ginny
Created June 19, 2011 11:37
Show Gist options
  • Select an option

  • Save Ginny/1034116 to your computer and use it in GitHub Desktop.

Select an option

Save Ginny/1034116 to your computer and use it in GitHub Desktop.
Nette authenticator
<?php
use Nette\Object,
Nette\Security as NS;
/**
* Users authenticator.
*/
class Authenticator extends Object implements NS\IAuthenticator {
/** @var Nette\Database\Table\Selection */
private $users;
public function __construct(Nette\Database\Table\Selection $users) {
$this->users = $users;
}
/**
* Performs an authentication
* @param array
* @return IIdentity
* @throws AuthenticationException
*/
public function authenticate(array $credentials) {
list($username, $password) = $credentials;
$row = $this->users->where('username', $username)->fetch();
if (!$row) {
throw new NS\AuthenticationException("Uživatel '$username' nebyl nalezen.", self::IDENTITY_NOT_FOUND);
}
if ($row->password !== $this->calculateHash($password)) {
throw new NS\AuthenticationException("Špatné heslo.", self::INVALID_CREDENTIAL);
}
unset($row->password);
return new NS\Identity($row->id, NULL, $row->toArray());
}
/**
* Computes salted password hash.
* @param string
* @return string
*/
public function calculateHash($password) {
return md5($password . str_repeat('*random salt*', 10));
}
}
services:
authenticator:
class: UsersModel
arguments: ["@database"]
robotLoader:
run: true
database:
class: Nette\Database\Connection
arguments: [sqlite2:%appDir%/models/demo.db] # nebo ['mysql:host=localhost;dbname=test', root, root]
modelLoader:
# do tejto statickej metody Nette vzdy preda DI\Container
class: ModelLoader
arguments: ["@database"]
<?php
use Nette\Object;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of UsersModel
*
* @author droid
*/
class UsersModel extends Object {
/** @var Nette\Database\Connection */
public $database;
public function __construct($connection) {
$this->database = $connection;
}
public function getAuthenticatorService() {
return new Authenticator($this->database->table('users'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment