-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuth.php
99 lines (88 loc) · 2.53 KB
/
Auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
* (c) 2012 Damien Corpataux
*
* LICENSE
* This library is licensed under the GNU GPL v3.0 license,
* accessible at http://www.gnu.org/licenses/gpl-3.0.html
*
**/
/**
* Auth container interface.
*
* Session infos structure:
* - username: username of the authenticted user
* - roles: csv roles
* @package xFreemwork
*/
class xAuth {
protected $role_separator = ',';
function __construct() {}
/**
* Sets auth information.
* To be used upon successful authentication.
* @param string Username
* @param string Comma-separated roles names
* @param array Additional user information
*/
function set($username, $roles, $info = array()) {
$_SESSION['x']['xAuth']['username'] = $username;
$_SESSION['x']['xAuth']['roles'] = $roles;
$_SESSION['x']['xAuth']['info'] = $info;
$_SESSION['x']['xAuth']['logintime'] = @mktime();
}
function clear() {
unset($_SESSION['x']['xAuth']);
}
/**
* Returns the authenticated user username.
* @return string
*/
function username() {
return @$_SESSION['x']['xAuth']['username'];
}
/**
* Returns an array containing the authenticated user roles.
* @return array
*/
function roles() {
return array_filter(
array_map('trim', explode($this->role_separator, @$_SESSION['x']['xAuth']['roles']))
);
}
/**
* Returns an array containing the authenticated user additional information.
* If a key is given, returns the corresponding value, or null if key is invalid.
* @param string Optional array key
* @return array|scalar
*/
function info($key = null) {
if ($key) return @$_SESSION['x']['xAuth']['info'][$key];
else return @$_SESSION['x']['xAuth']['info'];
}
/**
* Returns the timestamp of the login point in time.
* @return integer
*/
function logintime() {
return @$_SESSION['x']['xAuth']['logintime'];
}
/**
* Returns true if the authenticated user has the given role(s).
* @param string|array rolename(s) to check
* @return bool
*/
function is_role($rolenames) {
$rolenames = xUtil::arrize($rolenames);
$userroles = $this->roles();
return array_intersect($rolenames, $userroles);
}
/**
* Returns true if the authenticated username is the given username.
* @param string rolename
* @return bool
*/
function is_user($username) {
return $username === $this->username();
}
}