master_template/app/Http/Controllers/RolesController.php

126 lines
4.0 KiB
PHP
Raw Normal View History

2024-06-20 10:57:33 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Roles;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\CommonModelService;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Log;
use Exception;
class RolesController extends Controller
{
protected $modelService;
public function __construct(Roles $model)
{
$this->modelService = new CommonModelService($model);
}
public function index(Request $request)
{
createActivityLog(RolesController::class, 'index', ' Roles index');
$data = Role::whereNotIn('name', ['admin'])->get();
return view("crud.generated.roles.index", compact('data'));
}
public function create(Request $request)
{
createActivityLog(RolesController::class, 'create', ' Roles create');
$TableData = Roles::get();
$permissions = Permission::all();
return view("crud.generated.roles.create", compact('TableData', 'permissions'));
}
public function store(Request $request)
{
$validated = $request->validate(['name' => ['required', 'min:3']]);
$role = Role::create($validated);
if ($role->hasPermissionTo($request->permission, 'web')) {
return back()->with('message', 'Permission exists.');
}
$role->givePermissionTo($request->permission);
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Roles Created Successfully.'], 200);
}
return redirect()->route('roles.index')->with('success', 'The Roles created Successfully.');
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = Roles::find($id);
$companyArticle->display_order = $index + 1;
$companyArticle->save();
}
return response()->json(['status' => true, 'content' => 'The articles sorted successfully.'], 200);
}
public function show(Request $request, $id)
{
createActivityLog(RolesController::class, 'show', ' Roles show');
$data = Roles::findOrFail($id);
return view("crud.generated.roles.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(RolesController::class, 'edit', ' Roles edit');
$permissions = Permission::all();
$TableData = Role::get();
$data = Role::findOrFail($id);
if ($request->ajax()) {
$html = view("crud.generated.roles.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.roles.edit", compact('data', 'TableData', 'permissions'));
}
public function update(Request $request, Role $role)
{
$permission = $request->permission;
$validated = $request->validate(['name' => ['required', 'min:3']]);
$role->update($validated);
if($permission){
$role->givePermissionTo($permission);
}
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Roles updated Successfully.'], 200);
}
// return redirect()->route('roles.index')->with('success','The Roles updated Successfully.');
return redirect()->back()->with('success', 'The Roles updated successfully.');
}
public function destroy(Role $role)
{
$role->delete();
return response()->json(['status' => true, 'message' => 'The Roles Deleted Successfully.'], 200);
}
public function revokePermission(Role $role, Permission $permission)
{
if ($role->hasPermissionTo($permission)) {
$role->revokePermissionTo($permission);
return back()->with('message', 'Permission revoked.');
}
return back()->with('message', 'Permission not exists.');
}
}