Skip to content

Commit

Permalink
Merge pull request #43 from wanderlust-group-project-1/nirmal
Browse files Browse the repository at this point in the history
Nirmal
  • Loading branch information
nsavinda authored Oct 31, 2023
2 parents f853889 + 109abd3 commit f76aabc
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function index(string $a = '', string $b = '', string $c = ''): void {
// show("from the index function");

// echo "This is home controller";
$data['email'] = empty($_SESSION['USER']) ? 'Guest' : $_SESSION['USER']->email;
// $data['email'] = empty($_SESSION['USER']) ? 'Guest' : $_SESSION['USER']->email;
$data = [];


$this->view('home', $data);
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Logout {
use Controller;

public function index(): void {
$data['email'] = empty($_SESSION['USER']) ? '' : $_SESSION['USER']->email;
// $data['email'] = empty($_SESSION['USER']) ? '' : $_SESSION['USER']->email;


setcookie('jwt_auth_token', '', time() - 1, '/');
session_destroy();
Expand Down
17 changes: 14 additions & 3 deletions app/core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public function loadController(): void {
if ($URL[0] == 'admin'){
unset($URL[0]);
// echo $URL[1];
$this->controller = 'Dashboard';
$this->method = 'index';

if(empty($URL[1])){
$URL[1] = 'dashboard';
}

$filename = "../app/controllers/Admin/" . ucfirst($URL[1]) . ".php";

Expand All @@ -69,6 +75,9 @@ public function loadController(): void {
}


AdminMiddleware::run_middleware($this->controller, $this->method);



} else {
$filename = "../app/controllers/" . ucfirst($URL[0]) . ".php";
Expand All @@ -91,6 +100,11 @@ public function loadController(): void {
unset($URL[1]);
}
}
$user = UserMiddleware::user(AuthMiddleware::run_middleware($this->controller, $this->method));

$_SESSION['USER'] = $user;
// show($user);

}

// $filename = "../app/controllers/" . ucfirst($URL[0]) . ".php";
Expand All @@ -116,10 +130,7 @@ public function loadController(): void {
// }
// }
// $this->runMiddleware();
$user = UserMiddleware::user(AuthMiddleware::run_middleware($this->controller, $this->method));
// make user global to all views

$_SESSION['USER'] = $user;
// show($user);
// show($_SESSION['USER']);

Expand Down
1 change: 1 addition & 0 deletions app/core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
require 'App.php';
require '../app/middlewares/AuthMiddleware.php';
require '../app/middlewares/UserMiddleware.php';
require '../app/middlewares/AdminMiddleware.php';
144 changes: 144 additions & 0 deletions app/middlewares/AdminMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
use Firebase\JWT\JWT;
use Firebase\JWT\Key;


class AdminMiddleware {

static $user = [];

static $allowedColumns = ['id', 'email', 'name', 'role'];

// filter user with allowed columns

public static function getUser(): array {


// return array_filter(Self::$user, function ($key) {
// return in_array($key, Self::$allowedColumns);
// }, ARRAY_FILTER_USE_KEY);

// check if user is admin
if (Self::$user['role'] == 'admin') {
return true;
}
else{
return false;
}


}



// protected static $user;

public static function run_middleware(string $controller, string $method): mixed {
// show($controller);
$authRequired = [
'Dashboard' => ['index', 'method2'],
'Controller2' => ['method3'],
'Customer' => ['index', 'edit', 'update'],
'Profile' => ['index', 'edit', 'update'],
// 'Profile' => ['index', 'edit', 'update'],
];
$unauthRequired = [
'Login' => ['index'],
'Signup' => ['index']
];

$currentController = ucfirst($controller);

if (isset($authRequired[$currentController]) &&
in_array($method, $authRequired[$currentController])) {
Self::is_authenticated();
}
if (isset($unauthRequired[$currentController]) &&
in_array($method, $unauthRequired[$currentController])) {
Self::not_authenticated();
}else {
Self::check();
}

// return Self::$user;
// return Self::getUser();
return true;


}

private static function check():mixed {
$cookieName = 'jwt_auth_token';
// print_r($_COOKIE);
if (!isset($_COOKIE[$cookieName])) {
setcookie('jwt_auth_token', '', time() - 1, '/');
// redirect('login');
return false;
}

$token = $_COOKIE[$cookieName];

try {
// echo $token;
$decoded = JWT::decode($token, new Key( SECRET_KEY, 'HS256'));
// The token is valid; you can access the claims as $decoded->id, $decoded->email,

$user = new UserModel;

// show($user);
// $data = []
// $userId = $decoded->user_id;
$data['email'] = $decoded->email;
// $email = $decoded->email;
// show($user->first($data));

$userData = $user->first($data);
if(!$userData){
setcookie('jwt_auth_token', '', time() - 1, '/');
// redirect('login');
return false;
}
// return $userData;
// std class to array
// $this->$user = (array) $userData;

// check if user is admin
// Self::$user = (array) $userData;

if ($userData->role == 'admin') {
Self::$user = (array) $userData;
$_SESSION['ADMIN'] = (array) $userData;
}else{
setcookie('jwt_auth_token', '', time() - 1, '/');
redirect('admin/login');
// return false;
}




// Authorization checks

} catch (Exception $e) {
// Token is invalid; return an error response
// http_response_code(401);
// echo json_encode(['error' => 'Token is invalid']);
// exit();
setcookie('jwt_auth_token', '', time() - 1, '/');
// redirect('login');
return false;
}
return true;
}
public static function is_authenticated():void {
if(!self::check()){
redirect('admin/login');
}
}
public static function not_authenticated():void {
if(self::check()){
redirect('admin/dashboard');
}
}
}
?>
3 changes: 3 additions & 0 deletions app/middlewares/UserMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ static function user($user){
$user = (object) array_merge((array) $user, (array) $rental_service);
return $user;
}
if ($user['role'] == 'admin') {
return $user;
}
}

}
Expand Down
4 changes: 3 additions & 1 deletion app/views/components/navbar-auth.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<nav class="navbar">
<nav class="auth-nav">
<!-- <div class="auth-nav"> -->
<div class="logo" style="text-align: center;">
<img src="<?= ROOT_DIR ?>/assets/images/logo.png" alt="logo" style="display: block; margin: auto;">
<!-- </div> -->
</div>


Expand Down
24 changes: 22 additions & 2 deletions app/views/components/navbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<li class="nav-menu-item"><a href="#">Tips & Knowhows</a></li>
<li class="nav-menu-item"><a href="#">Complaints</a></li>
</ul>
<?php if (isset($_SESSION['USER'])){ $user = $_SESSION['USER']; ?>
<?php if (isset($_SESSION['USER'] ) && $_SESSION['USER']['role'] != 'admin' ){ $user = $_SESSION['USER']; ?>

<!-- profile avatar with dropdown -->

Expand All @@ -27,7 +27,27 @@
</div>


<?php }else { ?>
<?php }else if(isset($_SESSION['USER']) && $_SESSION['USER']['role'] == 'admin' ){ $admin = $_SESSION['USER']; ?>

<!-- profile avatar with dropdown -->
<div class="profile-avatar">
<img src="<?php echo ROOT_DIR?>/assets/images/1.png" alt="profile picture">
<div class="dropdown-menu" id="nav-dropdown">
<ul>
<li><a href="<?= ROOT_DIR ?>/admin/dashboard">Dashboard</a></li>
<!-- <li><a href="<?= ROOT_DIR ?>/settings">Settings</a></li> -->
<li><a href="<?= ROOT_DIR ?>/logout">Logout</a></li>
</ul>
</div>
</div>


<?php }else

{ ?>



<div class="login-button"><a href="<?= ROOT_DIR ?>/login">Login</a></div>

<?php } ?>
Expand Down
24 changes: 22 additions & 2 deletions public/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@
color: #4C5039;
}

/* Auth Nav bar */

.auth-nav {
background-color: #F4F4F4;
display: flex;
justify-content: space-around;
align-items: center;
padding: 10px 20px; /* Set a background color for the navbar */
}

.auth-nav .logo {
text-align: center;
}

.auth-nav .logo img {
display: block;
margin: auto;
}



/* nav profile avatar*/
Expand Down Expand Up @@ -368,7 +387,8 @@ body {
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
height:90%;
/* overflow-x: hidden; */
border-radius: 10px;
background: #ECECEC;
backdrop-filter: blur(67px);
Expand All @@ -380,7 +400,7 @@ body {
flex-direction: column;
max-width: 450px;
width:450px;
height: 600px;
height: fit-content;
padding: 40px;
border-radius: 10px;
background-color: #fff;
Expand Down

0 comments on commit f76aabc

Please sign in to comment.