rola pri registrácii nového užívateľa
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- kasiopia
- Člen | 69
Dobrý deň ked sa registruje nový užívateľ tak nie pridaná rola ako
automalicky mu môžem priradiť hodnotu Guest. Používam UserManager čo by
sommal editovať aby bola vložená hodnota v tabulke. Ďakujem za odpoved
Pri editovaní vypisuje
Missing argument 3 for App\Model\UserManager::add(), called in
C:\xampp\htdocs\try\sandbox\app\presenters\RegisterPresenter.php on line 38 and
defined
<?php
namespace App\Model;
use Nette;
use 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, $role)
{
try {
$this->database->table(self::TABLE_NAME)->insert(array(
self::COLUMN_NAME => $username,
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
self::COLUMN_ROLE => $role,
));
} catch (Nette\Database\UniqueConstraintViolationException $e) {
throw new DuplicateNameException;
}
}
}
class DuplicateNameException extends \Exception
{}
<?php
namespace App\Presenters;
use Nette;
use App\Model;
use App\Model\UserManager;
class RegisterPresenter extends BasePresenter
{
private $userManager;
protected function createComponentRegisterForm()
{
$form = new Nette\Application\UI\Form;
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');
$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');
$form->addSubmit('send', 'Register');
// call method signInFormSucceeded() on success
$form->onSuccess[] = array($this, 'registerFormSucceeded');
return $form;
}
public function __construct (UserManager $userManager)
{
$this->userManager = $userManager;
}
public function registerFormSucceeded ($form, $values)
{
$this->userManager->add($values['username'], $values['password']);
$this->flashMessage('User register.', 'success');
}
}
Editoval kasiopia (11. 2. 2016 1:20)
- David Matějka
- Moderator | 6445
chjo. sis pridal do metody add parametr role, ale kdyz metodu volas, tak ho tam nepredavas. tohle vubec nesouvisi s nette, ale se zaklady PHP