Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Model\DAO;
- use Nette,
- Kdyby\Doctrine\EntityManager,
- Kdyby\Doctrine\ResultSet,
- AppBundle\Model\Entities\Admin,
- AppBundle\Exceptions,
- AppBundle\Model\Queries\AdminsListQuery,
- Doctrine\DBAL\Exception\UniqueConstraintViolationException;
- /**
- * Admin users management.
- *
- * @author Martin Konečný
- */
- class AdminDAO implements Nette\Security\IAuthenticator
- {
- /** @var EntityManager */
- private $em;
- /**
- * @param EntityManager
- */
- public function __construct(EntityManager $em)
- {
- $this->em = $em;
- }
- /**
- * Gets admin by ID. If not exists, throws exception (by default) or returns NULL.
- *
- * @param int
- * @throws Exceptions\AdminNotFoundException
- * @return Admin|NULL
- */
- public function getAdmin($id, $throwException = TRUE)
- {
- $id = (int) $id;
- $admin = $this->em->find(Admin::class, $id);
- if ($admin === NULL && $throwException) {
- throw new Exceptions\AdminNotFoundException();
- }
- return $admin;
- }
- /**
- * Gets admin by custom conditions. If not exists, throws exception (by default) or returns NULL.
- *
- * @param array
- * @param bool
- * @return Admin|NULL
- * @thorws Exceptions\AdminNotFoundException
- */
- public function getAdminBy(array $by, $throwException = TRUE)
- {
- $admin = $this->em->getRepository(Admin::class)->findOneBy($by);
- if ($admin === NULL && $throwException) {
- throw new Exceptions\AdminNotFoundException();
- }
- return $admin;
- }
- /**
- * Performs an authentication by login name and password.
- *
- * @param array
- * @throws Exceptions\AdminNotFoundException
- * @throws Exceptions\InvalidPasswordException
- * @return Nette\Security\Identity
- */
- public function authenticate(array $credentials)
- {
- list($name, $password) = $credentials;
- $user = $this->em->createQuery("
- SELECT a
- FROM AppBundle\Model\Entities\Admin a
- WHERE a.loginName LIKE :name
- ")
- ->setParameter("name", $name)
- ->setMaxResults(1)
- ->getOneOrNullResult();
- if ($user === NULL) {
- throw new Exceptions\AdminNotFoundException();
- }
- $this->checkPassword($user, $password);
- return new Nette\Security\Identity($user->id);
- }
- /**
- * Checks if passed password matches admin's password.
- *
- * @param Admin
- * @param string
- * @throws Exceptions\InvalidPasswordException
- */
- public function checkPassword(Admin $admin, $password)
- {
- $hash = $this->hashPassword($admin, $password);
- if ($hash !== $admin->password) {
- throw new Exceptions\InvalidPasswordException();
- }
- }
- /**
- * Hashes password using SHA512 algorithm with admin's randomly generated token.
- *
- * @param Admin
- * @param string
- * @return string
- *
- * TODO: uvážit jiný způsob hashování
- */
- public function hashPassword(Admin $admin, $password)
- {
- return hash_hmac("SHA512", $password, $admin->token);
- }
- /**
- * Gets a list of admins by custom conditions.
- *
- * @param array
- * @return ResultSet
- */
- public function getAdmins(array $params = array())
- {
- $params = (object) $params;
- $query = new AdminsListQuery();
- if (isset($params->orderByName)) {
- $query->orderByName($params->orderByName);
- }
- return $this->em->getRepository(Admin::class)->fetch($query);
- }
- /**
- * @param Admin
- * @throws Exceptions\AdminAlreadyExistsException
- */
- public function addAdmin(Admin $admin)
- {
- try {
- $admin->registrationDate = new Nette\Utils\DateTime();
- $admin->token = Nette\Utils\Random::generate(32);
- $admin->password = $this->hashPassword($admin, $admin->password);
- $this->em->persist($admin);
- $this->em->flush();
- } catch (UniqueConstraintViolationException $e) {
- throw new Exceptions\AdminAlreadyExistsException(NULL, NULL, $e);
- }
- }
- /**
- * @param Admin
- * @throws Exceptions\AdminAlreadyExistsException
- */
- public function editAdmin(Admin $admin)
- {
- try {
- $this->em->persist($admin);
- $this->em->flush();
- } catch (UniqueConstraintViolationException $e) {
- throw new Exceptions\AdminAlreadyExistsException(NULL, NULL, $e);
- }
- }
- /**
- * @param int
- * @throws Exceptions\AdminNotFoundException
- * @throws Exceptions\NotAllowedException
- */
- public function deleteAdmin($id)
- {
- $admin = $this->getAdmin($id);
- if ($admin->isSuperadmin()) {
- throw new Exceptions\NotAllowedException();
- }
- $this->em->remove($admin);
- $this->em->flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement