Skip to content

Commit

Permalink
- Add feature verify user
Browse files Browse the repository at this point in the history
  • Loading branch information
ianriizky committed Feb 10, 2022
1 parent be48cef commit dccddf1
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function store(RegisterRequest $request)

Auth::login($user);

return redirect(RouteServiceProvider::HOME);
return redirect()->route('verification-user.notice');
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/Auth/UserVerificationPromptController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;

class UserVerificationPromptController extends Controller
{
/**
* Display the user verification prompt.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public function __invoke(Request $request)
{
return $request->user()->hasVerifiedUser()
? redirect()->intended(RouteServiceProvider::HOME)
: view('auth.verify-user');
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'user_is_verified' => \App\Http\Middleware\RedirectIfUnverified::class,
];
}
31 changes: 31 additions & 0 deletions app/Http/Middleware/RedirectIfUnverified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RedirectIfUnverified
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param array ...$guards
* @return mixed
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check() && !Auth::user()->hasVerifiedUser()) {
return redirect()->intended(route('verification-user.notice'));
}
}

return $next($request);
}
}
4 changes: 2 additions & 2 deletions app/Http/Requests/Auth/RegisterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function authorize()
*/
public function rules()
{
return array_merge(Arr::except(parent::rules(), 'role'), [
return array_merge(Arr::except(parent::rules(), ['role', 'is_verified']), [
'agree_with_terms' => 'required|boolean|in:1',
]);
}
Expand All @@ -32,7 +32,7 @@ public function rules()
*/
public function attributes()
{
return array_merge(Arr::except(parent::attributes(), 'role'), [
return array_merge(Arr::except(parent::attributes(), ['role', 'is_verified']), [
'agree_with_terms' => trans('Terms of Service'),
]);
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/User/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function attributes()
'name' => trans('Name'),
'email' => trans('Email'),
'role' => trans('Role'),
'is_verified' => trans('Verified'),
];
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/User/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function rules()
'email' => ['required', 'string', 'email', 'max:255', Rule::unique(User::class)],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'role' => ['required', Rule::exists(Role::class, 'name')],
'is_verified' => ['required', 'boolean'],
];
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/User/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function rules()
'email' => ['required', 'string', 'email', 'max:255', Rule::unique(User::class)->ignoreModel($this->route('user'))],
'password' => ['sometimes', 'nullable', 'confirmed', Rules\Password::defaults()],
'role' => ['required', Rule::exists(Role::class, 'name')],
'is_verified' => ['required', 'boolean'],
];
}

Expand Down
20 changes: 20 additions & 0 deletions app/Infrastructure/Contracts/Auth/MustVerifyUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Infrastructure\Contracts\Auth;

interface MustVerifyUser
{
/**
* Determine if the user has verified their account.
*
* @return bool
*/
public function hasVerifiedUser(): bool;

/**
* Mark the given user's email as verified.
*
* @return bool
*/
public function markUserAsVerified(): bool;
}
20 changes: 20 additions & 0 deletions app/Models/Concerns/User/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
* @property string $email
* @property \Illuminate\Support\Carbon $email_verified_at
* @property string $password
* @property boolean $is_verified
* @property-read string $remember_token
* @property-read string $role
* @property-read string $profile_image
* @property-read string $is_verified_badge
*
* @see \App\Models\User
*/
Expand Down Expand Up @@ -52,4 +54,22 @@ public function getProfileImageAttribute(): string
{
return gravatar_image($this->email);
}

/**
* Return "is_verified_badge" attribute value.
*
* @return string
*/
public function getIsActiveBadgeAttribute(): string
{
return sprintf(<<<'html'
<span class="badge badge-%s">
<i class="fa fa-%s"></i> %s
</span>
html,
$this->is_verified ? 'success' : 'danger',
$this->is_verified ? 'check-circle' : 'times-circle',
$this->is_verified ? trans('Active') : trans('Not Active')
);
}
}
21 changes: 20 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Infrastructure\Contracts\Auth\MustVerifyUser;
use App\Infrastructure\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -12,7 +13,7 @@
/**
* @method static \Database\Factories\UserFactory<static> factory(callable|array|int|null $count = null, callable|array $state = []) Get a new factory instance for the model.
*/
class User extends Authenticatable implements MustVerifyEmail
class User extends Authenticatable implements MustVerifyEmail, MustVerifyUser
{
use HasApiTokens, HasFactory, Notifiable,
Concerns\User\Attribute,
Expand All @@ -27,6 +28,7 @@ class User extends Authenticatable implements MustVerifyEmail
'name',
'email',
'password',
'is_verified',
];

/**
Expand All @@ -42,6 +44,7 @@ class User extends Authenticatable implements MustVerifyEmail
*/
protected $casts = [
'email_verified_at' => 'datetime',
'is_verified' => 'boolean',
];

/**
Expand Down Expand Up @@ -76,4 +79,20 @@ public function bypassEmailVerification(Carbon $date = null)

return $this;
}

/**
* {@inheritDoc}
*/
public function hasVerifiedUser(): bool
{
return $this->is_verified;
}

/**
* {@inheritDoc}
*/
public function markUserAsVerified(): bool
{
return $this->forceFill(['is_verified' => true])->save();
}
}
19 changes: 18 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,36 @@ public function definition()
'email_verified_at' => Carbon::now(),
'password' => 'password',
'remember_token' => Str::random(10),
'is_verified' => $this->faker->boolean(),
];
}

