testing php storm code cleanup and styling

This commit is contained in:
Andras Bacsai
2023-08-08 11:51:36 +02:00
parent a8ee779b31
commit f2228cec7b
368 changed files with 23834 additions and 2623 deletions

View File

@@ -2,28 +2,12 @@
namespace App\Models;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Masmerise\Toaster\Toastable;
use Masmerise\Toaster\Toaster;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
use Spatie\Activitylog\Models\Activity;
class Application extends BaseModel
{
protected static function booted()
{
static::created(function ($application) {
ApplicationSetting::create([
'application_id' => $application->id,
]);
});
static::deleting(function ($application) {
$application->settings()->delete();
$application->persistentStorages()->delete();
});
}
protected $fillable = [
'name',
'repository_project_id',
@@ -43,15 +27,42 @@ class Application extends BaseModel
'publish_directory',
'private_key_id'
];
public function type() {
protected static function booted()
{
static::created(function ($application) {
ApplicationSetting::create([
'application_id' => $application->id,
]);
});
static::deleting(function ($application) {
$application->settings()->delete();
$application->persistentStorages()->delete();
});
}
public function settings()
{
return $this->hasOne(ApplicationSetting::class);
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function type()
{
return 'application';
}
public function publishDirectory(): Attribute
{
return Attribute::make(
set: fn ($value) => $value ? '/' . ltrim($value, '/') : null,
set: fn($value) => $value ? '/' . ltrim($value, '/') : null,
);
}
public function gitBranchLocation(): Attribute
{
return Attribute::make(
@@ -63,6 +74,7 @@ class Application extends BaseModel
);
}
public function gitCommits(): Attribute
{
return Attribute::make(
@@ -73,99 +85,108 @@ class Application extends BaseModel
}
);
}
public function baseDirectory(): Attribute
{
return Attribute::make(
set: fn ($value) => '/' . ltrim($value, '/'),
set: fn($value) => '/' . ltrim($value, '/'),
);
}
public function portsMappings(): Attribute
{
return Attribute::make(
set: fn ($value) => $value === "" ? null : $value,
set: fn($value) => $value === "" ? null : $value,
);
}
// Normal Deployments
public function portsMappingsArray(): Attribute
{
return Attribute::make(
get: fn () =>
is_null($this->ports_mappings)
get: fn() => is_null($this->ports_mappings)
? []
: explode(',', $this->ports_mappings),
);
}
public function portsExposesArray(): Attribute
{
return Attribute::make(
get: fn () =>
is_null($this->ports_exposes)
get: fn() => is_null($this->ports_exposes)
? []
: explode(',', $this->ports_exposes)
);
}
// Normal Deployments
public function environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false);
}
public function runtime_environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->where('key', 'not like', 'NIXPACKS_%');
}
// Preview Deployments
public function build_environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->where('is_build_time', true)->where('key', 'not like', 'NIXPACKS_%');
}
public function nixpacks_environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->where('key', 'like', 'NIXPACKS_%');
}
// Preview Deployments
public function environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true);
}
public function runtime_environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('key', 'not like', 'NIXPACKS_%');
}
public function build_environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('is_build_time', true)->where('key', 'not like', 'NIXPACKS_%');
}
public function nixpacks_environment_variables_preview(): HasMany
{
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('key', 'like', 'NIXPACKS_%');
}
public function private_key()
{
return $this->belongsTo(PrivateKey::class);
}
public function environment()
{
return $this->belongsTo(Environment::class);
}
public function previews()
{
return $this->hasMany(ApplicationPreview::class);
}
public function settings()
{
return $this->hasOne(ApplicationSetting::class);
}
public function destination()
{
return $this->morphTo();
}
public function source()
{
return $this->morphTo();
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function deployments(int $skip = 0, int $take = 10)
{
@@ -177,10 +198,12 @@ class Application extends BaseModel
'deployments' => $deployments
];
}
public function get_deployment(string $deployment_uuid)
{
return Activity::where('subject_id', $this->id)->where('properties->type_uuid', '=', $deployment_uuid)->first();
}
public function isDeployable(): bool
{
if ($this->settings->is_auto_deploy_enabled) {
@@ -188,6 +211,7 @@ class Application extends BaseModel
}
return false;
}
public function isPRDeployable(): bool
{
if ($this->settings->is_preview_deployments_enabled) {
@@ -195,6 +219,7 @@ class Application extends BaseModel
}
return false;
}
public function deploymentType()
{
if (data_get($this, 'private_key_id')) {

View File

@@ -13,12 +13,14 @@ class ApplicationPreview extends BaseModel
'status',
'application_id',
];
public function application()
{
return $this->belongsTo(Application::class);
}
static function findPreviewByApplicationAndPullId(int $application_id, int $pull_request_id)
{
return self::where('application_id', $application_id)->where('pull_request_id', $pull_request_id)->firstOrFail();
}
public function application()
{
return $this->belongsTo(Application::class);
}
}

