Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show All 12 Lines | |||||
* The eloquent definition of a User. | * The eloquent definition of a User. | ||||
*/ | */ | ||||
class User extends Authenticatable implements JWTSubject | class User extends Authenticatable implements JWTSubject | ||||
{ | { | ||||
use Notifiable; | use Notifiable; | ||||
use NullableFields; | use NullableFields; | ||||
use UserSettingsTrait; | use UserSettingsTrait; | ||||
// a new user, default on creation | |||||
public const STATUS_NEW = 1 << 0; | |||||
// it's been activated | |||||
public const STATUS_ACTIVE = 1 << 1; | |||||
// user has been suspended | |||||
public const STATUS_SUSPENDED = 1 << 2; | |||||
// user has been deleted | |||||
public const STATUS_DELETED = 1 << 3; | |||||
// user has been created in LDAP | |||||
public const STATUS_LDAP_READY = 1 << 4; | |||||
// user mailbox has been created in IMAP | |||||
public const STATUS_IMAP_READY = 1 << 5; | |||||
// 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. | ||||
* | * | ||||
* @var array | * @var array | ||||
*/ | */ | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'name', 'email', 'password', 'password_ldap' | 'name', | ||||
'email', | |||||
'password', | |||||
'password_ldap', | |||||
'status' | |||||
]; | ]; | ||||
/** | /** | ||||
* 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_ldap', 'remember_token', | 'password', | ||||
'password_ldap', | |||||
'remember_token', | |||||
]; | ]; | ||||
protected $nullable = [ | protected $nullable = [ | ||||
'name', | 'name', | ||||
'password', | 'password', | ||||
'password_ldap' | 'password_ldap' | ||||
]; | ]; | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | public static function findByEmail(string $email): ?User | ||||
$user = self::where('email', $email)->first(); | $user = self::where('email', $email)->first(); | ||||
// TODO: Aliases, External email | // TODO: Aliases, External email | ||||
return $user; | return $user; | ||||
} | } | ||||
public function getJWTIdentifier() | |||||
{ | |||||
return $this->getKey(); | |||||
} | |||||
public function getJWTCustomClaims() | |||||
{ | |||||
return []; | |||||
} | |||||
/** | |||||
* Returns whether this domain is active. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isActive(): bool | |||||
{ | |||||
return $this->status & self::STATUS_ACTIVE; | |||||
} | |||||
/** | |||||
* Returns whether this domain is deleted. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isDeleted(): bool | |||||
{ | |||||
return $this->status & self::STATUS_DELETED; | |||||
} | |||||
/** | |||||
* Returns whether this (external) domain has been verified | |||||
* to exist in DNS. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isImapReady(): bool | |||||
{ | |||||
return $this->status & self::STATUS_IMAP_READY; | |||||
} | |||||
/** | |||||
* Returns whether this user is registered in LDAP. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isLdapReady(): bool | |||||
{ | |||||
return $this->status & self::STATUS_LDAP_READY; | |||||
} | |||||
/** | |||||
* Returns whether this user is new. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isNew(): bool | |||||
{ | |||||
return $this->status & self::STATUS_NEW; | |||||
} | |||||
/** | |||||
* Returns whether this domain is suspended. | |||||
* | |||||
* @return bool | |||||
*/ | |||||
public function isSuspended(): bool | |||||
{ | |||||
return $this->status & self::STATUS_SUSPENDED; | |||||
} | |||||
/** | |||||
* Any (additional) properties of this user. | |||||
* | |||||
* @return \App\UserSetting[] | |||||
*/ | |||||
public function settings() | public function settings() | ||||
{ | { | ||||
return $this->hasMany('App\UserSetting', 'user_id'); | return $this->hasMany('App\UserSetting', 'user_id'); | ||||
} | } | ||||
/** | /** | ||||
* Verification codes for this user. | * Verification codes for this user. | ||||
* | * | ||||
Show All 9 Lines | class User extends Authenticatable implements JWTSubject | ||||
* | * | ||||
* @return Wallet[] | * @return Wallet[] | ||||
*/ | */ | ||||
public function wallets() | public function wallets() | ||||
{ | { | ||||
return $this->hasMany('App\Wallet'); | return $this->hasMany('App\Wallet'); | ||||
} | } | ||||
public function getJWTIdentifier() | |||||
{ | |||||
return $this->getKey(); | |||||
} | |||||
public function getJWTCustomClaims() | |||||
{ | |||||
return []; | |||||
} | |||||
public function setPasswordAttribute($password) | public function setPasswordAttribute($password) | ||||
{ | { | ||||
if (!empty($password)) { | if (!empty($password)) { | ||||
$this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]); | $this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]); | ||||
$this->attributes['password_ldap'] = '{SSHA512}' . base64_encode( | $this->attributes['password_ldap'] = '{SSHA512}' . base64_encode( | ||||
pack('H*', hash('sha512', $password)) | pack('H*', hash('sha512', $password)) | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
public function setPasswordLdapAttribute($password) | public function setPasswordLdapAttribute($password) | ||||
{ | { | ||||
if (!empty($password)) { | if (!empty($password)) { | ||||
$this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]); | $this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]); | ||||
$this->attributes['password_ldap'] = '{SSHA512}' . base64_encode( | $this->attributes['password_ldap'] = '{SSHA512}' . base64_encode( | ||||
pack('H*', hash('sha512', $password)) | pack('H*', hash('sha512', $password)) | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* User 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, | |||||
self::STATUS_IMAP_READY, | |||||
]; | |||||
foreach ($allowed_values as $value) { | |||||
if ($status & $value) { | |||||
$new_status |= $value; | |||||
$status ^= $value; | |||||
} | |||||
} | |||||
if ($status > 0) { | |||||
throw new \Exception("Invalid user status: {$status}"); | |||||
} | |||||
$this->attributes['status'] = $new_status; | |||||
} | |||||
} | } |