globalna premenna pri prihlaseni
- cujan
- Člen | 410
Caute ako si na na zaciatku pri prihlaseni mozem zadefinovat globalnu
premennu, ktoru budem mat k dispozicii v kazdom presenteri?
Ide mi oto, ze viem si dostat napr IdUser alel potreboval by som aj ine hodnoty
z tabulky user ako id, username, role…
popripdade da sa to nejako zadefinovat koa funkcia…
vdaka
- ViPEr*CZ*
- Člen | 818
Ano dá… uživatel má v sobě objekt Identity a ten umí uchovávat pole
dat.
https://api.nette.org/…ity.php.html#…
viz. třetí parametr. V authenticatoru tedy rozšiřte tento objekt, který
Vám vytváří.
- cujan
- Člen | 410
moj authetifikator
<?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)
{
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;
}
}
}
class DuplicateNameException extends \Exception
{}
?>
- cujan
- Člen | 410
CZechBoY napsal(a):
Stačí naplnit proměnnou
$arr
v metodě authenticate.
oki a pristupuje sa potom ku jednotlivym premennym ako?
a dalo by sa urobit si niekde v projekte nejaku trie napr. premenne, kde by som mal funkcie, ktore by mi vracali jednolive premenne napr. podla id uzivatela?
- cujan
- Člen | 410
CZechBoY napsal(a):
Stačí naplnit proměnnou
$arr
v metodě authenticate.
pristupujem takto, a prestalo mi posielat pole IDENTITy resp je prazdne nechapem
<?php
public function renderDefault()
{
dump($this->user->getIdentity());
$this->template->posts = $this->database->table('zdruzenie')->where('id',$this->user->getIdentity()->zdruzenieId);
}
?>
autentifikator je
<?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 = 'roleId',
COLUMN_ZDRUZENIEID = 'zdruzenieId';
/** @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],$row[self::COLUMN_ZDRUZENIEID], $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;
}
}
}
class DuplicateNameException extends \Exception
{}
?>