View File

@@ -26,6 +26,7 @@ class ApplicationSetting extends Model
'is_git_submodules_enabled',
'is_git_lfs_enabled',
];
public function isStatic(): Attribute
{
return Attribute::make(
@@ -42,6 +43,7 @@ class ApplicationSetting extends Model
}
);
}
public function application()
{
return $this->belongsTo(Application::class);

View File

@@ -14,7 +14,7 @@ abstract class BaseModel extends Model
static::creating(function (Model $model) {
// Generate a UUID if one isn't set
if (!$model->uuid) {
$model->uuid = (string) new Cuid2(7);
$model->uuid = (string)new Cuid2(7);
}
});
}

View File

@@ -8,6 +8,7 @@ class Database extends BaseModel
{
return $this->belongsTo(Environment::class);
}
public function destination()
{
return $this->morphTo();

View File

@@ -11,32 +11,41 @@ class Environment extends Model
'name',
'project_id',
];
protected function name(): Attribute
public function can_delete_environment()
{
return Attribute::make(
set: fn (string $value) => strtolower($value),
);
}
public function can_delete_environment() {
return $this->applications()->count() == 0 && $this->postgresqls()->count() == 0;
}
public function databases() {
return $this->postgresqls();
}
public function project()
{
return $this->belongsTo(Project::class);
}
public function applications()
{
return $this->hasMany(Application::class);
}
public function postgresqls()
{
return $this->hasMany(StandalonePostgresql::class);
}
public function databases()
{
return $this->postgresqls();
}
public function project()
{
return $this->belongsTo(Project::class);
}
public function services()
{
return $this->hasMany(Service::class);
}
protected function name(): Attribute
{
return Attribute::make(
set: fn(string $value) => strtolower($value),
);
}
}

View File

@@ -15,21 +15,30 @@ class EnvironmentVariable extends Model
'value' => 'encrypted',
'is_build_time' => 'boolean',
];
protected static function booted()
{
static::created(function ($environment_variable) {
if ($environment_variable->application_id && !$environment_variable->is_preview) {
ModelsEnvironmentVariable::create([
'key' => $environment_variable->key,
'value' => $environment_variable->value,
'is_build_time' => $environment_variable->is_build_time,
'application_id' => $environment_variable->application_id,
'is_preview' => true,
]);
}
if ($environment_variable->application_id && !$environment_variable->is_preview) {
ModelsEnvironmentVariable::create([
'key' => $environment_variable->key,
'value' => $environment_variable->value,
'is_build_time' => $environment_variable->is_build_time,
'application_id' => $environment_variable->application_id,
'is_preview' => true,
]);
}
});
}
protected function value(): Attribute
{
return Attribute::make(
get: fn(string $value) => $this->get_environment_variables($value),
set: fn(string $value) => $this->set_environment_variables($value),
);
}
private function get_environment_variables(string $environment_variable): string|null
{
// $team_id = session('currentTeam')->id;
@@ -45,6 +54,7 @@ class EnvironmentVariable extends Model
}
return decrypt($environment_variable);
}
private function set_environment_variables(string $environment_variable): string|null
{
$environment_variable = trim($environment_variable);
@@ -53,17 +63,11 @@ class EnvironmentVariable extends Model
}
return $environment_variable;
}
protected function value(): Attribute
{
return Attribute::make(
get: fn (string $value) => $this->get_environment_variables($value),
set: fn (string $value) => $this->set_environment_variables($value),
);
}
protected function key(): Attribute
{
return Attribute::make(
set: fn (string $value) => Str::of($value)->trim(),
set: fn(string $value) => Str::of($value)->trim(),
);
}
}

