2023-03-24 13:54:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-05-25 16:27:52 +00:00
|
|
|
use App\Notifications\Channels\SendsDiscord;
|
2023-08-08 09:51:36 +00:00
|
|
|
use App\Notifications\Channels\SendsEmail;
|
2023-08-31 13:00:59 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2023-06-15 07:15:41 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-05-25 16:27:52 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2024-07-06 12:34:15 +00:00
|
|
|
use OpenApi\Attributes as OA;
|
2023-05-25 16:27:52 +00:00
|
|
|
|
2024-07-06 12:34:15 +00:00
|
|
|
#[OA\Schema(
|
|
|
|
description: 'Team model',
|
|
|
|
type: 'object',
|
|
|
|
properties: [
|
|
|
|
'id' => ['type' => 'integer', 'description' => 'The unique identifier of the team.'],
|
|
|
|
'name' => ['type' => 'string', 'description' => 'The name of the team.'],
|
|
|
|
'description' => ['type' => 'string', 'description' => 'The description of the team.'],
|
|
|
|
'personal_team' => ['type' => 'boolean', 'description' => 'Whether the team is personal or not.'],
|
|
|
|
'created_at' => ['type' => 'string', 'description' => 'The date and time the team was created.'],
|
|
|
|
'updated_at' => ['type' => 'string', 'description' => 'The date and time the team was last updated.'],
|
|
|
|
'smtp_enabled' => ['type' => 'boolean', 'description' => 'Whether SMTP is enabled or not.'],
|
|
|
|
'smtp_from_address' => ['type' => 'string', 'description' => 'The email address to send emails from.'],
|
|
|
|
'smtp_from_name' => ['type' => 'string', 'description' => 'The name to send emails from.'],
|
|
|
|
'smtp_recipients' => ['type' => 'string', 'description' => 'The email addresses to send emails to.'],
|
|
|
|
'smtp_host' => ['type' => 'string', 'description' => 'The SMTP host.'],
|
|
|
|
'smtp_port' => ['type' => 'string', 'description' => 'The SMTP port.'],
|
|
|
|
'smtp_encryption' => ['type' => 'string', 'description' => 'The SMTP encryption.'],
|
|
|
|
'smtp_username' => ['type' => 'string', 'description' => 'The SMTP username.'],
|
|
|
|
'smtp_password' => ['type' => 'string', 'description' => 'The SMTP password.'],
|
|
|
|
'smtp_timeout' => ['type' => 'string', 'description' => 'The SMTP timeout.'],
|
|
|
|
'smtp_notifications_test' => ['type' => 'boolean', 'description' => 'Whether to send test notifications via SMTP.'],
|
|
|
|
'smtp_notifications_deployments' => ['type' => 'boolean', 'description' => 'Whether to send deployment notifications via SMTP.'],
|
|
|
|
'smtp_notifications_status_changes' => ['type' => 'boolean', 'description' => 'Whether to send status change notifications via SMTP.'],
|
|
|
|
'smtp_notifications_scheduled_tasks' => ['type' => 'boolean', 'description' => 'Whether to send scheduled task notifications via SMTP.'],
|
|
|
|
'smtp_notifications_database_backups' => ['type' => 'boolean', 'description' => 'Whether to send database backup notifications via SMTP.'],
|
|
|
|
'discord_enabled' => ['type' => 'boolean', 'description' => 'Whether Discord is enabled or not.'],
|
|
|
|
'discord_webhook_url' => ['type' => 'string', 'description' => 'The Discord webhook URL.'],
|
|
|
|
'discord_notifications_test' => ['type' => 'boolean', 'description' => 'Whether to send test notifications via Discord.'],
|
|
|
|
'discord_notifications_deployments' => ['type' => 'boolean', 'description' => 'Whether to send deployment notifications via Discord.'],
|
|
|
|
'discord_notifications_status_changes' => ['type' => 'boolean', 'description' => 'Whether to send status change notifications via Discord.'],
|
|
|
|
'discord_notifications_database_backups' => ['type' => 'boolean', 'description' => 'Whether to send database backup notifications via Discord.'],
|
|
|
|
'discord_notifications_scheduled_tasks' => ['type' => 'boolean', 'description' => 'Whether to send scheduled task notifications via Discord.'],
|
|
|
|
'show_boarding' => ['type' => 'boolean', 'description' => 'Whether to show the boarding screen or not.'],
|
|
|
|
'resend_enabled' => ['type' => 'boolean', 'description' => 'Whether to enable resending or not.'],
|
|
|
|
'resend_api_key' => ['type' => 'string', 'description' => 'The resending API key.'],
|
|
|
|
'use_instance_email_settings' => ['type' => 'boolean', 'description' => 'Whether to use instance email settings or not.'],
|
|
|
|
'telegram_enabled' => ['type' => 'boolean', 'description' => 'Whether Telegram is enabled or not.'],
|
|
|
|
'telegram_token' => ['type' => 'string', 'description' => 'The Telegram token.'],
|
|
|
|
'telegram_chat_id' => ['type' => 'string', 'description' => 'The Telegram chat ID.'],
|
|
|
|
'telegram_notifications_test' => ['type' => 'boolean', 'description' => 'Whether to send test notifications via Telegram.'],
|
|
|
|
'telegram_notifications_deployments' => ['type' => 'boolean', 'description' => 'Whether to send deployment notifications via Telegram.'],
|
|
|
|
'telegram_notifications_status_changes' => ['type' => 'boolean', 'description' => 'Whether to send status change notifications via Telegram.'],
|
|
|
|
'telegram_notifications_database_backups' => ['type' => 'boolean', 'description' => 'Whether to send database backup notifications via Telegram.'],
|
|
|
|
'telegram_notifications_test_message_thread_id' => ['type' => 'string', 'description' => 'The Telegram test message thread ID.'],
|
|
|
|
'telegram_notifications_deployments_message_thread_id' => ['type' => 'string', 'description' => 'The Telegram deployment message thread ID.'],
|
|
|
|
'telegram_notifications_status_changes_message_thread_id' => ['type' => 'string', 'description' => 'The Telegram status change message thread ID.'],
|
|
|
|
'telegram_notifications_database_backups_message_thread_id' => ['type' => 'string', 'description' => 'The Telegram database backup message thread ID.'],
|
|
|
|
'custom_server_limit' => ['type' => 'string', 'description' => 'The custom server limit.'],
|
|
|
|
'telegram_notifications_scheduled_tasks' => ['type' => 'boolean', 'description' => 'Whether to send scheduled task notifications via Telegram.'],
|
|
|
|
'telegram_notifications_scheduled_tasks_thread_id' => ['type' => 'string', 'description' => 'The Telegram scheduled task message thread ID.'],
|
|
|
|
]
|
|
|
|
)]
|
2023-06-15 07:15:41 +00:00
|
|
|
class Team extends Model implements SendsDiscord, SendsEmail
|
2023-03-24 13:54:17 +00:00
|
|
|
{
|
2023-07-27 19:26:15 +00:00
|
|
|
use Notifiable;
|
2023-05-25 16:27:52 +00:00
|
|
|
|
2023-07-27 19:26:15 +00:00
|
|
|
protected $guarded = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-03-24 13:54:17 +00:00
|
|
|
protected $casts = [
|
|
|
|
'personal_team' => 'boolean',
|
2023-08-31 13:00:59 +00:00
|
|
|
'smtp_password' => 'encrypted',
|
|
|
|
'resend_api_key' => 'encrypted',
|
2023-03-24 13:54:17 +00:00
|
|
|
];
|
2023-05-25 16:27:52 +00:00
|
|
|
|
2023-09-15 09:28:44 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
2024-04-05 14:48:06 +00:00
|
|
|
static::saving(function ($team) {
|
2024-04-05 16:47:07 +00:00
|
|
|
if (auth()->user()?->isMember()) {
|
2024-04-05 14:48:06 +00:00
|
|
|
throw new \Exception('You are not allowed to update this team.');
|
|
|
|
}
|
|
|
|
});
|
2024-05-21 12:29:06 +00:00
|
|
|
|
|
|
|
static::deleting(function ($team) {
|
|
|
|
$keys = $team->privateKeys;
|
|
|
|
foreach ($keys as $key) {
|
2024-06-10 20:43:34 +00:00
|
|
|
ray('Deleting key: '.$key->name);
|
2024-05-21 12:29:06 +00:00
|
|
|
$key->delete();
|
|
|
|
}
|
|
|
|
$sources = $team->sources();
|
|
|
|
foreach ($sources as $source) {
|
2024-06-10 20:43:34 +00:00
|
|
|
ray('Deleting source: '.$source->name);
|
2024-05-21 12:29:06 +00:00
|
|
|
$source->delete();
|
|
|
|
}
|
|
|
|
$tags = Tag::whereTeamId($team->id)->get();
|
|
|
|
foreach ($tags as $tag) {
|
2024-06-10 20:43:34 +00:00
|
|
|
ray('Deleting tag: '.$tag->name);
|
2024-05-21 12:29:06 +00:00
|
|
|
$tag->delete();
|
|
|
|
}
|
|
|
|
$shared_variables = $team->environment_variables();
|
|
|
|
foreach ($shared_variables as $shared_variable) {
|
2024-06-10 20:43:34 +00:00
|
|
|
ray('Deleting team shared variable: '.$shared_variable->name);
|
2024-05-21 12:29:06 +00:00
|
|
|
$shared_variable->delete();
|
|
|
|
}
|
|
|
|
$s3s = $team->s3s;
|
|
|
|
foreach ($s3s as $s3) {
|
2024-06-10 20:43:34 +00:00
|
|
|
ray('Deleting s3: '.$s3->name);
|
2024-05-21 12:29:06 +00:00
|
|
|
$s3->delete();
|
|
|
|
}
|
|
|
|
});
|
2023-09-15 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 16:27:52 +00:00
|
|
|
public function routeNotificationForDiscord()
|
|
|
|
{
|
2023-07-27 19:26:15 +00:00
|
|
|
return data_get($this, 'discord_webhook_url', null);
|
2023-05-25 16:27:52 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-06 12:31:38 +00:00
|
|
|
public function routeNotificationForTelegram()
|
|
|
|
{
|
|
|
|
return [
|
2024-06-10 20:43:34 +00:00
|
|
|
'token' => data_get($this, 'telegram_token', null),
|
|
|
|
'chat_id' => data_get($this, 'telegram_chat_id', null),
|
2023-09-06 12:31:38 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-07-28 08:55:26 +00:00
|
|
|
public function getRecepients($notification)
|
2023-05-25 16:27:52 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
$recipients = data_get($notification, 'emails', null);
|
2023-07-28 08:55:26 +00:00
|
|
|
if (is_null($recipients)) {
|
|
|
|
$recipients = $this->members()->pluck('email')->toArray();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-07-28 08:55:26 +00:00
|
|
|
return $recipients;
|
2023-06-01 11:24:20 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-01 11:24:20 +00:00
|
|
|
return explode(',', $recipients);
|
2023-05-25 16:27:52 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public static function serverLimitReached()
|
2024-03-07 11:58:04 +00:00
|
|
|
{
|
2024-02-23 14:45:53 +00:00
|
|
|
$serverLimit = Team::serverLimit();
|
|
|
|
$team = currentTeam();
|
|
|
|
$servers = $team->servers->count();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-23 14:45:53 +00:00
|
|
|
return $servers >= $serverLimit;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-07 11:58:04 +00:00
|
|
|
public function serverOverflow()
|
|
|
|
{
|
2024-02-25 21:08:44 +00:00
|
|
|
if ($this->serverLimit() < $this->servers->count()) {
|
|
|
|
return true;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-25 21:08:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public static function serverLimit()
|
2024-02-23 14:45:53 +00:00
|
|
|
{
|
2024-03-01 10:41:28 +00:00
|
|
|
if (currentTeam()->id === 0 && isDev()) {
|
|
|
|
return 9999999;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-25 17:22:24 +00:00
|
|
|
return Team::find(currentTeam()->id)->limits['serverLimit'];
|
2024-02-23 14:45:53 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-31 13:00:59 +00:00
|
|
|
public function limits(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: function () {
|
|
|
|
if (config('coolify.self_hosted') || $this->id === 0) {
|
|
|
|
$subscription = 'self-hosted';
|
|
|
|
} else {
|
|
|
|
$subscription = data_get($this, 'subscription');
|
|
|
|
if (is_null($subscription)) {
|
|
|
|
$subscription = 'zero';
|
|
|
|
} else {
|
|
|
|
$subscription = $subscription->type();
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 14:45:53 +00:00
|
|
|
if ($this->custom_server_limit) {
|
|
|
|
$serverLimit = $this->custom_server_limit;
|
|
|
|
} else {
|
|
|
|
$serverLimit = config('constants.limits.server')[strtolower($subscription)];
|
|
|
|
}
|
2023-08-31 13:00:59 +00:00
|
|
|
$sharedEmailEnabled = config('constants.limits.email')[strtolower($subscription)];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-31 13:00:59 +00:00
|
|
|
return ['serverLimit' => $serverLimit, 'sharedEmailEnabled' => $sharedEmailEnabled];
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-23 14:45:53 +00:00
|
|
|
public function environment_variables()
|
|
|
|
{
|
2024-01-23 16:13:23 +00:00
|
|
|
return $this->hasMany(SharedEnvironmentVariable::class)->whereNull('project_id')->whereNull('environment_id');
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function members()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id')->withPivot('role');
|
|
|
|
}
|
|
|
|
|
2023-07-13 13:07:42 +00:00
|
|
|
public function subscription()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Subscription::class);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
public function applications()
|
2023-04-26 13:38:50 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
return $this->hasManyThrough(Application::class, Project::class);
|
2023-03-27 08:44:31 +00:00
|
|
|
}
|
2023-05-25 16:27:52 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function invitations()
|
2023-04-26 13:38:50 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
return $this->hasMany(TeamInvitation::class);
|
2023-03-27 12:31:42 +00:00
|
|
|
}
|
2023-05-25 16:27:52 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function isEmpty()
|
2023-04-26 13:38:50 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
if ($this->projects()->count() === 0 && $this->servers()->count() === 0 && $this->privateKeys()->count() === 0 && $this->sources()->count() === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
return false;
|
2023-03-30 17:50:27 +00:00
|
|
|
}
|
2023-05-25 16:27:52 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function projects()
|
2023-05-03 10:38:57 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
return $this->hasMany(Project::class);
|
2023-05-03 10:38:57 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
public function servers()
|
2023-06-02 10:34:45 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
return $this->hasMany(Server::class);
|
2023-06-09 13:55:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
public function privateKeys()
|
2023-06-09 13:55:21 +00:00
|
|
|
{
|
2023-08-08 09:51:36 +00:00
|
|
|
return $this->hasMany(PrivateKey::class);
|
2023-06-02 10:34:45 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-12 20:02:10 +00:00
|
|
|
public function sources()
|
|
|
|
{
|
|
|
|
$sources = collect([]);
|
|
|
|
$github_apps = $this->hasMany(GithubApp::class)->whereisPublic(false)->get();
|
|
|
|
$gitlab_apps = $this->hasMany(GitlabApp::class)->whereisPublic(false)->get();
|
|
|
|
$sources = $sources->merge($github_apps)->merge($gitlab_apps);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-12 20:02:10 +00:00
|
|
|
return $sources;
|
|
|
|
}
|
2023-08-09 12:44:36 +00:00
|
|
|
|
2023-08-11 14:13:53 +00:00
|
|
|
public function s3s()
|
2023-08-09 12:44:36 +00:00
|
|
|
{
|
2023-10-10 11:10:43 +00:00
|
|
|
return $this->hasMany(S3Storage::class)->where('is_usable', true);
|
2023-08-09 12:44:36 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-23 14:45:53 +00:00
|
|
|
public function trialEnded()
|
|
|
|
{
|
2023-09-12 09:19:21 +00:00
|
|
|
foreach ($this->servers as $server) {
|
|
|
|
$server->settings()->update([
|
|
|
|
'is_usable' => false,
|
|
|
|
'is_reachable' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-23 14:45:53 +00:00
|
|
|
public function trialEndedButSubscribed()
|
|
|
|
{
|
2023-09-12 09:19:21 +00:00
|
|
|
foreach ($this->servers as $server) {
|
|
|
|
$server->settings()->update([
|
|
|
|
'is_usable' => true,
|
|
|
|
'is_reachable' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-07 11:58:04 +00:00
|
|
|
public function isAnyNotificationEnabled()
|
|
|
|
{
|
|
|
|
if (isCloud()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($this->smtp_enabled || $this->resend_enabled || $this->discord_enabled || $this->telegram_enabled || $this->use_instance_email_settings) {
|
|
|
|
return true;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-07 11:58:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|