Implement roles management;

Add user profile;
Improve Ui;
Clean code;
Minor fix;
Typo;
This commit is contained in:
c.girardi
2024-02-21 16:56:08 +01:00
parent fca756b556
commit e6f3fcbb4e
27 changed files with 708 additions and 138 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Console;
use App\Jobs\SendMailJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -12,7 +13,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
//$schedule->job(new SendMailJob(null))->everyTenSeconds();
}
/**

View File

@@ -2,16 +2,33 @@
namespace App\Http\Controllers;
use App\Http\Requests\StoreRoleRequest;
use App\Http\Requests\UpdateRoleRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
class RolesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('permission:create-role|edit-role|delete-role', ['only' => ['index', 'show']]);
$this->middleware('permission:create-role', ['only' => ['create', 'store']]);
$this->middleware('permission:edit-role', ['only' => ['edit', 'update']]);
$this->middleware('permission:delete-role', ['only' => ['destroy']]);
}
/**
* Display a listing of the resource.
*/
public function index()
{
//
return view('roles.index', [
'roles' => Role::orderBy('id', 'DESC')->paginate(3)
]);
}
/**
@@ -19,46 +36,91 @@ class RolesController extends Controller
*/
public function create()
{
//
return view('roles.create', [
'permissions' => Permission::get()
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
public function store(StoreRoleRequest $request)
{
//
$role = Role::create(['name' => $request->name]);
$permissions = Permission::whereIn('id', $request->permissions)->get(['name'])->toArray();
$role->syncPermissions($permissions);
return redirect()->route('roles.index')
->withSuccess('New role is added successfully.');
}
/**
* Display the specified resource.
*/
public function show(string $id)
public function show(Role $role)
{
//
$rolePermissions = Permission::join("role_has_permissions", "permission_id", "=", "id")
->where("role_id", $role->id)
->select('name')
->get();
return view('roles.show', [
'role' => $role,
'rolePermissions' => $rolePermissions
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
public function edit(Role $role)
{
//
if ($role->name == 'ADMIN') {
abort(403, 'ADMIN ROLE CAN NOT BE EDITED');
}
$rolePermissions = DB::table("role_has_permissions")->where("role_id", $role->id)
->pluck('permission_id')
->all();
return view('roles.edit', [
'role' => $role,
'permissions' => Permission::get(),
'rolePermissions' => $rolePermissions
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
public function update(UpdateRoleRequest $request, Role $role)
{
//
$input = $request->only('name');
$role->update($input);
$permissions = Permission::whereIn('id', $request->permissions)->get(['name'])->toArray();
$role->syncPermissions($permissions);
return redirect()->back()
->withSuccess('Role is updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
/**
* Remove the specified resource from storage.
*/
public function destroy(Role $role)
{
//
if($role->name=='ADMIn'){
abort(403, 'ADMIN ROLE CAN NOT BE DELETED');
}
if(auth()->user()->hasRole($role->name)){
abort(403, 'CAN NOT DELETE SELF ASSIGNED ROLE');
}
$role->delete();
return redirect()->route('roles.index')
->withSuccess('Role is deleted successfully.');
}
}

View File

@@ -4,9 +4,11 @@ namespace App\Http\Controllers;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Jobs\SendMailJob;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Spatie\Permission\Models\Role;
class UsersController extends Controller
@@ -62,6 +64,7 @@ class UsersController extends Controller
*/
public function show(User $user)
{
//dispatch(new SendMailJob($user));
return view('users.show', [
'user' => $user
]);
@@ -72,9 +75,9 @@ class UsersController extends Controller
*/
public function edit(User $user)
{
// Check Only Super Admin can update his own Profile
if ($user->hasRole('ADMIN')){
if($user->id != auth()->user()->id){
// Check Only ADMIN can update his own Profile
if ($user->hasRole('ADMIN')) {
if ($user->id != auth()->user()->id) {
abort(403, 'USER DOES NOT HAVE THE RIGHT PERMISSIONS');
}
}
@@ -93,12 +96,19 @@ class UsersController extends Controller
{
$input = $request->all();
if(!empty($request->password)){
if (!empty($request->password)) {
$input['password'] = Hash::make($request->password);
}else{
} else {
$input = $request->except('password');
}
if ($request->hasFile('image')) {
$filename = $request->image->getClientOriginalName();
$request->image->storeAs('images', $filename, 'public');
$input['image'] = $filename;
}
$user->update($input);
$user->syncRoles($request->roles);
@@ -112,9 +122,8 @@ class UsersController extends Controller
*/
public function destroy(User $user)
{
// About if user is Super Admin or User ID belongs to Auth User
if ($user->hasRole('ADMIN') || $user->id == auth()->user()->id)
{
// About if user is ADMIN or User ID belongs to Auth User
if ($user->hasRole('ADMIN') || $user->id == auth()->user()->id) {
abort(403, 'USER DOES NOT HAVE THE RIGHT PERMISSIONS');
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreRoleRequest 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
{
return [
'name' => 'required|string|max:250|unique:roles,name',
'permissions' => 'required',
];
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRoleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:250|unique:roles,name,'.$this->role->id,
'permissions' => 'required',
];
}
}

View File

@@ -25,7 +25,8 @@ class UpdateUserRequest extends FormRequest
'name' => 'required|string|max:250',
'email' => 'required|string|email:rfc,dns|max:250|unique:users,email,'.$this->user->id,
'password' => 'nullable|string|min:8|confirmed',
'roles' => 'required'
'roles' => 'required',
'image' => 'file|image'
];
}
}

34
app/Jobs/SendMailJob.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class SendMailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Execute the job.
*/
public function handle(): void
{
Mail::to($this->user->email)
->send(new \App\Mail\WelcomeEmail($this->user));
}
}

69
app/Mail/WelcomeEmail.php Normal file
View File

@@ -0,0 +1,69 @@
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Headers;
use Illuminate\Queue\SerializesModels;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(Public User $user)
{
}
/**
* @return Headers
*/
public function headers(): Headers
{
return new Headers(
text: [
'X-Tags' => 'Motula-Translate',
],
);
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Welcome Email',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.welcome',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

View File

@@ -24,6 +24,7 @@ class User extends Authenticatable
'name',
'email',
'password',
'image'
];
/**

View File

@@ -17,6 +17,7 @@ return new class extends Migration
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('image')->nullable();
$table->rememberToken();
$table->timestamps();
});

View File

@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('jobs');
}
};

View File

@@ -20,9 +20,6 @@ class PermissionSeeder extends Seeder
'create-user',
'edit-user',
'delete-user',
'create-product',
'edit-product',
'delete-product'
];
// Looping and Inserting Array's Permissions into Permission Table

View File

@@ -14,13 +14,11 @@ class RoleSeeder extends Seeder
public function run(): void
{
$admin = Role::create(['name' => 'ADMIN']);
$admin = Role::create(['name' => 'MANAGER']);
$admin->givePermissionTo([
'create-user',
'edit-user',
'delete-user',
'create-product',
'edit-product',
'delete-product'
]);
}
}

View File

@@ -0,0 +1,3 @@
<div>
Price: {{ $user->username }}
</div>

View File

@@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<title>{{ config('app.name', 'Motula-Translate') }}</title>
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
@@ -20,6 +20,9 @@
'resources/vendors/flatpickr/flatpickr.min.css',
'resources/vendors/fontawesome/css/all.min.css',
])
@include('layouts.partials.styles')
@stack('head-scripts')
</head>
<body class="font-sans antialiased sidebar-dark">
<div class="main-wrapper">
@@ -28,6 +31,7 @@
<div class="page-wrapper">
@include('layouts.partials.navbar')
<div class="page-content">
@include('layouts.partials.flesh-message')
{{ $slot }}
</div>
@@ -36,6 +40,6 @@
</div>
@include('layouts.partials.scripts')
@stack('body-scripts')
</body>
</html>

View File

@@ -0,0 +1,35 @@
@if ($message = Session::get('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>{{ $message }}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if ($message = Session::get('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>{{ $message }}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if ($message = Session::get('warning'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>{{ $message }}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if ($message = Session::get('info'))
<div class="alert alert-info alert-dismissible fade show" role="alert">
<strong>{{ $message }}</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
@if ($errors->any())
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Please check the form below for errors</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif

View File

@@ -233,14 +233,24 @@
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="profileDropdown" role="button"
data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img class="wd-30 ht-30 rounded-circle" src="https://via.placeholder.com/30x30"
alt="profile">
@if(Auth::user()->image)
<img class="wd-30 ht-30 rounded-circle" src="{{asset('/storage/images/'.auth()->user()->image)}}" alt="{{auth()->user()->name}}">
@else
<img class="wd-30 ht-30 rounded-circle" src="https://via.placeholder.com/30x30"
alt="profile">
@endif
</a>
<div class="dropdown-menu p-0" aria-labelledby="profileDropdown">
<div class="d-flex flex-column align-items-center border-bottom px-5 py-3">
<div class="mb-3">
<img class="wd-80 ht-80 rounded-circle" src="https://via.placeholder.com/80x80"
alt="">
@if(Auth::user()->image)
<img class="wd-80 ht-80 rounded-circle" src="{{asset('/storage/images/'.auth()->user()->image)}}" alt="{{auth()->user()->name}}">
@else
<img class="wd-80 ht-80 rounded-circle" src="https://via.placeholder.com/80x80"
alt="profile">
@endif
</div>
<div class="text-center">
<p class="tx-16 fw-bolder">{{ auth()->user()->name }}</p>
@@ -249,13 +259,13 @@
</div>
<ul class="list-unstyled p-1">
<li class="dropdown-item py-2">
<a href="{{route('profile.edit')}}" class="text-body ms-0">
<a href="{{ route('users.show', auth()->user()->id) }}" class="text-body ms-0">
<i class="me-2 icon-md" data-feather="user"></i>
<span>Profile</span>
</a>
</li>
<li class="dropdown-item py-2">
<a href="javascript:;" class="text-body ms-0">
<a href="{{ route('users.edit', auth()->user()->id) }}" class="text-body ms-0">
<i class="me-2 icon-md" data-feather="edit"></i>
<span>Edit Profile</span>
</a>

View File

@@ -14,3 +14,4 @@
<!-- Custom js for this page -->
<script src="/assets/js/dashboard-dark.js"></script>
<!-- End custom js for this page -->
<script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.full.min.js"></script>

View File

@@ -24,15 +24,21 @@
<span class="link-title">Table Words</span>
</a>
</li>
@if(auth()->user()->hasRole('ADMIN'))
<li class="nav-item nav-category">Admin</li>
<li class="nav-item">
<a class="nav-link" href="{{route('users.index')}}">
<i class="link-icon" data-feather="users"></i>
<span class="link-title">Users</span>
</a>
</li>
@endif
@role('ADMIN')
<li class="nav-item nav-category">Admin</li>
<li class="nav-item">
<a class="nav-link" href="{{route('users.index')}}">
<i class="link-icon fa-regular fa-users"></i>
<span class="link-title">Users</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{route('roles.index')}}">
<i class="link-icon fa-regular fa-user-shield"></i>
<span class="link-title">Roles</span>
</a>
</li>
@endrole
{{-- <li class="nav-item nav-category">web apps</li>--}}
@@ -335,13 +341,13 @@
{{-- </ul>--}}
{{-- </div>--}}
{{-- </li>--}}
<li class="nav-item nav-category">Docs</li>
<li class="nav-item">
<a href="https://www.nobleui.com/html/documentation/docs.html" target="_blank" class="nav-link">
<i class="link-icon" data-feather="hash"></i>
<span class="link-title">Documentation</span>
</a>
</li>
<li class="nav-item nav-category">Docs</li>
<li class="nav-item">
<a href="https://www.nobleui.com/html/documentation/docs.html" target="_blank" class="nav-link">
<i class="link-icon" data-feather="hash"></i>
<span class="link-title">Documentation</span>
</a>
</li>
</ul>
</div>
</nav>

View File

@@ -0,0 +1,2 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" />

View File

@@ -0,0 +1,64 @@
<x-app-layout>
<div class="card">
<div class="card-header">
<div class="float-start">
Add New Role
</div>
<div class="float-end">
<a href="{{ route('roles.index') }}" class="btn btn-primary btn-sm">&larr; Back</a>
</div>
</div>
<div class="card-body">
<form action="{{ route('roles.store') }}" method="post">
@csrf
<div class="mb-3 row">
<label for="name" class="col-md-4 col-form-label text-md-end text-start">Name</label>
<div class="col-md-6">
<input type="text" class="form-control @error('name') is-invalid @enderror" id="name"
name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<label for="permissions" class="col-md-4 col-form-label text-md-end text-start">Permissions</label>
<div class="col-md-6">
<select class="form-select select-permissions @error('permissions') is-invalid @enderror"
multiple
aria-label="Permissions" id="permissions" name="permissions[]" style="height: 210px;">
@forelse ($permissions as $permission)
<option
value="{{ $permission->id }}" {{ in_array($permission->id, old('permissions') ?? []) ? 'selected' : '' }}>
{{ $permission->name }}
</option>
@empty
@endforelse
</select>
@if ($errors->has('permissions'))
<span class="text-danger">{{ $errors->first('permissions') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<input type="submit" class="col-md-3 offset-md-5 btn btn-primary" value="Add Role">
</div>
</form>
</div>
</div>
@push('body-scripts')
<script id="create-role" type="application/javascript">
$('.select-permissions').select2({
theme: "bootstrap-5",
width: '100%',
});
</script>
@endpush
</x-app-layout>

View File

@@ -0,0 +1,62 @@
<x-app-layout>
<div class="card">
<div class="card-header">
<div class="float-start">
Edit Role
</div>
<div class="float-end">
<a href="{{ route('roles.index') }}" class="btn btn-primary btn-sm">&larr; Back</a>
</div>
</div>
<div class="card-body">
<form action="{{ route('roles.update', $role->id) }}" method="post">
@csrf
@method("PUT")
<div class="mb-3 row">
<label for="name" class="col-md-4 col-form-label text-md-end text-start">Name</label>
<div class="col-md-6">
<input type="text" class="form-control @error('name') is-invalid @enderror" id="name"
name="name" value="{{ $role->name }}">
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<label for="permissions" class="col-md-4 col-form-label text-md-end text-start">Permissions</label>
<div class="col-md-6">
<select class="form-select select-permissions @error('permissions') is-invalid @enderror" multiple
aria-label="Permissions" id="permissions" name="permissions[]" style="height: 210px;">
@forelse ($permissions as $permission)
<option
value="{{ $permission->id }}" {{ in_array($permission->id, $rolePermissions ?? []) ? 'selected' : '' }}>
{{ $permission->name }}
</option>
@empty
@endforelse
</select>
@if ($errors->has('permissions'))
<span class="text-danger">{{ $errors->first('permissions') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<input type="submit" class="col-md-3 offset-md-5 btn btn-primary" value="Update Role">
</div>
</form>
</div>
</div>
@push('body-scripts')
<script id="edit-role" type="application/javascript">
$('.select-permissions').select2({
theme: "bootstrap-5",
width: '100%',
});
</script>
@endpush
</x-app-layout>

View File

@@ -0,0 +1,57 @@
<x-app-layout>
<div class="card">
<div class="card-header">Manage Roles</div>
<div class="card-body">
@can('create-role')
<a href="{{ route('roles.create') }}" class="btn btn-success btn-xs my-2"><i class="bi bi-plus-circle"></i> Add New Role</a>
@endcan
<table class="table table-striped table-hover table-sm">
<thead>
<tr>
<th scope="col">S#</th>
<th scope="col">Name</th>
<th scope="col" style="width: 250px;">Action</th>
</tr>
</thead>
<tbody>
@forelse ($roles as $role)
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $role->name }}</td>
<td>
<form action="{{ route('roles.destroy', $role->id) }}" method="post">
@csrf
@method('DELETE')
<a href="{{ route('roles.show', $role->id) }}" class="btn btn-warning btn-xs">Show</a>
@if ($role->name!='ADMIN')
@can('edit-role')
<a href="{{ route('roles.edit', $role->id) }}" class="btn btn-primary btn-xs">Edit</a>
@endcan
@can('delete-role')
@if ($role->name!=Auth::user()->hasRole($role->name))
<button type="submit" class="btn btn-danger btn-xs" onclick="return confirm('Do you want to delete this role?');">Delete</button>
@endif
@endcan
@endif
</form>
</td>
</tr>
@empty
<td colspan="3">
<span class="text-danger">
<strong>No Role Found!</strong>
</span>
</td>
@endforelse
</tbody>
</table>
{{ $roles->links() }}
</div>
</div>
</x-app-layout>

View File

@@ -0,0 +1,35 @@
<x-app-layout>
<div class="card">
<div class="card-header">
<div class="float-start">
Role Information
</div>
<div class="float-end">
<a href="{{ route('roles.index') }}" class="btn btn-primary btn-sm">&larr; Back</a>
</div>
</div>
<div class="card-body">
<div class="mb-3 row">
<label for="name" class="col-md-4 col-form-label text-md-end text-start"><strong>Name:</strong></label>
<div class="col-md-6" style="line-height: 35px;">
{{ $role->name }}
</div>
</div>
<div class="mb-3 row">
<label for="roles" class="col-md-4 col-form-label text-md-end text-start"><strong>Permissions:</strong></label>
<div class="col-md-6" style="line-height: 35px;">
@if ($role->name=='ADMIN')
<span class="badge bg-primary">All</span>
@else
@forelse ($rolePermissions as $permission)
<span class="badge bg-primary">{{ $permission->name }}</span>
@empty
@endforelse
@endif
</div>
</div>
</div>
</div>
</x-app-layout>

View File

@@ -1,13 +0,0 @@
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
Bla Bla Bla
<?php echo $name; ?>
<?= echo $name; ?>
{{$name}}

View File

@@ -9,7 +9,7 @@
</div>
</div>
<div class="card-body">
<form action="{{ route('users.store') }}" method="post">
<form action="{{ route('users.store') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="mb-3 row">
@@ -59,17 +59,17 @@
<div class="mb-3 row">
<label for="roles" class="col-md-4 col-form-label text-md-end text-start">Roles</label>
<div class="col-md-6">
<select class="form-select @error('roles') is-invalid @enderror" multiple
<select class="form-select select-roles @error('roles') is-invalid @enderror" multiple
aria-label="Roles" id="roles" name="roles[]">
@forelse ($roles as $role)
@if ($role!='Super Admin')
@if ($role!='ADMIN')
<option
value="{{ $role }}" {{ in_array($role, old('roles') ?? []) ? 'selected' : '' }}>
{{ $role }}
</option>
@else
@if (Auth::user()->hasRole('Super Admin'))
@if (Auth::user()->hasRole('ADMIN'))
<option
value="{{ $role }}" {{ in_array($role, old('roles') ?? []) ? 'selected' : '' }}>
{{ $role }}
@@ -87,6 +87,16 @@
</div>
</div>
<div class="mb-3 row">
<label for="roles" class="col-md-4 col-form-label text-md-end text-start">Profile Picture</label>
<div class="col-md-6">
<input class="form-control @error('image') is-invalid @enderror" type="file" name="image">
@if ($errors->has('image'))
<span class="text-danger">{{ $errors->first('image') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<input type="submit" class="col-md-3 offset-md-5 btn btn-primary" value="Add User">
</div>
@@ -94,4 +104,15 @@
</form>
</div>
</div>
@push('body-scripts')
<script id="create-user" type="application/javascript">
$('.select-roles').select2({
theme: "bootstrap-5",
width: '100%',
});
</script>
@endpush
</x-app-layout>

View File

@@ -7,92 +7,112 @@
<div class="float-end">
<a href="{{ route('users.index') }}" class="btn btn-primary btn-xs"> Back</a>
</div>
<div class="card-body">
<form action="{{ route('users.update', $user->id) }}" method="post">
@csrf
@method("PUT")
</div>
<div class="card-body">
<form action="{{ route('users.update', $user->id) }}" method="post" enctype="multipart/form-data">
@csrf
@method("PUT")
<div class="mb-3 row">
<label for="name" class="col-md-4 col-form-label text-md-end text-start">Name</label>
<div class="col-md-6">
<input type="text" class="form-control @error('name') is-invalid @enderror" id="name"
name="name" value="{{ $user->name }}">
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>
<div class="mb-3 row">
<label for="name" class="col-md-4 col-form-label text-md-end text-start">Name</label>
<div class="col-md-6">
<input type="text" class="form-control @error('name') is-invalid @enderror" id="name"
name="name" value="{{ $user->name }}">
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<label for="email" class="col-md-4 col-form-label text-md-end text-start">Email
Address</label>
<div class="col-md-6">
<input type="email" class="form-control @error('email') is-invalid @enderror" id="email"
name="email" value="{{ $user->email }}">
@if ($errors->has('email'))
<span class="text-danger">{{ $errors->first('email') }}</span>
@endif
</div>
<div class="mb-3 row">
<label for="email" class="col-md-4 col-form-label text-md-end text-start">Email
Address</label>
<div class="col-md-6">
<input type="email" class="form-control @error('email') is-invalid @enderror" id="email"
name="email" value="{{ $user->email }}">
@if ($errors->has('email'))
<span class="text-danger">{{ $errors->first('email') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<label for="password"
class="col-md-4 col-form-label text-md-end text-start">Password</label>
<div class="col-md-6">
<input type="password" class="form-control @error('password') is-invalid @enderror"
id="password" name="password">
@if ($errors->has('password'))
<span class="text-danger">{{ $errors->first('password') }}</span>
@endif
</div>
<div class="mb-3 row">
<label for="password"
class="col-md-4 col-form-label text-md-end text-start">Password</label>
<div class="col-md-6">
<input type="password" class="form-control @error('password') is-invalid @enderror"
id="password" name="password">
@if ($errors->has('password'))
<span class="text-danger">{{ $errors->first('password') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<label for="password_confirmation" class="col-md-4 col-form-label text-md-end text-start">Confirm
Password</label>
<div class="col-md-6">
<input type="password" class="form-control" id="password_confirmation"
name="password_confirmation">
</div>
<div class="mb-3 row">
<label for="password_confirmation" class="col-md-4 col-form-label text-md-end text-start">Confirm
Password</label>
<div class="col-md-6">
<input type="password" class="form-control" id="password_confirmation"
name="password_confirmation">
</div>
</div>
<div class="mb-3 row">
<label for="roles" class="col-md-4 col-form-label text-md-end text-start">Roles</label>
<div class="col-md-6">
<select class="form-select @error('roles') is-invalid @enderror" multiple
aria-label="Roles" id="roles" name="roles[]">
@forelse ($roles as $role)
<div class="mb-3 row">
<label for="roles" class="col-md-4 col-form-label text-md-end text-start">Roles</label>
<div class="col-md-6">
<select class="form-select select-roles @error('roles') is-invalid @enderror" multiple
aria-label="Roles" id="roles" name="roles[]">
@forelse ($roles as $role)
@if ($role!='Super Admin')
@if ($role!='ADMIN')
<option
value="{{ $role }}" {{ in_array($role, $userRoles ?? []) ? 'selected' : '' }}>
{{ $role }}
</option>
@else
@if (Auth::user()->hasRole('ADMIN'))
<option
value="{{ $role }}" {{ in_array($role, $userRoles ?? []) ? 'selected' : '' }}>
{{ $role }}
</option>
@else
@if (Auth::user()->hasRole('Super Admin'))
<option
value="{{ $role }}" {{ in_array($role, $userRoles ?? []) ? 'selected' : '' }}>
{{ $role }}
</option>
@endif
@endif
@endif
@empty
@empty
@endforelse
</select>
@if ($errors->has('roles'))
<span class="text-danger">{{ $errors->first('roles') }}</span>
@endif
</div>
@endforelse
</select>
@if ($errors->has('roles'))
<span class="text-danger">{{ $errors->first('roles') }}</span>
@endif
</div>
</div>
<div class="mb-3 row">
<input type="submit" class="col-md-3 offset-md-5 btn btn-primary" value="Update User">
<div class="mb-3 row">
<label for="roles" class="col-md-4 col-form-label text-md-end text-start">Profile Picture</label>
<div class="col-md-6">
<input class="form-control @error('image') is-invalid @enderror" type="file" name="image">
@if ($errors->has('image'))
<span class="text-danger">{{ $errors->first('image') }}</span>
@endif
</div>
</div>
</form>
</div>
<div class="mb-3 row">
<input type="submit" class="col-md-3 offset-md-5 btn btn-primary" value="Update User">
</div>
</form>
</div>
</div>
@push('body-scripts')
<script id="edit-user" type="application/javascript">
$('.select-roles').select2({
theme: "bootstrap-5",
width: '100%',
});
</script>
@endpush
</x-app-layout>