- Refactor 'app/Views/superuser/users.php' to fix user creation/editing logic using Alpine.js. - Ensure efficient form state management (userid, username, password handling) in user modal. - Standardize dashboard layouts and script initialization (window.BASEURL) across 'admin', 'cs', 'lab', 'phlebo', and 'superuser' main views. - Remove redundant 'app/Views/admin/users.php' to consolidate user management.
141 lines
3.8 KiB
PHP
141 lines
3.8 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
|
|
// Users Management
|
|
class UsersController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
protected $db;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = \Config\Database::connect();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$sql = "select u.USERID, u.USERROLEID, u.USERNAME from GDC_CMOD.dbo.USERS u
|
|
left join glendb.dbo.USERS u1 on u1.USERID=u.USERID
|
|
where u1.LOCKEDACCOUNT is null";
|
|
$query = $this->db->query($sql);
|
|
$results = $query->getResultArray();
|
|
$data['data'] = $results;
|
|
|
|
return $this->respond(['data' => $results]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
$userid = $input['userid'];
|
|
$userroleid = $input['userroleid'];
|
|
$password = $input['password'];
|
|
$password_2 = $input['password_2'];
|
|
|
|
if ($password != $password_2) {
|
|
return $this->response->setJSON(['message' => 'Password not the same']);
|
|
}
|
|
if (strlen($password) < 3) {
|
|
return $this->response->setJSON(['message' => 'Password must be more than 2 characters']);
|
|
}
|
|
|
|
$sql = $this->db->query("SELECT USERID FROM gdc_cmod.dbo.USERS WHERE USERID = ?", [$userid]);
|
|
$query = $sql->getRowArray();
|
|
if ($query != null) {
|
|
return $this->response->setJSON(['message' => 'Userid already exists']);
|
|
}
|
|
|
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
$this->db->transBegin();
|
|
try {
|
|
$sqlInsert = "
|
|
INSERT INTO gdc_cmod.dbo.USERS (USERID, USERROLEID, PASSWORD)
|
|
VALUES (?, ?, ?)
|
|
";
|
|
$this->db->query($sqlInsert, [$userid, $userroleid, $hashedPassword]);
|
|
|
|
$this->db->transCommit();
|
|
|
|
} catch (\Throwable $e) {
|
|
$this->db->transRollback();
|
|
return $this->response->setJSON(['message' => 'Server error']);
|
|
}
|
|
|
|
return $this->response->setJSON(['message' => 'User ' . $userid . ' Berhasil ditambahkan!']);
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
$userid = $input['userid'];
|
|
$userroleid = $input['userroleid'];
|
|
$password = $input['password'];
|
|
$password_2 = $input['password_2'];
|
|
|
|
if ($password != '' || $password_2 != '') {
|
|
if ($password != $password_2) {
|
|
return $this->response->setJSON(['message' => 'Password not the same']);
|
|
}
|
|
|
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
|
$sqlUpdate = "
|
|
UPDATE gdc_cmod.dbo.USERS
|
|
SET USERROLEID = ?,
|
|
PASSWORD = ?
|
|
WHERE USERID = ?
|
|
";
|
|
$fullUpdate = true;
|
|
|
|
} else {
|
|
$sqlUpdate = "
|
|
UPDATE gdc_cmod.dbo.USERS
|
|
SET USERROLEID = ?
|
|
WHERE USERID = ?
|
|
";
|
|
$fullUpdate = false;
|
|
}
|
|
|
|
$this->db->transBegin();
|
|
try {
|
|
|
|
if ($fullUpdate) {
|
|
$this->db->query($sqlUpdate, [$userroleid, $hashedPassword, $userid]);
|
|
} else {
|
|
$this->db->query($sqlUpdate, [$userroleid, $userid]);
|
|
}
|
|
|
|
$this->db->transCommit();
|
|
|
|
} catch (\Throwable $e) {
|
|
$this->db->transRollback();
|
|
return $this->response->setJSON(['message' => 'Terjadi kesalahan pada server.']);
|
|
}
|
|
|
|
return $this->response->setJSON(['message' => 'User ' . $userid . ' Berhasil Diupdate!']);
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
$this->db->transBegin();
|
|
try {
|
|
$sql = "DELETE FROM gdc_cmod.dbo.USERS WHERE USERID = ?";
|
|
$this->db->query($sql, [$id]);
|
|
|
|
if ($this->db->affectedRows() == 0) {
|
|
throw new \Exception('User not found or already deleted');
|
|
}
|
|
|
|
$this->db->transCommit();
|
|
} catch (\Throwable $e) {
|
|
$this->db->transRollback();
|
|
return $this->response->setStatusCode(500)->setJSON(['message' => 'Error deleting user: ' . $e->getMessage()]);
|
|
}
|
|
|
|
return $this->response->setJSON(['message' => 'User ' . $id . ' deleted successfully']);
|
|
}
|
|
|
|
}
|