Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show All 36 Lines | class User extends Authenticatable implements JWTSubject | ||||
// user has been suspended | // user has been suspended | ||||
public const STATUS_SUSPENDED = 1 << 2; | public const STATUS_SUSPENDED = 1 << 2; | ||||
// user has been deleted | // user has been deleted | ||||
public const STATUS_DELETED = 1 << 3; | public const STATUS_DELETED = 1 << 3; | ||||
// user has been created in LDAP | // user has been created in LDAP | ||||
public const STATUS_LDAP_READY = 1 << 4; | public const STATUS_LDAP_READY = 1 << 4; | ||||
// user mailbox has been created in IMAP | // user mailbox has been created in IMAP | ||||
public const STATUS_IMAP_READY = 1 << 5; | public const STATUS_IMAP_READY = 1 << 5; | ||||
// user in "limited feature-set" state | |||||
public const STATUS_DEGRADED = 1 << 6; | |||||
// change the default primary key type | // change the default primary key type | ||||
public $incrementing = false; | public $incrementing = false; | ||||
protected $keyType = 'bigint'; | protected $keyType = 'bigint'; | ||||
/** | /** | ||||
* The attributes that are mass assignable. | * The attributes that are mass assignable. | ||||
▲ Show 20 Lines • Show All 205 Lines • ▼ Show 20 Lines | public function canUpdate($object): bool | ||||
if ($object instanceof User && $this->id == $object->id) { | if ($object instanceof User && $this->id == $object->id) { | ||||
return true; | return true; | ||||
} | } | ||||
return $this->canDelete($object); | return $this->canDelete($object); | ||||
} | } | ||||
/** | /** | ||||
* Degrade the user | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function degrade(): void | |||||
{ | |||||
if ($this->isDegraded()) { | |||||
return; | |||||
} | |||||
$this->status |= User::STATUS_DEGRADED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Return the \App\Domain for this user. | * Return the \App\Domain for this user. | ||||
* | * | ||||
* @return \App\Domain|null | * @return \App\Domain|null | ||||
*/ | */ | ||||
public function domain() | public function domain() | ||||
{ | { | ||||
list($local, $domainName) = explode('@', $this->email); | list($local, $domainName) = explode('@', $this->email); | ||||
▲ Show 20 Lines • Show All 170 Lines • ▼ Show 20 Lines | public function hasSku($title): bool | ||||
if (!$sku) { | if (!$sku) { | ||||
return false; | return false; | ||||
} | } | ||||
return $this->entitlements()->where('sku_id', $sku->id)->count() > 0; | return $this->entitlements()->where('sku_id', $sku->id)->count() > 0; | ||||
} | } | ||||
/** | /** | ||||
* Returns whether this domain is active. | * Returns whether this user is active. | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isActive(): bool | public function isActive(): bool | ||||
{ | { | ||||
return ($this->status & self::STATUS_ACTIVE) > 0; | return ($this->status & self::STATUS_ACTIVE) > 0; | ||||
} | } | ||||
/** | /** | ||||
* Returns whether this domain is deleted. | * Returns whether this user (or its wallet owner) is degraded. | ||||
* | |||||
* @param bool $owner Check also the wallet owner instead just the user himself | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isDegraded(bool $owner = false): bool | |||||
{ | |||||
if ($this->status & self::STATUS_DEGRADED) { | |||||
return true; | |||||
} | |||||
if ($owner) { | |||||
$owner = $this->wallet()->owner; | |||||
return $owner && $owner->isDegraded(); | |||||
} | |||||
return false; | |||||
} | |||||
/** | |||||
* Returns whether this user is deleted. | |||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isDeleted(): bool | public function isDeleted(): bool | ||||
{ | { | ||||
return ($this->status & self::STATUS_DELETED) > 0; | return ($this->status & self::STATUS_DELETED) > 0; | ||||
} | } | ||||
/** | /** | ||||
* Returns whether this (external) domain has been verified | * Returns whether this user is registered in IMAP. | ||||
* to exist in DNS. | |||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isImapReady(): bool | public function isImapReady(): bool | ||||
{ | { | ||||
return ($this->status & self::STATUS_IMAP_READY) > 0; | return ($this->status & self::STATUS_IMAP_READY) > 0; | ||||
} | } | ||||
Show All 13 Lines | class User extends Authenticatable implements JWTSubject | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isNew(): bool | public function isNew(): bool | ||||
{ | { | ||||
return ($this->status & self::STATUS_NEW) > 0; | return ($this->status & self::STATUS_NEW) > 0; | ||||
} | } | ||||
/** | /** | ||||
* Returns whether this domain is suspended. | * Returns whether this user is suspended. | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isSuspended(): bool | public function isSuspended(): bool | ||||
{ | { | ||||
return ($this->status & self::STATUS_SUSPENDED) > 0; | return ($this->status & self::STATUS_SUSPENDED) > 0; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function settings() | public function settings() | ||||
{ | { | ||||
return $this->hasMany('App\UserSetting', 'user_id'); | return $this->hasMany('App\UserSetting', 'user_id'); | ||||
} | } | ||||
/** | /** | ||||
* Suspend this domain. | * Suspend this user. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function suspend(): void | public function suspend(): void | ||||
{ | { | ||||
if ($this->isSuspended()) { | if ($this->isSuspended()) { | ||||
return; | return; | ||||
} | } | ||||
$this->status |= User::STATUS_SUSPENDED; | $this->status |= User::STATUS_SUSPENDED; | ||||
$this->save(); | $this->save(); | ||||
} | } | ||||
/** | /** | ||||
* Unsuspend this domain. | * Un-degrade this user. | ||||
* | |||||
* @return void | |||||
*/ | |||||
public function undegrade(): void | |||||
{ | |||||
if (!$this->isDegraded()) { | |||||
return; | |||||
} | |||||
$this->status ^= User::STATUS_DEGRADED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Unsuspend this user. | |||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function unsuspend(): void | public function unsuspend(): void | ||||
{ | { | ||||
if (!$this->isSuspended()) { | if (!$this->isSuspended()) { | ||||
return; | return; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | public function setStatusAttribute($status) | ||||
$allowed_values = [ | $allowed_values = [ | ||||
self::STATUS_NEW, | self::STATUS_NEW, | ||||
self::STATUS_ACTIVE, | self::STATUS_ACTIVE, | ||||
self::STATUS_SUSPENDED, | self::STATUS_SUSPENDED, | ||||
self::STATUS_DELETED, | self::STATUS_DELETED, | ||||
self::STATUS_LDAP_READY, | self::STATUS_LDAP_READY, | ||||
self::STATUS_IMAP_READY, | self::STATUS_IMAP_READY, | ||||
self::STATUS_DEGRADED, | |||||
]; | ]; | ||||
foreach ($allowed_values as $value) { | foreach ($allowed_values as $value) { | ||||
if ($status & $value) { | if ($status & $value) { | ||||
$new_status |= $value; | $new_status |= $value; | ||||
$status ^= $value; | $status ^= $value; | ||||
} | } | ||||
} | } | ||||
if ($status > 0) { | if ($status > 0) { | ||||
throw new \Exception("Invalid user status: {$status}"); | throw new \Exception("Invalid user status: {$status}"); | ||||
} | } | ||||
$this->attributes['status'] = $new_status; | $this->attributes['status'] = $new_status; | ||||
} | } | ||||
} | } |