View File

@@ -16,6 +16,17 @@ class GithubApp extends BaseModel
'client_secret',
'webhook_secret',
];
static public function public()
{
return GithubApp::whereTeamId(session('currentTeam')->id)->whereisPublic(true)->whereNotNull('app_id')->get();
}
static public function private()
{
return GithubApp::whereTeamId(session('currentTeam')->id)->whereisPublic(false)->whereNotNull('app_id')->get();
}
protected static function booted(): void
{
static::deleting(function (GithubApp $github_app) {
@@ -25,14 +36,17 @@ class GithubApp extends BaseModel
}
});
}
public function applications()
{
return $this->morphMany(Application::class, 'source');
}
public function privateKey()
{
return $this->belongsTo(PrivateKey::class);
}
public function type(): Attribute
{
return Attribute::make(
@@ -43,12 +57,4 @@ class GithubApp extends BaseModel
},
);
}
static public function public()
{
return GithubApp::whereTeamId(session('currentTeam')->id)->whereisPublic(true)->whereNotNull('app_id')->get();
}
static public function private()
{
return GithubApp::whereTeamId(session('currentTeam')->id)->whereisPublic(false)->whereNotNull('app_id')->get();
}
}

View File

@@ -8,10 +8,12 @@ class GitlabApp extends BaseModel
'webhook_token',
'app_secret',
];
public function applications()
{
return $this->morphMany(Application::class, 'source');
}
public function privateKey()
{
return $this->belongsTo(PrivateKey::class);

View File

@@ -3,28 +3,29 @@
namespace App\Models;
use App\Notifications\Channels\SendsEmail;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
class InstanceSettings extends Model implements SendsEmail
{
use Notifiable;
protected $guarded = [];
protected $casts = [
'resale_license' => 'encrypted',
];
public static function get()
{
return InstanceSettings::findOrFail(0);
}
public function getRecepients($notification)
{
$recipients = data_get($notification,'emails',null);
$recipients = data_get($notification, 'emails', null);
if (is_null($recipients) || $recipients === '') {
return [];
}
return explode(',', $recipients);
}
public static function get()
{
return InstanceSettings::findOrFail(0);
}
}
}

View File

