diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php new file mode 100644 index 0000000..2f6aedf --- /dev/null +++ b/app/Console/Commands/CreateUser.php @@ -0,0 +1,106 @@ +ask('Type the wanted user username', 'admweb'); + $validator = Validator::make(['username' => $tmp], [ + 'username' => 'required|min:5,max:255|unique:users,username' + ]); + $validator->validate(); + $username = $tmp; + } catch (ValidationException $e) { + $this->error(sprintf( + 'Please provide a valid username %s', + \implode(',', collect($e->errors())->flatten()->all()) + )); + continue; + } + } + + $email = null; + while (is_null($email)) { + try { + $tmp = $this->ask('Type the wanted user email', 'equipe.web@ldc.fr'); + $validator = Validator::make(['email' => $tmp], [ + 'email' => 'required|unique:users,email|email:rfc,dns' + ]); + $validator->validate(); + $email = $tmp; + } catch (ValidationException $e) { + $this->error(sprintf( + 'Please provide a valid email %s', + \implode(',', collect($e->errors())->flatten()->all()) + )); + continue; + } + } + + $name = null; + while (is_null($name)) { + try { + $tmp = $this->ask('Type the wanted user name', 'Saïtama'); + $validator = Validator::make(['name' => $tmp], [ + 'name' => 'required|min:5,max:255' + ]); + $validator->validate(); + $name = $tmp; + } catch (ValidationException $e) { + $this->error(sprintf( + 'Please provide a valid name %s', + \implode(',', collect($e->errors())->flatten()->all()) + )); + continue; + } + } + + $password = $this->secret('Type the wanted user password'); + + $admin = new User([ + 'name' => $name, + 'email' => $email, + 'password' => $password + ]); + + $admin->username = $username; + $admin->email_verified_at = Carbon::now(); + $admin->saveOrFail(); + + $this->info('User created.'); + return 0; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a3d0163..687937a 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -12,8 +12,7 @@ class Kernel extends ConsoleKernel * * @var array */ - protected $commands = [ - ]; + protected $commands = []; /** * Define the application's command schedule. @@ -23,8 +22,6 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire') - // ->hourly(); } /** diff --git a/app/Models/User.php b/app/Models/User.php index bb331d7..10dcd50 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -1,9 +1,13 @@ 'datetime', ]; + + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted() + { + // * EVENT CLOSURES. + static::creating(function (User $user) { + static::assertUsernameIsUnique($user->id, $user->username); + static::assertEmailIsUnique($user->id, $user->email); + if (Hash::needsRehash($user->password)) { + $user->password = Hash::make($user->password); + } + $user->remember_token = Str::random(10); + }); + static::updating(function (User $user) { + static::assertUsernameIsUnique($user->id, $user->username); + static::assertEmailIsUnique($user->id, $user->email); + if (Hash::needsRehash($user->password)) { + $user->password = Hash::make($user->password); + } + $user->remember_token = Str::random(10); + }); + } + + /** + * Asserts using validation that the email is unique + * + * @param mixed $id + * @param mixed $email + * @return void + * @throws \Illuminate\Validation\ValidationException If user email as already used. + */ + private static function assertEmailIsUnique($id, $email) + { + self::assertFieldIsUnique($id, 'email', $email); + } + + /** + * Asserts using validation that the login is unique + * + * @param mixed $id + * @param mixed $login + * @return void + * @throws \Illuminate\Validation\ValidationException If user login as already used. + */ + private static function assertUsernameIsUnique($id, $login) + { + self::assertFieldIsUnique($id, 'username', $login); + } + + /** + * Asserts that the field is unique + * + * @param mixed $id + * @param string $field + * @param mixed $value + * @return void + * @throws \Illuminate\Validation\ValidationException If the field is not unique. + */ + private static function assertFieldIsUnique($id, string $field, $value) + { + Validator::make([$field => $value], [ + $field => Rule::unique('users', $field)->ignore($id), + ], [ + $field . '.unique' => trans('La valeur :value pour le champ :attribute est déjà utilisé', [ + 'attribute' => $field, + 'value' => $value + ]) + ])->validate(); + } } diff --git a/config/auth.php b/config/auth.php index 2051dfd..c30fc9c 100644 --- a/config/auth.php +++ b/config/auth.php @@ -68,13 +68,8 @@ return [ 'providers' => [ 'users' => [ 'driver' => 'eloquent', - 'model' => App\User::class, + 'model' => App\Models\User::class, ], - - // 'users' => [ - // 'driver' => 'database', - // 'table' => 'users', - // ], ], /*