Path: blob/master/src/applications/auth/controller/PhabricatorAuthSetPasswordController.php
12256 views
<?php12final class PhabricatorAuthSetPasswordController3extends PhabricatorAuthController {45public function shouldAllowPartialSessions() {6return true;7}89public function shouldAllowLegallyNonCompliantUsers() {10return true;11}1213public function handleRequest(AphrontRequest $request) {14$viewer = $this->getViewer();1516if (!PhabricatorPasswordAuthProvider::getPasswordProvider()) {17return new Aphront404Response();18}1920$token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(21$viewer,22$request,23'/');2425$key = $request->getStr('key');26$password_type = PhabricatorAuthPasswordResetTemporaryTokenType::TOKENTYPE;27if (!$key) {28return new Aphront404Response();29}3031$auth_token = id(new PhabricatorAuthTemporaryTokenQuery())32->setViewer($viewer)33->withTokenResources(array($viewer->getPHID()))34->withTokenTypes(array($password_type))35->withTokenCodes(array(PhabricatorHash::weakDigest($key)))36->withExpired(false)37->executeOne();38if (!$auth_token) {39return new Aphront404Response();40}4142$content_source = PhabricatorContentSource::newFromRequest($request);43$account_type = PhabricatorAuthPassword::PASSWORD_TYPE_ACCOUNT;4445$password_objects = id(new PhabricatorAuthPasswordQuery())46->setViewer($viewer)47->withObjectPHIDs(array($viewer->getPHID()))48->withPasswordTypes(array($account_type))49->withIsRevoked(false)50->execute();51if ($password_objects) {52$password_object = head($password_objects);53$has_password = true;54} else {55$password_object = PhabricatorAuthPassword::initializeNewPassword(56$viewer,57$account_type);58$has_password = false;59}6061$engine = id(new PhabricatorAuthPasswordEngine())62->setViewer($viewer)63->setContentSource($content_source)64->setPasswordType($account_type)65->setObject($viewer);6667$e_password = true;68$e_confirm = true;69$errors = array();70if ($request->isFormPost()) {71$password = $request->getStr('password');72$confirm = $request->getStr('confirm');7374$password_envelope = new PhutilOpaqueEnvelope($password);75$confirm_envelope = new PhutilOpaqueEnvelope($confirm);7677try {78$engine->checkNewPassword($password_envelope, $confirm_envelope, true);79$e_password = null;80$e_confirm = null;81} catch (PhabricatorAuthPasswordException $ex) {82$errors[] = $ex->getMessage();83$e_password = $ex->getPasswordError();84$e_confirm = $ex->getConfirmError();85}8687if (!$errors) {88$password_object89->setPassword($password_envelope, $viewer)90->save();9192// Destroy the token.93$auth_token->delete();9495return id(new AphrontRedirectResponse())->setURI('/');96}97}9899$min_len = PhabricatorEnv::getEnvConfig('account.minimum-password-length');100$min_len = (int)$min_len;101102$len_caption = null;103if ($min_len) {104$len_caption = pht('Minimum password length: %d characters.', $min_len);105}106107if ($has_password) {108$title = pht('Reset Password');109$crumb = pht('Reset Password');110$submit = pht('Reset Password');111} else {112$title = pht('Set Password');113$crumb = pht('Set Password');114$submit = pht('Set Account Password');115}116117$form = id(new AphrontFormView())118->setViewer($viewer)119->addHiddenInput('key', $key)120->appendChild(121id(new AphrontFormPasswordControl())122->setDisableAutocomplete(true)123->setLabel(pht('New Password'))124->setError($e_password)125->setName('password'))126->appendChild(127id(new AphrontFormPasswordControl())128->setDisableAutocomplete(true)129->setLabel(pht('Confirm Password'))130->setCaption($len_caption)131->setError($e_confirm)132->setName('confirm'))133->appendChild(134id(new AphrontFormSubmitControl())135->addCancelButton('/', pht('Skip This Step'))136->setValue($submit));137138$form_box = id(new PHUIObjectBoxView())139->setHeaderText($title)140->setFormErrors($errors)141->setBackground(PHUIObjectBoxView::WHITE_CONFIG)142->setForm($form);143144$main_view = id(new PHUITwoColumnView())145->setFooter($form_box);146147$crumbs = $this->buildApplicationCrumbs()148->addTextCrumb($crumb)149->setBorder(true);150151return $this->newPage()152->setTitle($title)153->setCrumbs($crumbs)154->appendChild($main_view);155}156}157158159