- Changed view extends from "admin.main" to "administrator.layout.master" across multiple files. - Updated route names in admin.php and administrator.php to reflect new structure for user, order, invoice, and other management functionalities. - Adjusted view includes to point to the new directory structure for blocks and components. - Ensured all permission checks remain intact while refactoring the views.
128 lines
3.2 KiB
PHP
128 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use PHPUnit\Exception;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
|
|
class RolesController extends Controller
|
|
{
|
|
//
|
|
public function index()
|
|
{
|
|
$roles = Role::get();
|
|
$title = 'Administrator Role';
|
|
|
|
return view('administrator.pages.role.index', compact('roles', 'title'));
|
|
}
|
|
public function create()
|
|
{
|
|
$permission = Permission::select('id','name')->orderby('id')->get();
|
|
|
|
$title = 'Add new administrator role';
|
|
|
|
return view('administrator.pages.role.add', compact('permission', 'title'));
|
|
}
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate(
|
|
[
|
|
'name' => 'required'
|
|
],
|
|
[
|
|
'name.required' => 'Please enter name role'
|
|
]
|
|
);
|
|
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
$role = Role::create([
|
|
'name' => $request->name,
|
|
'guard_name' => 'admins'
|
|
]);
|
|
|
|
$permissions = Permission::whereIn('id',$request->permission)->get();
|
|
|
|
$role->givePermissionTo($permissions);
|
|
|
|
DB::commit();
|
|
|
|
}catch (Exception $exception){
|
|
DB::rollBack();
|
|
return back()->withErrors($exception->getMessage());
|
|
}
|
|
return redirect()->route('role.index')->with('success','Add new administrator role successfully');
|
|
}
|
|
public function edit($id = 0)
|
|
{
|
|
$role = Role::find($id);
|
|
|
|
if (! $role) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
|
|
$permission = Permission::select('id','name')->orderby('id')->get();
|
|
|
|
$title = 'Edit administrator role';
|
|
|
|
return view('administrator.pages.role.edit', compact('permission', 'title', 'role'));
|
|
|
|
}
|
|
public function update(Request $request, $id = 0)
|
|
{
|
|
$role = Role::find($id);
|
|
|
|
if (! $role) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
$request->validate(
|
|
[
|
|
'name' => 'required'
|
|
],
|
|
[
|
|
'name.required' => 'Please enter name role'
|
|
]
|
|
);
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
$role->name = $request->name;
|
|
$role->save();
|
|
|
|
$permissions = Permission::whereIn('id',$request->permission)->get();
|
|
|
|
$role->syncPermissions($permissions);
|
|
|
|
DB::commit();
|
|
|
|
}catch (Exception $exception){
|
|
DB::rollBack();
|
|
return back()->withErrors($exception->getMessage());
|
|
}
|
|
return redirect()->route('role.index')->with('success','Update new value for administrator role successfully');
|
|
}
|
|
public function destroy($id = 0)
|
|
{
|
|
$role = Role::find($id);
|
|
|
|
if (! $role) {
|
|
return back()->withErrors('Invalid request');
|
|
}
|
|
|
|
$role->revokePermissionTo($role->getAllPermissions());
|
|
|
|
$role->delete();
|
|
|
|
return redirect()->route('role.index')->with('success','Remove administrator role successfully');
|
|
|
|
}
|
|
}
|