Example #1
0
 /**
  * @param Params $params
  * @param bool   $allowDraft
  *
  * @return \Jobs\Entity\Job|object
  * @throws \Doctrine\ODM\MongoDB\LockException
  */
 public function get(Params $params, $allowDraft = false)
 {
     /* @var \Jobs\Repository\Job $jobRepository */
     $jobRepository = $this->repositoryService->get('Jobs/Job');
     $idFromRoute = $params('id', 0);
     $idFromQuery = $params->fromQuery('id', 0);
     $idFromSubForm = $params->fromPost('job', 0);
     $id = empty($idFromRoute) ? empty($idFromQuery) ? $idFromSubForm : $idFromQuery : $idFromRoute;
     if (empty($id) && $allowDraft) {
         $this->acl->__invoke('Jobs/Manage', 'new');
         $user = $this->auth->getUser();
         /** @var \Jobs\Entity\Job $job */
         $job = $jobRepository->findDraft($user);
         if (empty($job)) {
             $job = $jobRepository->create();
             $job->setIsDraft(true);
             $job->setUser($user);
             $this->repositoryService->store($job);
         }
         return $job;
     }
     $job = $jobRepository->find($id);
     if (!$job) {
         throw new \RuntimeException('No job found with id "' . $id . '"');
     }
     return $job;
 }
Example #2
0
 public function indexAction()
 {
     if (!($user = $this->authenticationService->getUser())) {
         throw new UnauthorizedAccessException('You must be logged in.');
     }
     /** @var Request $request */
     $request = $this->getRequest();
     $this->form->bind($user);
     if ($request->isPost()) {
         $this->form->setData($request->getPost()->toArray());
         if ($this->form->isValid()) {
             $this->repositoryService->store($user);
             $vars = array('valid' => true);
             $this->notification()->success('Password successfully changed');
         } else {
             // form is invalid
             $vars = array('valid' => false);
             // @TODO the messages are distributed to the hierarchy of the subElements, either we reduce that to flat plain text, or we make a message handling in JS
             $messages = $this->form->getMessages();
             $this->notification()->error('Password could not be changed');
         }
     }
     $vars['form'] = $this->form;
     if ($request->isXmlHttpRequest()) {
         return new JsonModel($vars);
     }
     return $vars;
 }
Example #3
0
 private function checkAllTokens(UserInterface $user, $tokenHash)
 {
     $now = new \DateTime();
     $tokensToRemove = array();
     $throwExpirationDateException = false;
     /** @var Token $token */
     foreach ($user->getTokens() as $key => $token) {
         if ($token->getExpirationDate() < $now) {
             $tokensToRemove[$key] = $token;
         }
         if ($token->getHash() == $tokenHash) {
             if ($token->getExpirationDate() < $now) {
                 $throwExpirationDateException = true;
             }
         }
     }
     if (!empty($tokensToRemove)) {
         foreach ($tokensToRemove as $key => $token) {
             $user->getTokens()->remove($key);
             $this->repositoryService->remove($token);
         }
     }
     if ($throwExpirationDateException) {
         throw new TokenExpirationDateExpiredException();
     }
 }
 /**
  * @param string $key
  * @return IdentifiableEntityInterface|null
  */
 public function getAttachedEntity($key)
 {
     if (!isset($this->references[$key])) {
         return;
     }
     $reference = $this->references[$key];
     $entity = $this->repositories->getRepository($reference['repository'])->find($reference['id']);
     if (!$entity) {
         // remove reference if entity does not exists
         unset($this->references[$key]);
     }
     return $entity;
 }
 public function generate(UserInterface $user, $daysToLive = 1, $storeUser = true)
 {
     $tokenHash = Rand::getString(64, $this->charList);
     $dateStr = sprintf('+ %d day', $daysToLive);
     $expirationDate = new \Datetime($dateStr);
     /* @todo We should consider using the Prototype Design Pattern here. */
     $token = new Token();
     $token->setHash($tokenHash)->setExpirationDate($expirationDate);
     $user->getTokens()->add($token);
     if ($storeUser) {
         $this->repositoryService->store($user);
     }
     return $tokenHash;
 }
 /**
  * @param Params $params
  * @param bool   $allowDraft
  *
  * @return object|\Organizations\Entity\Organization
  * @throws UnauthorizedAccessException
  * @throws \Doctrine\ODM\MongoDB\LockException
  * @throws NotFoundException
  */
 public function process(Params $params, $allowDraft = true)
 {
     $repositories = $this->repositoryService;
     /* @var \Organizations\Repository\Organization $organizationRepository */
     $organizationRepository = $this->repositoryService->get('Organizations/Organization');
     $idFromRoute = $params('id', 0);
     $idFromSubForm = $params()->fromPost('id', 0);
     $user = $this->auth->getUser();
     /* @var $user \Auth\Entity\UserInterface */
     /* @var $organizationId string */
     $organizationId = empty($idFromRoute) ? $idFromSubForm : $idFromRoute;
     $editOwnOrganization = '__my__' === $organizationId;
     if ($editOwnOrganization) {
         /* @var $userOrg \Organizations\Entity\OrganizationReference */
         $userOrg = $user->getOrganization();
         if ($userOrg->hasAssociation() && !$userOrg->isOwner()) {
             throw new UnauthorizedAccessException('You may not edit this organization as you are only employer.');
         }
         $organizationId = $userOrg->hasAssociation() ? $userOrg->getId() : 0;
     }
     if (empty($organizationId) && $allowDraft) {
         /* @var $organization \Organizations\Entity\Organization */
         $organization = $organizationRepository->findDraft($user);
         if (empty($organization)) {
             $organization = $organizationRepository->create();
             $organization->setIsDraft(true);
             $organization->setUser($user);
             if (!$editOwnOrganization) {
                 /* @var $parent \Organizations\Entity\OrganizationReference */
                 $parent = $user->getOrganization();
                 if (!$parent->hasAssociation()) {
                     throw new MissingParentOrganizationException('You cannot create organizations, because you do not belong to a parent organization. Use "User menu -> create my organization" first.');
                 }
                 $organization->setParent($parent->getOrganization());
             }
             $repositories->store($organization);
         }
         return $organization;
     }
     $organization = $organizationRepository->find($organizationId);
     if (!$organization) {
         throw new NotFoundException($organizationId);
     }
     $this->acl->check($organization, 'edit');
     return $organization;
 }
