Base.class.php
2.08 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
<?php
/**
* 默认控制器
*/
class Controller_Admin_Base extends Controller_Abstract
{
/**
* 当前登录的用户信息
*
* @var array
*/
protected $_admin = array() ;
/**
* 管理账户列表
*
* @var unknown_type
*/
private $_admins = array
(
//array('username' => '用户名', 'password' => '密码'),
// array('username' => 'yohoadmin', 'password' => '9646abcdef'),
array('username' => 'test', 'password' => '123456')
);
/**
* 检测权限
*
*/
protected function beforeExecute()
{
//获取session
$session = Util_Utils_SafeCookie::get('_admin');
$session = unserialize($session);
//非默认控制器,则校验权限
if ($this->_request->controller_name != 'default' && !$session)
{
//跳转至登录页
header('Location: ' . url('default/index'));
exit();
}
$this->_admin = $session ;
$this->_view['_ctx'] = $this->_request ;
}
/**
* 处理登录
* @param string $username
* @param string $password
*
*/
protected function login($username, $password)
{
if ($username == '' || $password =='')
{
return false ;
}
$exitUser = array() ;
//查找用户
foreach ($this->_admins as $k => $val)
{
if ($val['username'] == $username)
{
$exitUser = $this->_admins[$k];
$exitUser['id'] = $k ;
}
}
if (empty($exitUser))
{
return false ;
}
//校验密码
if ($password == $exitUser['password'])
{
$exitUser = serialize($exitUser);
//通过
Util_Utils_SafeCookie::set('_admin', $exitUser);
return true ;
}
return false ;
}
/**
* 登出
*
*/
protected function logout()
{
Util_Utils_SafeCookie::clear('_admin');
}
}