*/ public static function passwordRules(): array { return ['string', PasswordRule::default()]; } /** * Pass the user being updated so its own email, username and external id are not taken for * duplicates. * * @return NormalizedValidationRules */ public static function rules(?User $user = null): array { return [ 'email' => ['required', 'email:strict', 'between:1,191', new UserEmail, Rule::unique('users', 'email')->ignore($user)], 'external_id' => ['sometimes', 'nullable', 'string', 'max:191', Rule::unique('users', 'external_id')->ignore($user)], 'username' => ['required', 'between:1,191', Rule::unique('users', 'username')->ignore($user), new Username], 'name_first' => ['required', 'string', 'between:1,191'], 'name_last' => ['required', 'string', 'between:1,191'], 'password' => ['sometimes', 'nullable', ...self::passwordRules()], 'root_admin' => ['boolean'], 'language' => ['string', new In(array_keys((new User)->getAvailableLanguages()))], ]; } public function create(array $input): User { $attributes = Validator::validate($input, self::rules()); $extensions = ValidatedExtensionValues::of($input['extensions'] ?? null); // Blank fields take their defaults, so a blank password, spaces included, is no password at all. $attributes = array_filter($attributes, filled(...)); $generateResetToken = ! isset($attributes['password']); $attributes['password'] ??= Str::random(30); [$user, $token] = DB::transaction(function () use ($attributes, $generateResetToken, $extensions): array { $user = User::query()->create($attributes); $this->extensions->save($user, $extensions); return [$user->refresh(), $generateResetToken ? Password::createToken($user) : null]; }); Activity::event('user:user.create') ->subject($user) ->property(['email' => $user->email, 'username' => $user->username, 'admin' => $user->root_admin]) ->log(); $user->notify(new AccountCreated($user, $token)); return $user; } }