Example #7
0
 /**
  * Assign a template to a job posting
  *
  * @return JsonModel
  */
 public function templateAction()
 {
     try {
         $jobEntity = $this->initializeJob()->get($this->params());
         $jobEntity->setTemplate($this->params('template', 'default'));
         $this->repositoryService->store($jobEntity);
         $this->notification()->success('Template changed');
     } catch (\Exception $e) {
         $this->notification()->danger('Template not changed');
     }
     return new JsonModel(array());
 }
 /**
  * @covers ::getAttachedEntity()
  */
 public function testGetAttachedEntityWithExistentKeyWithExistingEntityInRepository()
 {
     $id = 'someId';
     $entity = $this->getEntity($id);
     $key = 'someKey';
     $className = get_class($entity);
     $this->attachableEntityManager->addAttachedEntity($entity, $key);
     $this->assertArrayHasKey($key, $this->references);
     $repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->getMock();
     $repository->expects($this->once())->method('find')->with($this->equalTo($id))->willReturn($entity);
     $this->repositories->expects($this->once())->method('getRepository')->with($this->equalTo($className))->willReturn($repository);
     $this->assertSame($entity, $this->attachableEntityManager->getAttachedEntity($key));
     $this->assertArrayHasKey($key, $this->references);
 }
 public function testIndexAction_WithPostRequest()
 {
     $postData = array('valid data');
     $request = new Request();
     $request->setMethod(Request::METHOD_POST);
     $request->setPost(new Parameters($postData));
     $userEntity = UserEntityProvider::createEntityWithRandomData();
     $this->authenticationServiceMock->expects($this->once())->method('getUser')->willReturn($userEntity);
     $this->formMock->expects($this->once())->method('bind')->with($userEntity);
     $this->formMock->expects($this->once())->method('setData')->with($postData);
     $this->formMock->expects($this->once())->method('isValid')->willReturn(true);
     $this->repositoriesMock->expects($this->once())->method('store')->with($userEntity);
     $result = $this->controller->dispatch($request);
     $expected = array('valid' => true, 'form' => $this->formMock);
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
     $this->assertSame($expected, $result);
 }
 /**
  * @expectedException        \BadMethodCallException
  * @expectedExceptionMessage Method not exists for this class.
  */
 public function testCallDocumentManagerMethod()
 {
     $this->dm->expects($this->once())->method('getEventManager')->willReturn($this->eventManager);
     $this->assertEquals($this->eventManager, $this->rs->getEventManager());
     $this->rs->foo();
 }