Changeset View
Changeset View
Standalone View
Standalone View
src/app/Resource.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\ResourceConfigTrait; | use App\Traits\ResourceConfigTrait; | ||||
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 Resource. | * The eloquent definition of a Resource. | ||||
* | * | ||||
* @property int $id The resource identifier | * @property int $id The resource identifier | ||||
* @property string $email An email address | * @property string $email An email address | ||||
* @property string $name The resource name | * @property string $name The resource name | ||||
* @property int $status The resource status | * @property int $status The resource status | ||||
* @property int $tenant_id Tenant identifier | * @property int $tenant_id Tenant identifier | ||||
*/ | */ | ||||
class Resource extends Model | class Resource extends Model | ||||
{ | { | ||||
use BelongsToTenantTrait; | use BelongsToTenantTrait; | ||||
use EntitleableTrait; | use EntitleableTrait; | ||||
use ResourceConfigTrait; | use ResourceConfigTrait; | ||||
use SettingsTrait; | use SettingsTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
use StatusPropertyTrait; | |||||
use UuidIntKeyTrait; | use UuidIntKeyTrait; | ||||
// we've simply never heard of this resource | // we've simply never heard of this resource | ||||
public const STATUS_NEW = 1 << 0; | public const STATUS_NEW = 1 << 0; | ||||
// resource has been activated | // resource has been activated | ||||
public const STATUS_ACTIVE = 1 << 1; | public const STATUS_ACTIVE = 1 << 1; | ||||
// resource has been suspended. | // resource has been suspended. | ||||
// public const STATUS_SUSPENDED = 1 << 2; | // public const STATUS_SUSPENDED = 1 << 2; | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | public static function emailExists(string $email, bool $return_resource = false) | ||||
$resource = self::withTrashed()->where('email', $email)->first(); | $resource = self::withTrashed()->where('email', $email)->first(); | ||||
if ($resource) { | if ($resource) { | ||||
return $return_resource ? $resource : true; | return $return_resource ? $resource : true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | |||||
* Returns whether this resource is active. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isActive(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_ACTIVE) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this resource is deleted. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isDeleted(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_DELETED) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this resource's folder exists in IMAP. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isImapReady(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_IMAP_READY) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this resource is registered in LDAP. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isLdapReady(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_LDAP_READY) > 0; | |||||
} | |||||
/** | |||||
* Returns whether this resource is new. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isNew(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_NEW) > 0; | |||||
} | |||||
/** | |||||
* Resource status mutator | |||||
* | |||||
* @throws \Exception | |||||
*/ | |||||
public function setStatusAttribute($status) | |||||
{ | |||||
$new_status = 0; | |||||
$allowed_values = [ | |||||
self::STATUS_NEW, | |||||
self::STATUS_ACTIVE, | |||||
self::STATUS_DELETED, | |||||
self::STATUS_IMAP_READY, | |||||
self::STATUS_LDAP_READY, | |||||
]; | |||||
foreach ($allowed_values as $value) { | |||||
if ($status & $value) { | |||||
$new_status |= $value; | |||||
$status ^= $value; | |||||
} | |||||
} | |||||
if ($status > 0) { | |||||
throw new \Exception("Invalid resource status: {$status}"); | |||||
} | |||||
$this->attributes['status'] = $new_status; | |||||
} | |||||
} | } |