Update source into github
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
<?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('admin.role.index',compact('roles','title'));
|
||||
}
|
||||
public function create()
|
||||
{
|
||||
$permission = Permission::select('id','name')->orderby('id')->get();
|
||||
|
||||
$title = 'Add new administrator role';
|
||||
|
||||
return view('admin.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('admin.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');
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user