@@ -9,26 +9,31 @@ use Illuminate\Support\Str;
class LocalPersistentVolume extends Model
{
protected $guarded = [];
public function application()
{
return $this->morphTo();
}
public function standalone_postgresql()
{
return $this->morphTo();
}
protected function name(): Attribute
{
return Attribute::make(
set: fn (string $value) => Str::of($value)->trim()->value,
set: fn(string $value) => Str::of($value)->trim()->value,
);
}
protected function mountPath(): Attribute
{
return Attribute::make(
set: fn (string $value) => Str::of($value)->trim()->start('/')->value
set: fn(string $value) => Str::of($value)->trim()->start('/')->value
);
}
protected function hostPath(): Attribute
{
return Attribute::make(

View File

@@ -12,27 +12,13 @@ class PrivateKey extends BaseModel
'is_git_related',
'team_id',
];
static public function ownedByCurrentTeam(array $select = ['*'])
{
$selectArray = collect($select)->concat(['id']);
return PrivateKey::whereTeamId(session('currentTeam')->id)->select($selectArray->all());
}
public function applications()
{
return $this->hasMany(Application::class);
}
public function githubApps()
{
return $this->hasMany(GithubApp::class);
}
public function gitlabApps()
{
return $this->hasMany(GitlabApp::class);
}
public function servers()
{
return $this->hasMany(Server::class);
}
public function isEmpty()
{
if ($this->servers()->count() === 0 && $this->applications()->count() === 0 && $this->githubApps()->count() === 0 && $this->gitlabApps()->count() === 0) {
@@ -40,4 +26,24 @@ class PrivateKey extends BaseModel
}
return false;
}
public function servers()
{
return $this->hasMany(Server::class);
}
public function applications()
{
return $this->hasMany(Application::class);
}
public function githubApps()
{
return $this->hasMany(GithubApp::class);
}
public function gitlabApps()
{
return $this->hasMany(GitlabApp::class);
}
}

View File

@@ -4,6 +4,18 @@ namespace App\Models;
class Project extends BaseModel
{
protected $fillable = [
'name',
'description',
'team_id',
'project_id'
];
static public function ownedByCurrentTeam()
{
return Project::whereTeamId(session('currentTeam')->id)->orderBy('name');
}
protected static function booted()
{
static::created(function ($project) {
@@ -20,32 +32,27 @@ class Project extends BaseModel
$project->settings()->delete();
});
}
protected $fillable = [
'name',
'description',
'team_id',
'project_id'
];
static public function ownedByCurrentTeam()
{
return Project::whereTeamId(session('currentTeam')->id)->orderBy('name');
}
public function team()
{
return $this->belongsTo(Team::class);
}
public function environments()
{
return $this->hasMany(Environment::class);
}
public function settings()
{
return $this->hasOne(ProjectSetting::class);
}
public function team()
{
return $this->belongsTo(Team::class);
}
public function applications()
{
return $this->hasManyThrough(Application::class, Environment::class);
}
public function postgresqls()
{
return $this->hasManyThrough(StandalonePostgresql::class, Environment::class);

View File

@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class S3Storage extends BaseModel
{
use HasFactory;
protected $guarded = [];
protected $casts = [
'key' => 'encrypted',
@@ -18,12 +19,16 @@ class S3Storage extends BaseModel
$selectArray = collect($select)->concat(['id']);
return S3Storage::whereTeamId(session('currentTeam')->id)->select($selectArray->all())->orderBy('name');
}
public function awsUrl() {
public function awsUrl()
{
return "{$this->endpoint}/{$this->bucket}";
}
public function testConnection() {
public function testConnection()
{
set_s3_target($this);
return \Storage::disk('custom-s3')->files();
}
}
}

View File

@@ -9,12 +9,47 @@ use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
class Server extends BaseModel
{
use SchemalessAttributesTrait;
protected $schemalessAttributes = [
'proxy',
];
public $casts = [
'proxy' => SchemalessAttributes::class,
];
protected $schemalessAttributes = [
'proxy',
];
protected $fillable = [
'name',
'ip',
'user',
'port',
'team_id',
'private_key_id',
'proxy',
];
static public function isReachable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
}
static public function ownedByCurrentTeam(array $select = ['*'])
{
$selectArray = collect($select)->concat(['id']);
return Server::whereTeamId(session('currentTeam')->id)->with('settings')->select($selectArray->all())->orderBy('name');
}
static public function isUsable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true);
}
static public function destinationsByServer(string $server_id)
{
$server = Server::ownedByCurrentTeam()->get()->where('id', $server_id)->firstOrFail();
$standaloneDocker = collect($server->standaloneDockers->all());
$swarmDocker = collect($server->swarmDockers->all());
return $standaloneDocker->concat($swarmDocker);
}
protected static function booted()
{
static::created(function ($server) {
@@ -26,22 +61,17 @@ class Server extends BaseModel
$server->settings()->delete();
});
}
protected $fillable = [
'name',
'ip',
'user',
'port',
'team_id',
'private_key_id',
'proxy',
];
public function settings()
{
return $this->hasOne(ServerSetting::class);
}
public function scopeWithProxy(): Builder
{
return $this->proxy->modelScope();
}
public function isEmpty()
{
if ($this->applications()->count() === 0) {
@@ -49,18 +79,21 @@ class Server extends BaseModel
}
return false;
}
public function applications()
{
return $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications;
})->flatten();
}
public function destinations()
{
$standalone_docker = $this->hasMany(StandaloneDocker::class)->get();
$swarm_docker = $this->hasMany(SwarmDocker::class)->get();
return $standalone_docker->concat($swarm_docker);
}
public function standaloneDockers()
{
return $this->hasMany(StandaloneDocker::class);
@@ -76,38 +109,13 @@ class Server extends BaseModel
return $this->belongsTo(PrivateKey::class);
}
public function settings()
{
return $this->hasOne(ServerSetting::class);
}
public function muxFilename()
{
return "{$this->ip}_{$this->port}_{$this->user}";
}
public function team()
{
return $this->belongsTo(Team::class);
}
static public function ownedByCurrentTeam(array $select = ['*'])
{
$selectArray = collect($select)->concat(['id']);
return Server::whereTeamId(session('currentTeam')->id)->with('settings')->select($selectArray->all())->orderBy('name');
}
static public function isReachable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);
}
static public function isUsable()
{
return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true);
}
static public function destinationsByServer(string $server_id)
{
$server = Server::ownedByCurrentTeam()->get()->where('id', $server_id)->firstOrFail();
$standaloneDocker = collect($server->standaloneDockers->all());
$swarmDocker = collect($server->swarmDockers->all());
return $standaloneDocker->concat($swarmDocker);
}
}

