-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathFecadminbaseController.php
152 lines (136 loc) · 4.35 KB
/
FecadminbaseController.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
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecadmin;
use Yii;
use yii\helpers\Url;
use fec\helpers\CUrl;
use fec\helpers\CConfig;
use fec\helpers\CCache;
use fecadmin\models\AdminRole;
use fecadmin\models\AdminUserRole;
use fecadmin\models\AdminLog;
use yii\base\InvalidValueException;
/**
* @author Terry Zhao <[email protected]>
* @since 1.0
*/
use fec\controllers\FecController;
/**
* fec admin 模块的controller配置
*/
class FecadminbaseController extends FecController
{
public $enableCsrfValidation = false;
public function getViewPath()
{
return Yii::getAlias('@fecadmin/views') . DIRECTORY_SEPARATOR . $this->id;
}
# 进行是否登录的验证
public function __construct($id, $module, $config = []){
$isGuest = Yii::$app->user->isGuest;
//echo $isGuest;exit;
//\fec\helpers\CSession::set('a',1);
//echo \fec\helpers\CSession::get('a');
if($isGuest){
//$this->redirect("/fecadmin/login/index",200);
CUrl::redirect("/fecadmin/login/index"); # 立即跳转
}
//echo ;
//echo 1;
//echo Yii::$app->controller->id;
//exit;
parent::__construct($id, $module, $config);
}
# 如果登录成功,则进行账户权限的验证。
public function beforeAction($action)
{
# 当前的role key
$controller_role_key = $this->getCurrentControllerRoleKey();
# 配置中的各个不同的role_id 对应的role key
$roles_keys = $this->getCurrentRoleKeys();
# 如果当前的role_key 存在于 当前的权限role_keys数组中,则,可以使用role
$roles_keys = is_array($roles_keys) ? $roles_keys : [];
if($controller_role_key){
if(!in_array($controller_role_key,$roles_keys)){
# 如果不存在,则说明没有权限,禁止访问,exit
echo '<span style=" padding: 12px;color: #cc0000;display: block;font-size: 40px;margin: 30px 50px;">
You donot have role to visit this controller
</span>';
exit;
}
}
parent::beforeAction($action);
\fecadmin\helpers\CSystemlog::saveSystemLog();
return true;
}
# 得到当前controller Role key
public function getCurrentControllerRoleKey(){
# 进行权限验证 如果不满足权限,则停止执行。
$url_key = CUrl::getUrlKey();
$url_key = trim($url_key,"/");
$controller_role_key = '';
if($url_key){
$url_key_arr = explode("/",$url_key);
$action = $this->action->id;
if($url_key_arr[count($url_key_arr)-1] == $action){
unset($url_key_arr[count($url_key_arr)-1]);
}
$controller_role_key = "/".implode("/",$url_key_arr);
}
return $controller_role_key;
}
# 得当当前用户role 对应的菜单role_key数组
public function getCurrentRoleKeys(){
$identity = Yii::$app->user->identity;
$user_id = $identity->id ;
$roles = AdminUserRole::find()->asArray()->where([
'user_id' => $user_id,
])->all();
$AdminRole = new AdminRole;
# 缓存读取role key
if(!(CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE))){
if(!CCache::set(CCache::ALL_ROLE_KEY_CACHE_HANDLE,$AdminRole->getAllRoleMenuRoleKey())){
throw new InvalidValueException('save role key to cache error,check your cache if it can write!');
}
}
$roleKeys = CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE);
//var_dump($roleKeys);exit;
//$role_ids = [];
$menu_roles = [];
if(!empty($roles)){
foreach($roles as $role){
$role_id = $role['role_id'];
$menu_role = isset($roleKeys[$role_id]) ? $roleKeys[$role_id] : [];
$menu_roles = array_merge($menu_roles,$menu_role);
}
}
return $menu_roles;
}
# 保存系统日志。
public function saveSystemLog(){
$logConfig = CConfig::param("systemlog");
//var_dump($logConfig);
if(!is_array($logConfig) || !isset($logConfig['enable']) || !$logConfig['enable']){
return;
}
$systemLog = new AdminLog();
$user = Yii::$app->user->identity;
if($user){
$username = $user['username'];
$person = $user['person'];
$currentData= date('Y-m-d H:i:s');
$url = CUrl::getCurrentUrl();
$systemLog->account = $username;
$systemLog->person = $person;
$systemLog->created_at = $currentData;
$systemLog->url = $url;
$systemLog->save();
}
}
}