[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/settings/panel/ -> PhabricatorSettingsPanelMultiFactor.php (source)

   1  <?php
   2  
   3  final class PhabricatorSettingsPanelMultiFactor
   4    extends PhabricatorSettingsPanel {
   5  
   6    public function getPanelKey() {
   7      return 'multifactor';
   8    }
   9  
  10    public function getPanelName() {
  11      return pht('Multi-Factor Auth');
  12    }
  13  
  14    public function getPanelGroup() {
  15      return pht('Authentication');
  16    }
  17  
  18    public function processRequest(AphrontRequest $request) {
  19      if ($request->getExists('new')) {
  20        return $this->processNew($request);
  21      }
  22  
  23      if ($request->getExists('edit')) {
  24        return $this->processEdit($request);
  25      }
  26  
  27      if ($request->getExists('delete')) {
  28        return $this->processDelete($request);
  29      }
  30  
  31      $user = $this->getUser();
  32      $viewer = $request->getUser();
  33  
  34      $factors = id(new PhabricatorAuthFactorConfig())->loadAllWhere(
  35        'userPHID = %s',
  36        $user->getPHID());
  37  
  38      $rows = array();
  39      $rowc = array();
  40  
  41      $highlight_id = $request->getInt('id');
  42      foreach ($factors as $factor) {
  43  
  44        $impl = $factor->getImplementation();
  45        if ($impl) {
  46          $type = $impl->getFactorName();
  47        } else {
  48          $type = $factor->getFactorKey();
  49        }
  50  
  51        if ($factor->getID() == $highlight_id) {
  52          $rowc[] = 'highlighted';
  53        } else {
  54          $rowc[] = null;
  55        }
  56  
  57        $rows[] = array(
  58          javelin_tag(
  59            'a',
  60            array(
  61              'href' => $this->getPanelURI('?edit='.$factor->getID()),
  62              'sigil' => 'workflow',
  63            ),
  64            $factor->getFactorName()),
  65          $type,
  66          phabricator_datetime($factor->getDateCreated(), $viewer),
  67          javelin_tag(
  68            'a',
  69            array(
  70              'href' => $this->getPanelURI('?delete='.$factor->getID()),
  71              'sigil' => 'workflow',
  72              'class' => 'small grey button',
  73            ),
  74            pht('Remove')),
  75        );
  76      }
  77  
  78      $table = new AphrontTableView($rows);
  79      $table->setNoDataString(
  80        pht("You haven't added any authentication factors to your account yet."));
  81      $table->setHeaders(
  82        array(
  83          pht('Name'),
  84          pht('Type'),
  85          pht('Created'),
  86          '',
  87        ));
  88      $table->setColumnClasses(
  89        array(
  90          'wide pri',
  91          '',
  92          'right',
  93          'action',
  94        ));
  95      $table->setRowClasses($rowc);
  96      $table->setDeviceVisibility(
  97        array(
  98          true,
  99          false,
 100          false,
 101          true,
 102        ));
 103  
 104      $panel = new PHUIObjectBoxView();
 105      $header = new PHUIHeaderView();
 106  
 107      $help_uri = PhabricatorEnv::getDoclink(
 108        'User Guide: Multi-Factor Authentication');
 109  
 110      $help_icon = id(new PHUIIconView())
 111        ->setIconFont('fa-info-circle');
 112      $help_button = id(new PHUIButtonView())
 113        ->setText(pht('Help'))
 114        ->setHref($help_uri)
 115        ->setTag('a')
 116        ->setIcon($help_icon);
 117  
 118      $create_icon = id(new PHUIIconView())
 119        ->setIconFont('fa-plus');
 120      $create_button = id(new PHUIButtonView())
 121        ->setText(pht('Add Authentication Factor'))
 122        ->setHref($this->getPanelURI('?new=true'))
 123        ->setTag('a')
 124        ->setWorkflow(true)
 125        ->setIcon($create_icon);
 126  
 127      $header->setHeader(pht('Authentication Factors'));
 128      $header->addActionLink($help_button);
 129      $header->addActionLink($create_button);
 130  
 131      $panel->setHeader($header);
 132      $panel->appendChild($table);
 133  
 134      return $panel;
 135    }
 136  
 137    private function processNew(AphrontRequest $request) {
 138      $viewer = $request->getUser();
 139      $user = $this->getUser();
 140  
 141      $token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(
 142        $viewer,
 143        $request,
 144        $this->getPanelURI());
 145  
 146      $factors = PhabricatorAuthFactor::getAllFactors();
 147  
 148      $form = id(new AphrontFormView())
 149        ->setUser($viewer);
 150  
 151      $type = $request->getStr('type');
 152      if (empty($factors[$type]) || !$request->isFormPost()) {
 153        $factor = null;
 154      } else {
 155        $factor = $factors[$type];
 156      }
 157  
 158      $dialog = id(new AphrontDialogView())
 159        ->setUser($viewer)
 160        ->addHiddenInput('new', true);
 161  
 162      if ($factor === null) {
 163        $choice_control = id(new AphrontFormRadioButtonControl())
 164          ->setName('type')
 165          ->setValue(key($factors));
 166  
 167        foreach ($factors as $available_factor) {
 168          $choice_control->addButton(
 169            $available_factor->getFactorKey(),
 170            $available_factor->getFactorName(),
 171            $available_factor->getFactorDescription());
 172        }
 173  
 174        $dialog->appendParagraph(
 175          pht(
 176            'Adding an additional authentication factor improves the security '.
 177            'of your account. Choose the type of factor to add:'));
 178  
 179        $form
 180          ->appendChild($choice_control);
 181  
 182      } else {
 183        $dialog->addHiddenInput('type', $type);
 184  
 185        $config = $factor->processAddFactorForm(
 186          $form,
 187          $request,
 188          $user);
 189  
 190        if ($config) {
 191          $config->save();
 192  
 193          $log = PhabricatorUserLog::initializeNewLog(
 194            $viewer,
 195            $user->getPHID(),
 196            PhabricatorUserLog::ACTION_MULTI_ADD);
 197          $log->save();
 198  
 199          $user->updateMultiFactorEnrollment();
 200  
 201          // Terminate other sessions so they must log in and survive the
 202          // multi-factor auth check.
 203  
 204          id(new PhabricatorAuthSessionEngine())->terminateLoginSessions(
 205            $user,
 206            $request->getCookie(PhabricatorCookies::COOKIE_SESSION));
 207  
 208          return id(new AphrontRedirectResponse())
 209            ->setURI($this->getPanelURI('?id='.$config->getID()));
 210        }
 211      }
 212  
 213      $dialog
 214        ->setWidth(AphrontDialogView::WIDTH_FORM)
 215        ->setTitle(pht('Add Authentication Factor'))
 216        ->appendChild($form->buildLayoutView())
 217        ->addSubmitButton(pht('Continue'))
 218        ->addCancelButton($this->getPanelURI());
 219  
 220      return id(new AphrontDialogResponse())
 221        ->setDialog($dialog);
 222    }
 223  
 224    private function processEdit(AphrontRequest $request) {
 225      $viewer = $request->getUser();
 226      $user = $this->getUser();
 227  
 228      $factor = id(new PhabricatorAuthFactorConfig())->loadOneWhere(
 229        'id = %d AND userPHID = %s',
 230        $request->getInt('edit'),
 231        $user->getPHID());
 232      if (!$factor) {
 233        return new Aphront404Response();
 234      }
 235  
 236      $e_name = true;
 237      $errors = array();
 238      if ($request->isFormPost()) {
 239        $name = $request->getStr('name');
 240        if (!strlen($name)) {
 241          $e_name = pht('Required');
 242          $errors[] = pht(
 243            'Authentication factors must have a name to identify them.');
 244        }
 245  
 246        if (!$errors) {
 247          $factor->setFactorName($name);
 248          $factor->save();
 249  
 250          $user->updateMultiFactorEnrollment();
 251  
 252          return id(new AphrontRedirectResponse())
 253            ->setURI($this->getPanelURI('?id='.$factor->getID()));
 254        }
 255      } else {
 256        $name = $factor->getFactorName();
 257      }
 258  
 259      $form = id(new AphrontFormView())
 260        ->setUser($viewer)
 261        ->appendChild(
 262          id(new AphrontFormTextControl())
 263            ->setName('name')
 264            ->setLabel(pht('Name'))
 265            ->setValue($name)
 266            ->setError($e_name));
 267  
 268      $dialog = id(new AphrontDialogView())
 269        ->setUser($viewer)
 270        ->addHiddenInput('edit', $factor->getID())
 271        ->setTitle(pht('Edit Authentication Factor'))
 272        ->setErrors($errors)
 273        ->appendChild($form->buildLayoutView())
 274        ->addSubmitButton(pht('Save'))
 275        ->addCancelButton($this->getPanelURI());
 276  
 277      return id(new AphrontDialogResponse())
 278        ->setDialog($dialog);
 279    }
 280  
 281    private function processDelete(AphrontRequest $request) {
 282      $viewer = $request->getUser();
 283      $user = $this->getUser();
 284  
 285      $token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(
 286        $viewer,
 287        $request,
 288        $this->getPanelURI());
 289  
 290      $factor = id(new PhabricatorAuthFactorConfig())->loadOneWhere(
 291        'id = %d AND userPHID = %s',
 292        $request->getInt('delete'),
 293        $user->getPHID());
 294      if (!$factor) {
 295        return new Aphront404Response();
 296      }
 297  
 298      if ($request->isFormPost()) {
 299        $factor->delete();
 300  
 301        $log = PhabricatorUserLog::initializeNewLog(
 302          $viewer,
 303          $user->getPHID(),
 304          PhabricatorUserLog::ACTION_MULTI_REMOVE);
 305        $log->save();
 306  
 307        $user->updateMultiFactorEnrollment();
 308  
 309        return id(new AphrontRedirectResponse())
 310          ->setURI($this->getPanelURI());
 311      }
 312  
 313      $dialog = id(new AphrontDialogView())
 314        ->setUser($viewer)
 315        ->addHiddenInput('delete', $factor->getID())
 316        ->setTitle(pht('Delete Authentication Factor'))
 317        ->appendParagraph(
 318          pht(
 319            'Really remove the authentication factor %s from your account?',
 320            phutil_tag('strong', array(), $factor->getFactorName())))
 321        ->addSubmitButton(pht('Remove Factor'))
 322        ->addCancelButton($this->getPanelURI());
 323  
 324      return id(new AphrontDialogResponse())
 325        ->setDialog($dialog);
 326    }
 327  
 328  
 329  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1