357 lines
11 KiB
PHP
357 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\UserModel;
|
|
use App\Models\RoleModel;
|
|
use App\Models\AuditLogModel;
|
|
|
|
class Users extends BaseController
|
|
{
|
|
protected $userModel;
|
|
protected $roleModel;
|
|
protected $auditLogModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->userModel = new UserModel();
|
|
$this->roleModel = new RoleModel();
|
|
$this->auditLogModel = new AuditLogModel();
|
|
|
|
// Check if user is admin
|
|
if (session()->get('role') !== 'admin') {
|
|
throw new \CodeIgniter\Exceptions\PageNotFoundException();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display list of users
|
|
*/
|
|
public function index()
|
|
{
|
|
$perPage = 10;
|
|
$page = $this->request->getGet('page') ?? 1;
|
|
$role = $this->request->getGet('role');
|
|
$status = $this->request->getGet('status');
|
|
$search = $this->request->getGet('search');
|
|
|
|
// Build query with filters
|
|
$this->userModel->select('users.*, roles.name as role_name')
|
|
->join('roles', 'roles.id = users.role_id', 'left');
|
|
|
|
// Filter by role
|
|
if ($role) {
|
|
$this->userModel->where('roles.name', $role);
|
|
}
|
|
|
|
// Filter by status
|
|
if ($status !== null && $status !== '') {
|
|
$this->userModel->where('users.is_active', $status);
|
|
}
|
|
|
|
// Search
|
|
if ($search) {
|
|
$this->userModel->groupStart()
|
|
->like('users.username', $search)
|
|
->orLike('users.email', $search)
|
|
->orLike('users.phone_number', $search)
|
|
->groupEnd();
|
|
}
|
|
|
|
// Get paginated results
|
|
$users = $this->userModel->orderBy('users.created_at', 'DESC')
|
|
->paginate($perPage, 'default', $page);
|
|
|
|
$pager = $this->userModel->pager;
|
|
|
|
// Get roles for filter
|
|
$roles = $this->roleModel->findAll();
|
|
|
|
$data = [
|
|
'title' => 'Pengguna',
|
|
'users' => $users,
|
|
'pager' => $pager,
|
|
'roles' => $roles,
|
|
'currentRole' => $role,
|
|
'currentStatus' => $status,
|
|
'currentSearch' => $search,
|
|
'stats' => [
|
|
'total' => $this->userModel->countAllResults(),
|
|
'active' => $this->userModel->where('is_active', 1)->countAllResults(),
|
|
'inactive' => $this->userModel->where('is_active', 0)->countAllResults(),
|
|
],
|
|
];
|
|
|
|
return view('admin/users/index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show form to create new user
|
|
*/
|
|
public function create()
|
|
{
|
|
$roles = $this->roleModel->findAll();
|
|
|
|
$data = [
|
|
'title' => 'Tambah Pengguna',
|
|
'user' => null,
|
|
'roles' => $roles,
|
|
];
|
|
|
|
return view('admin/users/form', $data);
|
|
}
|
|
|
|
/**
|
|
* Store new user
|
|
*/
|
|
public function store()
|
|
{
|
|
$validation = \Config\Services::validation();
|
|
|
|
$rules = [
|
|
'username' => 'required|min_length[3]|max_length[100]|is_unique[users.username]',
|
|
'email' => 'required|valid_email|max_length[255]|is_unique[users.email]',
|
|
'password' => 'required|min_length[6]',
|
|
'role_id' => 'required|integer',
|
|
'phone_number' => 'permit_empty|max_length[20]|is_unique[users.phone_number]',
|
|
'telegram_id' => 'permit_empty|integer|is_unique[users.telegram_id]',
|
|
];
|
|
|
|
if (!$this->validate($rules)) {
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('validation', $validation);
|
|
}
|
|
|
|
$username = $this->request->getPost('username');
|
|
$email = $this->request->getPost('email');
|
|
$password = $this->request->getPost('password');
|
|
$roleId = $this->request->getPost('role_id');
|
|
$phoneNumber = $this->request->getPost('phone_number');
|
|
$telegramId = $this->request->getPost('telegram_id');
|
|
$isActive = $this->request->getPost('is_active') ? 1 : 0;
|
|
$userId = session()->get('user_id');
|
|
|
|
$data = [
|
|
'username' => $username,
|
|
'email' => $email,
|
|
'password_hash' => $password, // Will be hashed by beforeInsert
|
|
'role_id' => $roleId,
|
|
'phone_number' => !empty($phoneNumber) ? $phoneNumber : null,
|
|
'telegram_id' => !empty($telegramId) ? $telegramId : null,
|
|
'is_active' => $isActive,
|
|
];
|
|
|
|
if ($this->userModel->insert($data)) {
|
|
// Log action
|
|
$this->auditLogModel->logAction('user_created', $userId);
|
|
|
|
return redirect()->to('/admin/users')
|
|
->with('success', 'Pengguna berhasil ditambahkan.');
|
|
}
|
|
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('error', 'Gagal menambahkan pengguna.');
|
|
}
|
|
|
|
/**
|
|
* Show form to edit user
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$user = $this->userModel->find($id);
|
|
|
|
if (!$user) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Pengguna tidak ditemukan.');
|
|
}
|
|
|
|
$roles = $this->roleModel->findAll();
|
|
|
|
$data = [
|
|
'title' => 'Edit Pengguna',
|
|
'user' => $user,
|
|
'roles' => $roles,
|
|
];
|
|
|
|
return view('admin/users/form', $data);
|
|
}
|
|
|
|
/**
|
|
* Update user
|
|
*/
|
|
public function update($id)
|
|
{
|
|
$user = $this->userModel->find($id);
|
|
|
|
if (!$user) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Pengguna tidak ditemukan.');
|
|
}
|
|
|
|
$validation = \Config\Services::validation();
|
|
|
|
$rules = [
|
|
'username' => "required|min_length[3]|max_length[100]|is_unique[users.username,id,{$id}]",
|
|
'email' => "required|valid_email|max_length[255]|is_unique[users.email,id,{$id}]",
|
|
'role_id' => 'required|integer',
|
|
'phone_number' => "permit_empty|max_length[20]|is_unique[users.phone_number,id,{$id}]",
|
|
'telegram_id' => "permit_empty|integer|is_unique[users.telegram_id,id,{$id}]",
|
|
];
|
|
|
|
if (!$this->validate($rules)) {
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('validation', $validation);
|
|
}
|
|
|
|
$username = $this->request->getPost('username');
|
|
$email = $this->request->getPost('email');
|
|
$roleId = $this->request->getPost('role_id');
|
|
$phoneNumber = $this->request->getPost('phone_number');
|
|
$telegramId = $this->request->getPost('telegram_id');
|
|
$isActive = $this->request->getPost('is_active') ? 1 : 0;
|
|
$userId = session()->get('user_id');
|
|
|
|
$data = [
|
|
'username' => $username,
|
|
'email' => $email,
|
|
'role_id' => $roleId,
|
|
'phone_number' => !empty($phoneNumber) ? $phoneNumber : null,
|
|
'telegram_id' => !empty($telegramId) ? $telegramId : null,
|
|
'is_active' => $isActive,
|
|
];
|
|
|
|
if ($this->userModel->update($id, $data)) {
|
|
// Log action
|
|
$this->auditLogModel->logAction('user_updated', $userId);
|
|
|
|
return redirect()->to('/admin/users')
|
|
->with('success', 'Pengguna berhasil diperbarui.');
|
|
}
|
|
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('error', 'Gagal memperbarui pengguna.');
|
|
}
|
|
|
|
/**
|
|
* Reset user password
|
|
*/
|
|
public function resetPassword($id)
|
|
{
|
|
$user = $this->userModel->find($id);
|
|
|
|
if (!$user) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Pengguna tidak ditemukan.');
|
|
}
|
|
|
|
$validation = \Config\Services::validation();
|
|
|
|
$rules = [
|
|
'new_password' => 'required|min_length[6]',
|
|
'confirm_password' => 'required|matches[new_password]',
|
|
];
|
|
|
|
if (!$this->validate($rules)) {
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('validation', $validation)
|
|
->with('error', 'Password tidak valid atau tidak cocok.');
|
|
}
|
|
|
|
$newPassword = $this->request->getPost('new_password');
|
|
$userId = session()->get('user_id');
|
|
|
|
$data = [
|
|
'password_hash' => $newPassword, // Will be hashed by beforeUpdate
|
|
];
|
|
|
|
if ($this->userModel->update($id, $data)) {
|
|
// Log action
|
|
$this->auditLogModel->logAction('user_password_reset', $userId);
|
|
|
|
return redirect()->to('/admin/users')
|
|
->with('success', 'Password pengguna berhasil direset.');
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('error', 'Gagal mereset password.');
|
|
}
|
|
|
|
/**
|
|
* Toggle user active status
|
|
*/
|
|
public function toggleActive($id)
|
|
{
|
|
$user = $this->userModel->find($id);
|
|
|
|
if (!$user) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Pengguna tidak ditemukan.');
|
|
}
|
|
|
|
// Prevent deactivating yourself
|
|
if ($id == session()->get('user_id')) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Anda tidak dapat menonaktifkan akun sendiri.');
|
|
}
|
|
|
|
$newStatus = $user['is_active'] ? 0 : 1;
|
|
$userId = session()->get('user_id');
|
|
|
|
$data = [
|
|
'is_active' => $newStatus,
|
|
];
|
|
|
|
if ($this->userModel->update($id, $data)) {
|
|
// Log action
|
|
$action = $newStatus ? 'user_activated' : 'user_deactivated';
|
|
$this->auditLogModel->logAction($action, $userId);
|
|
|
|
$message = $newStatus ? 'Pengguna berhasil diaktifkan.' : 'Pengguna berhasil dinonaktifkan.';
|
|
return redirect()->to('/admin/users')
|
|
->with('success', $message);
|
|
}
|
|
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Gagal mengubah status pengguna.');
|
|
}
|
|
|
|
/**
|
|
* Delete user
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
$user = $this->userModel->find($id);
|
|
|
|
if (!$user) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Pengguna tidak ditemukan.');
|
|
}
|
|
|
|
// Prevent deleting yourself
|
|
if ($id == session()->get('user_id')) {
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Anda tidak dapat menghapus akun sendiri.');
|
|
}
|
|
|
|
$userId = session()->get('user_id');
|
|
|
|
if ($this->userModel->delete($id)) {
|
|
// Log action
|
|
$this->auditLogModel->logAction('user_deleted', $userId);
|
|
|
|
return redirect()->to('/admin/users')
|
|
->with('success', 'Pengguna berhasil dihapus.');
|
|
}
|
|
|
|
return redirect()->to('/admin/users')
|
|
->with('error', 'Gagal menghapus pengguna.');
|
|
}
|
|
}
|
|
|