-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Birkner
committed
Dec 2, 2016
1 parent
0d6d267
commit 7cfb3aa
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Nitrapi\SSHKeys; | ||
|
||
use Nitrapi\Nitrapi; | ||
|
||
class Manager | ||
{ | ||
/** | ||
* @var Nitrapi | ||
*/ | ||
private $api; | ||
|
||
public function __construct(Nitrapi $api) | ||
{ | ||
$this->api = $api; | ||
} | ||
|
||
/** | ||
* Returns all your SSH Public Keys | ||
* | ||
* @return array | ||
*/ | ||
public function getPublicKeys() | ||
{ | ||
$url = "user/ssh_keys"; | ||
$keys = []; | ||
foreach ($this->api->dataGet($url)['keys'] as $key) { | ||
$keys[] = new SSHKey($this->api, $key); | ||
} | ||
return $keys; | ||
} | ||
|
||
/** | ||
* Uploads a new SSH Public Key | ||
* | ||
* @param $key | ||
* @param bool $enabled | ||
* @return bool | ||
*/ | ||
public function uploadPublicKey($key, $enabled = true) | ||
{ | ||
$url = "user/ssh_keys"; | ||
$this->api->dataPost($url, [ | ||
'key' => $key, | ||
'enabled' => ($enabled ? 'true' : 'false') | ||
]); | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Nitrapi\SSHKeys; | ||
|
||
use Nitrapi\Nitrapi; | ||
|
||
class SSHKey { | ||
|
||
/** | ||
* @var Nitrapi | ||
*/ | ||
private $api; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $data; | ||
|
||
public function __construct(Nitrapi $api, array $data) { | ||
$this->api = $api; | ||
$this->data = $data; | ||
$this->data['full_public_key'] = $this->data['type'] . ' ' . $this->data['public_key'] . ' ' . $this->data['comment']; | ||
} | ||
|
||
/** | ||
* Returns the full SSH public key | ||
* | ||
* @return string | ||
*/ | ||
public function getPublicKey() | ||
{ | ||
return $this->data['full_public_key']; | ||
} | ||
|
||
/** | ||
* Updates the existing SSH public key | ||
* | ||
* @return SSHKey | ||
*/ | ||
public function setPublicKey($key) | ||
{ | ||
$this->data['full_public_key'] = $key; | ||
$this->doUpdate(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns true if the key is enabled | ||
* | ||
* @return bool | ||
*/ | ||
public function isEnabled() | ||
{ | ||
return (bool)$this->data['enabled']; | ||
} | ||
|
||
/** | ||
* Returns true if the key is enabled | ||
* | ||
* @return SSHKey | ||
*/ | ||
public function setEnabled($enabled = true) | ||
{ | ||
$this->data['enabled'] = $enabled; | ||
$this->doUpdate(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Deletes this SSH public key | ||
* | ||
* @return bool | ||
*/ | ||
public function doDelete() | ||
{ | ||
$url = "user/ssh_keys/" . $this->data['id']; | ||
$this->api->dataDelete($url); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Updates this SSH public key in database | ||
* | ||
* @return $this | ||
*/ | ||
private function doUpdate() | ||
{ | ||
$url = "user/ssh_keys/" . $this->data['id']; | ||
$this->api->dataPost($url, [ | ||
'key' => $this->getPublicKey(), | ||
'enabled' => ($this->isEnabled() ? 'true' : 'false') | ||
]); | ||
|
||
return $this; | ||
} | ||
|
||
} |