View File

@@ -10,6 +10,7 @@ class ServerSetting extends Model
'server_id',
'is_usable',
];
public function server()
{
return $this->belongsTo(Server::class);

View File

@@ -9,6 +9,7 @@ class Service extends BaseModel
{
return $this->belongsTo(Environment::class);
}
public function destination()
{
return $this->morphTo();

View File

@@ -9,18 +9,22 @@ class StandaloneDocker extends BaseModel
'network',
'server_id',
];
public function applications()
{
return $this->morphMany(Application::class, 'destination');
}
public function postgresqls()
{
return $this->morphMany(StandalonePostgresql::class, 'destination');
}
public function server()
{
return $this->belongsTo(Server::class);
}
public function attachedTo()
{
return $this->applications->count() > 0 || $this->databases->count() > 0;

View File

@@ -4,12 +4,16 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Models\EnvironmentVariable;
use App\Models\LocalPersistentVolume;
class StandalonePostgresql extends BaseModel
{
use HasFactory;
protected $guarded = [];
protected $casts = [
'postgres_password' => 'encrypted',
];
protected static function booted()
{
static::created(function ($database) {
@@ -23,29 +27,32 @@ class StandalonePostgresql extends BaseModel
]);
});
}
protected $guarded = [];
protected $casts = [
'postgres_password' => 'encrypted',
];
public function type() {
public function type()
{
return 'standalone-postgresql';
}
public function environment()
{
return $this->belongsTo(Environment::class);
}
public function destination()
{
return $this->morphTo();
}
public function environment_variables(): HasMany
public function environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class);
}
public function runtime_environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class);
}
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');

View File

