/** * Login via an external Application. This will get obsolet as soon we'll have a full featured Rest API. * * Passed in params: * - appKey: Application identifier key * - user: Name of the user to log in * - pass: Password of the user to log in * * Returns an json response with the session-id. * Non existent users will be created! * */ public function loginExternAction() { $services = $this->serviceLocator; $adapter = $services->get('ExternalApplicationAdapter'); $appKey = $this->params()->fromPost('appKey'); $adapter->setIdentity($this->params()->fromPost('user'))->setCredential($this->params()->fromPost('pass'))->setApplicationKey($appKey); $auth = $this->auth; $result = $auth->authenticate($adapter); if ($result->isValid()) { $this->logger->info('User ' . $this->params()->fromPost('user') . ' logged via ' . $appKey); // the external login may include some parameters for an update $updateParams = $this->params()->fromPost(); unset($updateParams['user'], $updateParams['pass'], $updateParams['appKey']); $resultMessage = $result->getMessages(); $password = null; if (array_key_exists('firstLogin', $resultMessage) && $resultMessage['firstLogin'] === true) { $password = substr(md5(uniqid()), 0, 6); $updateParams['password'] = $password; } if (!empty($updateParams)) { $user = $auth->getUser(); try { foreach ($updateParams as $updateKey => $updateValue) { if ('email' == $updateKey) { $user->info->email = $updateValue; } $user->{$updateKey} = $updateValue; } } catch (\Exception $e) { } $services->get('repositories')->store($user); } $resultMessage = $result->getMessages(); // TODO: send a mail also when required (maybe first mail failed or email has changed) if (array_key_exists('firstLogin', $resultMessage) && $resultMessage['firstLogin'] === true) { // first external Login $userName = $this->params()->fromPost('user'); $this->logger->debug('first login for User: '******'/^(.*)@\\w+$/', $userName, $realUserName)) { $userName = $realUserName[1]; } $mail = $this->mailer('htmltemplate'); /* @var $mail \Core\Mail\HTMLTemplateMessage */ $apps = $this->config('external_applications'); $apps = array_flip($apps); $application = isset($apps[$appKey]) ? $apps[$appKey] : null; $mail->setVariables(array('application' => $application, 'login' => $userName, 'password' => $password)); $mail->setSubject($this->options->getMailSubjectRegistration()); $mail->setTemplate('mail/first-external-login'); $mail->addTo($user->getInfo()->getEmail()); try { $this->mailer($mail); $this->logger->info('Mail first-login sent to ' . $userName); } catch (\Zend\Mail\Transport\Exception\ExceptionInterface $e) { $this->logger->warn('No Mail was sent'); $this->logger->debug($e); } } return new JsonModel(array('status' => 'success', 'token' => session_id())); } else { $this->logger->info('Failed to authenticate User ' . $this->params()->fromPost('user') . ' via ' . $this->params()->fromPost('appKey')); $this->getResponse()->setStatusCode(Response::STATUS_CODE_401); return new JsonModel(array('status' => 'failure', 'user' => $this->params()->fromPost('user'), 'appKey' => $this->params()->fromPost('appKey'), 'code' => $result->getCode(), 'messages' => $result->getMessages())); } }