Oddělené přihlašování uživatelů pro každý modul
- gonner47
- Člen | 27
Ahoj, mám aplikaci která má dva moduly. AdminModule a FrontModul. Pro
každý modul jsem si vytvořil vlastní přihlašování, protože je chci mít
od dělené. Přihlášení proběhne v bez problému ale po přihlášení
nemám žádnou identitu. V debugg baru mám Unlogged, no identity.
Ještě přikládám kody jak mám přihlašování udělané.
AuthenticarotRoute.php
<?php
namespace Libs\Security;
use Nette\Object;
use Nette\Security\IAuthenticator;
use Nette\Security\IUserStorage;
/**
* Class AuthenticatorRouter
* @package Lib\Security
*/
class AuthenticatorRoute extends Object implements IAuthenticator
{
private $authenticators = [];
private $userStorage;
public function __construct(IUserStorage $userStorage)
{
$this->userStorage = $userStorage;
}
public function add($namespace, IAuthenticator $authenticator)
{
$this->authenticators[$namespace] = $authenticator;
}
public function authenticate(array $credentials)
{
$ns = $this->userStorage->getNamespace();
if(!isset($this->authenticators[$ns])) {
throw new \Exception;
}
return $this->authenticators[$ns]->authenticate($credentials);
}
}
AdminAuthenticator.php
<?php
namespace Libs\Security;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Libs\Security\Entity\AdminUser;
use Nette\Application\ApplicationException;
use Nette\Object;
use Nette\Security\AuthenticationException;
use Nette\Security\IAuthenticator;
use Nette\Security\Passwords;
use Libs\Security\Facade;
/**
* Class AdminAuthenticator
* @package Libs\Security
*/
class AdminAuthenticator extends Object implements IAuthenticator
{
/** @var EntityManager */
private $em;
/** @var EntityRepository */
private $adminUser;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
$this->adminUser = $em->getRepository(AdminUser::getClassName());
}
/**
* @param array $credentials
* @return null|AdminUser
* @throws ApplicationException
*/
public function authenticate(array $credentials)
{
list($email, $password) = $credentials;
$user = $this->adminUser->findOneBy(['email' => $email]);
if(!$user) {
throw new ApplicationException('Byl zadaný špatný e-mail.', self::IDENTITY_NOT_FOUND);
} elseif(!Passwords::verify($password, $user->password)) {
throw new ApplicationException('Bylo špatně zadané heslo.', self::INVALID_CREDENTIAL);
} elseif(Passwords::needsRehash($user->password)) {
$user->setPassword(Passwords::hash($password));
$user->em->persist($email);
$user->em->flush();
}
return $user;
}
}
FrontAuthenticator.php
<?php
namespace Libs\Security;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Libs\Security\Entity\User;
use Nette\Object;
use Nette\Security\AuthenticationException;
use Nette\Security\IAuthenticator;
use Nette\Security\Passwords;
/**
* Class Authenticator
*
* @package App\Model\Security
*/
class FrontAuthenticator extends Object implements IAuthenticator
{
/** @var EntityManager */
private $em;
/** @var EntityRepository */
private $users;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
$this->users = $em->getRepository(User::getClassName());
}
/**
* @param array $credentials
* @return null|User
* @throws AuthenticationException
*/
public function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$user = $this->users->findOneBy(['username' => $username]);
if (!$user) {
throw new AuthenticationException('Invalid credentials.', self::IDENTITY_NOT_FOUND);
} elseif (!Passwords::verify($password, $user->password)) {
throw new AuthenticationException('Invalid credentials.', self::INVALID_CREDENTIAL);
} elseif (Passwords::needsRehash($user->password)) {
$user->setPassword(Passwords::hash($password));
$this->em->persist($username);
$this->em->flush();
}
return $user;
}
}
confic.neon
frontAuthenticator: {class: Libs\Security\FrontAuthenticator, autowired: false}
adminAuthenticator: {class: Libs\Security\AdminAuthenticator, autowired: false}
authenticator:
class: Libs\Security\AuthenticatorRoute
setup:
- add(Admin, @adminAuthenticator)
- add(Front, @adminAuthenticator)
a zpracování přihlášení
public function proccessSignInForm(Form $form, $values)
{
$user = $this->getUser();
$user->getStorage()->setNamespace('Admin');
try {
$user->login($values->email, $values->pass);
$this->flashMessage('Výtejte v administraci.', 'success');
$this->redirect(':Admin:Homepage:default');
} catch(Nette\Application\ApplicationException $e) {
$this->flashMessage($e->getMessage(), 'warning');
}
}
Čumím do toho už dvě hodiny a pořád nevím kde by mohla být chyba.. Děkuji za každou radu.
- David Matějka
- Moderator | 6445
Namespace nestaci nastavit v tom prihlasovacim callbacku, musis to nastavit vsude, kde se ma ten NS pouzivat, viz doc: https://doc.nette.org/…thentication#…
- David Matějka
- Moderator | 6445
Proste musis v BasePresenter pro admin i pro front nastavit ten namespace. To muzes udelat prekrytim ty metody checkRequirements, jak je to v doc, kde ten namespace nastavis.