Skip to content

Commit 886873a

Browse files
committed
user management implementation
1 parent f07f95b commit 886873a

File tree

6 files changed

+111
-9
lines changed

6 files changed

+111
-9
lines changed

config/routing.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
* Default site routing
44
*/
55

6-
$default['controller'] = "";
7-
$default['action'] = "";
8-
$default['query'] = array();
6+
$routing_default['controller'] = "";
7+
$routing_default['action'] = "";
8+
$routing_default['query'] = array();
9+
10+
/**
11+
* User login page
12+
*/
13+
$routing_user['controller'] = "";
14+
$routing_user['action'] = "";
15+
$routing_user['query'] = array();

core/basic.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ function setReporting()
3232
function callHook()
3333
{
3434
global $url; // defined in public/index.php
35+
$user = new User;
3536

3637
if ($url == "") {
37-
global $default;
38+
global $routing_default;
3839

39-
$controller = $default['controller'];
40-
$action = $default['action'];
41-
$queryString = $default['query'];
40+
$controller = $routing_default['controller'];
41+
$action = $routing_default['action'];
42+
$queryString = $routing_default['query'];
4243
} else {
4344
$urlArray = array();
4445
$urlArray = explode("/", $url);
@@ -53,6 +54,18 @@ function callHook()
5354
$controllerName = ucwords($controller) . 'Controller';
5455
$dispatch = new $controllerName($controller, $action);
5556

57+
if ($dispatch->requireUser == true && $user->isLoggedIn == false) {
58+
// deny access for requested action
59+
global $routing_user;
60+
61+
$controller = $routing_user['controller'];
62+
$action = $routing_user['action'];
63+
$queryString = $routing_user['query'];
64+
65+
$controllerName = ucwords($controller) . 'Controller';
66+
$dispatch = new $controllerName($controller, $action);
67+
}
68+
5669
if ((int)method_exists($controllerName, $action)) {
5770
call_user_func_array(array($dispatch, $action), $queryString);
5871
} else {

core/controller.class.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ abstract class Controller
1818
/** @var object Points to template engine class */
1919
protected $_template;
2020

21+
/** @var bool True if controller requires user to be logged in **/
22+
public $requireUser;
23+
2124
/** @var bool If true, page will be displayed */
2225
public $renderPage;
2326

@@ -38,7 +41,9 @@ function __construct($controller, $action)
3841

3942
$this->renderPage = true;
4043
$this->renderHeader = true;
41-
44+
45+
$this->requireUser = false;
46+
4247
$this->_controller = ucfirst($controller);
4348
$this->_action = $action;
4449

core/template.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function __construct($controller, $action)
3232
/**
3333
* Stores variables used on template
3434
*
35-
* @param string $name Variables name
35+
* @param string $name Variable name
3636
* @param mixed $value Value of given variable
3737
*/
3838
function set($name, $value)

core/user.class.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* User management class
5+
*
6+
* Manages handling of user information through session
7+
*/
8+
class User
9+
{
10+
/** @var bool True if user is loged in */
11+
public $isLogedIn;
12+
13+
/** @var array Stores custom fields */
14+
private $_variables;
15+
16+
17+
/**
18+
* Constructor function for User class
19+
*
20+
* Handles loading data from session
21+
*/
22+
function __construct()
23+
{
24+
if (isset($_SESSION['_user']['logged_in']) == true) {
25+
$this->isLogedIn = $_SESSION['_user']['logged_in'];
26+
} else {
27+
$this->isLogedIn = false;
28+
}
29+
30+
if ($this->isLogedIn == true) {
31+
$this->_variables = $_SESSION['_user']['variables'];
32+
}
33+
}
34+
35+
/**
36+
* Destructor function for User class
37+
*
38+
* Handles data storage into session
39+
*/
40+
function __destruct()
41+
{
42+
$_SESSION['_user']['logged_in'] = $this->isLogedIn;
43+
44+
if ($this->isLogedIn == true) {
45+
$_SESSION['_user']['variables'] = $this->_variables;
46+
}
47+
}
48+
49+
/**
50+
* Handles custom variable storage
51+
*
52+
* @param string $name Variable name
53+
* @param mixed $value Value of given variable
54+
*/
55+
function set($name, $value)
56+
{
57+
$this->_variables[$name] = $value;
58+
}
59+
60+
/**
61+
* Handles retreving values from custom variables
62+
*
63+
* @param strin $name Variable name
64+
* @return mixed Value of given variable, false if variable was not previously defined
65+
*/
66+
function get($name)
67+
{
68+
if (array_key_exists($name) == true) {
69+
$value = $this->_variables[$name];
70+
} else {
71+
$value = false;
72+
}
73+
74+
return $value;
75+
}
76+
}

public/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* This page is the single entry point for all requests made by the application
66
*/
7+
session_start();
78

89
/** directory separator string */
910
define('DS', DIRECTORY_SEPARATOR);

0 commit comments

Comments
 (0)