@@ -2,12 +2,12 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Subscription extends Model
{
protected $guarded = [];
public function team()
{
return $this->belongsTo(Team::class);

View File

@@ -8,6 +8,7 @@ class SwarmDocker extends BaseModel
{
return $this->morphMany(Application::class, 'destination');
}
public function server()
{
return $this->belongsTo(Server::class);

View File

@@ -2,13 +2,10 @@
namespace App\Models;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\Channels\SendsDiscord;
use Illuminate\Database\Eloquent\Builder;
use App\Notifications\Channels\SendsEmail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
class Team extends Model implements SendsDiscord, SendsEmail
{
@@ -23,9 +20,10 @@ class Team extends Model implements SendsDiscord, SendsEmail
{
return data_get($this, 'discord_webhook_url', null);
}
public function getRecepients($notification)
{
$recipients = data_get($notification,'emails',null);
$recipients = data_get($notification, 'emails', null);
if (is_null($recipients)) {
$recipients = $this->members()->pluck('email')->toArray();
return $recipients;
@@ -33,10 +31,34 @@ class Team extends Model implements SendsDiscord, SendsEmail
return explode(',', $recipients);
}
public function members()
{
return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id')->withPivot('role');
}
public function subscription()
{
return $this->hasOne(Subscription::class);
}
public function applications()
{
return $this->hasManyThrough(Application::class, Project::class);
}
public function invitations()
{
return $this->hasMany(TeamInvitation::class);
}
public function isEmpty()
{
if ($this->projects()->count() === 0 && $this->servers()->count() === 0 && $this->privateKeys()->count() === 0 && $this->sources()->count() === 0) {
return true;
}
return false;
}
public function projects()
{
return $this->hasMany(Project::class);
@@ -47,23 +69,11 @@ class Team extends Model implements SendsDiscord, SendsEmail
return $this->hasMany(Server::class);
}
public function applications()
{
return $this->hasManyThrough(Application::class, Project::class);
}
public function privateKeys()
{
return $this->hasMany(PrivateKey::class);
}
public function members()
{
return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id')->withPivot('role');
}
public function invitations()
{
return $this->hasMany(TeamInvitation::class);
}
public function sources()
{
$sources = collect([]);
@@ -73,11 +83,4 @@ class Team extends Model implements SendsDiscord, SendsEmail
$sources = $sources->merge($github_apps)->merge($gitlab_apps);
return $sources;
}
public function isEmpty()
{
if ($this->projects()->count() === 0 && $this->servers()->count() === 0 && $this->privateKeys()->count() === 0 && $this->sources()->count() === 0) {
return true;
}
return false;
}
}
}

View File

@@ -14,6 +14,7 @@ class TeamInvitation extends Model
'link',
'via',
];
public function team()
{
return $this->belongsTo(Team::class);

View File

@@ -3,18 +3,17 @@
namespace App\Models;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\TrnsactionalEmails\ResetPassword;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Visus\Cuid2\Cuid2;
use Laravel\Fortify\TwoFactorAuthenticatable;
use App\Notifications\TrnsactionalEmails\ResetPassword;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements SendsEmail
{
use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
protected $fillable = [
'id',
'name',
@@ -28,6 +27,7 @@ class User extends Authenticatable implements SendsEmail
protected $casts = [
'email_verified_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
@@ -44,18 +44,27 @@ class User extends Authenticatable implements SendsEmail
$user->teams()->attach($new_team, ['role' => 'owner']);
});
}
public function teams()
{
return $this->belongsToMany(Team::class)->withPivot('role');
}
public function getRecepients($notification)
{
return $this->email;
}
public function sendPasswordResetNotification($token): void
{
$this->notify(new ResetPassword($token));
}
public function isAdmin()
{
return $this->pivot->role === 'admin' || $this->pivot->role === 'owner';
}
public function isAdminFromSession()
{
if (auth()->user()->id === 0) {
@@ -73,6 +82,7 @@ class User extends Authenticatable implements SendsEmail
$role = $teams->where('id', session('currentTeam')->id)->first()->pivot->role;
return $role === 'admin' || $role === 'owner';
}
public function isInstanceAdmin()
{
$found_root_team = auth()->user()->teams->filter(function ($team) {
@@ -83,18 +93,17 @@ class User extends Authenticatable implements SendsEmail
});
return $found_root_team->count() > 0;
}
public function personalTeam()
{
return $this->teams()->where('personal_team', true)->first();
}
public function teams()
{
return $this->belongsToMany(Team::class)->withPivot('role');
}
public function currentTeam()
{
return $this->teams()->where('team_id', session('currentTeam')->id)->first();
}
public function otherTeams()
{
$team_id = session('currentTeam')->id;
@@ -102,6 +111,7 @@ class User extends Authenticatable implements SendsEmail
return $team->id != $team_id;
});
}
public function role()
{
if ($this->teams()->where('team_id', 0)->first()) {
@@ -109,10 +119,11 @@ class User extends Authenticatable implements SendsEmail
}
return $this->teams()->where('team_id', session('currentTeam')->id)->first()->pivot->role;
}
public function resources()
{
$team_id = session('currentTeam')->id;
$data = Application::where('team_id', $team_id)->get();
return $data;
}
}
}

View File

@@ -2,7 +2,6 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Webhook extends Model
@@ -11,4 +10,4 @@ class Webhook extends Model
protected $casts = [
'payload' => 'encrypted',
];
}
}