coolify/app/Models/Team.php

188 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2023-05-25 18:27:52 +02:00
use App\Notifications\Channels\SendsDiscord;
use App\Notifications\Channels\SendsEmail;
2023-08-31 15:00:59 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2023-06-15 09:15:41 +02:00
use Illuminate\Database\Eloquent\Model;
2023-05-25 18:27:52 +02:00
use Illuminate\Notifications\Notifiable;
2023-06-15 09:15:41 +02:00
class Team extends Model implements SendsDiscord, SendsEmail
{
use Notifiable;
2023-05-25 18:27:52 +02:00
protected $guarded = [];
protected $casts = [
'personal_team' => 'boolean',
2023-08-31 15:00:59 +02:00
'smtp_password' => 'encrypted',
'resend_api_key' => 'encrypted',
];
2023-05-25 18:27:52 +02:00
2023-09-15 11:28:44 +02:00
protected static function booted()
{
static::saving(function ($team) {
2024-04-05 18:47:07 +02:00
if (auth()->user()?->isMember()) {
throw new \Exception('You are not allowed to update this team.');
}
});
2023-09-15 11:28:44 +02:00
}
2023-05-25 18:27:52 +02:00
public function routeNotificationForDiscord()
{
return data_get($this, 'discord_webhook_url', null);
2023-05-25 18:27:52 +02:00
}
2023-09-06 14:31:38 +02:00
public function routeNotificationForTelegram()
{
return [
"token" => data_get($this, 'telegram_token', null),
2023-09-08 14:15:28 +02:00
"chat_id" => data_get($this, 'telegram_chat_id', null),
2023-09-06 14:31:38 +02:00
];
}
2023-07-28 10:55:26 +02:00
public function getRecepients($notification)
2023-05-25 18:27:52 +02:00
{
$recipients = data_get($notification, 'emails', null);
2023-07-28 10:55:26 +02:00
if (is_null($recipients)) {
$recipients = $this->members()->pluck('email')->toArray();
return $recipients;
2023-06-01 13:24:20 +02:00
}
return explode(',', $recipients);
2023-05-25 18:27:52 +02:00
}
static public function serverLimitReached()
{
2024-02-23 15:45:53 +01:00
$serverLimit = Team::serverLimit();
$team = currentTeam();
$servers = $team->servers->count();
return $servers >= $serverLimit;
}
public function serverOverflow()
{
if ($this->serverLimit() < $this->servers->count()) {
return true;
}
return false;
}
2024-02-23 15:45:53 +01:00
static public function serverLimit()
{
if (currentTeam()->id === 0 && isDev()) {
return 9999999;
}
return Team::find(currentTeam()->id)->limits['serverLimit'];
2024-02-23 15:45:53 +01:00
}
2023-08-31 15:00:59 +02: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 15:45:53 +01:00
if ($this->custom_server_limit) {
$serverLimit = $this->custom_server_limit;
} else {
$serverLimit = config('constants.limits.server')[strtolower($subscription)];
}
2023-08-31 15:00:59 +02:00
$sharedEmailEnabled = config('constants.limits.email')[strtolower($subscription)];
return ['serverLimit' => $serverLimit, 'sharedEmailEnabled' => $sharedEmailEnabled];
}
);
}
2024-02-23 15:45:53 +01:00
public function environment_variables()
{
2024-01-23 17:13:23 +01:00
return $this->hasMany(SharedEnvironmentVariable::class)->whereNull('project_id')->whereNull('environment_id');
}
public function members()
{
return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id')->withPivot('role');
}
2023-07-13 15:07:42 +02:00
public function subscription()
{
return $this->hasOne(Subscription::class);
}
public function applications()
{
return $this->hasManyThrough(Application::class, Project::class);
}
2023-05-25 18:27:52 +02:00
public function invitations()
{
return $this->hasMany(TeamInvitation::class);
2023-03-27 14:31:42 +02:00
}
2023-05-25 18:27:52 +02:00
public function isEmpty()
{
if ($this->projects()->count() === 0 && $this->servers()->count() === 0 && $this->privateKeys()->count() === 0 && $this->sources()->count() === 0) {
return true;
}
return false;
2023-03-30 19:50:27 +02:00
}
2023-05-25 18:27:52 +02:00
public function projects()
2023-05-03 12:38:57 +02:00
{
return $this->hasMany(Project::class);
2023-05-03 12:38:57 +02:00
}
public function servers()
2023-06-02 12:34:45 +02:00
{
return $this->hasMany(Server::class);
2023-06-09 15:55:21 +02:00
}
public function privateKeys()
2023-06-09 15:55:21 +02:00
{
return $this->hasMany(PrivateKey::class);
2023-06-02 12:34:45 +02:00
}
2023-06-12 22:02:10 +02: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);
return $sources;
}
2023-08-11 16:13:53 +02:00
public function s3s()
{
2023-10-10 13:10:43 +02:00
return $this->hasMany(S3Storage::class)->where('is_usable', true);
}
2024-02-23 15:45:53 +01:00
public function trialEnded()
{
2023-09-12 11:19:21 +02:00
foreach ($this->servers as $server) {
$server->settings()->update([
'is_usable' => false,
'is_reachable' => false,
]);
}
}
2024-02-23 15:45:53 +01:00
public function trialEndedButSubscribed()
{
2023-09-12 11:19:21 +02:00
foreach ($this->servers as $server) {
$server->settings()->update([
'is_usable' => true,
'is_reachable' => true,
]);
}
}
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;
}
return false;
}
}