coolify/app/Models/PrivateKey.php

73 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use phpseclib3\Crypt\PublicKeyLoader;
2023-06-07 15:08:35 +02:00
class PrivateKey extends BaseModel
{
protected $fillable = [
'name',
'description',
'private_key',
2023-06-19 10:58:00 +02:00
'is_git_related',
'team_id',
];
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 22:43:34 +02:00
public static function ownedByCurrentTeam(array $select = ['*'])
2023-06-07 15:08:35 +02:00
{
2023-06-16 12:05:52 +02:00
$selectArray = collect($select)->concat(['id']);
2024-06-10 22:43:34 +02:00
2023-08-22 17:44:49 +02:00
return PrivateKey::whereTeamId(currentTeam()->id)->select($selectArray->all());
2023-06-07 15:08:35 +02:00
}
public function publicKey()
{
try {
2024-06-10 22:43:34 +02:00
return PublicKeyLoader::load($this->private_key)->getPublicKey()->toString('OpenSSH', ['comment' => '']);
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
return 'Error loading private key';
}
}
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 22:43:34 +02:00
return false;
}
public function servers()
{
return $this->hasMany(Server::class);
}
2023-06-19 09:44:39 +02:00
public function applications()
{
return $this->hasMany(Application::class);
}
2023-06-19 09:44:39 +02:00
public function githubApps()
{
return $this->hasMany(GithubApp::class);
}
2023-06-19 09:44:39 +02:00
public function gitlabApps()
{
return $this->hasMany(GitlabApp::class);
}
}