From 7dd590ac0668c47e903513cb9606cae43dabe13b Mon Sep 17 00:00:00 2001 From: Farrel Augusta Dinata Date: Sat, 30 Nov 2024 19:39:56 +0700 Subject: [PATCH 1/6] add auth for login and connect to database --- app/controllers/AuthController.php | 112 +++++++++++++++-------------- app/core/Database.php | 9 +-- app/models/Admin.php | 8 ++- app/models/User.php | 31 ++++++++ app/routes/web.php | 3 +- app/views/pages/login.php | 2 +- 6 files changed, 101 insertions(+), 64 deletions(-) create mode 100644 app/models/User.php diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 5339f2a..540afcc 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -3,60 +3,26 @@ namespace App\Controllers; use App\Core\Controller; -use App\Models\Admin; +use App\Models\{User, Admin, Students}; -class AuthController extends Controller { +class AuthController extends Controller +{ + private User $user; private Admin $admin; + private Students $student; - public function __construct() { - $this->admin = new Admin(); - } - - public function viewLogin(): void { - $data['title'] = "Login"; - $this->view("templates/header", $data); - $this->view("pages/login"); - $this->view("templates/footer"); - } - - public function loginProcess(): void { - /** - * ====================================================== - * NOTE: THIS IS ONLY FOR TEMPORARY PURPOSES - * DON'T KEEP THIS CODE IN FUTURE DEVELOPMENT - * - * REPLACE THIS WITH DATABASE INTEGRATION - * AND MAKE IT SECURE! - * ====================================================== - */ - if (isset($_POST["user_id"]) && isset($_POST['password'])) { - $user_id = $_POST['user_id']; - $password = $_POST['password']; - } else { - // WARNING: A warning to user if they do not send user ID and password - // Stop the login process - echo "USER ID AND PASSSWORD NOT FOUND!"; - return; - } - session_start(); - if ($user_id == "mahasiswa" && $password == "123") { - $_SESSION['user_id'] = $user_id; - $_SESSION['role'] = 'student'; - header('Location: /dashboard'); - } else if ($user_id == "admin" && $password == "admin123") { - $_SESSION['user_id'] = $user_id; - $_SESSION['role'] = 'admin'; - header('Location: /dashboard'); - } else { - // WARNING: A warning to user if they send wrong user ID and password - echo "WRONG USER ID AND PASSWORD"; - } + public function __construct() + { + $this->user = new User(); + $this->admin = new Admin(); + $this->student = new Students(); } - public function adminLogin(): void { + public function adminLogin(): void + { if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; @@ -72,20 +38,62 @@ public function adminLogin(): void { if ($result === false) { http_response_code(404); echo json_encode([ - "status"=>"failed", - "message"=>"User not found" + "status" => "failed", + "message" => "User not found" ]); } else { http_response_code(200); echo json_encode([ - "status"=>"success", - "message"=>"User successfully found", - "data"=>$result + "status" => "success", + "message" => "User successfully found", + "data" => $result ]); } } - public function logout(): void { + public function login(): void + { + session_start(); + if (!isset($_POST["user_id"]) || !isset($_POST['password'])) { + $data['title'] = "Login"; + $data['message'] = "User fail to authenticate!"; + $this->view("templates/header", $data); + $this->view("pages/user_fail_authenticate", $data); + $this->view("templates/footer"); + return; + } + + $user = $this->user->getUserDataByUserIDAndPassword($_POST['user_id'], $_POST['password']); + + if ($user != false) { + $data['title'] = "Dashboard"; + $this->view("templates/header", $data); + + switch ($user['role']) { + case 'mahasiswa': + $this->view("pages/student/dashboard"); + break; + case 'Admin TA': + $this->view("pages/admin_ta/dashboard"); + break; + case 'Admin Prodi': + $this->view("pages/admin_prodi/dashboard"); + break; + case 'Admin Jurusan': + $this->view("pages/admin_jurusan/dashboard"); + break; + } + + $this->view("templates/footer"); + } else { + $data['message'] = "User fail to authenticate! Wrong user id or password"; + $this->view("pages/user_fail_authenticate", $data); + } + + } + + public function logout(): void + { $data['title'] = "Logout"; $this->view("templates/header", $data); $this->view("pages/logout"); diff --git a/app/core/Database.php b/app/core/Database.php index a392207..b45699c 100644 --- a/app/core/Database.php +++ b/app/core/Database.php @@ -27,13 +27,8 @@ public function __construct() } } - public function query(string $query): \PDOStatement + public function getConnection(): \PDO { - return $this->conn->query($query); - } - - public function prepareQuery(string $query): \PDOStatement - { - return $this->conn->prepare($query); + return $this->conn; } } diff --git a/app/models/Admin.php b/app/models/Admin.php index 99aae1b..354b682 100644 --- a/app/models/Admin.php +++ b/app/models/Admin.php @@ -7,11 +7,13 @@ use App\Core\Model; -class Admin extends Model { - public function checkUserIsAvailable(string $username, string $password): bool|array { +class Admin extends Model +{ + public function checkUserIsAvailable(string $username, string $password): bool|array + { $query = "SELECT * FROM USERS.Admin WHERE nama_lengkap = ? AND password = ?"; - $stmt = $this->db->prepareQuery($query); + $stmt = $this->db->getConnection()->prepare($query); $stmt->bindParam(1, $username); $stmt->bindParam(2, $password); $stmt->execute(); diff --git a/app/models/User.php b/app/models/User.php new file mode 100644 index 0000000..c868545 --- /dev/null +++ b/app/models/User.php @@ -0,0 +1,31 @@ +db->getConnection()->prepare($query); + $stmt->bindParam(1, $user_id, \PDO::PARAM_STR); + $stmt->bindParam(2, $password, \PDO::PARAM_STR); + $stmt->bindParam(3, $user_id, \PDO::PARAM_STR); + $stmt->bindParam(4, $password, \PDO::PARAM_STR); + $stmt->execute(); + return $stmt->fetch(\PDO::FETCH_ASSOC); + } +} diff --git a/app/routes/web.php b/app/routes/web.php index d0e5149..67374a9 100644 --- a/app/routes/web.php +++ b/app/routes/web.php @@ -9,9 +9,10 @@ Router::add('GET', '/', HomeController::class, 'index'); Router::add('GET', '/kontak', HomeController::class,'contact'); Router::add('GET','/login', AuthController::class,'viewLogin'); -Router::add('POST', '/auth', AuthController::class,'loginProcess'); Router::add('GET', '/dashboard', HomeController::class,'dashboard'); +Router::add('POST', '/auth', AuthController::class,'login'); + Router::add('GET', '/tugas-akhir', StudentsController::class, 'tugasAkhir'); Router::add('GET', '/ta-terkirim', StudentsController::class, 'tugasAkhirTerkirim'); Router::add('GET', '/ta-terverif', StudentsController::class, 'tugasAkhirTerverif'); diff --git a/app/views/pages/login.php b/app/views/pages/login.php index e0c1535..acccd75 100644 --- a/app/views/pages/login.php +++ b/app/views/pages/login.php @@ -2,7 +2,7 @@

FINALIS JTI

-
+
From 77f8fc48f154e3cf71b03a704f36f2f31e2b7300 Mon Sep 17 00:00:00 2001 From: Farrel Augusta Dinata Date: Sat, 30 Nov 2024 19:43:12 +0700 Subject: [PATCH 2/6] refactor: change file name from sidebar_admin to sidebar --- .../admin_prodi/{sidebar_admin.php => sidebar.php} | 0 app/views/pages/admin_prodi/dashboard.php | 2 +- app/views/pages/admin_prodi/detail_permintaan.php | 2 +- app/views/pages/admin_prodi/permintaan_verifikasi.php | 6 +++--- 4 files changed, 5 insertions(+), 5 deletions(-) rename app/views/components/admin_prodi/{sidebar_admin.php => sidebar.php} (100%) diff --git a/app/views/components/admin_prodi/sidebar_admin.php b/app/views/components/admin_prodi/sidebar.php similarity index 100% rename from app/views/components/admin_prodi/sidebar_admin.php rename to app/views/components/admin_prodi/sidebar.php diff --git a/app/views/pages/admin_prodi/dashboard.php b/app/views/pages/admin_prodi/dashboard.php index 6a112ae..14a4c01 100644 --- a/app/views/pages/admin_prodi/dashboard.php +++ b/app/views/pages/admin_prodi/dashboard.php @@ -1,6 +1,6 @@
- +
diff --git a/app/views/pages/admin_prodi/detail_permintaan.php b/app/views/pages/admin_prodi/detail_permintaan.php index 440bf60..9fc5284 100644 --- a/app/views/pages/admin_prodi/detail_permintaan.php +++ b/app/views/pages/admin_prodi/detail_permintaan.php @@ -1,6 +1,6 @@
- +
diff --git a/app/views/pages/admin_prodi/permintaan_verifikasi.php b/app/views/pages/admin_prodi/permintaan_verifikasi.php index 414a02b..eed643b 100644 --- a/app/views/pages/admin_prodi/permintaan_verifikasi.php +++ b/app/views/pages/admin_prodi/permintaan_verifikasi.php @@ -1,6 +1,6 @@
- +
@@ -8,8 +8,8 @@
-

Permintaan Verifikasi

- +

Permintaan Verifikasi

+
Seluruh Permintaan
From 167c9c0d3adc3599c65ea0daac328f3a11ad599d Mon Sep 17 00:00:00 2001 From: Farrel Augusta Dinata Date: Sat, 30 Nov 2024 19:47:41 +0700 Subject: [PATCH 3/6] bring back method viewLogin to display login page --- app/controllers/AuthController.php | 7 +++++++ app/routes/web.php | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 540afcc..66257d6 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -21,6 +21,13 @@ public function __construct() $this->student = new Students(); } + public function viewLogin(): void { + $data['title'] = "Login"; + $this->view("templates/header", $data); + $this->view("pages/login"); + $this->view("templates/footer"); + } + public function adminLogin(): void { if (isset($_POST['username']) && isset($_POST['password'])) { diff --git a/app/routes/web.php b/app/routes/web.php index 67374a9..11cdaf8 100644 --- a/app/routes/web.php +++ b/app/routes/web.php @@ -8,10 +8,10 @@ Router::add('GET', '/', HomeController::class, 'index'); Router::add('GET', '/kontak', HomeController::class,'contact'); -Router::add('GET','/login', AuthController::class,'viewLogin'); Router::add('GET', '/dashboard', HomeController::class,'dashboard'); -Router::add('POST', '/auth', AuthController::class,'login'); +Router::add('GET','/login', AuthController::class,'viewLogin'); +Router::add('POST', '/login', AuthController::class,'login'); Router::add('GET', '/tugas-akhir', StudentsController::class, 'tugasAkhir'); Router::add('GET', '/ta-terkirim', StudentsController::class, 'tugasAkhirTerkirim'); From 6198390633137107bf3c1a04cadd5442d6786288 Mon Sep 17 00:00:00 2001 From: Farrel Augusta Dinata Date: Sat, 30 Nov 2024 19:54:56 +0700 Subject: [PATCH 4/6] fix: action form login to /login with method post --- app/views/pages/login.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/pages/login.php b/app/views/pages/login.php index acccd75..53944db 100644 --- a/app/views/pages/login.php +++ b/app/views/pages/login.php @@ -2,7 +2,7 @@

FINALIS JTI

- +
From bae75c8f1a0534d2f81a7142cb59ae1680614bf7 Mon Sep 17 00:00:00 2001 From: Farrel Augusta Dinata Date: Sat, 30 Nov 2024 20:14:20 +0700 Subject: [PATCH 5/6] add logout account feature --- app/controllers/AuthController.php | 28 ++++---------------- app/routes/web.php | 1 + app/views/components/admin_prodi/sidebar.php | 24 +++++++++++++++-- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 66257d6..4975345 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -73,25 +73,7 @@ public function login(): void $user = $this->user->getUserDataByUserIDAndPassword($_POST['user_id'], $_POST['password']); if ($user != false) { - $data['title'] = "Dashboard"; - $this->view("templates/header", $data); - - switch ($user['role']) { - case 'mahasiswa': - $this->view("pages/student/dashboard"); - break; - case 'Admin TA': - $this->view("pages/admin_ta/dashboard"); - break; - case 'Admin Prodi': - $this->view("pages/admin_prodi/dashboard"); - break; - case 'Admin Jurusan': - $this->view("pages/admin_jurusan/dashboard"); - break; - } - - $this->view("templates/footer"); + header('Location: /dashboard'); } else { $data['message'] = "User fail to authenticate! Wrong user id or password"; $this->view("pages/user_fail_authenticate", $data); @@ -101,9 +83,9 @@ public function login(): void public function logout(): void { - $data['title'] = "Logout"; - $this->view("templates/header", $data); - $this->view("pages/logout"); - $this->view("templates/footer"); + session_start(); + session_unset(); + session_destroy(); + header('Location: /'); } } \ No newline at end of file diff --git a/app/routes/web.php b/app/routes/web.php index 11cdaf8..4f6ebae 100644 --- a/app/routes/web.php +++ b/app/routes/web.php @@ -12,6 +12,7 @@ Router::add('GET','/login', AuthController::class,'viewLogin'); Router::add('POST', '/login', AuthController::class,'login'); +Router::add('POST', '/logout', AuthController::class,'logout'); Router::add('GET', '/tugas-akhir', StudentsController::class, 'tugasAkhir'); Router::add('GET', '/ta-terkirim', StudentsController::class, 'tugasAkhirTerkirim'); diff --git a/app/views/components/admin_prodi/sidebar.php b/app/views/components/admin_prodi/sidebar.php index 1048561..c52af30 100644 --- a/app/views/components/admin_prodi/sidebar.php +++ b/app/views/components/admin_prodi/sidebar.php @@ -21,7 +21,7 @@ +
+ + + + \ No newline at end of file From 89f748d089b902c2a7c4241257d63f914f74cfbb Mon Sep 17 00:00:00 2001 From: Farrel Augusta Dinata Date: Sat, 30 Nov 2024 20:34:42 +0700 Subject: [PATCH 6/6] add user session and make code to redirect to their dashboard --- app/controllers/AuthController.php | 3 +++ app/controllers/HomeController.php | 14 ++++++++++++-- app/models/User.php | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 4975345..900df33 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -73,6 +73,9 @@ public function login(): void $user = $this->user->getUserDataByUserIDAndPassword($_POST['user_id'], $_POST['password']); if ($user != false) { + $_SESSION['user_id'] = $user['user_id']; + $_SESSION['full_name'] = $user['nama_lengkap']; + $_SESSION['role'] = $user['role']; header('Location: /dashboard'); } else { $data['message'] = "User fail to authenticate! Wrong user id or password"; diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.php index 62ce504..1b06e7a 100644 --- a/app/controllers/HomeController.php +++ b/app/controllers/HomeController.php @@ -25,12 +25,22 @@ public function dashboard(): void { $data['title'] = "Dashboard"; $data['active_page'] = "dashboard"; switch ($_SESSION['role']) { - case 'admin': + case 'Admin Prodi': $this->view("templates/header", $data); $this->view("pages/admin_prodi/dashboard", $data); $this->view("templates/footer"); break; - case 'student': + case 'Admin TA': + $this->view("templates/header", $data); + $this->view("pages/admin_ta/dashboard", $data); + $this->view("templates/footer"); + break; + case 'Admin Jurusan': + $this->view("templates/header", $data); + $this->view("pages/admin_jurusan/dashboard", $data); + $this->view("templates/footer"); + break; + case 'mahasiswa': $this->view("templates/header", $data); $this->view("pages/student/dashboard", $data); $this->view("templates/footer"); diff --git a/app/models/User.php b/app/models/User.php index c868545..3f9e7b5 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -11,11 +11,11 @@ class User extends Model public function getUserDataByUserIDAndPassword(string $user_id, string $password): bool|array { $query = <<