2023-03-24 14:47:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-07-09 08:45:10 +00:00
|
|
|
use OpenApi\Attributes as OA;
|
2023-09-11 08:15:45 +00:00
|
|
|
use phpseclib3\Crypt\PublicKeyLoader;
|
2023-06-07 13:08:35 +00:00
|
|
|
|
2024-07-09 08:45:10 +00:00
|
|
|
#[OA\Schema(
|
|
|
|
description: 'Private Key model',
|
|
|
|
type: 'object',
|
|
|
|
properties: [
|
|
|
|
'id' => ['type' => 'integer'],
|
|
|
|
'uuid' => ['type' => 'string'],
|
|
|
|
'name' => ['type' => 'string'],
|
|
|
|
'description' => ['type' => 'string'],
|
|
|
|
'private_key' => ['type' => 'string', 'format' => 'private-key'],
|
|
|
|
'is_git_related' => ['type' => 'boolean'],
|
|
|
|
'team_id' => ['type' => 'integer'],
|
|
|
|
'created_at' => ['type' => 'string'],
|
|
|
|
'updated_at' => ['type' => 'string'],
|
|
|
|
],
|
|
|
|
)]
|
2023-03-24 14:47:58 +00:00
|
|
|
class PrivateKey extends BaseModel
|
|
|
|
{
|
2023-04-26 13:38:50 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'private_key',
|
2023-06-19 08:58:00 +00:00
|
|
|
'is_git_related',
|
2023-04-26 13:38:50 +00:00
|
|
|
'team_id',
|
|
|
|
];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-07-05 11:35:51 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::saving(function ($key) {
|
|
|
|
$privateKey = data_get($key, 'private_key');
|
|
|
|
if (substr($privateKey, -1) !== "\n") {
|
|
|
|
$key->private_key = $privateKey."\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public static function ownedByCurrentTeam(array $select = ['*'])
|
2023-06-07 13:08:35 +00:00
|
|
|
{
|
2023-06-16 10:05:52 +00:00
|
|
|
$selectArray = collect($select)->concat(['id']);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-22 15:44:49 +00:00
|
|
|
return PrivateKey::whereTeamId(currentTeam()->id)->select($selectArray->all());
|
2023-06-07 13:08:35 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-11 08:15:45 +00:00
|
|
|
public function publicKey()
|
|
|
|
{
|
|
|
|
try {
|
2024-06-10 20:43:34 +00:00
|
|
|
return PublicKeyLoader::load($this->private_key)->getPublicKey()->toString('OpenSSH', ['comment' => '']);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-11 08:15:45 +00:00
|
|
|
return 'Error loading private key';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
if ($this->servers()->count() === 0 && $this->applications()->count() === 0 && $this->githubApps()->count() === 0 && $this->gitlabApps()->count() === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function servers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Server::class);
|
|
|
|
}
|
|
|
|
|
2023-06-19 07:44:39 +00:00
|
|
|
public function applications()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Application::class);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-19 07:44:39 +00:00
|
|
|
public function githubApps()
|
|
|
|
{
|
|
|
|
return $this->hasMany(GithubApp::class);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-19 07:44:39 +00:00
|
|
|
public function gitlabApps()
|
|
|
|
{
|
|
|
|
return $this->hasMany(GitlabApp::class);
|
|
|
|
}
|
2023-03-24 14:47:58 +00:00
|
|
|
}
|