/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
* @return static
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
'is_verified' => false,
];
});
}

/**
* Indicate that the model's email address should be verified.
*
* @return static
*/
public function verified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => Carbon::now(),
'is_verified' => true,
];
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function up()
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->boolean('is_verified')->default(false);

$table->timestamps();
});
Expand Down
2 changes: 2 additions & 0 deletions database/seeders/BranchUserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ protected function generateAdmin(string $branchName): array
'email' => env('ADMIN_EMAIL', 'admin@admin.com'),
'email_verified_at' => Carbon::now(),
'password' => env('ADMIN_PASSWORD', 'admin12345'),
'is_verified' => true,
]),
],
];
Expand All @@ -158,6 +159,7 @@ protected function generateStaff(string $branchName): array
'email' => 'staff@staff.com',
'email_verified_at' => Carbon::now(),
'password' => 'staff12345',
'is_verified' => true,
]),
],
];
Expand Down
6 changes: 5 additions & 1 deletion lang/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -808,5 +808,9 @@
"Number of New CiN": "Jumlah New CiN",
"Start Date": "Tanggal Awal",
"End Date": "Tanggal Akhir",
"Amount": "Jumlah"
"Amount": "Jumlah",
"Verify User": "Verifikasi Pengguna",
"Verified": "Terverifikasi",
"Not Verified": "Tidak Terverifikasi",
"Thanks for signing up! Our administrator will verify your email address before you can start using the application. If your account is still unverified, please contact the administrator.": "Terima kasih telah mendaftar! Administrator kami akan memverifikasi alamat email Anda sebelum Anda dapat mulai menggunakan aplikasi. Jika akun Anda masih belum diverifikasi, harap hubungi administrator."
}
41 changes: 41 additions & 0 deletions resources/views/auth/verify-user.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@extends('layouts.master')

@section('title', __('Verify User'))

@section('content')
<section class="section">
<div class="container mt-5">
<div class="row">
<div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 col-lg-6 offset-lg-3 col-xl-4 offset-xl-4">
<div class="login-brand">
<img src="{{ asset('img/logo.png') }}" alt="logo" width="100" class="shadow-light rounded-circle">
</div>

<div class="card card-primary">
<div class="card-header">
<h4>@lang('Verify User')</h4>
</div>

<div class="card-body">
<p class="text-muted">
@lang('Thanks for signing up! Our administrator will verify your email address before you can start using the application. If your account is still unverified, please contact the administrator.')
</p>

<form method="post">
@csrf

<div class="form-group">
<button type="submit" formaction="{{ route('logout') }}" class="btn btn-danger btn-lg btn-block">
@lang('Logout')
</button>
</div>
</form>
</div>
</div>

@include('components.footer')
</div>
</div>
</div>
</section>
@endsection
30 changes: 30 additions & 0 deletions resources/views/master/user/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,36 @@ class="form-control select2 @error('role') is-invalid @enderror"
<x-invalid-feedback :name="'role'"/>
</div>
{{-- /.role --}}

{{-- is_verified --}}
<div class="form-group col-12 col-lg-6">
<label for="is_verified" class="d-block">@lang('Verified')<span class="text-danger">*</span></label>

<div class="custom-control custom-radio custom-control-inline">
<input type="radio"
name="is_verified"
id="is_verified_true"
value="1"
@if (old('is_verified', $user->is_verified)) checked @endif
class="custom-control-input">

<label class="custom-control-label" for="is_verified_true">@lang('Yes')</label>
</div>

<div class="custom-control custom-radio custom-control-inline">
<input type="radio"
name="is_verified"
id="is_verified_false"
value="0"
@unless (old('is_verified', $user->is_verified)) checked @endunless
class="custom-control-input">

<label class="custom-control-label" for="is_verified_false">@lang('No')</label>
</div>

<x-invalid-feedback :name="'is_verified'"/>
</div>
{{-- /.is_verified --}}
</div>

@if ($user->exists)
Expand Down
7 changes: 6 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\AchievementController;
use App\Http\Controllers\Auth\UserVerificationPromptController;
use App\Http\Controllers\BranchController;
use App\Http\Controllers\EventController;
use App\Http\Controllers\ProfileController;
Expand All @@ -24,7 +25,11 @@

Route::view('/', 'welcome');

Route::middleware('auth')->group(function () {
Route::get('/verify-user', [UserVerificationPromptController::class, '__invoke'])
->middleware('auth')
->name('verification-user.notice');

Route::middleware('auth:web', 'user_is_verified')->group(function () {
Route::view('/dashboard', 'dashboard')->name('dashboard');

Route::prefix('/master')->name('master.')->group(function () {
Expand Down
Loading

0 comments on commit dccddf1

Please sign in to comment.