FecadminbaseController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin;
  10. use Yii;
  11. use yii\helpers\Url;
  12. use fec\helpers\CUrl;
  13. use fec\helpers\CConfig;
  14. use fec\helpers\CCache;
  15. use fecadmin\models\AdminRole;
  16. use fecadmin\models\AdminUserRole;
  17. use fecadmin\models\AdminLog;
  18. use yii\base\InvalidValueException;
  19. /**
  20. * @author Terry Zhao <2358269014@qq.com>
  21. * @since 1.0
  22. */
  23. use fec\controllers\FecController;
  24. /**
  25. * fec admin 模块的controller配置
  26. */
  27. class FecadminbaseController extends FecController
  28. {
  29. public $enableCsrfValidation = false;
  30. public function getViewPath()
  31. {
  32. return Yii::getAlias('@fecadmin/views') . DIRECTORY_SEPARATOR . $this->id;
  33. }
  34. # 进行是否登录的验证
  35. public function __construct($id, $module, $config = []){
  36. $isGuest = Yii::$app->user->isGuest;
  37. //echo $isGuest;exit;
  38. //\fec\helpers\CSession::set('a',1);
  39. //echo \fec\helpers\CSession::get('a');
  40. if($isGuest){
  41. //$this->redirect("/fecadmin/login/index",200);
  42. CUrl::redirect("/fecadmin/login/index"); # 立即跳转
  43. }
  44. //echo ;
  45. //echo 1;
  46. //echo Yii::$app->controller->id;
  47. //exit;
  48. parent::__construct($id, $module, $config);
  49. }
  50. # 如果登录成功,则进行账户权限的验证。
  51. public function beforeAction($action)
  52. {
  53. # 当前的role key
  54. $controller_role_key = $this->getCurrentControllerRoleKey();
  55. # 配置中的各个不同的role_id 对应的role key
  56. $roles_keys = $this->getCurrentRoleKeys();
  57. # 如果当前的role_key 存在于 当前的权限role_keys数组中,则,可以使用role
  58. $roles_keys = is_array($roles_keys) ? $roles_keys : [];
  59. if($controller_role_key){
  60. if(!in_array($controller_role_key,$roles_keys)){
  61. # 如果不存在,则说明没有权限,禁止访问,exit
  62. echo '<span style=" padding: 12px;color: #cc0000;display: block;font-size: 40px;margin: 30px 50px;">
  63. You donot have role to visit this controller
  64. </span>';
  65. exit;
  66. }
  67. }
  68. parent::beforeAction($action);
  69. \fecadmin\helpers\CSystemlog::saveSystemLog();
  70. return true;
  71. }
  72. # 得到当前controller Role key
  73. public function getCurrentControllerRoleKey(){
  74. # 进行权限验证 如果不满足权限,则停止执行。
  75. $url_key = CUrl::getUrlKey();
  76. $url_key = trim($url_key,"/");
  77. $controller_role_key = '';
  78. if($url_key){
  79. $url_key_arr = explode("/",$url_key);
  80. $action = $this->action->id;
  81. if($url_key_arr[count($url_key_arr)-1] == $action){
  82. unset($url_key_arr[count($url_key_arr)-1]);
  83. }
  84. $controller_role_key = "/".implode("/",$url_key_arr);
  85. }
  86. return $controller_role_key;
  87. }
  88. # 得当当前用户role 对应的菜单role_key数组
  89. public function getCurrentRoleKeys(){
  90. $identity = Yii::$app->user->identity;
  91. $user_id = $identity->id ;
  92. $roles = AdminUserRole::find()->asArray()->where([
  93. 'user_id' => $user_id,
  94. ])->all();
  95. $AdminRole = new AdminRole;
  96. # 缓存读取role key
  97. if(!(CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE))){
  98. if(!CCache::set(CCache::ALL_ROLE_KEY_CACHE_HANDLE,$AdminRole->getAllRoleMenuRoleKey())){
  99. throw new InvalidValueException('save role key to cache error,check your cache if it can write!');
  100. }
  101. }
  102. $roleKeys = CCache::get(CCache::ALL_ROLE_KEY_CACHE_HANDLE);
  103. //var_dump($roleKeys);exit;
  104. //$role_ids = [];
  105. $menu_roles = [];
  106. if(!empty($roles)){
  107. foreach($roles as $role){
  108. $role_id = $role['role_id'];
  109. $menu_role = isset($roleKeys[$role_id]) ? $roleKeys[$role_id] : [];
  110. $menu_roles = array_merge($menu_roles,$menu_role);
  111. }
  112. }
  113. return $menu_roles;
  114. }
  115. # 保存系统日志。
  116. public function saveSystemLog(){
  117. $logConfig = CConfig::param("systemlog");
  118. //var_dump($logConfig);
  119. if(!is_array($logConfig) || !isset($logConfig['enable']) || !$logConfig['enable']){
  120. return;
  121. }
  122. $systemLog = new AdminLog();
  123. $user = Yii::$app->user->identity;
  124. if($user){
  125. $username = $user['username'];
  126. $person = $user['person'];
  127. $currentData= date('Y-m-d H:i:s');
  128. $url = CUrl::getCurrentUrl();
  129. $systemLog->account = $username;
  130. $systemLog->person = $person;
  131. $systemLog->created_at = $currentData;
  132. $systemLog->url = $url;
  133. $systemLog->save();
  134. }
  135. }
  136. }