forked from cynial/STBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.php
More file actions
332 lines (283 loc) · 7.4 KB
/
Copy pathusers.php
File metadata and controls
332 lines (283 loc) · 7.4 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* STBlog
*
* 基于Codeigniter的单用户多权限开源博客系统
*
*
* @package STBLOG
* @author Saturn
* @copyright Copyright (c) 2009 - 2010, cnsaturn.com.
* @license GNU General Public License 2.0
* @link http://code.google.com/p/stblog/
* @since Version 0.1
* @filesource
*/
// ------------------------------------------------------------------------
/**
* STBLOG Users Class
*
* 本类用于Users管理逻辑
*
* @package STBLOG
* @subpackage Controller
* @category Admin Controller
* @author Saturn <huyanggang@gmail.com>
* @link http://code.google.com/p/stblog/
*/
class Users extends ST_Auth_Controller {
/**
* 传递到对应视图的数据
*
* @access private
* @var array
*/
private $_data = array();
/**
* 当前用户ID
*
* @access private
* @var integer
*/
private $_uid = 0;
/**
* 解析函数
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
/** privilege confirm */
$this->auth->exceed('administrator');
/** common data */
$this->_data['parentPage'] = 'manage-posts';
$this->_data['currentPage'] = 'manage-users';
$this->_data['page_title'] = '管理用户';
}
/**
* 配置表单验证规则
*
* @access private
* @return void
*/
private function _load_validation_rules()
{
$this->form_validation->set_rules('uname', '用户名', 'required|trim|alpha_numeric|callback__name_check|strip_tags');
$this->form_validation->set_rules('password', '新的密码', 'required|min_length[6]|trim|matches[confirm]');
$this->form_validation->set_rules('confirm', '确认的密码', 'required|min_length[6]|trim');
$this->form_validation->set_rules('screenName', '昵称', 'trim|callback__screenName_check|strip_tags');
$this->form_validation->set_rules('url', '个人主页', 'trim|prep_url');
$this->form_validation->set_rules('mail', '邮箱地址', 'required|trim|valid_email|callback__email_check');
$this->form_validation->set_rules('group', '用户组', 'trim');
}
/**
* 回调函数:检查Name是否唯一
*
* @access public
* @param $str 输入值
* @return bool
*/
public function _name_check($str)
{
if($this->users_mdl->check_exist('name', $str, $this->_uid))
{
$this->form_validation->set_message('_name_check', '系统已经存在一个为 '.$str.' 的用户名');
return FALSE;
}
return TRUE;
}
/**
* 回调函数:检查Email是否唯一
*
* @access public
* @param $str 输入值
* @return bool
*/
public function _email_check($str)
{
if($this->users_mdl->check_exist('mail', $str, $this->_uid))
{
$this->form_validation->set_message('_email_check', '系统已经存在一个为 '.$str.' 的邮箱');
return FALSE;
}
return TRUE;
}
/**
* 回调函数:检查screenName是否唯一
*
* @access public
* @param $str 输入值
* @return bool
*/
public function _screenName_check($str)
{
if($this->users_mdl->check_exist('screenName', $str, $this->_uid))
{
$this->form_validation->set_message('_screenName_check', '系统已经存在一个为 '.$str.' 的昵称');
return FALSE;
}
return TRUE;
}
/**
* 添加一个用户
*
* @access private
* @return void
*/
private function _add_user()
{
$this->_data['page_title'] = '增加用户';
$this->_data['group'] = 'contributor';
$this->_load_validation_rules();
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/add_user',$this->_data);
}
else
{
$this->users_mdl->add_user(
array(
'name' => $this->input->post('uname',TRUE),
'password' => $this->input->post('password',TRUE),
'mail' => $this->input->post('mail',TRUE),
'url' => $this->input->post('url',TRUE),
'screenName'=> ($this->input->post('screenName'))?$this->input->post('screenName',TRUE):$this->input->post('uname',TRUE),
'created' => time(),
'activated' => 0,
'logged' => 0,
'group' => $this->input->post('group',TRUE)
)
);
$this->session->set_flashdata('success', '成功添加一个用户账号');
go_back();
}
}
/**
* 编辑一个用户
*
* @access private
* @param $uid
* @return void
*/
private function _edit_user($uid)
{
$this->_uid = $uid;
$user = $this->users_mdl->get_user_by_id($uid);
if(!$user)
{
show_error('用户不存在或已经被删除');
exit();
}
$this->_data['uid'] = $user['uid'];
$this->_data['uname'] = $user['name'];
$this->_data['screenName'] = $user['screenName'];
$this->_data['url'] = $user['url'];
$this->_data['mail'] = $user['mail'];
$this->_data['password'] = '';
$this->_data['group'] = $user['group'];
$this->_data['page_title'] = '编辑用户: '.$user['name'];
$this->_load_validation_rules();
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/add_user',$this->_data);
}
else
{
$this->users_mdl->update_user(
$uid,
array(
'password' => $this->input->post('password',TRUE),
'mail' => $this->input->post('mail',TRUE),
'url' => $this->input->post('url',TRUE),
'screenName'=> ($this->input->post('screenName'))?$this->input->post('screenName',TRUE):$this->input->post('name',TRUE),
'group' => $this->input->post('group',TRUE)
),
FALSE
);
$this->session->set_flashdata('success', '成功修改用户 '. $user['name'] .'的账号信息');
go_back();
}
}
/**
* 默认执行函数
*
* @access public
* @return void
*/
public function index()
{
redirect('admin/users/manage');
}
/**
* 用户管理列表
*
* @access public
* @return void
*/
public function manage()
{
$users = $this->users_mdl->get_users();
foreach($users->result() as $user)
{
$user->posts_num = $this->posts_mdl->get_posts_by_author($user->uid)->num_rows();
}
$this->_data['users'] = $users;
$this->load->view('admin/manage_users', $this->_data);
}
/**
* 用户操作分发器
*
* @access public
* @return void
*/
public function user()
{
if (FALSE === $this->uri->segment(4))
{
$this->_add_user();
}
else
{
$uid = $this->security->xss_clean($this->uri->segment(4));
is_numeric($uid)?$this->_edit_user($uid):show_error('禁止访问:危险操作');
}
}
/**
* 批量删除用户
*
* @access public
* @return void
*/
public function remove()
{
$users = $this->input->post('uid',TRUE);
$deleted = 0;
if($users && is_array($users))
{
foreach($users as $user)
{
/** 不能删除自己 */
if($user == $this->user->uid)
{
continue;
}
$posts_num = $this->posts_mdl->get_posts_by_author($user)->num_rows();
/** 不能删除文章数大于0的作者 */
if($posts_num > 0)
{
continue;
}
$this->users_mdl->remove_user($user);
$deleted++;
}
}
$msg = ($deleted > 0)?'用户已经删除':'没有用户被删除';
$notify = ($deleted > 0)?'success':'error';
$this->session->set_flashdata($notify, $msg);
go_back();
}
}
/* End of file Users.php */
/* Location: ./application/controllers/admin/Users.php */