Guest User

Untitled

a guest
Dec 24th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. <?php
  2.  
  3. namespace UCPG\Entity\Repository;
  4.  
  5. use Doctrine\ORM\EntityRepository,
  6. UCPG\Entity\User,
  7. UCPG\Entity\Account,
  8. UCPG\Entity\Praktijk;
  9.  
  10. /**
  11. * AccountRepository
  12. *
  13. * This class was generated by the Doctrine ORM. Add your own custom
  14. * repository methods below.
  15. */
  16. class AccountRepository extends EntityRepository
  17. {
  18. CONST ACCOUNT_NOT_FOUND = 1;
  19. CONST WRONG_PASSWORD = 2;
  20. /**
  21. * @var string Tijdelijke container voor gebruikersnaam
  22. */
  23. public $username;
  24. /**
  25. * @var string Tijdelijke container voor wachtwoord
  26. */
  27. public $password;
  28. /**
  29. * @param string $username
  30. * @param string $password
  31. * @return Account
  32. * @throws Exception
  33. */
  34. public function authenticate($username, $password)
  35. {
  36. $account = $this->findOneBy(array('username' => $username));
  37. \Doctrine\Common\Util\Debug::dump($account); die();
  38. if ($account) {
  39. if($account->getPassword() === $password)
  40. return $account;
  41. throw new \Exception(self::WRONG_PASSWORD);
  42. }
  43. throw new \Exception(self::ACCOUNT_NOT_FOUND);
  44. }
  45. /**
  46. * Return aantal account met specifieke rol
  47. * @param array $roles
  48. * @return int
  49. */
  50. public function count(array $roles, Praktijk $praktijk = null)
  51. {
  52. $i = 0;
  53. $query = 'SELECT COUNT(u.id) FROM UCPG\Entity\Account u WHERE';
  54. if ($praktijk instanceof Praktijk) $query .= ' u.user_praktijk_id = ' . $praktijk->getId() . ' AND (';
  55. foreach ($roles as $role)
  56. {
  57. $query .= ($i !== 0) ? " OR u.role = '$role'" : " u.role = '$role'";
  58. $i++;
  59. }
  60. if ($praktijk instanceof Praktijk) $query .= ')';
  61. return $this->_em->createQuery($query)->getSingleScalarResult();
  62. }
  63. /**
  64. * Aanmaken account
  65. * @param Account $entity
  66. */
  67. public function create(Account $entity)
  68. {
  69. $this->username = $this->defaultUsername($entity->getUser());
  70. $this->password = $this->createPassword();
  71.  
  72. $entity->setUsername($this->username);
  73. $entity->setPassword(md5($this->password));
  74.  
  75. $this->_em->persist($entity);
  76. $this->_em->persist($entity->getUser());
  77. $this->_em->flush();
  78.  
  79. return $this;
  80. }
  81. /**
  82. * Return default username from User object
  83. * @param User $entity
  84. * @return string Default username as string
  85. */
  86. public function defaultUsername(User $entity)
  87. {
  88. return strtolower(substr($entity->getFirstname(), 0, 4) . substr($entity->getLastname(), 0, 4) . $entity->getDob()->format('d'));
  89. }
  90. /**
  91. * Verwijder account
  92. * @param Account $entity
  93. */
  94. public function delete(Account $entity)
  95. {
  96. $this->_em->remove($entity);
  97. $this->_em->remove($entity->getUser());
  98. $this->_em->flush();
  99. }
  100. /**
  101. * Return Accounts with specified roles
  102. * @param array $roles
  103. * @return array
  104. */
  105. public function findByRoles(array $roles)
  106. {
  107. $i = 0;
  108. $query = 'SELECT u FROM UCPG\Entity\Account u WHERE';
  109. foreach ($roles as $role)
  110. {
  111. $query .= ($i !== 0) ? " OR u.role = '$role'" : " u.role = '$role'";
  112. $i++;
  113. }
  114. return $this->_em->createQuery($query)->getResult();
  115. }
  116. /**
  117. * Aanpassen account
  118. * @param Account $entity
  119. */
  120. public function update(Account $entity)
  121. {
  122. $this->username = $this->defaultUsername($entity->getUser());
  123. $this->password = $this->createPassword();
  124.  
  125. $entity->setUsername($this->username);
  126. $entity->setPassword(md5($this->password));
  127.  
  128. $this->_em->persist($entity->getUser());
  129. $this->_em->persist($entity);
  130. $this->_em->flush();
  131.  
  132. return $this;
  133. }
  134. /**
  135. * Generate standard password
  136. * @return string $password
  137. */
  138. private function createPassword()
  139. {
  140. $arrayChar = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0');
  141. $password = '';
  142. for($i=0;$i<8;$i++) {
  143. $randomValue = rand(0,35);
  144. $password .= $arrayChar[$randomValue];
  145. }
  146. return $password;
  147. }
  148. }
Add Comment
Please, Sign In to add comment