[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/passphrase/conduit/ -> PassphraseQueryConduitAPIMethod.php (source)

   1  <?php
   2  
   3  final class PassphraseQueryConduitAPIMethod
   4    extends PassphraseConduitAPIMethod {
   5  
   6    public function getAPIMethodName() {
   7      return 'passphrase.query';
   8    }
   9  
  10    public function getMethodDescription() {
  11      return pht('Query credentials.');
  12    }
  13  
  14    public function defineParamTypes() {
  15      return array(
  16        'ids'           => 'optional list<int>',
  17        'phids'         => 'optional list<phid>',
  18        'needSecrets'   => 'optional bool',
  19        'needPublicKeys'   => 'optional bool',
  20      ) + $this->getPagerParamTypes();
  21    }
  22  
  23    public function defineReturnType() {
  24      return 'list<dict>';
  25    }
  26  
  27    public function defineErrorTypes() {
  28      return array();
  29    }
  30  
  31    protected function execute(ConduitAPIRequest $request) {
  32      $query = id(new PassphraseCredentialQuery())
  33        ->setViewer($request->getUser());
  34  
  35      if ($request->getValue('ids')) {
  36        $query->withIDs($request->getValue('ids'));
  37      }
  38  
  39      if ($request->getValue('phids')) {
  40        $query->withPHIDs($request->getValue('phids'));
  41      }
  42  
  43      if ($request->getValue('needSecrets')) {
  44        $query->needSecrets(true);
  45      }
  46  
  47      $pager = $this->newPager($request);
  48      $credentials = $query->executeWithCursorPager($pager);
  49  
  50      $results = array();
  51      foreach ($credentials as $credential) {
  52        $type = PassphraseCredentialType::getTypeByConstant(
  53          $credential->getCredentialType());
  54        if (!$type) {
  55          continue;
  56        }
  57  
  58        $public_key = null;
  59        if ($request->getValue('needPublicKeys') && $type->hasPublicKey()) {
  60          $public_key = $type->getPublicKey(
  61            $request->getUser(),
  62            $credential);
  63        }
  64  
  65        $secret = null;
  66        if ($request->getValue('needSecrets')) {
  67          if ($credential->getAllowConduit()) {
  68            $secret = $credential->getSecret()->openEnvelope();
  69          }
  70        }
  71  
  72        $material = array();
  73        switch ($credential->getCredentialType()) {
  74          case PassphraseCredentialTypeSSHPrivateKeyFile::CREDENTIAL_TYPE:
  75            if ($secret) {
  76              $material['file'] = $secret;
  77            }
  78            if ($public_key) {
  79              $material['publicKey'] = $public_key;
  80            }
  81            break;
  82          case PassphraseCredentialTypeSSHGeneratedKey::CREDENTIAL_TYPE:
  83          case PassphraseCredentialTypeSSHPrivateKeyText::CREDENTIAL_TYPE:
  84            if ($secret) {
  85              $material['privateKey'] = $secret;
  86            }
  87            if ($public_key) {
  88              $material['publicKey'] = $public_key;
  89            }
  90            break;
  91          case PassphraseCredentialTypePassword::CREDENTIAL_TYPE:
  92            if ($secret) {
  93              $material['password'] = $secret;
  94            }
  95            break;
  96        }
  97  
  98        if (!$credential->getAllowConduit()) {
  99          $material['noAPIAccess'] = pht(
 100            'This credential\'s private material '.
 101            'is not accessible via API calls.');
 102        }
 103  
 104        $results[$credential->getPHID()] = array(
 105          'id' => $credential->getID(),
 106          'phid' => $credential->getPHID(),
 107          'type' => $credential->getCredentialType(),
 108          'name' => $credential->getName(),
 109          'uri' =>
 110            PhabricatorEnv::getProductionURI('/'.$credential->getMonogram()),
 111          'monogram' => $credential->getMonogram(),
 112          'username' => $credential->getUsername(),
 113          'material' => $material,
 114        );
 115      }
 116  
 117      $result = array(
 118        'data' => $results,
 119      );
 120  
 121      return $this->addPagerResults($result, $pager);
 122    }
 123  
 124  }


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