forked from galichvitaliy/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.php
152 lines (129 loc) · 2.92 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Created by PhpStorm.
* User: galych
* Date: 14.01.15
* Time: 15:36
*/
namespace Mirage;
class Auth {
static $user = array();
static $cookie = false;
/**
*
*/
static function attempt($param = [], $remember = false) {
if(self::validate($param)) {
self::createSession(self::$user['id'], $remember); //auth
$cookie = [
'login' => self::$user['login'],
'photo' => self::$user['photo'],
'email' => self::$user['email']
];
setcookie('lli', base64_encode(serialize($cookie)), time()+1209600, "/");
$session = App::get('session');
$session->set('user', self::$user);
return true;
}
return false;
}
/**
* Determining If A User Is Authenticated
*/
static function check() {
$session = App::get('session');
return $session->get('auth');
}
/**
* Get user data
*/
static function data($key = false) {
$session = App::get('session');
$data = $session->get('user');
return $key ? (isset($data[$key]) ? $data[$key] : false) : $data;
}
/**
* Validating User Credentials Without Login
*/
static function validate($param = []) {
$ident = isset($param['ident']) ? $param['ident'] : 'email';
$user = DB::findOne('users', " $ident = ? ", [$param[$ident],]);
if(password_verify($param['password'], $user->password)) {
if($user->group > 0) {
$user['rights'] = self::loadRights((int)$user->group);
}
self::$user = $user;
return true;
}
return false;
}
/**
* Validating User Credentials Without Login
*/
static private function createSession($uid, $remember = false) {
$session = App::get('session');
if($remember) {
//$cookie = $uid . '|' . md5($uid . MD5_SOLT . $_SERVER['HTTP_USER_AGENT'] );
//setcookie(AUTH_ID, $cookie, time()+10800, "/");
}
$session->set('auth', $uid);
}
/**
* Determining If User Authed Via Remember
*/
static function viaRemember() {
return self::$cookie;
}
/**
* Log a user into the application by their ID
*/
static function loginUsingId($id) {
$session = App::get('session');
$user = DB::load('users', $id);
if($user->group > 0) {
$user['rights'] = self::loadRights((int)$user->group);
}
self::$user = $user;
$session->set('user', self::$user);
self::createSession($id);
}
/**
*
*/
static function logout() {
$session = App::get('session');
$session->invalidate();
}
/**
*
*/
static function hasRole($id = false) {
if(!self::check()) {
return false;
}
$rights = self::data('rights');
if(is_array($rights) && in_array($id, $rights)) {
return true;
}
return false;
}
/**
*
*/
static function isAdmin() {
return self::hasRole('general.admin_access');
}
/**
* Retrieve the authenticated user's ID
*/
static function id() {
return self::data('id');
}
/**
*
*/
static private function loadRights($group_id = false) {
$rights = DB::getCol('SELECT rule FROM users_rules WHERE users_groups_id = ?', [$group_id]);
return $rights;
}
}