Files
BE_IBL/application/controllers/Auth_placeholder.php
2026-04-15 15:24:53 +07:00

132 lines
5.1 KiB
PHP

<?php
class Auth_placeholder extends CI_Controller {
var $SECRET_KEY = "--one_api-secret-2019-04-01";
public function index()
{
echo "AUTH API";
}
public function __construct()
{
parent::__construct();
}
function corss() {
global $_SERVER;
if (isset($_SERVER["HTTP_ORIGIN"])) {
header('Access-Control-Allow-Origin: ' . $_SERVER["HTTP_ORIGIN"]);
} else {
header('Access-Control-Allow-Origin: */*' );
}
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
if ( isset($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "OPTIONS") {
http_response_code(200);
echo json_encode("OK");
exit;
}
}
function menu() {
$this->corss();
$rst = [];
$rst[] = [
"privilege" => "Admin Lab",
"menus" => [
["title" => "Pasien", "path" => "/masterdata/pasien" ],
["title" => "Pemeriksaan", "path" => "/masterdata/px" ],
]
];
$rst[] = [
"privilege" => "Keu",
"menus" => [
["title" => "Company", "path" => "/masterdata/company" ],
["title" => "Agreement", "path" => "/masterdata/agreement" ],
]
];
echo json_encode(["status" => "OK", "data" => $rst]);
}
function old_menu() {
//for preflight
$this->corss();
$rst = [
["id"=>1,"icon" => "SendIcon","label" => "Menu 01", "target" => "url:/other_module" , "level" => 1],
["id"=>2,"icon" => "DrafsIcon","label" => "Menu 02", "target" => "", "level" => 1,
"children" => [
["id"=>3,"icon" => "DrafsIcon","label" => "Sub Menu 0201", "target" => "link:/def", "level" => 2,],
["id"=>4,"icon" => "StarIcon","label" => "Sub Menu 0202", "target" => "link:/def", "level" => 2],
["id"=>5,"icon" => "InboxIcon","label" => "Sub Menu 0203", "target" => "link:/def", "level" => 2],
["id"=>6,"icon" => "","label" => "Sub Menu 0204", "target" => "link:/def", "level" => 2,
"children" => [
["id"=>7,"icon" => "DrafsIcon","label" => "Sub Menu 020401", "target" => "link:/def", "level" => 3],
["id"=>8,"icon" => "StarIcon","label" => "Sub Menu 020402", "target" => "link:/def", "level" => 3],
["id"=>9,"icon" => "InboxIcon","label" => "Sub Menu 020403", "target" => "link:/def", "level" => 3],
["id"=>10,"icon" => "","label" => "Sub Menu 020404", "target" => "link:/def" , "level" => 3],
],
]
],
],
["id"=>11,"icon" => "InboxIcon","label" => "Menu 03", "target" => "url:/other_module", "level" => 1 ],
["id"=>12,"icon" => "StarIcon","label" => "Menu 04", "target" => "url:/other_module", "level" => 1 ],
["id"=>13,"icon" => "SendIcon","label" => "Menu 05", "target" => "url:/other_module" , "level" => 1],
];
echo json_encode(["status"=>"OK","records" => $rst ]);
}
function check_token() {
$req_headers = $headers = array_change_key_case(getallheaders(), CASE_LOWER);
$isLogin = false;
$user = [];
if (isset($req_headers["authorization"])) {
//have bearer
list($bearer, $token) = explode(" ", trim($req_headers["authorization"]));
$user =JWT::decode($token,$this->SECRET_KEY);
$isLogin = true;
}
return [$isLogin,$user];
}
function info() {
list($isLogin,$user) = $this->check_token();
echo json_encode(["isLogin" => $isLogin,"user" => $user]);
}
function refresh_token () {
list($isLogin,$user) = $this->check_token();
if (! $isLogin) {
$this->sys_error("Invalid Token");
} else {
$token_expire = date("Y-m-d H:i:s",strtotime("now + 5 minute"));
$user["token_expire"] = $token_expire;
$token = JWT::encode($user,$this->SECRET_KEY);
$data = array(
"status" => "OK",
"user" => $user,
"token" => $token,
"token_expire" => $token_expire
);
echo json_encode($data);
}
}
function login() {
$this->corss();
$prm = json_decode(file_get_contents("php://input"),true);
if ($prm["username"] =="admin" && $prm["password"] == "123") {
$token_expire = date("Y-m-d H:i:s",strtotime("now + 5 minute"));
$user = ["name" => "The Admin" , "level" => "Admin", "expire" => $token_expire];
$this->load->library("Jwt");
$token = JWT::encode($user,$this->SECRET_KEY);
echo json_encode([
"status" => "OK",
"token" => $token,
"token_expire" => $token_expire,
"redirect" => "/xinfo",
"user" => $user
]);
exit;
}
echo json_encode(["status" => "ERR" , "message" => "Invalid username/password"]);
}
function logout()
{
echo json_encode(["status" => "OK" , "message" => ""]);
}
}
?>