CluPilotCloud/app/Models/Seat.php

88 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Seat extends Model
{
/** @use HasFactory<\Database\Factories\SeatFactory> */
use HasFactory, HasUuid;
public const ROLES = ['owner', 'admin', 'member', 'readonly'];
/**
* Welche Nextcloud-Gruppe eine Rolle bedeutet.
*
* Rollen sind Gruppen im Gast, damit weitere spaeter eine Zeile sind und
* kein Umbau. `owner` und `admin` teilen sich `admin` — der Unterschied
* zwischen beiden ist eine CluPilot-Angelegenheit (der letzte owner darf
* nicht entfernt werden), keine Nextcloud-Angelegenheit.
*/
public const GROUPS = [
'owner' => 'admin',
'admin' => 'admin',
'member' => 'mitarbeiter',
'readonly' => 'nur-lesen',
];
public const STATE_NONE = 'none';
public const STATE_PENDING = 'pending';
public const STATE_SYNCED = 'synced';
public const STATE_FAILED = 'failed';
protected $fillable = [
'customer_id', 'email', 'name', 'role', 'status', 'invited_at',
'nc_username', 'nc_state', 'nc_error', 'nc_synced_at',
];
protected function casts(): array
{
return ['invited_at' => 'datetime', 'nc_synced_at' => 'datetime'];
}
/**
* Die Rolle, die keinen eigenen Speicherplatz bekommt — und die einzige,
* bei der ein eigener Wert am Konto richtig ist. Siehe NextcloudUsers.
*/
public function isReadonly(): bool
{
return $this->role === 'readonly';
}
/**
* Verknuepft einen Inhaber-Sitz mit dem Admin-Konto seiner Instanz.
*
* Dieses Konto existiert in der Nextcloud tatsaechlich — CreateCustomerAdmin
* hat es beim Aufbau angelegt. Ein zweites Admin-Konto fuer dieselbe Person
* waere eines zu viel.
*/
public function linkToInstanceAdmin(): void
{
$ref = $this->customer?->instances()
->whereIn('status', ['active', 'cancellation_scheduled'])
->latest('id')->first()?->nc_admin_ref;
if (blank($ref)) {
return;
}
$this->fill([
'nc_username' => $ref,
'nc_state' => self::STATE_SYNCED,
'nc_synced_at' => now(),
])->save();
}
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
}