Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 336 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
/** | /** | ||||
* Find whether an email address exists (user or alias). | * Find whether an email address exists (user or alias). | ||||
* Note: This will also find deleted users. | * Note: This will also find deleted users. | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param bool $return_user Return User instance instead of boolean | * @param bool $return_user Return User instance instead of boolean | ||||
* @param bool $is_alias Set to True if the existing email is an alias | * @param bool $is_alias Set to True if the existing email is an alias | ||||
* @param bool $existing Ignore deleted users | |||||
* | * | ||||
* @return \App\User|bool True or User model object if found, False otherwise | * @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) | public static function emailExists(string $email, bool $return_user = false, &$is_alias = false, $existing = false) | ||||
{ | { | ||||
if (strpos($email, '@') === false) { | if (strpos($email, '@') === false) { | ||||
return false; | return false; | ||||
} | } | ||||
$email = \strtolower($email); | $email = \strtolower($email); | ||||
if ($existing) { | |||||
$user = self::where('email', $email)->first(); | |||||
} else { | |||||
$user = self::withTrashed()->where('email', $email)->first(); | $user = self::withTrashed()->where('email', $email)->first(); | ||||
} | |||||
if ($user) { | if ($user) { | ||||
return $return_user ? $user : true; | return $return_user ? $user : true; | ||||
} | } | ||||
$alias = UserAlias::where('alias', $email)->first(); | $aliases = UserAlias::where('alias', $email); | ||||
if ($existing) { | |||||
$aliases = $aliases->join('users', 'user_id', '=', 'users.id') | |||||
->whereNull('users.deleted_at'); | |||||
} | |||||
$alias = $aliases->first(); | |||||
if ($alias) { | if ($alias) { | ||||
$is_alias = true; | $is_alias = true; | ||||
return $return_user ? User::withTrashed()->find($alias->user_id) : true; | return $return_user ? self::withTrashed()->find($alias->user_id) : true; | ||||
} | } | ||||
return false; | 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 an external email. | * main email address, alias or an external email. | ||||
▲ Show 20 Lines • Show All 316 Lines • Show Last 20 Lines |