forked from cynial/STBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth.php
More file actions
206 lines (178 loc) · 4.21 KB
/
Auth.php
File metadata and controls
206 lines (178 loc) · 4.21 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* STBlog Blogging System
*
* 基于Codeigniter的单用户多权限开源博客系统
*
* STBlog is an open source multi-privilege blogging System built on the
* well-known PHP framework Codeigniter.
*
* @package STBLOG
* @author Saturn <[email protected]>
* @copyright Copyright (c) 2009 - 2010, cnsaturn.com.
* @license GNU General Public License 2.0
* @link http://code.google.com/p/stblog/
* @version 0.1.0
*/
// ------------------------------------------------------------------------
/**
* STBLOG Auth Library Class
*
* 控制用户登陆和登出,以及一个简单的权限控制ACL实现
*
* @package STBLOG
* @subpackage Libraries
* @category Libraries
* @author Saturn <[email protected]>
* @link http://code.google.com/p/stblog/
*/
class Auth
{
/**
* 用户
*
* @access private
* @var array
*/
private $_user = array();
/**
* 是否已经登录
*
* @access private
* @var boolean
*/
private $_hasLogin = NULL;
/**
* 用户组
*
* @access public
* @var array
*/
public $groups = array(
'administrator' => 0,
'editor' => 1,
'contributor' => 2
);
/**
* CI句柄
*
* @access private
* @var object
*/
private $_CI;
/**
* 构造函数
*
* @access public
* @return void
*/
public function __construct()
{
/** 获取CI句柄 */
$this->_CI = & get_instance();
$this->_CI->load->model('users_mdl');
$this->_user = unserialize($this->_CI->session->userdata('user'));
log_message('debug', "STBLOG: Authentication library Class Initialized");
}
/**
* 判断用户是否已经登录
*
* @access public
* @return void
*/
public function hasLogin()
{
/** 检查session,并与数据库里的数据相匹配 */
if (NULL !== $this->_hasLogin)
{
return $this->_hasLogin;
}
else
{
if(!empty($this->_user) && NULL !== $this->_user['uid'])
{
$user = $this->_CI->users_mdl->get_user_by_id($this->_user['uid']);
if($user && $user['token'] == $this->_user['token'])
{
$user['activated'] = time();
$this->_CI->users_mdl->update_user($this->_user['uid'],$user);
return ($this->_hasLogin = TRUE);
}
}
return ($this->_hasLogin = FALSE);
}
}
/**
* 判断用户权限
*
* @access public
* @param string $group 用户组
* @param boolean $return 是否为返回模式
* @return boolean
*/
public function exceed($group, $return = false)
{
/** 权限验证通过 */
if(array_key_exists($group, $this->groups) && $this->groups[$this->_user['group']] <= $this->groups[$group])
{
return TRUE;
}
/** 权限验证未通过,同时为返回模式 */
if($return)
{
return FALSE;
}
/** 非返回模式 */
show_error('禁止访问:你的权限不足');
return;
}
/**
* 处理用户登出
*
* @access public
* @return void
*/
public function process_logout()
{
$this->_CI->session->sess_destroy();
redirect('admin/login');
}
/**
* 处理用户登录
*
* @access public
* @param array $user 用户信息
* @return boolean
*/
public function process_login($user)
{
/** 获取用户信息 */
$this->_user = $user;
/** 每次登陆时需要更新的数据 */
$this->_user['logged'] = now();
$this->_user['activated'] = $user['logged'];
/** 每登陆一次更新一次token */
$this->_user['token'] = sha1(now().rand());
if($this->_CI->users_mdl->update_user($this->_user['uid'],$this->_user))
{
/** 设置session */
$this->_set_session();
$this->_hasLogin = TRUE;
return TRUE;
}
return FALSE;
}
/**
* 设置session
*
* @access private
* @return void
*/
private function _set_session()
{
$session_data = array('user' => serialize($this->_user));
$this->_CI->session->set_userdata($session_data);
}
}
/* End of file Auth.php */
/* Location: ./application/libraries/Auth.php */