Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace UCPG\Entity\Repository;
- use Doctrine\ORM\EntityRepository,
- UCPG\Entity\User,
- UCPG\Entity\Account,
- UCPG\Entity\Praktijk;
- /**
- * AccountRepository
- *
- * This class was generated by the Doctrine ORM. Add your own custom
- * repository methods below.
- */
- class AccountRepository extends EntityRepository
- {
- CONST ACCOUNT_NOT_FOUND = 1;
- CONST WRONG_PASSWORD = 2;
- /**
- * @var string Tijdelijke container voor gebruikersnaam
- */
- public $username;
- /**
- * @var string Tijdelijke container voor wachtwoord
- */
- public $password;
- /**
- * @param string $username
- * @param string $password
- * @return Account
- * @throws Exception
- */
- public function authenticate($username, $password)
- {
- $account = $this->findOneBy(array('username' => $username));
- \Doctrine\Common\Util\Debug::dump($account); die();
- if ($account) {
- if($account->getPassword() === $password)
- return $account;
- throw new \Exception(self::WRONG_PASSWORD);
- }
- throw new \Exception(self::ACCOUNT_NOT_FOUND);
- }
- /**
- * Return aantal account met specifieke rol
- * @param array $roles
- * @return int
- */
- public function count(array $roles, Praktijk $praktijk = null)
- {
- $i = 0;
- $query = 'SELECT COUNT(u.id) FROM UCPG\Entity\Account u WHERE';
- if ($praktijk instanceof Praktijk) $query .= ' u.user_praktijk_id = ' . $praktijk->getId() . ' AND (';
- foreach ($roles as $role)
- {
- $query .= ($i !== 0) ? " OR u.role = '$role'" : " u.role = '$role'";
- $i++;
- }
- if ($praktijk instanceof Praktijk) $query .= ')';
- return $this->_em->createQuery($query)->getSingleScalarResult();
- }
- /**
- * Aanmaken account
- * @param Account $entity
- */
- public function create(Account $entity)
- {
- $this->username = $this->defaultUsername($entity->getUser());
- $this->password = $this->createPassword();
- $entity->setUsername($this->username);
- $entity->setPassword(md5($this->password));
- $this->_em->persist($entity);
- $this->_em->persist($entity->getUser());
- $this->_em->flush();
- return $this;
- }
- /**
- * Return default username from User object
- * @param User $entity
- * @return string Default username as string
- */
- public function defaultUsername(User $entity)
- {
- return strtolower(substr($entity->getFirstname(), 0, 4) . substr($entity->getLastname(), 0, 4) . $entity->getDob()->format('d'));
- }
- /**
- * Verwijder account
- * @param Account $entity
- */
- public function delete(Account $entity)
- {
- $this->_em->remove($entity);
- $this->_em->remove($entity->getUser());
- $this->_em->flush();
- }
- /**
- * Return Accounts with specified roles
- * @param array $roles
- * @return array
- */
- public function findByRoles(array $roles)
- {
- $i = 0;
- $query = 'SELECT u FROM UCPG\Entity\Account u WHERE';
- foreach ($roles as $role)
- {
- $query .= ($i !== 0) ? " OR u.role = '$role'" : " u.role = '$role'";
- $i++;
- }
- return $this->_em->createQuery($query)->getResult();
- }
- /**
- * Aanpassen account
- * @param Account $entity
- */
- public function update(Account $entity)
- {
- $this->username = $this->defaultUsername($entity->getUser());
- $this->password = $this->createPassword();
- $entity->setUsername($this->username);
- $entity->setPassword(md5($this->password));
- $this->_em->persist($entity->getUser());
- $this->_em->persist($entity);
- $this->_em->flush();
- return $this;
- }
- /**
- * Generate standard password
- * @return string $password
- */
- private function createPassword()
- {
- $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');
- $password = '';
- for($i=0;$i<8;$i++) {
- $randomValue = rand(0,35);
- $password .= $arrayChar[$randomValue];
- }
- return $password;
- }
- }
Add Comment
Please, Sign In to add comment