master_template/app/Http/Controllers/PermissionsController.php

107 lines
2.8 KiB
PHP
Raw Normal View History

2024-06-20 10:57:33 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
2024-06-23 11:17:56 +00:00
use App\Repositories\RoleRepository;
2024-06-20 10:57:33 +00:00
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
2024-06-23 11:17:56 +00:00
use App\Repositories\PermissionRepository;
2024-06-20 10:57:33 +00:00
class PermissionsController extends Controller
{
2024-06-23 11:17:56 +00:00
private $permissionRepository;
private $role;
public function __construct(permissionRepository $permissionRepository,
RoleRepository $role)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$this->permissionRepository = $permissionRepository;
$this->role = $role;
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Display a listing of the resource.
*/
public function index()
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['title'] = 'Permission Lists';
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
$data['roles'] = $this->role->pluck();
$data['editable'] = false;
return view('crud.generated.permissions.index', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Show the form for creating a new resource.
*/
public function create()
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['editable'] = false;
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
return view('user::role.create', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Store a newly created resource in storage.
*/
2024-06-20 10:57:33 +00:00
public function store(Request $request)
{
}
2024-06-23 11:17:56 +00:00
/**
* Display the specified resource.
*/
public function show(string $id)
2024-06-20 10:57:33 +00:00
{
}
2024-06-23 11:17:56 +00:00
/**
* Show the form for editing the specified resource.
*/
public function edit(Permission $permission)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['title'] = "Edit Role";
$data['editable'] = true;
$data['role'] = $this->role->getRoleById($id);
$data['permissionIDsArray'] = $data['role']?->permissions?->pluck('id')->toArray();
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
return view('user::role.edit', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Permission $permission)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
//
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$permissionDelete = $this->permissionRepository->getPermissionById($id);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$permissionDelete->delete();
return response()->json(['status' => true, 'message' => 'Permission has been deleted'] );
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function generatePermissionFromRoutes()
2024-06-20 10:57:33 +00:00
{
try {
2024-06-23 11:17:56 +00:00
$this->permissionRepository->generatePermissionFromRoutes();
toastr()->success('Permission generated successfully!');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
return to_route('permissions.index');
2024-06-20 10:57:33 +00:00
}
}