[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/auth/provider/ -> PhabricatorOAuthAuthProvider.php (source)

   1  <?php
   2  
   3  abstract class PhabricatorOAuthAuthProvider extends PhabricatorAuthProvider {
   4  
   5    const PROPERTY_NOTE = 'oauth:app:note';
   6  
   7    protected $adapter;
   8  
   9    abstract protected function newOAuthAdapter();
  10    abstract protected function getIDKey();
  11    abstract protected function getSecretKey();
  12  
  13    public function getDescriptionForCreate() {
  14      return pht('Configure %s OAuth.', $this->getProviderName());
  15    }
  16  
  17    public function getAdapter() {
  18      if (!$this->adapter) {
  19        $adapter = $this->newOAuthAdapter();
  20        $this->adapter = $adapter;
  21        $this->configureAdapter($adapter);
  22      }
  23      return $this->adapter;
  24    }
  25  
  26    public function isLoginFormAButton() {
  27      return true;
  28    }
  29  
  30    public function readFormValuesFromProvider() {
  31      $config = $this->getProviderConfig();
  32      $id = $config->getProperty($this->getIDKey());
  33      $secret = $config->getProperty($this->getSecretKey());
  34      $note = $config->getProperty(self::PROPERTY_NOTE);
  35  
  36      return array(
  37        $this->getIDKey()     => $id,
  38        $this->getSecretKey() => $secret,
  39        self::PROPERTY_NOTE   => $note,
  40      );
  41    }
  42  
  43    public function readFormValuesFromRequest(AphrontRequest $request) {
  44      return array(
  45        $this->getIDKey()     => $request->getStr($this->getIDKey()),
  46        $this->getSecretKey() => $request->getStr($this->getSecretKey()),
  47        self::PROPERTY_NOTE   => $request->getStr(self::PROPERTY_NOTE),
  48      );
  49    }
  50  
  51    protected function processOAuthEditForm(
  52      AphrontRequest $request,
  53      array $values,
  54      $id_error,
  55      $secret_error) {
  56  
  57      $errors = array();
  58      $issues = array();
  59      $key_id = $this->getIDKey();
  60      $key_secret = $this->getSecretKey();
  61  
  62      if (!strlen($values[$key_id])) {
  63        $errors[] = $id_error;
  64        $issues[$key_id] = pht('Required');
  65      }
  66  
  67      if (!strlen($values[$key_secret])) {
  68        $errors[] = $secret_error;
  69        $issues[$key_secret] = pht('Required');
  70      }
  71  
  72      // If the user has not changed the secret, don't update it (that is,
  73      // don't cause a bunch of "****" to be written to the database).
  74      if (preg_match('/^[*]+$/', $values[$key_secret])) {
  75        unset($values[$key_secret]);
  76      }
  77  
  78      return array($errors, $issues, $values);
  79    }
  80  
  81    public function getConfigurationHelp() {
  82      $help = $this->getProviderConfigurationHelp();
  83  
  84      return $help."\n\n".
  85        pht('Use the **OAuth App Notes** field to record details about which '.
  86            'account the external application is registered under.');
  87    }
  88  
  89    abstract protected function getProviderConfigurationHelp();
  90  
  91    protected function extendOAuthEditForm(
  92      AphrontRequest $request,
  93      AphrontFormView $form,
  94      array $values,
  95      array $issues,
  96      $id_label,
  97      $secret_label) {
  98  
  99      $key_id = $this->getIDKey();
 100      $key_secret = $this->getSecretKey();
 101      $key_note = self::PROPERTY_NOTE;
 102  
 103      $v_id = $values[$key_id];
 104      $v_secret = $values[$key_secret];
 105      if ($v_secret) {
 106        $v_secret = str_repeat('*', strlen($v_secret));
 107      }
 108      $v_note = $values[$key_note];
 109  
 110      $e_id = idx($issues, $key_id, $request->isFormPost() ? null : true);
 111      $e_secret = idx($issues, $key_secret, $request->isFormPost() ? null : true);
 112  
 113      $form
 114        ->appendChild(
 115          id(new AphrontFormTextControl())
 116            ->setLabel($id_label)
 117            ->setName($key_id)
 118            ->setValue($v_id)
 119            ->setError($e_id))
 120        ->appendChild(
 121          id(new AphrontFormPasswordControl())
 122            ->setLabel($secret_label)
 123            ->setDisableAutocomplete(true)
 124            ->setName($key_secret)
 125            ->setValue($v_secret)
 126            ->setError($e_secret))
 127        ->appendChild(
 128          id(new AphrontFormTextAreaControl())
 129          ->setLabel(pht('OAuth App Notes'))
 130          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)
 131          ->setName($key_note)
 132          ->setValue($v_note));
 133    }
 134  
 135    public function renderConfigPropertyTransactionTitle(
 136      PhabricatorAuthProviderConfigTransaction $xaction) {
 137  
 138      $author_phid = $xaction->getAuthorPHID();
 139      $old = $xaction->getOldValue();
 140      $new = $xaction->getNewValue();
 141      $key = $xaction->getMetadataValue(
 142        PhabricatorAuthProviderConfigTransaction::PROPERTY_KEY);
 143  
 144      switch ($key) {
 145        case self::PROPERTY_NOTE:
 146          if (strlen($old)) {
 147            return pht(
 148              '%s updated the OAuth application notes for this provider.',
 149              $xaction->renderHandleLink($author_phid));
 150          } else {
 151            return pht(
 152              '%s set the OAuth application notes for this provider.',
 153              $xaction->renderHandleLink($author_phid));
 154          }
 155  
 156      }
 157  
 158      return parent::renderConfigPropertyTransactionTitle($xaction);
 159    }
 160  
 161    protected function willSaveAccount(PhabricatorExternalAccount $account) {
 162      parent::willSaveAccount($account);
 163      $this->synchronizeOAuthAccount($account);
 164    }
 165  
 166    abstract protected function synchronizeOAuthAccount(
 167      PhabricatorExternalAccount $account);
 168  
 169  }


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