Neplatné heslo uživatele při přihlašování
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- jt
- Člen | 2
Zdravím
bojuju s jednoduchou databází uživatelů pro kterou jsem si vytvořil
dočasný formulář na registraci + přihlášení. Problém je, že po
přidání uživatele do databáze přes registrační formulář se na něj
nelze přihlásit, jelikož to vyhodí error neplatné heslo (které určitě
zadávám stejné jako při registraci).
Využívám třídy Usermanager/SignPresenter/SignFormfactory do kterých jsem
přidal pár metod, co jsem potřeboval. Děkuji za odpověď.
UserManager
<?php
namespace App\Model;
use Nette,
Nette\Utils\Strings,
Nette\Security\Passwords;
/**
* Users management.
*/
class UserManager extends Nette\Object implements Nette\Security\IAuthenticator {
const
TABLE_NAME = 'users',
COLUMN_ID = 'id',
COLUMN_NAME = 'username',
COLUMN_PASSWORD_HASH = 'password',
COLUMN_ROLE = 'role';
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database) {
$this->database = $database;
}
/**
* Performs an authentication.
* @return Nette\Security\Identity
* @throws Nette\Security\AuthenticationException
*/
public function authenticate(array $credentials) {
list($username, $password) = $credentials;
$row = $this->database->table(self::TABLE_NAME)->where(self::COLUMN_NAME, $username)->fetch();
if (!$row) {
throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
} elseif (!Passwords::verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
} elseif (Passwords::needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
$row->update(array(
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
));
}
$arr = $row->toArray();
unset($arr[self::COLUMN_PASSWORD_HASH]);
return new Nette\Security\Identity($row[self::COLUMN_ID], $row[self::COLUMN_ROLE], $arr);
}
/**
* Adds new user.
* @param string
* @param string
* @return void
*/
public function add($username, $password) {
try {
$this->database->table(self::TABLE_NAME)->insert(array(
self::COLUMN_NAME => $username,
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
));
} catch (Nette\Database\UniqueConstraintViolationException $e) {
throw new DuplicateNameException;
}
}
public function isUsernameValid($username) {
if ($this->database->table(self::TABLE_NAME)->where(self::COLUMN_NAME, $username)->count() == 0) {
return true;
} else {
return false;
}
}
}
class DuplicateNameException extends \Exception {
}
SignFormFactory
<?php
namespace App\Forms;
use Nette,
Nette\Application\UI\Form,
App\Model\UserManager,
Nette\Security\User;
class SignFormFactory extends Nette\Object {
/** @var User */
private $user;
private $manager;
public function __construct(User $user, \App\Model\UserManager $manager) {
$this->user = $user;
$this->manager = $manager;
}
/**
* @return Form
*/
public function create() {
$form = new Form;
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');
$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');
$form->addCheckbox('remember', 'Keep me signed in');
$form->addSubmit('send', 'Sign in');
$form->onSuccess[] = array($this, 'formSucceeded');
return $form;
}
public function createRegister() {
$form = new Form;
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');
$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');
$form->addSubmit('send', 'Register');
$form->onSuccess[] = array($this, 'registerFormSucceeded');
return $form;
}
public function formSucceeded($form, $values) {
if ($values->remember) {
$this->user->setExpiration('14 days', FALSE);
} else {
$this->user->setExpiration('20 minutes', TRUE);
}
try {
$this->user->login($values->username, $values->password);
} catch (Nette\Security\AuthenticationException $e) {
$form->addError($e->getMessage());
}
}
public function registerFormSucceeded($form, $values) {
if (! $this->manager->isUsernameValid($values->username)) {
$form->addError('Username used');
} else {
$this->manager->add($values->username, $values->password);
}
}
}
SignPresenter
<?php
namespace App\Presenters;
use Nette,
App\Forms\SignFormFactory,
App\Model\UserManager;
/**
* Sign in/out presenters.
*/
class SignPresenter extends BasePresenter {
/** @var SignFormFactory @inject */
public $factory;
/** @var UserManager @inject */
public $manager;
/**
* Sign-in form factory.
* @return Nette\Application\UI\Form
*/
protected function createComponentSignInForm() {
$form = $this->factory->create();
$form->onSuccess[] = function ($form) {
$form->getPresenter()->redirect('Homepage:');
};
return $form;
}
public function actionOut() {
$this->getUser()->logout();
$this->flashMessage('You have been signed out.');
$this->redirect('in');
}
protected function createComponentRegisterForm() {
$form = $this->factory->createRegister();
$form->onSuccess[] = function ($form) {
$this->flashMessage('Account created, u can sign in.');
$form->getPresenter()->redirect('Sign:default');
};
return $form;
}
}
Editoval jt (5. 7. 2015 10:40)
- David Matějka
- Moderator | 6445
Mas dostatecnou velikost toho sloupce v db? retezec vraceny
Password::hash
ma 60 znaku