public function onAuthenticationSuccess(Request $request, TokenInterface $token) { $username = $token->getUser()->getUsername(); $user = new UserModel(); $website = Website::get(); if (!$user->readByUserName($username) or $user->role != SystemRoles::SYSTEM_ADMIN and !($user->siteRole->offsetExists($website->domain) and $user->siteRole[$website->domain] != SiteRoles::NONE)) { return $this->httpUtils->createRedirectResponse($request, '/app/logout'); } $request->getSession()->set('user_id', $user->id->asString()); $request->getSession()->set('user', array('username' => $username)); $projectId = $user->getDefaultProjectId($website->domain); if ($projectId) { $request->getSession()->set('projectId', $projectId); } $referer = $this->determineTargetUrl($request); if ($referer and strpos($referer, '/app/') !== false) { return $this->httpUtils->createRedirectResponse($request, $referer); } elseif ($projectId) { $project = ProjectModel::getById($projectId); $url = '/app/' . $project->appName . '/' . $projectId; return $this->httpUtils->createRedirectResponse($request, $url); } else { return $this->httpUtils->createRedirectResponse($request, '/'); } }
public function forgotPassword(Request $request, Application $app) { $username = $request->request->get('_username'); $identityCheck = UserCommands::checkIdentity($username, '', $this->website); if (!$identityCheck->usernameExists) { $app['session']->getFlashBag()->add('errorMessage', 'User not found.'); return $this->view($request, $app, 'forgot_password'); } $user = new UserModel(); $user->readByUserName($username); if (!$identityCheck->usernameExistsOnThisSite and $user->role != SystemRoles::SYSTEM_ADMIN) { $app['session']->getFlashBag()->add('errorMessage', sprintf('Username "%s" not available on "%s". Use "Create an Account".', $username, $this->website->domain)); return $this->view($request, $app, 'forgot_password'); } Communicate::sendForgotPasswordVerification($user, $this->website); $app['session']->getFlashBag()->add('infoMessage', 'Password Reset email sent for username "' . $username . '"'); return $app->redirect('/auth/login'); }
/** * Utility to check if a username already exists and if an email address matches the account * @param string $username * @param string $email * @param Website $website * @return IdentityCheck */ public static function checkIdentity($username, $email = '', $website = null) { $identityCheck = new IdentityCheck(); $user = new UserModel(); $emailUser = new UserModel(); $identityCheck->usernameExists = $user->readByUserName($username); // This utility assumes username matches the account $identityCheck->usernameMatchesAccount = true; if ($website) { $identityCheck->allowSignupFromOtherSites = $website->allowSignupFromOtherSites; if ($identityCheck->usernameExists) { $identityCheck->usernameExistsOnThisSite = $user->hasRoleOnSite($website); } } if ($email) { $identityCheck->emailExists = $emailUser->readByProperty('email', $email); } $identityCheck->emailIsEmpty = empty($user->email); if (!$identityCheck->emailIsEmpty && !empty($email)) { $identityCheck->emailMatchesAccount = $user->email === $email; } return $identityCheck; }
public function testReadByUserName_userNotFound_EmptyModel() { $e = new MongoTestEnvironment(); $e->clean(); $e->createUser('jsmith', 'joe smith', '*****@*****.**'); $user = new UserModel(); $result = $user->readByUserName('adam'); $this->assertFalse($result); $this->assertEqual($user->email, ''); }