Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 330 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
public function addEntitlement($entitlement) | public function addEntitlement($entitlement) | ||||
{ | { | ||||
if (!$this->entitlements->contains($entitlement)) { | if (!$this->entitlements->contains($entitlement)) { | ||||
return $this->entitlements()->save($entitlement); | return $this->entitlements()->save($entitlement); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Find whether an email address exists (user or alias). | |||||
* Note: This will also find deleted users. | |||||
* | |||||
* @param string $email Email address | |||||
* @param bool $return_user Return User instance instead of boolean | |||||
* @param bool $is_alias Set to True if the existing email is an alias | |||||
* | |||||
* @return \App\User|bool True or User model object if found, False otherwise | |||||
*/ | |||||
public static function emailExists(string $email, bool $return_user = false, &$is_alias = false) | |||||
{ | |||||
if (strpos($email, '@') === false) { | |||||
return false; | |||||
} | |||||
$email = \strtolower($email); | |||||
$user = self::withTrashed()->where('email', $email)->first(); | |||||
if ($user) { | |||||
return $return_user ? $user : true; | |||||
} | |||||
$alias = UserAlias::where('alias', $email)->first(); | |||||
if ($alias) { | |||||
$is_alias = true; | |||||
return $return_user ? User::withTrashed()->find($alias->user_id) : true; | |||||
} | |||||
return false; | |||||
} | |||||
/** | |||||
* 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 an external email. | ||||
* | |||||
* If there's more than one alias NULL will be returned. | |||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param bool $external Search also by an external email | * @param bool $external Search also for 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, bool $external = false): ?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(); | ||||
if ($user) { | if ($user) { | ||||
return $user; | return $user; | ||||
} | } | ||||
$alias = UserAlias::where('alias', $email)->first(); | $aliases = UserAlias::where('alias', $email)->get(); | ||||
if ($alias) { | if (count($aliases) == 1) { | ||||
return $alias->user; | return $aliases->first()->user; | ||||
} | } | ||||
// TODO: External email | // TODO: External email | ||||
return null; | return null; | ||||
} | } | ||||
public function getJWTIdentifier() | public function getJWTIdentifier() | ||||
▲ Show 20 Lines • Show All 287 Lines • Show Last 20 Lines |