Skip to content

Commit

Permalink
add: new route to view generated summary report
Browse files Browse the repository at this point in the history
  • Loading branch information
FarrelAD committed Dec 22, 2024
1 parent 53092a7 commit 686b7e0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 17 deletions.
17 changes: 15 additions & 2 deletions app/controllers/AdminJurusanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Core\Controller;
use App\Models\Mahasiswa;
use App\Repository\{AdminRepository, MahasiswaRepository, StatistikRepository};
use App\Repository\{AdminRepository, MahasiswaRepository, StatistikRepository, ApiRepository};
use App\Models\Admin;
use Dompdf\{Dompdf, Options};

Expand Down Expand Up @@ -437,8 +437,21 @@ public function viewLaporan(): void
$this->view("templates/footer");
}

public function viewLaporanUmum(): void
public function viewLaporanUmum(string $api_key = null): void
{
if ($api_key != null) {
$result = ApiRepository::validateApiKey($api_key);
if (!$result) {
http_response_code(403);
$this->view("templates/header", [
'title' => 'Not Authorized!'
]);
$this->view("pages/general/not_authorized");
$this->view("templates/footer");
exit;
}
}

$d4_ti = StatistikRepository::getTotalPaidOffAndUnpaidStudent("D4 Teknik Informatika");
$d4_sib = StatistikRepository::getTotalPaidOffAndUnpaidStudent("D4 Sistem Informasi Bisnis");
$d2_ppls = StatistikRepository::getTotalPaidOffAndUnpaidStudent("D2 Pengembangan Perangkat Lunak Situs");
Expand Down
55 changes: 40 additions & 15 deletions app/middlewares/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@
namespace App\Middlewares;

use App\Controllers\AuthController;
use App\Repository\ApiRepository;

class AuthMiddleware
{

public static function checkAuth(string $user_role): void
{
if (!isset($_SESSION['user_id'])) {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}

if ($user_role == 'any') {
if (!in_array($_SESSION['role'], ['mahasiswa', 'Admin Prodi', 'Admin TA', 'Admin Jurusan'])) {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}
return;
switch ($user_role) {
case 'any':
if (isset($_SESSION['user_id'])) {
if (!in_array($_SESSION['role'], ['mahasiswa', 'Admin Prodi', 'Admin TA', 'Admin Jurusan'])) {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}
} else {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}
return;
case 'admin prodi':
case 'admin ta':
case 'admin jurusan':
if (isset($_SESSION['user_id'])) {
if (strcasecmp($_SESSION['role'], $user_role) != 0) {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}
} else {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}
return;
case 'desktop':
// $headers = getallheaders();
// $api_key = isset($headers['Authorization']) ? trim(str_replace('Bearer ', '', $headers['Authorization'])) : null;
// if ($api_key != null) {
// if (!ApiRepository::validateApiKey($api_key)) {
// (new AuthController)->sendNotAuthorizedWarning();
// exit;
// }
// } else {
// (new AuthController)->sendNotAuthorizedWarning();
// exit;
// }
return;
}

if (strcasecmp($_SESSION['role'], $user_role) != 0) {
(new AuthController)->sendNotAuthorizedWarning();
exit;
}

}
}
26 changes: 26 additions & 0 deletions app/repository/ApiRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Repository;

use App\Core\Database;
use App\Helpers\ErrorLog;

class ApiRepository
{
public static function validateApiKey(string $api_key): bool
{
try {
$stmt = Database::getConnection()->prepare(<<<SQL
SELECT id, user_id, api_key
FROM APP.ApiKeys
WHERE api_key = :api_key
SQL);
$stmt->bindValue(':api_key', $api_key, \PDO::PARAM_STR);
$stmt->execute();
return ($stmt->fetch() != false) ? true : false;
} catch (\PDOException $e) {
error_log(ErrorLog::formattedErrorLog($e->getMessage()), 3, LOG_FILE_PATH);
throw new \PDOException($e->getMessage());
}
}
}
7 changes: 7 additions & 0 deletions app/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,11 @@
'function' => 'checkAuth',
'args' => 'admin jurusan'
]
]);
Router::add('GET', '/api/laporan/laporan-umum/([a-zA-Z0-9]+)', AdminJurusanController::class, 'viewLaporanUmum', [
[
'class' => AuthMiddleware::class,
'function' => 'checkAuth',
'args' => 'desktop'
]
]);

0 comments on commit 686b7e0

Please sign in to comment.