Skip to content

Commit

Permalink
Merge pull request #61 from ManiruzzamanAkash/akash/change/refactor-r…
Browse files Browse the repository at this point in the history
…equest-files

Refactor Request files for better validation and improve some UX
  • Loading branch information
ManiruzzamanAkash authored Aug 13, 2024
2 parents e0eb83f + 7efe7f5 commit 2278a5c
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 87 deletions.
46 changes: 13 additions & 33 deletions app/Http/Controllers/Backend/AdminsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\AdminRequest;
use App\Models\Admin;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

Expand All @@ -18,31 +18,24 @@ public function index(): Renderable
{
$this->checkAuthorization(auth()->user(), ['admin.view']);

$admins = Admin::all();
return view('backend.pages.admins.index', compact('admins'));
return view('backend.pages.admins.index', [
'admins' => Admin::all(),
]);
}

public function create(): Renderable
{
$this->checkAuthorization(auth()->user(), ['admin.create']);

$roles = Role::all();
return view('backend.pages.admins.create', compact('roles'));
return view('backend.pages.admins.create', [
'roles' => Role::all(),
]);
}

public function store(Request $request): RedirectResponse
public function store(AdminRequest $request): RedirectResponse
{
$this->checkAuthorization(auth()->user(), ['admin.create']);

// Validation Data.
$request->validate([
'name' => 'required|max:50',
'email' => 'required|max:100|email|unique:admins',
'username' => 'required|max:100|unique:admins',
'password' => 'required|min:6|confirmed',
]);

// Create New Admin.
$admin = new Admin();
$admin->name = $request->name;
$admin->username = $request->username;
Expand All @@ -62,26 +55,18 @@ public function edit(int $id): Renderable
{
$this->checkAuthorization(auth()->user(), ['admin.edit']);

$admin = Admin::findOrFail($id);
return view('backend.pages.admins.edit', [
'admin' => Admin::find($id),
'admin' => $admin,
'roles' => Role::all(),
]);
}

public function update(Request $request, int $id): RedirectResponse
public function update(AdminRequest $request, int $id): RedirectResponse
{
$this->checkAuthorization(auth()->user(), ['admin.edit']);

// Create New Admin.
$admin = Admin::find($id);

// Validation Data.
$request->validate([
'name' => 'required|max:50',
'email' => 'required|max:100|email|unique:admins,email,' . $id,
'password' => 'nullable|min:6|confirmed',
]);

$admin = Admin::findOrFail($id);
$admin->name = $request->name;
$admin->email = $request->email;
$admin->username = $request->username;
Expand All @@ -103,12 +88,7 @@ public function destroy(int $id): RedirectResponse
{
$this->checkAuthorization(auth()->user(), ['admin.delete']);

$admin = Admin::find($id);
if (!$admin) {
session()->flash('error', 'Admin not found.');
return back();
}

$admin = Admin::findOrFail($id);
$admin->delete();
session()->flash('success', 'Admin has been deleted.');
return back();
Expand Down
36 changes: 16 additions & 20 deletions app/Http/Controllers/Backend/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use App\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\RoleRequest;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

Expand All @@ -33,17 +33,10 @@ public function create(): Renderable
]);
}

public function store(Request $request): RedirectResponse
public function store(RoleRequest $request): RedirectResponse
{
$this->checkAuthorization(auth()->user(), ['role.create']);

// Validation Data.
$request->validate([
'name' => 'required|max:100|unique:roles'
], [
'name.requried' => 'Please give a role name'
]);

// Process Data.
$role = Role::create(['name' => $request->name, 'guard_name' => 'admin']);

Expand All @@ -58,31 +51,34 @@ public function store(Request $request): RedirectResponse
return redirect()->route('admin.roles.index');
}

public function edit(int $id): Renderable
public function edit(int $id): Renderable|RedirectResponse
{
$this->checkAuthorization(auth()->user(), ['role.edit']);

$role = Role::findById($id, 'admin');
if (!$role) {
session()->flash('error', 'Role not found.');
return back();
}

return view('backend.pages.roles.edit', [
'role' => Role::findById($id, 'admin'),
'role' => $role,
'all_permissions' => Permission::all(),
'permission_groups' => User::getpermissionGroups(),
]);
}

public function update(Request $request, int $id): RedirectResponse
public function update(RoleRequest $request, int $id): RedirectResponse
{
$this->checkAuthorization(auth()->user(), ['role.edit']);

// Validation Data
$request->validate([
'name' => 'required|max:100|unique:roles,name,' . $id
], [
'name.requried' => 'Please give a role name'
]);

$role = Role::findById($id, 'admin');
$permissions = $request->input('permissions');
if (!$role) {
session()->flash('error', 'Role not found.');
return back();
}

$permissions = $request->input('permissions');
if (!empty($permissions)) {
$role->name = $request->name;
$role->save();
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Requests/AdminRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AdminRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$adminId = $this->route('admin');

return [
'name' => 'required|max:50',
'email' => 'required|max:100|email|unique:admins,email,' . $adminId,
'username' => 'required|max:100|unique:admins,username,' . $adminId,
'password' => $adminId ? 'nullable|min:6|confirmed' : 'required|min:6|confirmed',
];
}
}
34 changes: 34 additions & 0 deletions app/Http/Requests/RoleRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RoleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$roleId = $this->route('role');

