3 final class PhabricatorAuthSSHKey
4 extends PhabricatorAuthDAO
6 PhabricatorPolicyInterface
,
7 PhabricatorDestructibleInterface
,
8 PhabricatorApplicationTransactionInterface
{
10 protected $objectPHID;
15 protected $keyComment = '';
16 protected $isTrusted = 0;
19 private $object = self
::ATTACHABLE
;
21 public static function initializeNewSSHKey(
22 PhabricatorUser
$viewer,
23 PhabricatorSSHPublicKeyInterface
$object) {
25 // You must be able to edit an object to create a new key on it.
26 PhabricatorPolicyFilter
::requireCapability(
29 PhabricatorPolicyCapability
::CAN_EDIT
);
31 $object_phid = $object->getPHID();
35 ->setObjectPHID($object_phid)
36 ->attachObject($object);
39 protected function getConfiguration() {
41 self
::CONFIG_AUX_PHID
=> true,
42 self
::CONFIG_COLUMN_SCHEMA
=> array(
44 'keyType' => 'text255',
45 'keyIndex' => 'bytes12',
47 'keyComment' => 'text255',
48 'isTrusted' => 'bool',
49 'isActive' => 'bool?',
51 self
::CONFIG_KEY_SCHEMA
=> array(
52 'key_object' => array(
53 'columns' => array('objectPHID'),
55 'key_active' => array(
56 'columns' => array('isActive', 'objectPHID'),
58 // NOTE: This unique key includes a nullable column, effectively
59 // constraining uniqueness on active keys only.
60 'key_activeunique' => array(
61 'columns' => array('keyIndex', 'isActive'),
65 ) + parent
::getConfiguration();
68 public function save() {
69 $this->setKeyIndex($this->toPublicKey()->getHash());
70 return parent
::save();
73 public function toPublicKey() {
74 return PhabricatorAuthSSHPublicKey
::newFromStoredKey($this);
77 public function getEntireKey() {
81 $this->getKeyComment(),
83 return trim(implode(' ', $parts));
86 public function getObject() {
87 return $this->assertAttached($this->object);
90 public function attachObject(PhabricatorSSHPublicKeyInterface
$object) {
91 $this->object = $object;
95 public function generatePHID() {
96 return PhabricatorPHID
::generateNewPHID(
97 PhabricatorAuthSSHKeyPHIDType
::TYPECONST
);
100 public function getURI() {
101 $id = $this->getID();
102 return "/auth/sshkey/view/{$id}/";
105 /* -( PhabricatorPolicyInterface )----------------------------------------- */
108 public function getCapabilities() {
110 PhabricatorPolicyCapability
::CAN_VIEW
,
111 PhabricatorPolicyCapability
::CAN_EDIT
,
115 public function getPolicy($capability) {
116 if (!$this->getIsActive()) {
117 if ($capability == PhabricatorPolicyCapability
::CAN_EDIT
) {
118 return PhabricatorPolicies
::POLICY_NOONE
;
122 return $this->getObject()->getPolicy($capability);
125 public function hasAutomaticCapability($capability, PhabricatorUser
$viewer) {
126 if (!$this->getIsActive()) {
130 return $this->getObject()->hasAutomaticCapability($capability, $viewer);
133 public function describeAutomaticCapability($capability) {
134 if (!$this->getIsACtive()) {
136 'Revoked SSH keys can not be edited or reinstated.');
140 'SSH keys inherit the policies of the user or object they authenticate.');
143 /* -( PhabricatorDestructibleInterface )----------------------------------- */
146 public function destroyObjectPermanently(
147 PhabricatorDestructionEngine
$engine) {
149 $this->openTransaction();
151 $this->saveTransaction();
155 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
158 public function getApplicationTransactionEditor() {
159 return new PhabricatorAuthSSHKeyEditor();
162 public function getApplicationTransactionTemplate() {
163 return new PhabricatorAuthSSHKeyTransaction();