master_template/app/Http/Controllers/RolesController.php

104 lines
3.3 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\PermissionRepository;
2024-06-20 10:57:33 +00:00
use Illuminate\Http\Request;
use App\Models\Roles;
2024-06-23 11:17:56 +00:00
use Illuminate\Http\RedirectResponse;
use Brian2694\Toastr\Facades\Toastr;
use App\Repositories\RoleRepository;
2024-06-20 10:57:33 +00:00
use Log;
use Exception;
class RolesController extends Controller
{
2024-06-23 11:17:56 +00:00
private $roleRepository;
private $permissionRepository;
public function __construct(
RoleRepository $roleRepository,
PermissionRepository $permissionRepository
) {
$this->roleRepository = $roleRepository;
$this->permissionRepository = $permissionRepository;
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
2024-06-20 10:57:33 +00:00
public function index(Request $request)
{
2024-06-23 11:17:56 +00:00
$data['title'] = "List Roles";
2024-06-20 10:57:33 +00:00
createActivityLog(RolesController::class, 'index', ' Roles index');
2024-06-23 11:17:56 +00:00
$roles = $this->roleRepository->findAll();
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
return view("crud.generated.roles.index", compact('roles'));
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function create()
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['title'] = "Create Role";
$data['editable'] = false;
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
return view('crud.generated.roles.create', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function store(Request $request): RedirectResponse
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
try {
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$validatedData = $request->validate([
'name' => 'required',
'guard_name' => 'string',
]);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$role = $this->roleRepository->create($validatedData);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$role->permissions()->attach($request->permissions);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
Toastr::success('New Role has been created', 'Title', ["positionClass" => "toast-top-center"]);
} catch (\Throwable $th) {
Toastr::success('Error while creating', 'Title', ["positionClass" => "toast-top-center"]);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
return redirect()->route('roles.index');
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function show(string $id)
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
public function edit($id)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['title'] = "Edit Role";
$data['editable'] = false;
$data['role'] = $this->roleRepository->getRoleById($id);
$data['permissionIDsArray'] = $data['role']?->permissions?->pluck('id')->toArray();
$data['permissionLists'] = $this->permissionRepository->getPermissionListsArrangedByPrefix();
return view('crud.generated.roles.edit', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function update(Request $request, $id): RedirectResponse
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
try {
$validatedData = $request->validate([
'name' => 'required',
'guard_name' => 'string',
]);
$role = $this->roleRepository->update($id, $validatedData);
$role->permissions()->sync($request->permissions);
Toastr::success('Role has been updated', 'Title', ["positionClass" => "toast-top-center"]);
} catch (\Throwable $th) {
Toastr::success('Error while updating', 'Title', ["positionClass" => "toast-top-center"]);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
return redirect()->route('roles.index');
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function destroy($id)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$this->roleRepository->delete($id);
Toastr::success('Role has been deleted', 'Title', ["positionClass" => "toast-top-center"]);
return response()->json(['status' => true, 'message' => 'Role has been deleted!']);
2024-06-20 10:57:33 +00:00
}
}