Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | class User extends Authenticatable | ||||
// 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 | // user in "limited feature-set" state | ||||
public const STATUS_DEGRADED = 1 << 6; | public const STATUS_DEGRADED = 1 << 6; | ||||
// a restricted user | |||||
public const STATUS_RESTRICTED = 1 << 7; | |||||
/** @var array<int, string> The attributes that are mass assignable */ | /** @var array<int, string> The attributes that are mass assignable */ | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'id', | 'id', | ||||
'email', | 'email', | ||||
'password', | 'password', | ||||
'password_ldap', | 'password_ldap', | ||||
'status', | 'status', | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | class User extends Authenticatable | ||||
* Check if current user can delete another object. | * Check if current user can delete another object. | ||||
* | * | ||||
* @param mixed $object A user|domain|wallet|group object | * @param mixed $object A user|domain|wallet|group object | ||||
* | * | ||||
* @return bool True if he can, False otherwise | * @return bool True if he can, False otherwise | ||||
*/ | */ | ||||
public function canDelete($object): bool | public function canDelete($object): bool | ||||
{ | { | ||||
if (!method_exists($object, 'wallet')) { | if (!is_object($object) || !method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
$wallet = $object->wallet(); | $wallet = $object->wallet(); | ||||
// TODO: For now controller can delete/update the account owner, | // TODO: For now controller can delete/update the account owner, | ||||
// this may change in future, controllers are not 0-regression feature | // this may change in future, controllers are not 0-regression feature | ||||
▲ Show 20 Lines • Show All 223 Lines • ▼ Show 20 Lines | public function isDegraded(bool $owner = false): bool | ||||
if ($owner && ($wallet = $this->wallet())) { | if ($owner && ($wallet = $this->wallet())) { | ||||
return $wallet->owner && $wallet->owner->isDegraded(); | return $wallet->owner && $wallet->owner->isDegraded(); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Returns whether this user is restricted. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isRestricted(): bool | |||||
{ | |||||
return ($this->status & self::STATUS_RESTRICTED) > 0; | |||||
} | |||||
/** | |||||
* A shortcut to get the user name. | * A shortcut to get the user name. | ||||
* | * | ||||
* @param bool $fallback Return "<aa.name> User" if there's no name | * @param bool $fallback Return "<aa.name> User" if there's no name | ||||
* | * | ||||
* @return string Full user name | * @return string Full user name | ||||
*/ | */ | ||||
public function name(bool $fallback = false): string | public function name(bool $fallback = false): string | ||||
{ | { | ||||
Show All 14 Lines | class User extends Authenticatable | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function passwords() | public function passwords() | ||||
{ | { | ||||
return $this->hasMany(UserPassword::class); | return $this->hasMany(UserPassword::class); | ||||
} | } | ||||
/** | /** | ||||
* Restrict this user. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function restrict(): void | |||||
{ | |||||
if ($this->isRestricted()) { | |||||
return; | |||||
} | |||||
$this->status |= User::STATUS_RESTRICTED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Return resources controlled by the current user. | * Return resources controlled by the current user. | ||||
* | * | ||||
* @param bool $with_accounts Include resources assigned to wallets | * @param bool $with_accounts Include resources assigned to wallets | ||||
* the current user controls but not owns. | * the current user controls but not owns. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
public function resources($with_accounts = true) | public function resources($with_accounts = true) | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | public function undegrade(): void | ||||
return; | return; | ||||
} | } | ||||
$this->status ^= User::STATUS_DEGRADED; | $this->status ^= User::STATUS_DEGRADED; | ||||
$this->save(); | $this->save(); | ||||
} | } | ||||
/** | /** | ||||
* Un-restrict this user. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function unrestrict(): void | |||||
{ | |||||
if (!$this->isRestricted()) { | |||||
return; | |||||
} | |||||
$this->status ^= User::STATUS_RESTRICTED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Return users controlled by the current user. | * Return users controlled by the current user. | ||||
* | * | ||||
* @param bool $with_accounts Include users assigned to wallets | * @param bool $with_accounts Include users assigned to wallets | ||||
* the current user controls but not owns. | * the current user controls but not owns. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
public function users($with_accounts = true) | public function users($with_accounts = true) | ||||
▲ Show 20 Lines • Show All 62 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, | self::STATUS_DEGRADED, | ||||
self::STATUS_RESTRICTED, | |||||
]; | ]; | ||||
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; | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 201 Lines • Show Last 20 Lines |