Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.47 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Model\DAO;
  4.  
  5. use Nette,
  6.     Kdyby\Doctrine\EntityManager,
  7.     Kdyby\Doctrine\ResultSet,
  8.     AppBundle\Model\Entities\Admin,
  9.     AppBundle\Exceptions,
  10.     AppBundle\Model\Queries\AdminsListQuery,
  11.     Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  12.  
  13.  
  14. /**
  15.  * Admin users management.
  16.  *
  17.  * @author Martin Konečný
  18.  */
  19. class AdminDAO implements Nette\Security\IAuthenticator
  20. {
  21.    
  22.     /** @var EntityManager */
  23.     private $em;
  24.    
  25.    
  26.     /**
  27.      * @param EntityManager
  28.      */
  29.     public function __construct(EntityManager $em)
  30.     {
  31.         $this->em = $em;
  32.     }
  33.    
  34.    
  35.     /**
  36.      * Gets admin by ID. If not exists, throws exception (by default) or returns NULL.
  37.      *
  38.      * @param int
  39.      * @throws Exceptions\AdminNotFoundException
  40.      * @return Admin|NULL
  41.      */
  42.     public function getAdmin($id, $throwException = TRUE)
  43.     {
  44.         $id = (int) $id;
  45.        
  46.         $admin = $this->em->find(Admin::class, $id);
  47.         if ($admin === NULL && $throwException) {
  48.             throw new Exceptions\AdminNotFoundException();
  49.         }
  50.        
  51.         return $admin;
  52.     }
  53.    
  54.    
  55.     /**
  56.      * Gets admin by custom conditions. If not exists, throws exception (by default) or returns NULL.
  57.      *
  58.      * @param array
  59.      * @param bool
  60.      * @return Admin|NULL
  61.      * @thorws Exceptions\AdminNotFoundException
  62.      */
  63.     public function getAdminBy(array $by, $throwException = TRUE)
  64.     {
  65.         $admin = $this->em->getRepository(Admin::class)->findOneBy($by);
  66.         if ($admin === NULL && $throwException) {
  67.             throw new Exceptions\AdminNotFoundException();
  68.         }
  69.        
  70.         return $admin;
  71.     }
  72.    
  73.    
  74.     /**
  75.      * Performs an authentication by login name and password.
  76.      *
  77.      * @param array
  78.      * @throws Exceptions\AdminNotFoundException
  79.      * @throws Exceptions\InvalidPasswordException
  80.      * @return Nette\Security\Identity
  81.      */
  82.     public function authenticate(array $credentials)
  83.     {
  84.         list($name, $password) = $credentials;
  85.        
  86.         $user = $this->em->createQuery("
  87.             SELECT a
  88.             FROM AppBundle\Model\Entities\Admin a
  89.             WHERE a.loginName LIKE :name
  90.         ")
  91.             ->setParameter("name", $name)
  92.             ->setMaxResults(1)
  93.             ->getOneOrNullResult();
  94.        
  95.         if ($user === NULL) {
  96.             throw new Exceptions\AdminNotFoundException();
  97.         }
  98.        
  99.         $this->checkPassword($user, $password);
  100.        
  101.         return new Nette\Security\Identity($user->id);
  102.     }
  103.    
  104.    
  105.     /**
  106.      * Checks if passed password matches admin's password.
  107.      *
  108.      * @param Admin
  109.      * @param string
  110.      * @throws Exceptions\InvalidPasswordException
  111.      */
  112.     public function checkPassword(Admin $admin, $password)
  113.     {
  114.         $hash = $this->hashPassword($admin, $password);
  115.         if ($hash !== $admin->password) {
  116.             throw new Exceptions\InvalidPasswordException();
  117.         }
  118.     }
  119.    
  120.    
  121.     /**
  122.      * Hashes password using SHA512 algorithm with admin's randomly generated token.
  123.      *
  124.      * @param Admin
  125.      * @param string
  126.      * @return string
  127.      *
  128.      * TODO: uvážit jiný způsob hashování
  129.      */
  130.     public function hashPassword(Admin $admin, $password)
  131.     {
  132.         return hash_hmac("SHA512", $password, $admin->token);
  133.     }
  134.    
  135.    
  136.     /**
  137.      * Gets a list of admins by custom conditions.
  138.      *
  139.      * @param array
  140.      * @return ResultSet
  141.      */
  142.     public function getAdmins(array $params = array())
  143.     {
  144.         $params = (object) $params;
  145.         $query = new AdminsListQuery();
  146.        
  147.         if (isset($params->orderByName)) {
  148.             $query->orderByName($params->orderByName);
  149.         }
  150.        
  151.         return $this->em->getRepository(Admin::class)->fetch($query);
  152.     }
  153.    
  154.    
  155.     /**
  156.      * @param Admin
  157.      * @throws Exceptions\AdminAlreadyExistsException
  158.      */
  159.     public function addAdmin(Admin $admin)
  160.     {
  161.         try {
  162.             $admin->registrationDate = new Nette\Utils\DateTime();
  163.             $admin->token = Nette\Utils\Random::generate(32);
  164.             $admin->password = $this->hashPassword($admin, $admin->password);
  165.            
  166.             $this->em->persist($admin);
  167.             $this->em->flush();
  168.         } catch (UniqueConstraintViolationException $e) {
  169.             throw new Exceptions\AdminAlreadyExistsException(NULL, NULL, $e);
  170.         }
  171.     }
  172.    
  173.    
  174.     /**
  175.      * @param Admin
  176.      * @throws Exceptions\AdminAlreadyExistsException
  177.      */
  178.     public function editAdmin(Admin $admin)
  179.     {
  180.         try {
  181.             $this->em->persist($admin);
  182.             $this->em->flush();
  183.         } catch (UniqueConstraintViolationException $e) {
  184.             throw new Exceptions\AdminAlreadyExistsException(NULL, NULL, $e);
  185.         }
  186.     }
  187.    
  188.    
  189.     /**
  190.      * @param int
  191.      * @throws Exceptions\AdminNotFoundException
  192.      * @throws Exceptions\NotAllowedException
  193.      */
  194.     public function deleteAdmin($id)
  195.     {
  196.         $admin = $this->getAdmin($id);
  197.         if ($admin->isSuperadmin()) {
  198.             throw new Exceptions\NotAllowedException();
  199.         }
  200.        
  201.         $this->em->remove($admin);
  202.         $this->em->flush();
  203.     }
  204.    
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement