CSY2028-assignment-2/jobs/controllers/User.php

63 lines
2.2 KiB
PHP
Raw Normal View History

2023-01-25 13:22:20 +00:00
<?php
namespace jobs\controllers;
class User {
private $usersTable;
private $catsTable;
private $vars;
2023-01-25 17:44:11 +00:00
public function __construct(\jobs\JobDatabaseTable $usersTable, \jobs\JobDatabaseTable $catsTable) {
2023-01-25 13:22:20 +00:00
$this->usersTable = $usersTable;
$this->catsTable = $catsTable;
$this->vars['cats'] = $this->catsTable->findAll();
$this->vars['response'] = '';
}
2023-02-05 23:35:31 +00:00
//Login page
public function login() { //Route: jobs.v.je/user/login
2023-01-25 13:22:20 +00:00
return ['template' => 'login.html.php',
'title' => 'Jo\'s Jobs- Login',
'vars' => $this->vars];
}
2023-02-05 23:35:31 +00:00
//Login page POST
public function loginSubmit() { //Route: jobs.v.je/user/login
2023-01-25 15:36:10 +00:00
if ($_POST['username'] != '' && $_POST['password'] != '') {
2023-02-04 22:19:29 +00:00
$user = $this->usersTable->find(["username"], ['value0' => $_POST['username']]);
2023-02-05 12:16:43 +00:00
if (password_verify($_POST['password'], $user[0]->password)) {
2023-01-25 17:02:57 +00:00
$user = $user[0];
2023-01-25 13:41:24 +00:00
$_SESSION['loggedin'] = $user->id;
$_SESSION['userType'] = $user->userType;
2023-01-25 13:41:24 +00:00
$this->vars['response'] = 'You are now logged in';
}
else {
unset($_SESSION['loggedin']);
unset($_SESSION['userType']);
2023-01-25 13:41:24 +00:00
$this->vars['response'] = 'Login Unsuccessful';
}
}
else {
if ($_POST['username'] == '') {
$this->vars['response'] .= "No Username was entered \n";
}
if ($_POST['password'] == '') {
2023-02-03 20:16:28 +00:00
$this->vars['response'] .= "No Password was entered \n";
2023-01-25 13:41:24 +00:00
}
$this->vars['response'] .= 'Login Unsuccessful';
}
2023-01-25 16:21:56 +00:00
return ['template' => 'response.html.php',
2023-01-25 13:41:24 +00:00
'title' => 'Jo\'s Jobs- Login',
'vars' => $this->vars
];
}
2023-02-05 23:35:31 +00:00
//Logout page
public function logout() { //Route: jobs.v.je/user/logout
2023-01-25 13:41:24 +00:00
unset($_SESSION['loggedin']);
unset($_SESSION['userType']);
2023-01-25 13:41:24 +00:00
$this->vars['response'] = 'Logged Out Successfully';
return ['template' => 'response.html.php',
'title' => 'Jo\'s Jobs- Logged Out',
'vars' => $this->vars];
}
2023-01-25 17:14:23 +00:00
}
?>