From bdc08e8c19eeff8239d8833185351d6d19502142 Mon Sep 17 00:00:00 2001 From: Guilherme Capanema Date: Sun, 31 May 2020 18:24:33 -0300 Subject: [PATCH] Implementa base do cadastro de associados --- app/Events/UserCreating.php | 29 ++++++++++ .../{Register.php => Register/Individual.php} | 19 ++----- app/Listeners/SendUserPasswordEmail.php | 37 +++++++++++++ app/Mail/UserPassword.php | 50 +++++++++++++++++ app/Providers/EventServiceProvider.php | 7 ++- app/User.php | 36 +++++++++++- app/UserCategory.php | 30 ++++++++++ composer.json | 1 + composer.lock | 46 +++++++++++++++- config/app.php | 4 +- config/auth.php | 17 ++++++ database/factories/UserFactory.php | 3 +- .../2014_10_12_000000_create_users_table.php | 14 +++++ resources/lang/pt-BR/auth.php | 2 + resources/lang/pt-BR/user_categories.php | 5 ++ resources/lang/pt-BR/users.php | 8 +++ resources/views/auth/register.blade.php | 8 --- .../views/auth/register/individual.blade.php | 8 +++ .../views/emails/users/password.blade.php | 23 ++++++++ .../individual.blade.php} | 38 +++---------- routes/web.php | 3 +- .../IndividualTest.php} | 55 ++++--------------- 22 files changed, 339 insertions(+), 104 deletions(-) create mode 100644 app/Events/UserCreating.php rename app/Http/Livewire/Auth/{Register.php => Register/Individual.php} (58%) create mode 100644 app/Listeners/SendUserPasswordEmail.php create mode 100644 app/Mail/UserPassword.php create mode 100644 app/UserCategory.php create mode 100644 resources/lang/pt-BR/user_categories.php create mode 100644 resources/lang/pt-BR/users.php delete mode 100644 resources/views/auth/register.blade.php create mode 100644 resources/views/auth/register/individual.blade.php create mode 100644 resources/views/emails/users/password.blade.php rename resources/views/livewire/auth/{register.blade.php => register/individual.blade.php} (60%) rename tests/Feature/Auth/{RegisterTest.php => Register/IndividualTest.php} (60%) diff --git a/app/Events/UserCreating.php b/app/Events/UserCreating.php new file mode 100644 index 0000000..a9338c0 --- /dev/null +++ b/app/Events/UserCreating.php @@ -0,0 +1,29 @@ +user = $user; + } +} diff --git a/app/Http/Livewire/Auth/Register.php b/app/Http/Livewire/Auth/Register/Individual.php similarity index 58% rename from app/Http/Livewire/Auth/Register.php rename to app/Http/Livewire/Auth/Register/Individual.php index 7b5b00e..9a06c38 100644 --- a/app/Http/Livewire/Auth/Register.php +++ b/app/Http/Livewire/Auth/Register/Individual.php @@ -1,14 +1,14 @@ validate([ 'name' => ['required'], 'email' => ['required', 'email', 'unique:users'], - 'password' => ['required', 'min:8', 'same:passwordConfirmation'], ]); $user = User::create([ 'email' => $this->email, 'name' => $this->name, - 'password' => Hash::make($this->password), + 'category_id' => UserCategory::where('key', 'individual')->first()->id, ]); - $user->sendEmailVerificationNotification(); - Auth::login($user, true); redirect(route('home')); @@ -45,6 +36,6 @@ class Register extends Component public function render() { - return view('livewire.auth.register'); + return view('livewire.auth.register.individual'); } } diff --git a/app/Listeners/SendUserPasswordEmail.php b/app/Listeners/SendUserPasswordEmail.php new file mode 100644 index 0000000..3204358 --- /dev/null +++ b/app/Listeners/SendUserPasswordEmail.php @@ -0,0 +1,37 @@ +user->password = Hash::make($password); + Mail::to($event->user)->send(new UserPassword($event->user, $password)); + } +} diff --git a/app/Mail/UserPassword.php b/app/Mail/UserPassword.php new file mode 100644 index 0000000..86999f3 --- /dev/null +++ b/app/Mail/UserPassword.php @@ -0,0 +1,50 @@ +user = $user; + $this->password = $password; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->subject(__('Seu cadastro em ') . config('app.name')) + ->markdown('emails.users.password'); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 723a290..785a23f 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Events\UserCreating; +use App\Listeners\SendUserPasswordEmail; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -15,8 +17,11 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ + UserCreating::class => [ + SendUserPasswordEmail::class, + ], Registered::class => [ - SendEmailVerificationNotification::class, + // SendEmailVerificationNotification::class, ], ]; diff --git a/app/User.php b/app/User.php index e79dab7..6c71516 100644 --- a/app/User.php +++ b/app/User.php @@ -2,6 +2,7 @@ namespace App; +use App\Events\UserCreating; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -16,7 +17,7 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'name', 'email', 'password', + 'name', 'email', 'password', 'category_id', ]; /** @@ -36,4 +37,37 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + /** + * The event map for the model. + * + * @var array + */ + protected $dispatchesEvents = [ + 'creating' => UserCreating::class, + ]; + + // + // + // ===================================================================== + // RELATIONSHIPS + // ===================================================================== + // + // + + /** + * Get the user's category + */ + public function category() + { + return $this->belongsTo(UserCategory::class); + } + + // + // + // ===================================================================== + // OTHER METHODS + // ===================================================================== + // + // } diff --git a/app/UserCategory.php b/app/UserCategory.php new file mode 100644 index 0000000..39b0b81 --- /dev/null +++ b/app/UserCategory.php @@ -0,0 +1,30 @@ + 'individual', + 'name' => 'Associado Indivíduo', + ], + [ + 'key' => 'company', + 'name' => 'Associada Empresa Apoiadora', + ], + [ + 'key' => 'acting-institution', + 'name' => 'Associada Instituição Atuante', + ], + [ + 'key' => 'supporting-institution', + 'name' => 'Associada Instituição Apoiadora', + ], + ]; +} diff --git a/composer.json b/composer.json index 68358b0..17b8f22 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "license": "MIT", "require": { "php": "^7.2.5", + "calebporzio/sushi": "^2.0", "fideloper/proxy": "^4.2", "fruitcake/laravel-cors": "^1.0", "guzzlehttp/guzzle": "^6.3", diff --git a/composer.lock b/composer.lock index d28b720..ee0af9c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5f1d4ac11559c3a277baf833b1777cea", + "content-hash": "61a6d19307e7210b7b3f5262d1c1cdb4", "packages": [ { "name": "asm89/stack-cors", @@ -104,6 +104,50 @@ ], "time": "2020-04-15T15:59:35+00:00" }, + { + "name": "calebporzio/sushi", + "version": "v2.0.0", + "source": { + "type": "git", + "url": "https://github.com/calebporzio/sushi.git", + "reference": "df69a1af1fbbc0aed96f6563770f8d79fe0eb6a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/calebporzio/sushi/zipball/df69a1af1fbbc0aed96f6563770f8d79fe0eb6a5", + "reference": "df69a1af1fbbc0aed96f6563770f8d79fe0eb6a5", + "shasum": "" + }, + "require": { + "illuminate/database": "^5.8 || ^6.0 || ^7.0", + "illuminate/support": "^5.8 || ^6.0 || ^7.0", + "php": "^7.1.3" + }, + "require-dev": { + "doctrine/dbal": "^2.10", + "orchestra/database": "3.8.* || 3.9.* || ^4.0", + "orchestra/testbench": "3.8.* || 3.9.* || ^4.0", + "phpunit/phpunit": "^7.5 || ^8.4 || ^9.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Sushi\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Caleb Porzio", + "email": "calebporzio@gmail.com" + } + ], + "description": "Eloquent's missing \"array\" driver.", + "time": "2020-03-04T02:49:53+00:00" + }, { "name": "dnoegel/php-xdg-base-dir", "version": "v0.1.1", diff --git a/config/app.php b/config/app.php index 8409e00..ff3b6f1 100644 --- a/config/app.php +++ b/config/app.php @@ -80,7 +80,7 @@ return [ | */ - 'locale' => 'en', + 'locale' => 'pt-BR', /* |-------------------------------------------------------------------------- @@ -106,7 +106,7 @@ return [ | */ - 'faker_locale' => 'en_US', + 'faker_locale' => 'pt_BR', /* |-------------------------------------------------------------------------- diff --git a/config/auth.php b/config/auth.php index aaf982b..3a54923 100644 --- a/config/auth.php +++ b/config/auth.php @@ -114,4 +114,21 @@ return [ 'password_timeout' => 10800, + /* + |-------------------------------------------------------------------------- + | Password requirements + |-------------------------------------------------------------------------- + | + | This array specifies the password requirements such as length, special + | characters, etc. + | + */ + + 'password_requirements' => [ + 'users' => [ + 'min_length' => 10, + 'max_length' => 255, + ], + ], + ]; diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 741edea..904e625 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -3,6 +3,7 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\User; +use App\UserCategory; use Faker\Generator as Faker; use Illuminate\Support\Str; @@ -21,8 +22,8 @@ $factory->define(User::class, function (Faker $faker) { return [ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, - 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'category_id' => UserCategory::where('key', 'individual')->first()->id, 'remember_token' => Str::random(10), ]; }); diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 621a24e..840ae45 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -19,6 +19,20 @@ class CreateUsersTable extends Migration $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); + + // User profile + $table->foreignId('category_id'); +// $table->foreignId('nature_id'); +// $table->foreignId('type_id'); +// $table->string('avatar')->nullable(); +// $table->date('birthday'); +// $table->json('document')->nullable(); +// $table->json('address'); +// $table->string('phone'); +// $table->string('discussion'); +// $table->integer('donation')->nullable(); +// $table->json('profile'); + $table->rememberToken(); $table->timestamps(); }); diff --git a/resources/lang/pt-BR/auth.php b/resources/lang/pt-BR/auth.php index c6cb01d..4548952 100644 --- a/resources/lang/pt-BR/auth.php +++ b/resources/lang/pt-BR/auth.php @@ -15,5 +15,7 @@ return [ 'failed' => 'Credenciais informadas não correspondem com nossos registros.', 'throttle' => 'Você realizou muitas tentativas de login. Por favor, tente novamente em :seconds segundos.', + 'login' => 'Entre na sua conta', + 'register' => 'Cadastrar', ]; diff --git a/resources/lang/pt-BR/user_categories.php b/resources/lang/pt-BR/user_categories.php new file mode 100644 index 0000000..4b69a42 --- /dev/null +++ b/resources/lang/pt-BR/user_categories.php @@ -0,0 +1,5 @@ + 'Associado Indivíduo', +]; diff --git a/resources/lang/pt-BR/users.php b/resources/lang/pt-BR/users.php new file mode 100644 index 0000000..5b6a2f1 --- /dev/null +++ b/resources/lang/pt-BR/users.php @@ -0,0 +1,8 @@ + [ + 'name' => 'Nome', + 'email' => 'E-mail', + ], +]; diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php deleted file mode 100644 index b2e06ba..0000000 --- a/resources/views/auth/register.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -@extends('layouts.auth') -@section('title', 'Create a new account') - -@section('content') -
- @livewire('auth.register') -
-@endsection diff --git a/resources/views/auth/register/individual.blade.php b/resources/views/auth/register/individual.blade.php new file mode 100644 index 0000000..ef56704 --- /dev/null +++ b/resources/views/auth/register/individual.blade.php @@ -0,0 +1,8 @@ +@extends('layouts.auth') +@section('title', __('user_categories.individual')) + +@section('content') +
+ @livewire('auth.register.individual') +
+@endsection diff --git a/resources/views/emails/users/password.blade.php b/resources/views/emails/users/password.blade.php new file mode 100644 index 0000000..8c0cd15 --- /dev/null +++ b/resources/views/emails/users/password.blade.php @@ -0,0 +1,23 @@ +@component('mail::message') + +{{ __('Olá') }}! + +{{ __('Você acabou de se registrar em ') }} {{ config('app.name', 'Laravel') }}. {{ __('Sua senha é') }}: + +@component('mail::panel') +**{{ $password }}** +@endcomponent + +@component('mail::button', ['url' => route('login') ]) +{{ __('Fazer login') }} +@endcomponent + +{{ __('Obrigado,') }} + +{{ __('Equipe') }} {{ config('app.name') }} + +@component('mail::footer') +{{ __('Se o botão acima não funcionar, copie e cole a URL a seguir em seu navegador:') }} {{ route('login') }} +@endcomponent + +@endcomponent diff --git a/resources/views/livewire/auth/register.blade.php b/resources/views/livewire/auth/register/individual.blade.php similarity index 60% rename from resources/views/livewire/auth/register.blade.php rename to resources/views/livewire/auth/register/individual.blade.php index 006f0d3..e4c4853 100644 --- a/resources/views/livewire/auth/register.blade.php +++ b/resources/views/livewire/auth/register/individual.blade.php @@ -5,13 +5,13 @@

- Create a new account + {{ __('user_categories.individual') }}

- Or - - sign in to your account + Ou + + {{ __('auth.login') }}

@@ -21,7 +21,7 @@
@@ -35,7 +35,7 @@
@@ -47,34 +47,10 @@ @enderror
-
- - -
- -
- - @error('password') -

{{ $message }}

- @enderror -
- -
- - -
- -
-
-
diff --git a/routes/web.php b/routes/web.php index 003669c..6208a22 100644 --- a/routes/web.php +++ b/routes/web.php @@ -17,7 +17,8 @@ Route::view('/', 'welcome')->name('home'); Route::middleware('guest')->group(function () { Route::view('login', 'auth.login')->name('login'); - Route::view('register', 'auth.register')->name('register'); + Route::redirect('register', 'register/individual')->name('register'); + Route::view('register/individual', 'auth.register.individual')->name('register.individual'); }); Route::view('password/reset', 'auth.passwords.email')->name('password.request'); diff --git a/tests/Feature/Auth/RegisterTest.php b/tests/Feature/Auth/Register/IndividualTest.php similarity index 60% rename from tests/Feature/Auth/RegisterTest.php rename to tests/Feature/Auth/Register/IndividualTest.php index 3f5843c..0c24de7 100644 --- a/tests/Feature/Auth/RegisterTest.php +++ b/tests/Feature/Auth/Register/IndividualTest.php @@ -1,6 +1,6 @@ get(route('register')) + $this->get(route('register.individual')) ->assertSuccessful() - ->assertSeeLivewire('auth.register'); + ->assertSeeLivewire('auth.register.individual'); } /** @test */ @@ -29,18 +29,16 @@ class RegisterTest extends TestCase $this->be($user); - $this->get(route('register')) + $this->get(route('register.individual')) ->assertRedirect(route('home')); } /** @test */ function a_user_can_register() { - Livewire::test('auth.register') + Livewire::test('auth.register.individual') ->set('name', 'Tall Stack') ->set('email', 'tallstack@example.com') - ->set('password', 'password') - ->set('passwordConfirmation', 'password') ->call('register') ->assertRedirect(route('home')); @@ -51,7 +49,7 @@ class RegisterTest extends TestCase /** @test */ function name_is_required() { - Livewire::test('auth.register') + Livewire::test('auth.register.individual') ->set('name', '') ->call('register') ->assertHasErrors(['email' => 'required']); @@ -60,7 +58,7 @@ class RegisterTest extends TestCase /** @test */ function email_is_required() { - Livewire::test('auth.register') + Livewire::test('auth.register.individual') ->set('email', '') ->call('register') ->assertHasErrors(['email' => 'required']); @@ -69,7 +67,7 @@ class RegisterTest extends TestCase /** @test */ function email_is_valid_email() { - Livewire::test('auth.register') + Livewire::test('auth.register.individual') ->set('email', 'tallstack') ->call('register') ->assertHasErrors(['email' => 'email']); @@ -80,7 +78,7 @@ class RegisterTest extends TestCase { factory(User::class)->create(['email' => 'tallstack@example.com']); - Livewire::test('auth.register') + Livewire::test('auth.register.individual') ->set('email', 'tallstack@example.com') ->call('register') ->assertHasErrors(['email' => 'unique']); @@ -91,42 +89,11 @@ class RegisterTest extends TestCase { factory(User::class)->create(['email' => 'tallstack@example.com']); - Livewire::test('auth.register') + Livewire::test('auth.register.individual') ->set('email', 'smallstack@gmail.com') ->assertHasNoErrors() ->set('email', 'tallstack@example.com') ->call('register') ->assertHasErrors(['email' => 'unique']); } - - /** @test */ - function password_is_required() - { - Livewire::test('auth.register') - ->set('password', '') - ->set('passwordConfirmation', 'password') - ->call('register') - ->assertHasErrors(['password' => 'required']); - } - - /** @test */ - function password_is_minimum_of_eight_characters() - { - Livewire::test('auth.register') - ->set('password', 'secret') - ->set('passwordConfirmation', 'secret') - ->call('register') - ->assertHasErrors(['password' => 'min']); - } - - /** @test */ - function password_matches_password_confirmation() - { - Livewire::test('auth.register') - ->set('email', 'tallstack@example.com') - ->set('password', 'password') - ->set('passwordConfirmation', 'not-password') - ->call('register') - ->assertHasErrors(['password' => 'same']); - } }