119 lines
3.3 KiB
PHP
119 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\User\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Modules\User\Services\RoleService;
|
|
use Modules\User\Services\PermissionService;
|
|
use Spatie\Permission\Models\Role;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class RoleController extends Controller
|
|
{
|
|
private $roleService;
|
|
private $permissionService;
|
|
public function __construct(RoleService $roleService, PermissionService $permissionService)
|
|
{
|
|
$this->roleService = $roleService;
|
|
$this->permissionService = $permissionService;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
if (request()->ajax()) {
|
|
$model = Role::query()->latest();
|
|
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->setRowClass('tableRow')
|
|
->addColumn('action', 'user::role.datatable.action')
|
|
->rawColumns(['action'])
|
|
->toJson();
|
|
}
|
|
|
|
return view('user::role.index', [
|
|
'title' => 'Role Lists',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = "Create Role";
|
|
$data['editable'] = false;
|
|
$data['permissionLists'] = $this->permissionService->getPermissionListsArrangedByPrefix();
|
|
return view('user::role.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required',
|
|
'guard_name' => 'string',
|
|
]);
|
|
|
|
$role = $this->roleService->storeRole($validated, $request->permissions);
|
|
|
|
flash()->success("Role for {$role->name} has been created.");
|
|
|
|
return redirect()->route('role.index');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['title'] = "Edit Role";
|
|
$data['editable'] = true;
|
|
$data['role'] = $role = $this->roleService->getRoleById($id);
|
|
$data['permissionIDsArray'] = $role?->permissions?->pluck('id')->toArray();
|
|
$data['permissionLists'] = $this->permissionService->getPermissionListsArrangedByPrefix();
|
|
return view('user::role.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required',
|
|
'guard_name' => 'string',
|
|
]);
|
|
|
|
$role = $this->roleService->updateRole($id, $validated, $request->permissions);
|
|
flash()->success("Role for {$role->name} has been updated.");
|
|
return redirect()->route('role.index');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id): Response
|
|
{
|
|
$this->roleService->deleteRole($id);
|
|
flash()->success("Role has been deleted.");
|
|
return response()->json(['status' => 200, 'message' => 'Role has been deleted!'], 200);
|
|
}
|
|
}
|