96 lines
2.7 KiB
PHP
96 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Libraries\AuditLogger;
|
|
use App\Models\UserModel;
|
|
|
|
class UserController extends BaseController
|
|
{
|
|
public function index()
|
|
{
|
|
$userModel = new UserModel();
|
|
$search = trim((string) $this->request->getGet('search'));
|
|
$status = (string) $this->request->getGet('status');
|
|
|
|
$builder = $userModel->orderBy('id', 'DESC');
|
|
if ($search !== '') {
|
|
$builder = $builder->groupStart()
|
|
->like('name', $search)
|
|
->orLike('email', $search)
|
|
->groupEnd();
|
|
}
|
|
|
|
if ($status !== '' && in_array($status, ['0', '1'], true)) {
|
|
$builder = $builder->where('is_active', (int) $status);
|
|
}
|
|
|
|
return view('admin/users/index', [
|
|
'title' => 'Manage Users | Chart-Board',
|
|
'users' => $builder->paginate(10),
|
|
'pager' => $userModel->pager,
|
|
'search' => $search,
|
|
'status' => $status,
|
|
]);
|
|
}
|
|
|
|
public function edit(int $id)
|
|
{
|
|
$userModel = new UserModel();
|
|
$user = $userModel->find($id);
|
|
|
|
if (! $user) {
|
|
return redirect()->to('/admin/users')->with('error', 'User not found.');
|
|
}
|
|
|
|
return view('admin/users/edit', [
|
|
'title' => 'Edit User | Chart-Board',
|
|
'user' => $user,
|
|
]);
|
|
}
|
|
|
|
public function update(int $id)
|
|
{
|
|
$rules = [
|
|
'role' => 'required|in_list[superadmin,user]',
|
|
'is_active' => 'required|in_list[0,1]',
|
|
];
|
|
|
|
if (! $this->validate($rules)) {
|
|
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
|
}
|
|
|
|
$userModel = new UserModel();
|
|
$user = $userModel->find($id);
|
|
|
|
if (! $user) {
|
|
return redirect()->to('/admin/users')->with('error', 'User not found.');
|
|
}
|
|
|
|
$oldSnap = [
|
|
'role' => $user['role'] ?? null,
|
|
'is_active' => $user['is_active'] ?? null,
|
|
];
|
|
$userModel->update($id, [
|
|
'role' => (string) $this->request->getPost('role'),
|
|
'is_active' => (int) $this->request->getPost('is_active'),
|
|
]);
|
|
|
|
AuditLogger::log(
|
|
'user.admin_role_updated',
|
|
'user',
|
|
$id,
|
|
$oldSnap,
|
|
[
|
|
'role' => (string) $this->request->getPost('role'),
|
|
'is_active' => (int) $this->request->getPost('is_active'),
|
|
],
|
|
null,
|
|
(int) $this->session->get('user_id')
|
|
);
|
|
|
|
return redirect()->to('/admin/users')->with('success', 'User updated.');
|
|
}
|
|
}
|