Skip to content

Commit

Permalink
- Changed password validation to use Password rule
Browse files Browse the repository at this point in the history
- Fixed register controller
  • Loading branch information
dash8x committed May 8, 2024
1 parent 20daf89 commit 68ab148
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 82 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/ConfirmPasswordContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

interface ConfirmPasswordContract
{
public function applyMiddlewares(): void;

public function getConfirmForm(): View;

public function determinePathForRedirectUsing(): \Javaabu\Auth\User;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/ForgotPasswordContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

interface ForgotPasswordContract
{
public function applyMiddlewares(): void;

public function getBroker(): PasswordBroker;

public function getPasswordResetForm(): View;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/LoginContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

interface LoginContract
{
public function applyMiddlewares(): void;

public function getGuard(): StatefulGuard;

public function getLoginForm(): View;
Expand Down
9 changes: 8 additions & 1 deletion src/Contracts/RegisterContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Javaabu\Auth\Contracts;

use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\View\View;

interface RegisterContract
{
public function showRegistrationForm(): View;
public function applyMiddlewares(): void;

public function getGuard(): StatefulGuard;

public function showRegistrationForm();

public function determinePathForRedirectUsing(): \Javaabu\Auth\User;

public function userClass(): string;
}
2 changes: 2 additions & 0 deletions src/Contracts/UpdatePasswordContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

interface UpdatePasswordContract
{
public function applyMiddlewares(): void;

public function getGuard(): StatefulGuard;

public function getBroker(): PasswordBroker;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/VerificationContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

interface VerificationContract
{
public function applyMiddlewares(): void;

public function getEmailVerificationView();

public function getVerificationResultView();
Expand Down
39 changes: 13 additions & 26 deletions src/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\View\View;
use Illuminate\Validation\Rules\Password;
use Javaabu\Auth\Contracts\RegisterContract;
use Javaabu\Auth\Http\Controllers\AuthBaseController;

Expand Down Expand Up @@ -40,30 +40,26 @@ protected function validator(array $data)
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'password' => ['required', 'string', Password::min(8), 'confirmed'],
]);
}

/**
* Create a new user instance after a valid registration.
*
* @return User
*/
protected function create(array $data)
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
$class = $this->userClass();

/**
* Display the registration form
*/
public function showRegistrationForm(): View
{
return view('admin.auth.register');
$user = new $class();

$user->name = $data['name'];
$user->email = $data['email'];
$user->password = $data['password'];

$user->save();

return $user;
}

/**
Expand All @@ -75,13 +71,4 @@ public function redirectPath()
{
return with($this->determinePathForRedirectUsing())->homeUrl();
}

/**
* Apply middlewares for the controller. Used in the constructor.
* Helps with applying/changing applied middlewares for the controller.
*/
public function applyMiddlewares(): void
{
$this->middleware('guest:web_admin');
}
}
41 changes: 23 additions & 18 deletions src/Http/Requests/UsersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Javaabu\Auth\Enums\UserStatuses;
use Javaabu\Auth\User;
use Javaabu\Helpers\Media\AllowedMimeTypes;
Expand Down Expand Up @@ -45,51 +46,55 @@ protected function getUserBeingEdited(): ?User
protected function baseRules(bool $password_required = true, bool $email_required = true): array
{
$rules = [
'email' => 'string|email|max:255|unique:'.$this->tableName(),
'name' => 'string|max:255',
'email_verified' => 'nullable|boolean',
'password' => 'string|min:8|confirmed',
'email' => ['string', 'email', 'max:255'],
'name' => ['string', 'max:255'],
'email_verified' => ['nullable', 'boolean'],
'password' => ['string', Password::min(8), 'confirmed'],
'status' => [Rule::in(UserStatuses::getKeys())],
'avatar' => AllowedMimeTypes::getValidationRule('image'),
'action' => 'in:approve,ban,mark_pending,update_password',
'require_password_update' => 'boolean',
'action' => ['in:approve,ban,mark_pending,update_password'],
'require_password_update' => ['boolean'],
];

$unique_email = Rule::unique($this->tableName(), 'email');

if (! $email_required) {
$rules['email'] .= '|nullable';
$rules['email'][] = 'nullable';
}

// updating
if ($user = $this->getUserBeingEdited()) {
$rules['email'] .= ',email,'.$user->id;
$rules['password'] .= '|required_if:action,update_password';
$unique_email->ignore($user->id);
$rules['password'][] = 'required_if:action,update_password';

// current password required
if ($this->editingCurrentUser() && $user->password) {
$rules['current_password'] = 'required_with:password|passcheck:'.$this->tableName().
',password,id,'.$user->id;
$rules['current_password'] = [
'required_with:password',
'passcheck:'.$this->tableName().',password,id,'.$user->id
];
}
} else { // creation
if ($password_required) {
$rules['password'] .= '|required';
$rules['password'][] = 'required';
} else {
$rules['password'] .= '|nullable';
$rules['password'][] = 'nullable';
}

if ($email_required) {
$rules['email'] .= '|required';
$rules['email'][] = 'required';
}

$rules['name'] .= '|required';
$rules['name'][] = 'required';
}

$rules['email'][] = $unique_email;

return $rules;
}

public function rules(): array
{
$rules = $this->baseRules();

return $rules;
return $this->baseRules();
}
}
9 changes: 9 additions & 0 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ abstract class User extends Authenticatable implements AdminModel, HasMedia, Mus
'email',
];

