Guest User

Untitled

a guest
Dec 24th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. ## REPO
  2.  
  3. <?php
  4.  
  5. namespace UCPG\Entity\Repository;
  6.  
  7. use Doctrine\ORM\EntityRepository,
  8. UCPG\Entity\User,
  9. UCPG\Entity\Account,
  10. UCPG\Entity\Praktijk;
  11.  
  12. /**
  13. * AccountRepository
  14. *
  15. * This class was generated by the Doctrine ORM. Add your own custom
  16. * repository methods below.
  17. */
  18. class AccountRepository extends EntityRepository
  19. {
  20. CONST ACCOUNT_NOT_FOUND = 1;
  21. CONST WRONG_PASSWORD = 2;
  22. /**
  23. * @var string Tijdelijke container voor gebruikersnaam
  24. */
  25. public $username;
  26. /**
  27. * @var string Tijdelijke container voor wachtwoord
  28. */
  29. public $password;
  30. /**
  31. * @param string $username
  32. * @param string $password
  33. * @return Account
  34. * @throws Exception
  35. */
  36. public function authenticate($username, $password)
  37. {
  38. $account = $this->findOneBy(array('username' => $username));
  39. \Doctrine\Common\Util\Debug::dump($account); die();
  40. if ($account) {
  41. if($account->getPassword() === $password)
  42. return $account;
  43. throw new \Exception(self::WRONG_PASSWORD);
  44. }
  45. throw new \Exception(self::ACCOUNT_NOT_FOUND);
  46. }
  47. /**
  48. * Return aantal account met specifieke rol
  49. * @param array $roles
  50. * @return int
  51. */
  52. public function count(array $roles, Praktijk $praktijk = null)
  53. {
  54. $i = 0;
  55. $query = 'SELECT COUNT(u.id) FROM UCPG\Entity\Account u WHERE';
  56. if ($praktijk instanceof Praktijk) $query .= ' u.user_praktijk_id = ' . $praktijk->getId() . ' AND (';
  57. foreach ($roles as $role)
  58. {
  59. $query .= ($i !== 0) ? " OR u.role = '$role'" : " u.role = '$role'";
  60. $i++;
  61. }
  62. if ($praktijk instanceof Praktijk) $query .= ')';
  63. return $this->_em->createQuery($query)->getSingleScalarResult();
  64. }
  65. /**
  66. * Aanmaken account
  67. * @param Account $entity
  68. */
  69. public function create(Account $entity)
  70. {
  71. $this->username = $this->defaultUsername($entity->getUser());
  72. $this->password = $this->createPassword();
  73.  
  74. $entity->setUsername($this->username);
  75. $entity->setPassword(md5($this->password));
  76.  
  77. $this->_em->persist($entity);
  78. $this->_em->persist($entity->getUser());
  79. $this->_em->flush();
  80.  
  81. return $this;
  82. }
  83. /**
  84. * Return default username from User object
  85. * @param User $entity
  86. * @return string Default username as string
  87. */
  88. public function defaultUsername(User $entity)
  89. {
  90. return strtolower(substr($entity->getFirstname(), 0, 4) . substr($entity->getLastname(), 0, 4) . $entity->getDob()->format('d'));
  91. }
  92. /**
  93. * Verwijder account
  94. * @param Account $entity
  95. */
  96. public function delete(Account $entity)
  97. {
  98. $this->_em->remove($entity);
  99. $this->_em->remove($entity->getUser());
  100. $this->_em->flush();
  101. }
  102. /**
  103. * Return Accounts with specified roles
  104. * @param array $roles
  105. * @return array
  106. */
  107. public function findByRoles(array $roles)
  108. {
  109. $i = 0;
  110. $query = 'SELECT u FROM UCPG\Entity\Account u WHERE';
  111. foreach ($roles as $role)
  112. {
  113. $query .= ($i !== 0) ? " OR u.role = '$role'" : " u.role = '$role'";
  114. $i++;
  115. }
  116. return $this->_em->createQuery($query)->getResult();
  117. }
  118. /**
  119. * Aanpassen account
  120. * @param Account $entity
  121. */
  122. public function update(Account $entity)
  123. {
  124. $this->username = $this->defaultUsername($entity->getUser());
  125. $this->password = $this->createPassword();
  126.  
  127. $entity->setUsername($this->username);
  128. $entity->setPassword(md5($this->password));
  129.  
  130. $this->_em->persist($entity->getUser());
  131. $this->_em->persist($entity);
  132. $this->_em->flush();
  133.  
  134. return $this;
  135. }
  136. /**
  137. * Generate standard password
  138. * @return string $password
  139. */
  140. private function createPassword()
  141. {
  142. $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');
  143. $password = '';
  144. for($i=0;$i<8;$i++) {
  145. $randomValue = rand(0,35);
  146. $password .= $arrayChar[$randomValue];
  147. }
  148. return $password;
  149. }
  150. }
  151.  
  152. ## ADAPTER
  153.  
  154. <?php
  155. /**
  156. * Authentication adapter for doctrine
  157. *
  158. * @author Martijn Swaagman
  159. * @copyright Prestar
  160. * @version 1.0
  161. */
  162. class UCPG_Auth_Adapter implements Zend_Auth_Adapter_Interface
  163. {
  164. const SUCCESS_MSG = 'U bent succesvol ingelogd!';
  165. const ACCOUNT_NOT_FOUND_MSG = 'Account niet gevonden!';
  166. const WRONG_PASSWORD_MSG = 'Onjuist wachtwoord voor account!';
  167. /**
  168. * Doctrine Account object
  169. * @var Account
  170. */
  171. private $account;
  172. /**
  173. * Username of user
  174. * @var string
  175. */
  176. private $username;
  177. /**
  178. * Password of user (md5)
  179. * @var string
  180. */
  181. private $password;
  182. /**
  183. * @param string $username
  184. * @param string $password
  185. */
  186. public function __construct($username = null, $password = null)
  187. {
  188. $this->username = (!is_null($username)) ? (string) $username : null;
  189. $this->password = (!is_null($password)) ? (string) md5($password) : null;
  190. }
  191. /**
  192. * (non-PHPdoc)
  193. * @see Zend_Auth_Adapter_Interface::authenticate()
  194. */
  195. public function authenticate()
  196. {
  197. try {
  198. $doctrine = Zend_Registry::get('doctrine');
  199. $em = $doctrine->getEntityManager();
  200. $this->account = $em->getRepository('UCPG\Entity\Account')
  201. ->authenticate($this->username, $this->password);
  202. return $this->createResult(Zend_Auth_Result::SUCCESS, UCPG_Auth_Result::TYPE_SUCCESS, self::SUCCESS_MSG);
  203. } catch (Exception $e) {
  204. if ($e->getMessage() == UCPG\Entity\Repository\AccountRepository::ACCOUNT_NOT_FOUND)
  205. return $this->createResult(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, UCPG_Auth_Result::TYPE_ERROR, self::ACCOUNT_NOT_FOUND_MSG);
  206. if ($e->getMessage() == UCPG\Entity\Repository\AccountRepository::WRONG_PASSWORD)
  207. return $this->createResult(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, UCPG_Auth_Result::TYPE_ERROR, self::WRONG_PASSWORD_MSG);
  208. }
  209. }
  210. /**
  211. * Standard Authentication response
  212. * @param Zend_Auth_Result::CONST $code
  213. * @param array $messages Message for user
  214. * @return Zend_Auth_Result
  215. */
  216. private function createResult($code, $type, $messages)
  217. {
  218. return new UCPG_Auth_Result($code, $this->account, $type, array($messages));
  219. }
  220. }
Add Comment
Please, Sign In to add comment