Vlastní autenticator z nette 2 do 3
- Allconius
- Člen | 313
Ahoj, snažil jsem se předělat vlastní Autenticator z nette 2 do nette 3 podle návodu zde
původní verze pro 2.4 :
<?php
namespace App\Auth;
use Nette;
use Nette\Security as NS;
class Authenticator implements NS\IAuthenticator
{
/** @var Nette\Database\Context */
private $db2;
/**
* @var array
*/
private $config;
private $aplikace;
public function __construct(array $config, Nette\Database\Context $db2)
{
$this->db2 = $db2;
$this->config = $config;
$this->aplikace = $this->config['aplikace'];
}
function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->db2->table('trideni')
->where('aplikace', $this->aplikace)
->where('uzivatele.login', $username)->fetch();
if (!$row) {
throw new NS\AuthenticationException('User not found.');
//$this->flashMessage('Uživatel s tímto loginem neexistuje.');
}
if ($password <> $row->uzivatele->heslo) {
throw new NS\AuthenticationException('Invalid password.');
//$this->flashMessage('Špatné heslo.');
}
//return new NS\Identity($row->id, $row->role);
return new NS\Identity($row->iduzivatele, ['prava' => $row->prava, 'pristup' => $row->pristup], ['aplikace' => $this->aplikace, 'username' => $row->uzivatele->login]);
}
}
?>
nová pro 3.0 :
<?php
namespace App\Auth;
use Nette;
use Nette\Security as NS;
class Authenticator implements NS\IAuthenticator
{
/** @var Nette\Database\Context */
private $db2;
private $passwords;
private $aplikace;
public function __construct(Nette\Database\Context $db2, Nette\Security\Passwords $passwords)
{
$this->db2 = $db2;
$this->passwords = $passwords;
$this->aplikace = 137;
}
public function authenticate(array $credentials): Nette\Security\IIdentity
{
[$username, $password] = $credentials;
$row = $this->db2->table('trideni')
->where('aplikace', $this->aplikace)
->where('uzivatele.login', $username)->fetch();
if (!$row) {
throw new Nette\Security\AuthenticationException('User not found.');
}
if (!$this->passwords->verify($password, $row->uzivatele->heslo)) {
throw new Nette\Security\AuthenticationException('Invalid password.');
}
return new Nette\Security\Identity($row->iduzivatele, ['prava' => $row->prava, 'pristup' => $row->pristup], ['aplikace' => $this->aplikace, 'username' => $row->uzivatele->login]);
}
}
?>
ale „háže“ mi to pořád „Argument 1 passed to App\Auth\Authenticator::__construct() must be an instance of Nette\Database\Context, array given, called in /data/docs/www/html/dotace-new/temp/cache/nette.configurator/Container_c75aa90bc0.php on line 434“ přitom ten první parametr je instance Nette\Database\Context, co jsem tam udělal špatně ?
Editoval Allconius (18. 10. 2019 10:59)
- Marek Bartoš
- Nette Blogger | 1165
Změnil jsi konstruktor a nejspíš zapomněl změnit nastavení služby v neonu, pořád si tam předáváš pole
- Allconius
- Člen | 313
Mabar napsal(a):
Změnil jsi konstruktor a nejspíš zapomněl změnit nastavení služby v neonu, pořád si tam předáváš pole
Ahoj, díky máš pravdu nechal jsem tam to pole nakonec:
<?php
public function __construct(array $config, Nette\Database\Context $db2, Nette\Security\Passwords $passwords)
{
?>
v neonu mám:
<?php
authenticator:
App\Auth\Authenticator(%config%, @database.db2.context)
?>
nevadí, že v neonu nemám uvedený ten třetí parametr passwords ? Každopádně už to chybu neháže a objeví se mi přihlašovací tabulka, ale nějak to píše že mám špatně jméno nebo heslo přitom zadávám stejný login a heslo jako v té verzi 2.4, tak tam mám asi ještě nějakou botu …
- Allconius
- Člen | 313
Allconius napsal(a):
Mabar napsal(a):
Změnil jsi konstruktor a nejspíš zapomněl změnit nastavení služby v neonu, pořád si tam předáváš pole
Ahoj, díky máš pravdu nechal jsem tam to pole nakonec:
<?php public function __construct(array $config, Nette\Database\Context $db2, Nette\Security\Passwords $passwords) { ?>
v neonu mám:
<?php authenticator: App\Auth\Authenticator(%config%, @database.db2.context) ?>
nevadí, že v neonu nemám uvedený ten třetí parametr passwords ? Každopádně už to chybu neháže a objeví se mi přihlašovací tabulka, ale nějak to píše že mám špatně jméno nebo heslo přitom zadávám stejný login a heslo jako v té verzi 2.4, tak tam mám asi ještě nějakou botu …
Nějak mi to nepřejde přes ten login:
<?php
$user->login($values->username, md5($values->password));
?>
a vyhodí to „Nesprávné přihlašovací jméno nebo heslo.“ i když ty předané hodnoty $values->username a $values->password jsou správně.
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette;
use App\Model\Db2Manager;
use App\Auth\Authenticator;
use Nette\Application\UI\Form;
use Tracy\Debugger;
class SignPresenter extends Nette\Application\UI\Presenter
{
/** @var Db2Manager */
private $db2Manager;
/** @var Authenticator */
private $authenticator;
public function __construct(Db2Manager $db2Manager, Authenticator $authenticator)
{
$this->db2Manager = $db2Manager;
$this->authenticator = $authenticator;
}
public function signInFormSucceeded(Form $form, $values)
{
try {
$user = $this->getUser();
$user->setAuthenticator($this->authenticator);
$user->login($values->username, md5($values->password));
echo "3";
$id = $this->getUser()->getIdentity()->id;
$result = $this->db2Manager->InitUser($id);
$this->db2Manager->LogIN($values->username);
if ($result>0){
//incializace uzivatele
$this->redirect('Sign:change');
}else{
$this->redirect('Homepage:');
}
} catch (Nette\Security\AuthenticationException $e) {
$form->addError('Nesprávné přihlašovací jméno nebo heslo.');
//uloz hackera
$this->db2Manager->Hacker($values->username, $values->password);
}
?>