Changeset View
Changeset View
Standalone View
Standalone View
src/app/Group.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Traits\BelongsToTenantTrait; | use App\Traits\BelongsToTenantTrait; | ||||
use App\Traits\EntitleableTrait; | use App\Traits\EntitleableTrait; | ||||
use App\Traits\GroupConfigTrait; | use App\Traits\GroupConfigTrait; | ||||
use App\Traits\SettingsTrait; | use App\Traits\SettingsTrait; | ||||
use App\Traits\StatusPropertyTrait; | |||||
use App\Traits\UuidIntKeyTrait; | use App\Traits\UuidIntKeyTrait; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
/** | /** | ||||
* The eloquent definition of a Group. | * The eloquent definition of a Group. | ||||
* | * | ||||
* @property int $id The group identifier | * @property int $id The group identifier | ||||
* @property string $email An email address | * @property string $email An email address | ||||
* @property string $members A comma-separated list of email addresses | * @property string $members A comma-separated list of email addresses | ||||
* @property string $name The group name | * @property string $name The group name | ||||
* @property int $status The group status | * @property int $status The group status | ||||
* @property int $tenant_id Tenant identifier | * @property int $tenant_id Tenant identifier | ||||
*/ | */ | ||||
class Group extends Model | class Group extends Model | ||||
{ | { | ||||
use BelongsToTenantTrait; | use BelongsToTenantTrait; | ||||
use EntitleableTrait; | use EntitleableTrait; | ||||
use GroupConfigTrait; | use GroupConfigTrait; | ||||
use SettingsTrait; | use SettingsTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
use StatusPropertyTrait; | |||||
use UuidIntKeyTrait; | use UuidIntKeyTrait; | ||||
// we've simply never heard of this group | // we've simply never heard of this group | ||||
public const STATUS_NEW = 1 << 0; | public const STATUS_NEW = 1 << 0; | ||||
// group has been activated | // group has been activated | ||||
public const STATUS_ACTIVE = 1 << 1; | public const STATUS_ACTIVE = 1 << 1; | ||||
// group has been suspended. | // group has been suspended. | ||||
public const STATUS_SUSPENDED = 1 << 2; | public const STATUS_SUSPENDED = 1 << 2; | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | class Group extends Model | ||||
* @return array Email addresses of the group members, as an array | * @return array Email addresses of the group members, as an array | ||||
*/ | */ | ||||
public function getMembersAttribute($members): array | public function getMembersAttribute($members): array | ||||
{ | { | ||||
return $members ? explode(',', $members) : []; | return $members ? explode(',', $members) : []; | ||||
} | } | ||||
/** | /** | ||||
* Returns whether this group is active. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isActive(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_ACTIVE) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this group is deleted. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isDeleted(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_DELETED) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this group is new. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isNew(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_NEW) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this group is registered in LDAP. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isLdapReady(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_LDAP_READY) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this group is suspended. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isSuspended(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_SUSPENDED) > 0; | |||||
} | |||||
/** | |||||
* Ensure the email is appropriately cased. | * Ensure the email is appropriately cased. | ||||
* | * | ||||
* @param string $email Group email address | * @param string $email Group email address | ||||
*/ | */ | ||||
public function setEmailAttribute(string $email) | public function setEmailAttribute(string $email) | ||||
{ | { | ||||
$this->attributes['email'] = strtolower($email); | $this->attributes['email'] = strtolower($email); | ||||
} | } | ||||
/** | /** | ||||
* Ensure the members are appropriately formatted. | * Ensure the members are appropriately formatted. | ||||
* | * | ||||
* @param array $members Email addresses of the group members | * @param array $members Email addresses of the group members | ||||
*/ | */ | ||||
public function setMembersAttribute(array $members): void | public function setMembersAttribute(array $members): void | ||||
{ | { | ||||
$members = array_unique(array_filter(array_map('strtolower', $members))); | $members = array_unique(array_filter(array_map('strtolower', $members))); | ||||
sort($members); | sort($members); | ||||
$this->attributes['members'] = implode(',', $members); | $this->attributes['members'] = implode(',', $members); | ||||
} | } | ||||
/** | |||||
* Group status mutator | |||||
* | |||||
* @throws \Exception | |||||
*/ | |||||
public function setStatusAttribute($status) | |||||
{ | |||||
$new_status = 0; | |||||
$allowed_values = [ | |||||
self::STATUS_NEW, | |||||
self::STATUS_ACTIVE, | |||||
self::STATUS_SUSPENDED, | |||||
self::STATUS_DELETED, | |||||
self::STATUS_LDAP_READY, | |||||
]; | |||||
foreach ($allowed_values as $value) { | |||||
if ($status & $value) { | |||||
$new_status |= $value; | |||||
$status ^= $value; | |||||
} | |||||
} | |||||
if ($status > 0) { | |||||
throw new \Exception("Invalid group status: {$status}"); | |||||
} | |||||
$this->attributes['status'] = $new_status; | |||||
} | |||||
/** | |||||
* Suspend this group. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function suspend(): void | |||||
{ | |||||
if ($this->isSuspended()) { | |||||
return; | |||||
} | |||||
$this->status |= Group::STATUS_SUSPENDED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Unsuspend this group. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function unsuspend(): void | |||||
{ | |||||
if (!$this->isSuspended()) { | |||||
return; | |||||
} | |||||
$this->status ^= Group::STATUS_SUSPENDED; | |||||
$this->save(); | |||||
} | |||||
} | } |