/** * @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; }
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; }
public function indexAction() { $user = $this->auth->getUser(); $error = false; if ($this->params()->fromPost('confirm')) { if ($this->dependencies->removeItems($user)) { $this->auth->clearIdentity(); $user->setStatus(Status::INACTIVE); return $this->redirect()->toRoute('lang'); } else { $error = true; } } return ['lists' => $this->dependencies->getLists(), 'user' => $user, 'limit' => 20, 'error' => $error]; }
/** * @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; }
/** * Deactivate a job posting * * @return null|ViewModel */ public function deactivateAction() { $user = $this->auth->getUser(); $jobEntity = $this->initializeJob()->get($this->params()); try { $jobEntity->changeStatus(Status::INACTIVE, sprintf("Job was deactivated by %s", $user->getInfo()->getDisplayName())); $this->notification()->success('Job has been deactivated'); } catch (\Exception $e) { $this->notification()->danger('Job could not be deactivated'); } return $this->save(array('page' => 2)); }
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); }
/** * Checks, if a user is an Admin * * @return bool */ public function isAdmin() { return $this->authenticationService->getUser()->getRole() == User::ROLE_ADMIN; }
/** * Login with username and password * * @return \Zend\Http\Response|ViewModel */ public function indexAction() { if ($this->auth->hasIdentity()) { return $this->redirect()->toRoute('lang'); } $viewModel = new ViewModel(); $services = $this->serviceLocator; /* @var $loginForm Login */ $loginForm = $this->forms[self::LOGIN]; /* @var $registerForm Register */ $registerForm = $this->forms[self::REGISTER]; /* @var $request \Zend\Http\Request */ $request = $this->getRequest(); if ($request->isPost()) { $data = $this->params()->fromPost(); $adapter = $services->get('Auth/Adapter/UserLogin'); // inject suffixes via shared Events $loginSuffix = ''; // @TODO: replace this by the Plugin LoginFilter $e = $this->getEvent(); $loginSuffixResponseCollection = $this->getEventManager()->trigger('login.getSuffix', $e); if (!$loginSuffixResponseCollection->isEmpty()) { $loginSuffix = $loginSuffixResponseCollection->last(); } $loginForm->setData($data); if (array_key_exists('credentials', $data) && array_key_exists('login', $data['credentials']) && array_key_exists('credential', $data['credentials'])) { $adapter->setIdentity($data['credentials']['login'] . $loginSuffix)->setCredential($data['credentials']['credential']); } $auth = $this->auth; $result = $auth->authenticate($adapter); if ($result->isValid()) { $user = $auth->getUser(); $language = $services->get('Core/Locale')->detectLanguage($request, $user); $this->logger->info('User ' . $user->login . ' logged in'); $ref = $this->params()->fromQuery('ref', false); if ($ref) { $ref = urldecode($ref); $url = preg_replace('~/[a-z]{2}(/|$)~', '/' . $language . '$1', $ref); $url = $request->getBasePath() . $url; } else { $urlHelper = $services->get('ViewHelperManager')->get('url'); $url = $urlHelper('lang', array('lang' => $language)); } $this->notification()->success('You are now logged in.'); return $this->redirect()->toUrl($url); } else { $loginName = $data['credentials']['login']; if (!empty($loginSuffix)) { $loginName = $loginName . ' (' . $loginName . $loginSuffix . ')'; } $this->logger->info('Failed to authenticate User ' . $loginName); $this->notification()->danger('Authentication failed.'); } } $ref = $this->params()->fromQuery('ref', false); if ($ref) { $req = $this->params()->fromQuery('req', false); if ($req) { $this->getResponse()->setStatusCode(Response::STATUS_CODE_401); $viewModel->setVariable('required', true); } $viewModel->setVariable('ref', $ref); } $allowRegister = $this->options->getEnableRegistration(); $allowResetPassword = $this->options->getEnableResetPassword(); if (isset($allowRegister)) { $viewModel->setVariables(['allowRegister' => $allowRegister, 'allowResetPassword' => $allowResetPassword]); } $viewModel->setVariable('loginForm', $loginForm); $viewModel->setVariable('registerForm', $registerForm); /* @deprecated use loginForm instead of form in your view scripts */ $viewModel->setVariable('form', $loginForm); return $viewModel; }
/** * Login with username and password */ public function indexAction() { if ($this->auth->hasIdentity()) { return $this->redirect()->toRoute('lang'); } $viewModel = new ViewModel(); $services = $this->getServiceLocator(); $form = $this->loginForm; if ($this->request->isPost()) { $data = $this->params()->fromPost(); $adapter = $services->get('Auth/Adapter/UserLogin'); // inject suffixes via shared Events $loginSuffix = ''; // @TODO: replace this by the Plugin LoginFilter $e = $this->getEvent(); $loginSuffixResponseCollection = $this->getEventManager()->trigger('login.getSuffix', $e); if (!$loginSuffixResponseCollection->isEmpty()) { $loginSuffix = $loginSuffixResponseCollection->last(); } $form->setData($data); if (array_key_exists('credentials', $data) && array_key_exists('login', $data['credentials']) && array_key_exists('credential', $data['credentials'])) { $adapter->setIdentity($data['credentials']['login'] . $loginSuffix)->setCredential($data['credentials']['credential']); } $auth = $this->auth; $result = $auth->authenticate($adapter); if ($result->isValid()) { $user = $auth->getUser(); $settings = $user->getSettings('Core'); $language = $settings->localization->language; if (!$language) { $headers = $this->getRequest()->getHeaders(); if ($headers->has('Accept-Language')) { $locales = $headers->get('Accept-Language')->getPrioritized(); $language = $locales[0]->type; } else { $language = 'en'; } } $this->logger->info('User ' . $user->login . ' logged in'); $ref = $this->params()->fromQuery('ref', false); if ($ref) { $ref = urldecode($ref); $url = preg_replace('~/[a-z]{2}(/|$)~', '/' . $language . '$1', $ref); $url = $this->getRequest()->getBasePath() . $url; } else { $urlHelper = $services->get('ViewHelperManager')->get('url'); $url = $urlHelper('lang', array('lang' => $language)); } $this->notification()->success('You are now logged in.'); return $this->redirect()->toUrl($url); } else { $databaseName = ''; $config = $services->get('config'); if (array_key_exists('database', $config) && array_key_exists('databaseName', $config['database'])) { $databaseName = $config['database']['databaseName']; } // update for Doctrine if (empty($databaseName) && array_key_exists('doctrine', $config) && array_key_exists('configuration', $config['doctrine']) && array_key_exists('odm_default', $config['doctrine']['configuration']) && array_key_exists('default_db', $config['doctrine']['configuration']['odm_default'])) { $databaseName = $config['doctrine']['configuration']['odm_default']['default_db']; } $loginName = $data['credentials']['login']; if (!empty($loginSuffix)) { $loginName = $loginName . ' (' . $loginName . $loginSuffix . ')'; } $this->logger->info('Failed to authenticate User ' . $loginName . (empty($databaseName) ? '' : ', Database-Name: ' . $databaseName)); $this->notification()->danger('Authentication failed.'); } } $ref = $this->params()->fromQuery('ref', false); if ($ref) { $req = $this->params()->fromQuery('req', false); if ($req) { $this->getResponse()->setStatusCode(403); $viewModel->setVariable('required', true); } $viewModel->setVariable('ref', $ref); } $allowRegister = $services->get('controllerPluginManager')->get('config')->get('allowRegister'); if (isset($allowRegister)) { $viewModel->setVariable('allowRegister', $allowRegister); } $viewModel->setVariable('form', $form); return $viewModel; }