Path: blob/master/src/applications/auth/storage/PhabricatorAuthSSHKey.php
12256 views
<?php12final class PhabricatorAuthSSHKey3extends PhabricatorAuthDAO4implements5PhabricatorPolicyInterface,6PhabricatorDestructibleInterface,7PhabricatorApplicationTransactionInterface {89protected $objectPHID;10protected $name;11protected $keyType;12protected $keyIndex;13protected $keyBody;14protected $keyComment = '';15protected $isTrusted = 0;16protected $isActive;1718private $object = self::ATTACHABLE;1920public static function initializeNewSSHKey(21PhabricatorUser $viewer,22PhabricatorSSHPublicKeyInterface $object) {2324// You must be able to edit an object to create a new key on it.25PhabricatorPolicyFilter::requireCapability(26$viewer,27$object,28PhabricatorPolicyCapability::CAN_EDIT);2930$object_phid = $object->getPHID();3132return id(new self())33->setIsActive(1)34->setObjectPHID($object_phid)35->attachObject($object);36}3738protected function getConfiguration() {39return array(40self::CONFIG_AUX_PHID => true,41self::CONFIG_COLUMN_SCHEMA => array(42'name' => 'text255',43'keyType' => 'text255',44'keyIndex' => 'bytes12',45'keyBody' => 'text',46'keyComment' => 'text255',47'isTrusted' => 'bool',48'isActive' => 'bool?',49),50self::CONFIG_KEY_SCHEMA => array(51'key_object' => array(52'columns' => array('objectPHID'),53),54'key_active' => array(55'columns' => array('isActive', 'objectPHID'),56),57// NOTE: This unique key includes a nullable column, effectively58// constraining uniqueness on active keys only.59'key_activeunique' => array(60'columns' => array('keyIndex', 'isActive'),61'unique' => true,62),63),64) + parent::getConfiguration();65}6667public function save() {68$this->setKeyIndex($this->toPublicKey()->getHash());69return parent::save();70}7172public function toPublicKey() {73return PhabricatorAuthSSHPublicKey::newFromStoredKey($this);74}7576public function getEntireKey() {77$parts = array(78$this->getKeyType(),79$this->getKeyBody(),80$this->getKeyComment(),81);82return trim(implode(' ', $parts));83}8485public function getObject() {86return $this->assertAttached($this->object);87}8889public function attachObject(PhabricatorSSHPublicKeyInterface $object) {90$this->object = $object;91return $this;92}9394public function generatePHID() {95return PhabricatorPHID::generateNewPHID(96PhabricatorAuthSSHKeyPHIDType::TYPECONST);97}9899public function getURI() {100$id = $this->getID();101return "/auth/sshkey/view/{$id}/";102}103104/* -( PhabricatorPolicyInterface )----------------------------------------- */105106107public function getCapabilities() {108return array(109PhabricatorPolicyCapability::CAN_VIEW,110PhabricatorPolicyCapability::CAN_EDIT,111);112}113114public function getPolicy($capability) {115if (!$this->getIsActive()) {116if ($capability == PhabricatorPolicyCapability::CAN_EDIT) {117return PhabricatorPolicies::POLICY_NOONE;118}119}120121return $this->getObject()->getPolicy($capability);122}123124public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {125if (!$this->getIsActive()) {126return false;127}128129return $this->getObject()->hasAutomaticCapability($capability, $viewer);130}131132public function describeAutomaticCapability($capability) {133if (!$this->getIsACtive()) {134return pht(135'Revoked SSH keys can not be edited or reinstated.');136}137138return pht(139'SSH keys inherit the policies of the user or object they authenticate.');140}141142/* -( PhabricatorDestructibleInterface )----------------------------------- */143144145public function destroyObjectPermanently(146PhabricatorDestructionEngine $engine) {147148$this->openTransaction();149$this->delete();150$this->saveTransaction();151}152153154/* -( PhabricatorApplicationTransactionInterface )------------------------- */155156157public function getApplicationTransactionEditor() {158return new PhabricatorAuthSSHKeyEditor();159}160161public function getApplicationTransactionTemplate() {162return new PhabricatorAuthSSHKeyTransaction();163}164165}166167168