https://www.youtube.com/watch?v=ArikDlZxwyI&list=PLJlg6RBt94MG7UygN6gE1UHGWBHr4lJqi&index=12&pp=gAQBiAQB
https://www.youtube.com/watch?v=RNyq9FtQzz4&list=PLJlg6RBt94MG7UygN6gE1UHGWBHr4lJqi&index=13&pp=gAQBiAQB
assign poermission to roles
assign roles to user
if user has this pemrission we will priocess otherwise acce denied
we will implemtent seders
i will helpful if we not access the portal
composer require spatie/laravel-permission
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
permisisoncontroller
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class PermissionController extends Controller
{
public function index(Request $request)
{
$rules = [
'per_page' => 'integer|in:1,5,10,25,50,100',
];
$customMessages = [
'per_page.integer' => 'The per page value must be an integer.',
'per_page.in' => 'The per page value must be one of the following: 1, 5, 10, 25, 50, 100.',
];
$details = $this->validate($request, $rules, $customMessages);
$data = Permission::query();
if ($request->has('search')) {
$data->where('name', 'like', '%' . $request->search . '%');
}
$per_page = $request->has('per_page') ? $details['per_page'] : 10;
if ($request->has('sort_by') && $request->has('order')) {
$data = $data->orderBy($request['sort_by'], $request['order']);
} else {
$data = $data->orderBy('id', 'desc');
}
return response()->json([
'status' => 'success',
'message' => 'Fetched Successfully',
'data' => $data->paginate($per_page)
], 201);
}
public function store(Request $request)
{
$rules = [
'name' => 'required|string',
];
$customMessages = [
];
$details = $this->validate($request, $rules, $customMessages);
Permission::create($details);
return response()->json([
'status' => 'success',
'message' => 'Created successfully',
'data' => ''
], 201);
}
public function edit(Permission $permission)
{
return response()->json([
'status' => 'success',
'message' => 'Data Fetched successfully',
'data' => $permission
], 201);
}
public function update(Request $request, Permission $permission)
{
$rules = [
'name' => 'required|string',
];
$customMessages = [
];
$details = $this->validate($request, $rules, $customMessages);
$permission->update($details);
return response()->json([
'status' => 'success',
'message' => 'Data Updated successfully',
'data' => ''
], 201);
}
public function destroy(Permission $permission)
{
$permission->delete();
return response()->json([
'status' => 'success',
'message' => 'Requested Data Deleted successfully',
'data' => ''
], 201);
}
}
rolecontroller
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Response;
use Spatie\Permission\Models\Permission;
class RoleController extends Controller
{
public function index(Request $request)
{
// if (!Auth::user()->hasAnyPermission(['role:view'])) {
// return response()->view('errors.403', [], 403);
// }
$rules = [
'per_page' => 'integer|in:1,5,10,25,50,100',
];
$customMessages = [
'per_page.integer' => 'The per page value must be an integer.',
'per_page.in' => 'The per page value must be one of the following: 1, 5, 10, 25, 50, 100.',
];
$details = $this->validate($request, $rules, $customMessages);
$data = Role::query();
if ($request->has('search')) {
$data->where('name', 'like', '%' . $request->search . '%');
}
$per_page = $request->has('per_page') ? $details['per_page'] : 10;
if ($request->has('sort_by') && $request->has('order')) {
$data = $data->orderBy($request['sort_by'], $request['order']);
} else {
$data = $data->orderBy('id', 'desc');
}
return response()->json([
'status' => 'success',
'message' => 'Fetched Successfully',
'data' => $data->paginate($per_page)
], 201);
}
public function create()
{
// if (!Auth::user()->hasAnyPermission(['role:create'])) {
// return response()->view('errors.403', [], 403);
// }
return view('pages.roles.create');
}
public function store(Request $request)
{
// if (!Auth::user()->hasAnyPermission(['role:create'])) {
// return response()->view('errors.403', [], 403);
// }
$rules = [
'name' => 'required|string',
];
$customMessages = [
];
$details = $this->validate($request, $rules, $customMessages);
Role::create($details );
return response()->json([
'status' => 'success',
'message' => 'Created successfully',
'data' => ''
], 201);
}
public function edit(Role $role)
{
$role['permissions']=$role->permissions;
$permissions=Permission::all();
$data['role']=$role;
$data['permissions']=$permissions;
return response()->json([
'status' => 'success',
'message' => 'Data Fetched successfully',
'data' => $data
], 201);
}
public function update(Request $request, Role $role)
{
// if (!Auth::user()->hasAnyPermission(['role:edit'])) {
// return response()->view('errors.403', [], 403);
// }
$rules = [
'name' => 'required|string',
'permissions.*' => 'exists:permissions,name', // Validate each item in the permissions array
];
$customMessages = [
'permissions.*.exists' => 'One or more of the permissions is invalid.', // Custom error message for the exists rule
];
$details = $this->validate($request, $rules, $customMessages);
$role->syncPermissions( $details['permissions']);
$role->update($details);
return response()->json([
'status' => 'success',
'message' => 'Data Updated successfully',
'data' => ''
], 201);
}
public function destroy(Role $role)
{
// if (!Auth::user()->hasAnyPermission(['role:delete'])) {
// return response()->view('errors.403', [], 403);
// }
$role->delete();
return response()->json([
'status' => 'success',
'message' => 'Requested Data Delete successfully',
'data' => ''
], 201);
}
}
role/edit
<!-- resources/views/cards/edit.blade.php -->
@extends('layouts.app')
@section('content')
<div class="container">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{ route('roles.index') }}">Roles</a></li>
<li class="breadcrumb-item active" aria-current="page">Edit</li>
</ol>
</nav>
<h2>Edit Card</h2>
@include('layouts.error')
<form action="{{ route('roles.update', $role->id) }}" method="POST">
@csrf
@method('PUT')
@php
$elements = [
['method'=>'input','label' => 'Enter Role Name', 'key' => 'name', 'place_holder' => 'Enter Role Name', 'type' => 'text','required'=>true],
];
$data=$role;
@endphp
@include('components.form-elements', ['elements' => $elements,'data'=>$data])
@foreach($permissions as $permission)
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="{{$permission->id}}" name="permissions[]" value="{{$permission->name}}"
@if(collect($role['permissions'])->contains('name', $permission->name)) checked @endif>
<label class="form-check-label" for="{{$permission->id}}"> {{$permission->name}}</label>
</div>
@endforeach
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
@endsection
to run with seeders php artisan migrate:fresh --seed
=
No comments:
Post a Comment