userManager a registrační formulář
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- brontosaurus
- Člen | 49
<?php
namespace App\FrontModule\Presenters;
use Nette,
App\Model,
Nette\Application\UI,
Nette\Application\UI\Form as Form;
class RegisterPresenter extends BasePresenter {
public function renderRegister(){
}
public $userManager;
protected function createComponentRegisterForm() {
$form = new Form;
$form->addText('name', 'Jméno');
$form->addText('email', 'E-mail: *', 35)
->setEmptyValue('@')
->addRule(Form::FILLED, 'Vyplňte Váš email')
->addCondition(Form::FILLED)
->addRule(Form::EMAIL, 'Neplatná emailová adresa');
$form->addPassword('password', 'Heslo: *', 20)
->setOption('description', 'Alespoň 6 znaků')
->addRule(Form::FILLED, 'Vyplňte Vaše heslo')
->addRule(Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaků.', 6);
$form->addPassword('password2', 'Heslo znovu: *', 20)
->addConditionOn($form['password'], Form::VALID)
->addRule(Form::FILLED, 'Heslo znovu')
->addRule(Form::EQUAL, 'Hesla se neshodují.', $form['password']);
$form->addSubmit('send', 'Registrovat');
$form->onSuccess[] = callback($this, 'registerFormSubmitted');
return $form;
}
public function registerFormSubmitted(UI\Form $form) {
$values = $form->getValues();
$this->userManager->add($values->name, $values->email, $values->password);
$this->flashMessage('Registrace se zdařila, můžete pokračovat v přihlášení');
$this->redirect('Homepage:');
}
}
?>
userManager mám zaregistrovaný v config.neon jako službu.
<?php
services:
- App\UserManager
?>
Nette mi vyhodí chybu na :
<?php
$this->userManager->add($values->name, $values->email, $values->password);
?>
Call to a member function add() on a non-object
Netuším proč .
- filipsedivy
- Člen | 37
Je to z důvodu že nemáš daný model v presenteru registrovaný.
Třída by měla začínat nějak takto
class RegisterPresenter extends BasePresenter {
private $userManager;
// Díky Davide, zde je nutné místo __constructor -> __construct
public function __construct(Model\UserManager $userManager){
$this->userManager = $userManager;
}
A pak kdekoliv ve třídě si jí můžeš stáhnout pomocí
$this->userManager->....
Editoval filipsedivy (23. 5. 2015 19:02)
- brontosaurus
- Člen | 49
Service ‚application.3‘: Class App\Model\UserManager needed by App\FrontModule\Presenters\RegisterPresenter::__construct() not found. Check type hint and ‚use‘ statements
- brontosaurus
- Člen | 49
<?php
namespace App;
use Nette,
Nette\Utils\Strings;
/**
* Users management.
*/
class UserManager extends Nette\Object implements Nette\Security\IAuthenticator
{
const
TABLE_NAME = 'users',
COLUMN_ID = 'id',
COLUMN_NAME = 'name',
COLUMN_EMAIL = 'email',
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;
$password = self::removeCapsLock($password);
$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($name, $email, $password)
{
$this->database->table(self::TABLE_NAME)->insert(array(
self::COLUMN_NAME => $name,
self::COLUMN_EMAIL => $email,
self::COLUMN_PASSWORD_HASH => Passwords::hash(self::removeCapsLock($password)),
));
}
/**
* Fixes caps lock accidentally turned on.
* @return string
*/
private static function removeCapsLock($password)
{
return $password === Strings::upper($password)
? Strings::lower($password)
: $password;
}
}
?>
- filipsedivy
- Člen | 37
Máš to jenom v jmenném prostoru App.
Tak že v configu si to registruješ jako
- App\UserManager
A v Presenteru si to injectuješ jako
<?php
namespace App\FrontModule\Presenters;
use Nette,
App,
Nette\Application\UI,
Nette\Application\UI\Form as Form;
class RegisterPresenter extends BasePresenter {
private $userManager;
public function __construct(App\UserManager $userManager){
$this->userManager = $userManger;
}
Nyní vše by mělo fungovat.