Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/UsersController.php
Show First 20 Lines • Show All 508 Lines • ▼ Show 20 Lines | protected function validateUserRequest(Request $request, $user, &$settings = []) | ||||
$controller = $user ? $user->wallet()->owner : $this->guard()->user(); | $controller = $user ? $user->wallet()->owner : $this->guard()->user(); | ||||
// For new user validate email address | // For new user validate email address | ||||
if (empty($user)) { | if (empty($user)) { | ||||
$email = $request->email; | $email = $request->email; | ||||
if (empty($email)) { | if (empty($email)) { | ||||
$errors['email'] = \trans('validation.required', ['attribute' => 'email']); | $errors['email'] = \trans('validation.required', ['attribute' => 'email']); | ||||
} elseif ($error = \App\Utils::validateEmail($email, $controller, false)) { | } elseif ($error = self::validateEmail($email, $controller, false)) { | ||||
$errors['email'] = $error; | $errors['email'] = $error; | ||||
} | } | ||||
} | } | ||||
// Validate aliases input | // Validate aliases input | ||||
if (isset($request->aliases)) { | if (isset($request->aliases)) { | ||||
$aliases = []; | $aliases = []; | ||||
$existing_aliases = $user ? $user->aliases()->get()->pluck('alias')->toArray() : []; | $existing_aliases = $user ? $user->aliases()->get()->pluck('alias')->toArray() : []; | ||||
foreach ($request->aliases as $idx => $alias) { | foreach ($request->aliases as $idx => $alias) { | ||||
if (is_string($alias) && !empty($alias)) { | if (is_string($alias) && !empty($alias)) { | ||||
// Alias cannot be the same as the email address (new user) | // Alias cannot be the same as the email address (new user) | ||||
if (!empty($email) && Str::lower($alias) == Str::lower($email)) { | if (!empty($email) && Str::lower($alias) == Str::lower($email)) { | ||||
continue; | continue; | ||||
} | } | ||||
// validate new aliases | // validate new aliases | ||||
if ( | if ( | ||||
!in_array($alias, $existing_aliases) | !in_array($alias, $existing_aliases) | ||||
&& ($error = \App\Utils::validateEmail($alias, $controller, true)) | && ($error = self::validateEmail($alias, $controller, true)) | ||||
) { | ) { | ||||
if (!isset($errors['aliases'])) { | if (!isset($errors['aliases'])) { | ||||
$errors['aliases'] = []; | $errors['aliases'] = []; | ||||
} | } | ||||
$errors['aliases'][$idx] = $error; | $errors['aliases'][$idx] = $error; | ||||
continue; | continue; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public static function execProcessStep(User $user, string $step): bool | ||||
return $user->isImapReady(); | return $user->isImapReady(); | ||||
} | } | ||||
} catch (\Exception $e) { | } catch (\Exception $e) { | ||||
\Log::error($e); | \Log::error($e); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | |||||
* Email address (login or alias) validation | |||||
* | |||||
* @param string $email Email address | |||||
* @param \App\User $user The account owner | |||||
* @param bool $is_alias The email is an alias | |||||
* | |||||
* @return string Error message on validation error | |||||
*/ | |||||
public static function validateEmail( | |||||
string $email, | |||||
\App\User $user, | |||||
bool $is_alias = false | |||||
): ?string { | |||||
$attribute = $is_alias ? 'alias' : 'email'; | |||||
if (strpos($email, '@') === false) { | |||||
return \trans('validation.entryinvalid', ['attribute' => $attribute]); | |||||
} | |||||
list($login, $domain) = explode('@', $email); | |||||
// Check if domain exists | |||||
$domain = Domain::where('namespace', Str::lower($domain))->first(); | |||||
if (empty($domain)) { | |||||
return \trans('validation.domaininvalid'); | |||||
} | |||||
// Validate login part alone | |||||
$v = Validator::make( | |||||
[$attribute => $login], | |||||
[$attribute => ['required', new UserEmailLocal(!$domain->isPublic())]] | |||||
); | |||||
if ($v->fails()) { | |||||
return $v->errors()->toArray()[$attribute][0]; | |||||
} | |||||
// Check if it is one of domains available to the user | |||||
// TODO: We should have a helper that returns "flat" array with domain names | |||||
// I guess we could use pluck() somehow | |||||
$domains = array_map( | |||||
function ($domain) { | |||||
return $domain->namespace; | |||||
}, | |||||
$user->domains() | |||||
); | |||||
if (!in_array($domain->namespace, $domains)) { | |||||
return \trans('validation.entryexists', ['attribute' => 'domain']); | |||||
} | |||||
// Check if a user with specified address already exists | |||||
// Allow assigning the same alias to a user in the same group account | |||||
if ($exists = User::emailExists($email, true, $alias_exists)) { | |||||
if (!$is_alias || !$alias_exists || $exists->wallet()->user_id != $user->id) { | |||||
return \trans('validation.entryexists', ['attribute' => $attribute]); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
} | } |