40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin\Roles;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\Admin\Roles\DestroyRoleRequest;
|
||
|
|
use App\Models\Role;
|
||
|
|
use App\Services\RoleService;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Controller for deleting a role.
|
||
|
|
*/
|
||
|
|
class RoleDestroyController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Remove the specified role.
|
||
|
|
*
|
||
|
|
* @param \App\Http\Requests\Admin\Roles\DestroyRoleRequest $request
|
||
|
|
* @param \App\Models\Role $role
|
||
|
|
* @param \App\Services\RoleService $roleService
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function __invoke(DestroyRoleRequest $request, Role $role, RoleService $roleService)
|
||
|
|
{
|
||
|
|
if ($role->is_protected) {
|
||
|
|
return redirect()->back()->withErrors(['The protected role cannot be deleted.']);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($role->users()->exists()) {
|
||
|
|
return redirect()->back()->withErrors(['The role cannot be deleted because it is assigned to users.']);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($roleService->delete($role)) {
|
||
|
|
return redirect()->route('admin.roles.index')->with('status', 'Role deleted successfully.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return redirect()->back()->with('error', 'Failed to delete role.');
|
||
|
|
}
|
||
|
|
}
|