/**
* The default values
*
* @var array
*/
protected $attributes = [
'status' => UserStatuses::PENDING,
];

/**
* Get the attributes that should be cast.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/Http/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Javaabu\Auth\Tests\Feature\Http\Controllers;

use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Javaabu\Auth\Http\Controllers\Auth\RegisterController as JavaabuRegisterController;
use Javaabu\Auth\User;

class RegisterController extends JavaabuRegisterController
{
public function applyMiddlewares(): void
{
$this->middleware('guest:web');
}

public function getGuard(): StatefulGuard
{
return Auth::guard('web');
}

/**
* Display the registration form
*/
public function showRegistrationForm(): View
{
return view('register');
}

public function getLoginForm(): View
{
return view('login');
}

public function determinePathForRedirectUsing(): User
{
return new \Javaabu\Auth\Tests\Feature\Models\User();
}

public function userClass(): string
{
return \Javaabu\Auth\Tests\Feature\Models\User::class;
}
}
60 changes: 23 additions & 37 deletions tests/Feature/RegisterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Javaabu\Auth\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;
use Javaabu\Auth\Notifications\VerifyEmail;
use Javaabu\Auth\Tests\Feature\Models\User;
use Javaabu\Auth\Tests\InteractsWithDatabase;
use Javaabu\Auth\Tests\TestCase;

Expand All @@ -20,66 +24,48 @@ public function setUp(): void
}

/** @test */
public function it_wont_display_the_user_registration_page()
public function it_can_display_the_user_registration_page()
{
$this->get('/register')
->assertStatus(404)
->assertDontSee('Register');
}

/** @test */
public function it_wont_register_a_user()
{
$this->post('/register', [
'name' => 'User',
'email' => 'user@javaabu.com',
'password' => 'Jv7528222',
'password_confirmation' => 'Jv7528222',
])
->assertStatus(404);

$this->assertDatabaseMissing('users', [
'name' => 'User',
'email' => 'user@javaabu.com',
]);
}
$this->withoutExceptionHandling();

/** @test */
/*public function it_can_display_the_user_registration_page()
{
$this->get('/register')
->assertStatus(200)
->assertSee('Register');
}*/
}

/** @test */
/*public function it_can_register_a_user()
public function it_can_register_a_user()
{
$this->withoutExceptionHandling();

$this->post('/register', [
'name' => 'User',
'email' => 'user@javaabu.com',
'password' => 'Jv7528222',
'password_confirmation' => 'Jv7528222'
'password' => 'TestPass123',
'password_confirmation' => 'TestPass123'
])
->assertSessionMissing('errors')
->assertRedirect('');

$user = User::whereEmail('user@javaabu.com')->first();

$this->assertDatabaseHas('users', [
'id' => $user->id,
'name' => 'User',
'email' => 'user@javaabu.com',
'status' => UserStatuses::UNVERIFIED,
'email_verified_at' => null,
]);

$user = User::first();
$this->assertEquals($user->id, Auth::guard('web')->id());

Notification::assertSentTo(
[$user],
EmailVerification::class
VerifyEmail::class
);
}*/
}

/** @test */
/*public function it_can_validate_the_registration_inputs()
public function it_can_validate_the_registration_inputs()
{
$this->post('/register', [
'name' => 'User',
Expand All @@ -93,13 +79,13 @@ public function it_wont_register_a_user()
$this->post('/register', [
'name' => '',
'email' => '',
'password' => 'Jv7528222',
'password_confirmation' => 'Jv7528222',
'password' => 'TestPass123',
'password_confirmation' => 'TestPass123',
])
->assertSessionHasErrors('name', 'email');

$this->assertDatabaseMissing('users', [
'name' => '',
]);
}*/
}
}
1 change: 1 addition & 0 deletions tests/Feature/views/register.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Register

0 comments on commit 68ab148

Please sign in to comment.