Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* The attributes that should be hidden for arrays. | * The attributes that should be hidden for arrays. | ||||
* | * | ||||
* @var array | * @var array | ||||
*/ | */ | ||||
protected $hidden = [ | protected $hidden = [ | ||||
'password', | 'password', | ||||
'password_ldap', | 'password_ldap', | ||||
'remember_token', | 'remember_token', | ||||
'role' | |||||
]; | ]; | ||||
protected $nullable = [ | protected $nullable = [ | ||||
'name', | 'name', | ||||
'password', | 'password', | ||||
'password_ldap' | 'password_ldap' | ||||
]; | ]; | ||||
▲ Show 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* @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 (!method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
if ($this->role == "admin") { | |||||
return true; | |||||
} | |||||
$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 | ||||
return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | ||||
} | } | ||||
/** | /** | ||||
* Check if current user can read data of another object. | * Check if current user can read data of another object. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object A user|domain object | * @param \App\User|\App\Domain $object A user|domain object | ||||
* | * | ||||
* @return bool True if he can, False otherwise | * @return bool True if he can, False otherwise | ||||
*/ | */ | ||||
public function canRead($object): bool | public function canRead($object): bool | ||||
{ | { | ||||
if (!method_exists($object, 'wallet')) { | if (!method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
if ($this->role == "admin") { | |||||
return true; | |||||
} | |||||
if ($object instanceof User && $this->id == $object->id) { | if ($object instanceof User && $this->id == $object->id) { | ||||
return true; | return true; | ||||
} | } | ||||
$wallet = $object->wallet(); | $wallet = $object->wallet(); | ||||
return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | ||||
} | } | ||||
/** | /** | ||||
* Check if current user can update data of another object. | * Check if current user can update data of another object. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object A user|domain object | * @param \App\User|\App\Domain $object A user|domain object | ||||
* | * | ||||
* @return bool True if he can, False otherwise | * @return bool True if he can, False otherwise | ||||
*/ | */ | ||||
public function canUpdate($object): bool | public function canUpdate($object): bool | ||||
{ | { | ||||
if (!method_exists($object, 'wallet')) { | if (!method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
if ($this->role == "admin") { | |||||
return true; | |||||
} | |||||
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); | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | public function addEntitlement($entitlement) | ||||
return $this->entitlements()->save($entitlement); | return $this->entitlements()->save($entitlement); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Helper to find user by email address, whether it is | * Helper to find user by email address, whether it is | ||||
* main email address, alias or external email | * main email address, alias or external email | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param bool $external Search also by an external email | |||||
* | * | ||||
* @return \App\User User model object if found | * @return \App\User User model object if found | ||||
*/ | */ | ||||
public static function findByEmail(string $email): ?User | public static function findByEmail(string $email, bool $external = false): ?User | ||||
{ | { | ||||
if (strpos($email, '@') === false) { | if (strpos($email, '@') === false) { | ||||
return null; | return null; | ||||
} | } | ||||
$email = \strtolower($email); | $email = \strtolower($email); | ||||
$user = self::where('email', $email)->first(); | $user = self::where('email', $email)->first(); | ||||
▲ Show 20 Lines • Show All 90 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. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function suspend(): void | |||||
{ | |||||
if ($this->isSuspended()) { | |||||
return; | |||||
} | |||||
$this->status |= User::STATUS_SUSPENDED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Unsuspend this domain. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function unsuspend(): void | |||||
{ | |||||
if (!$this->isSuspended()) { | |||||
return; | |||||
} | |||||
$this->status ^= User::STATUS_SUSPENDED; | |||||
$this->save(); | |||||
} | |||||
/** | |||||
* Return users controlled by the current user. | * Return users controlled by the current user. | ||||
* | * | ||||
* Users assigned to wallets the current user controls or owns. | * Users assigned to wallets the current user controls or owns. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
public function users() | public function users() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 112 Lines • Show Last 20 Lines |