/** * Create a new user with an automatillay generated username. * If an $account is provided, the username will try to match the account personal info. * * @param Account $account * * @return User * @throws \Exception */ public static function autoCreate(Account $account = null) { // Profile of the user $profile = Profile::findOrFail(config('site.auto-registration-profile')); // Seeds for the username $seeds = !$account ? [] : array_filter([$account->first_name, $account->last_name, $account->name, $account->nickname, preg_replace('/([^@]*).*/', '$1', $account->email), $account->first_name . str_random(4), $account->last_name . str_random(4), $account->name . str_random(4), $account->nickname . str_random(4), $account->provider->name . $account->uid]); // Attempt to create user $user = new User(['username' => generate_username($seeds), 'name' => $account ? $account->nameForHumans() : null, 'password' => $password = str_random(60), 'password_confirmation' => $password, 'profile_id' => $profile->id]); if (!$user->save()) { throw new \Exception(_('Unable to create user')); } return $user; }