return [
'name' => 'required|max:100|unique:roles,name,' . $roleId,
'permissions' => 'required|array|min:1',
'permissions.*' => 'string|exists:permissions,name',
];
}
}
17 changes: 9 additions & 8 deletions resources/views/backend/pages/admins/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,50 @@
<div class="col-12 mt-5">
<div class="card">
<div class="card-body">
<h4 class="header-title">Create New Role</h4>
<h4 class="header-title">Create New Admin</h4>
@include('backend.layouts.partials.messages')

<form action="{{ route('admin.admins.store') }}" method="POST">
@csrf
<div class="form-row">
<div class="form-group col-md-6 col-sm-12">
<label for="name">Admin Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Name">
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Name" required autofocus value="{{ old('name') }}">
</div>
<div class="form-group col-md-6 col-sm-12">
<label for="email">Admin Email</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Enter Email">
<input type="text" class="form-control" id="email" name="email" placeholder="Enter Email" required value="{{ old('email') }}">
</div>
</div>

<div class="form-row">
<div class="form-group col-md-6 col-sm-12">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
</div>
<div class="form-group col-md-6 col-sm-12">
<label for="password_confirmation">Confirm Password</label>
<input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Enter Password">
<input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Enter Password" required>
</div>
</div>

<div class="form-row">
<div class="form-group col-md-6 col-sm-6">
<label for="password">Assign Roles</label>
<select name="roles[]" id="roles" class="form-control select2" multiple>
<select name="roles[]" id="roles" class="form-control select2" multiple required>
@foreach ($roles as $role)
<option value="{{ $role->name }}">{{ $role->name }}</option>
@endforeach
</select>
</div>
<div class="form-group col-md-6 col-sm-6">
<label for="username">Admin Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Enter Username" required>
<input type="text" class="form-control" id="username" name="username" placeholder="Enter Username" required value="{{ old('username') }}">
</div>
</div>

<button type="submit" class="btn btn-primary mt-4 pr-4 pl-4">Save Admin</button>
<button type="submit" class="btn btn-primary mt-4 pr-4 pl-4">Save</button>
<a href="{{ route('admin.admins.index') }}" class="btn btn-secondary mt-4 pr-4 pl-4">Cancel</a>
</form>
</div>
</div>
Expand Down
13 changes: 7 additions & 6 deletions resources/views/backend/pages/admins/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,29 @@
<div class="form-row">
<div class="form-group col-md-6 col-sm-12">
<label for="name">Admin Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Name" value="{{ $admin->name }}">
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Name" value="{{ $admin->name }}" required autofocus>
</div>
<div class="form-group col-md-6 col-sm-12">
<label for="email">Admin Email</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Enter Email" value="{{ $admin->email }}">
<input type="text" class="form-control" id="email" name="email" placeholder="Enter Email" value="{{ $admin->email }}" required>
</div>
</div>

<div class="form-row">
<div class="form-group col-md-6 col-sm-12">
<label for="password">Password</label>
<label for="password">Password (Optional)</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter Password">
</div>
<div class="form-group col-md-6 col-sm-12">
<label for="password_confirmation">Confirm Password</label>
<label for="password_confirmation">Confirm Password (Optional)</label>
<input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Enter Password">
</div>
</div>

<div class="form-row">
<div class="form-group col-md-6 col-sm-6">
<label for="password">Assign Roles</label>
<select name="roles[]" id="roles" class="form-control select2" multiple>
<select name="roles[]" id="roles" class="form-control select2" multiple required>
@foreach ($roles as $role)
<option value="{{ $role->name }}" {{ $admin->hasRole($role->name) ? 'selected' : '' }}>{{ $role->name }}</option>
@endforeach
Expand All @@ -86,7 +86,8 @@
</div>
</div>

<button type="submit" class="btn btn-primary mt-4 pr-4 pl-4">Save Admin</button>
<button type="submit" class="btn btn-primary mt-4 pr-4 pl-4">Save</button>
<a href="{{ route('admin.admins.index') }}" class="btn btn-secondary mt-4 pr-4 pl-4">Cancel</a>
</form>
</div>
</div>
Expand Down
23 changes: 13 additions & 10 deletions resources/views/backend/pages/roles/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@
<div class="col-12 mt-5">
<div class="card">
<div class="card-body">
<h4 class="header-title">Create New Role</h4>
@include('backend.layouts.partials.messages')

<form action="{{ route('admin.roles.store') }}" method="POST">
@csrf
<div class="row mb-2">
<div class="col-md-6">
<h4 class="header-title">Create New Role</h4>
</div>
<div class="col-md-6 text-right">
<button type="submit" class="btn btn-primary pr-4 pl-4">Save</button>
</div>
</div>
@include('backend.layouts.partials.messages')
<div class="form-group">
<label for="name">Role Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter a Role Name">
<input type="text" class="form-control" id="name" name="name" placeholder="Enter a Role Name" required autofocus value="{{ old('name') }}">
</div>

<div class="form-group">
Expand Down Expand Up @@ -84,16 +90,13 @@
@endforeach
<br>
</div>

</div>
@php $i++; @endphp
@endforeach


</div>

<button type="submit" class="btn btn-primary mt-4 pr-4 pl-4">Save Role</button>

<button type="submit" class="btn btn-primary mt-4 pr-4 pl-4">Save</button>
<a href="{{ route('admin.admins.index') }}" class="btn btn-secondary mt-4 pr-4 pl-4">Cancel</a>
</form>
</div>
</div>
Expand Down
Loading

0 comments on commit 2278a5c

Please sign in to comment.