90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Project extends Model
|
|
{
|
|
use HasFactory, HasUlids;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'platform',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Project $project) {
|
|
$project->dsn_public_key ??= Str::uuid()->toString();
|
|
$project->dsn_secret_key ??= Str::uuid()->toString();
|
|
});
|
|
}
|
|
|
|
public function getDsnAttribute(): string
|
|
{
|
|
$host = config('app.url');
|
|
|
|
return "{$host}/api/{$this->id}/store/?sentry_key={$this->dsn_public_key}";
|
|
}
|
|
|
|
public function users(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class)
|
|
->withPivot('role')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function issues(): HasMany
|
|
{
|
|
return $this->hasMany(Issue::class);
|
|
}
|
|
|
|
public function events(): HasMany
|
|
{
|
|
return $this->hasMany(Event::class);
|
|
}
|
|
|
|
public function alertRules(): HasMany
|
|
{
|
|
return $this->hasMany(AlertRule::class);
|
|
}
|
|
|
|
public function invitations(): HasMany
|
|
{
|
|
return $this->hasMany(ProjectInvitation::class);
|
|
}
|
|
|
|
public function regenerateKeys(): void
|
|
{
|
|
$this->update([
|
|
'dsn_public_key' => Str::uuid()->toString(),
|
|
'dsn_secret_key' => Str::uuid()->toString(),
|
|
]);
|
|
}
|
|
|
|
public function userRole(User $user): ?string
|
|
{
|
|
return $this->users()
|
|
->where('users.id', $user->id)
|
|
->first()
|
|
?->pivot
|
|
?->role;
|
|
}
|
|
|
|
public function hasOwner(User $user): bool
|
|
{
|
|
return $this->userRole($user) === 'owner';
|
|
}
|
|
|
|
public function hasAdmin(User $user): bool
|
|
{
|
|
return in_array($this->userRole($user), ['owner', 'admin']);
|
|
}
|
|
}
|