master_template/app/Http/Controllers/UsersController.php

118 lines
3.7 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\User;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use App\Service\CommonModelService;
use Log;
use Exception;
class UsersController extends Controller
{
protected $modelService;
public function __construct(User $model)
{
$this->modelService = new CommonModelService($model);
}
public function index(Request $request)
{
createActivityLog(UsersController::class, 'index', 'User index');
$data = User::get();
return view("crud.generated.users.index", compact('data'));
}
public function show(Request $request, $id)
{
createActivityLog(UsersController::class, 'show', 'User show');
$data = User::findOrFail($id);
return view("crud.generated.User.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(UsersController::class, 'edit', 'User edit');
$TableData = User::get();
$data = User::findOrFail($id);
$roles = Role::all();
$permissions = Permission::all();
if ($request->ajax()) {
$html = view("crud.generated.User.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.users.edit", compact('data', 'TableData', 'roles', 'permissions'));
}
public function update(Request $request, User $user)
{
// dd($request->toArray());
if ($request->permission) {
if ($user->hasPermissionTo($request->permission)) {
return redirect()->back()->with('error', 'The User already has this permission.');
}
$user->givePermissionTo($request->permission);
}
if ($request->role) {
if ($user->hasRole($request->role)) {
return redirect()->back()->with('error', 'The User already has this role.');
}
$user->assignRole($request->role);
}
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The User updated Successfully.'], 200);
}
// Logic to update user should be added here
return redirect()->back()->with('success', 'The User updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(UsersController::class, 'destroy', 'User destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(UsersController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The User Deleted Successfully.'], 200);
}
public function removeRole(User $user, Role $role)
{
if ($user->hasRole($role)) {
$user->removeRole($role);
return back()->with('message', 'Role removed.');
}
return back()->with('message', 'Role not exists.');
}
public function revokePermission(User $user, Permission $permission)
{
if ($user->hasPermissionTo($permission)) {
$user->revokePermissionTo($permission);
return back()->with('message', 'Permission revoked.');
}
return back()->with('message', 'Permission does not exists.');
}
}