Jak zprovoznit statické ACL s Nette 2.1.1
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- _enigma
- Člen | 17
Jel jsem podle podle https://doc.nette.org/…thentication
Ještě to jednou vyzkouším plus pročtu fórum a napsal bych zde konkrétní problémy, které pak snad věčným začátečníkům jako jsem já pomohou.
- _enigma
- Člen | 17
Tak první věc, laděnka mi píše následující:
Nette\DI\ServiceCreationException
Service ‚authenticator‘: No service of type Nette\Database\Context found. Make sure the type hint in App\Model\Authenticator::__construct() is written correctly and service of this type is registered.
config.neon
php:
date.timezone: Europe/Prague
nette:
application:
mapping:
*: OnMyMind\*Module\Presenters\*Presenter
parameters:
database:
driver: mysql
host: localhost
dbname: ...
user: ...
password: ...
services:
database:
class: Nette\Database\Connection
arguments: ['%database.driver%:host=%database.host%;dbname=%database.dbname%',%database.user%,%database.password%]
authorizator:
class: Nette\Security\Permission
setup:
- addRole('guest')
- addRole('admin')
- addResource('Admin:Default')
- allow('admin')
authenticator: App\Model\Authenticator
App/Model/authenticator.php
<?php
namespace App\Model;
use Nette,
Nette\Security,
Nette\Utils\Strings;
/**
* Users authenticator.
*/
class Authenticator extends Nette\Object implements Security\IAuthenticator
{
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}
/**
* Performs an authentication.
* @param array
* @return Nette\Security\Identity
* @throws Nette\Security\AuthenticationException
*/
public function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->database->table('users')->where('username', $username)->fetch();
if (!$row) {
throw new Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
}
if ($row->password !== $this->generateHash($password, $row->password)) {
throw new Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
}
$arr = $row->toArray();
unset($arr['password']);
return new Security\Identity($row->id, NULL, $arr);
}
/**
* Computes salted password hash.
* @return string
*/
public function generateHash($password, $salt = NULL)
{
if ($password === Strings::upper($password)) { // perhaps caps lock is on
$password = Strings::lower($password);
}
return crypt($password, $salt ?: '$2a$07$' . Strings::random(23));
}
}
Tušíte někdo, co s tím?
- enumag
- Člen | 2118
Smaž sekci parametrs a servicu database. Dále postupuj dle https://doc.nette.org/cs/configuring#….
- David Matějka
- Moderator | 6445
neregistruj sluzbu database\connection v services, nastav to dle configu v sandboxu