PermissionController.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace KIF\Core;
  3. /**
  4. * 权限管理控制器
  5. * 对整个控制器设定访问权限,也可以对控制器中的单个action设定访问权限
  6. * @author li.shuming@kimiss.com
  7. */
  8. use KIF\Verify;
  9. class PermissionController extends \KIF\Core\BKController {
  10. /**
  11. * 开启权限管理
  12. * 如果这里设置为true,那么只要继承了Permission的控制器,内部所有的行为都需要有权限才允许操作。
  13. * 默认为true,设置为false 时,可以在需要有权限操作的方法里加上 $this->requireCompetence()方法,要求有操作权限
  14. * @var Boolean
  15. */
  16. static private $useCompetence = KIF_CREATE;
  17. /**
  18. * 用户访问权限
  19. * @param Boolean $isHalt 没有权限访问时,是否要停机。默认停机,跳转到错误提示页
  20. * @return Boolean
  21. */
  22. public function requireCompetence($isHalt = true) {
  23. if (!self::$useCompetence) {
  24. return true;
  25. }
  26. if (is_null($isHalt)) {
  27. $isHalt = true;
  28. }
  29. # 先登录
  30. parent::requireLogin();
  31. if (!self::isCompetence()) {
  32. if ($isHalt) {
  33. #TODO
  34. self::no_permission_exit();
  35. }
  36. return false;
  37. }
  38. return true;
  39. }
  40. /**
  41. * 当前用户是否有权访问
  42. * @return Boolean
  43. */
  44. private function isCompetence() {
  45. if (!self::isLogin()) {
  46. return false;
  47. }
  48. # 超级管理员啥权限都有
  49. if (self::isSuperadmin()) {
  50. return true;
  51. }
  52. # 以下普通帐号权限
  53. $c = Request::getInstance()->param('c');
  54. $arr_class_path = array_map(function ($tmpV) {
  55. return lcfirst($tmpV);
  56. }, explode('_', $c));
  57. $c = implode('_', $arr_class_path);
  58. $a = Request::getInstance()->param('a');
  59. $requestParams = array( //当前请求参数名称
  60. 'app_name' => lcfirst(Config::getInstance()->get('Namespace')),
  61. 'control_name' => $c,
  62. 'action_name' => $a ? lcfirst($a) : 'default',
  63. );
  64. $is_cpt = true;
  65. //普通帐号没有的权限
  66. $notCompetences = array(
  67. array('control_name' => 'admin_platform_index'), //平台列表管理
  68. array('control_name' => 'admin_backUser', 'action_name' => 'userList'), //帐号管理 列表
  69. array('control_name' => 'admin_backUser', 'action_name' => 'CreateUser'),//帐号管理 创建帐号
  70. array('control_name' => 'admin_backUser', 'action_name' => 'setPermission'),
  71. array('control_name' => 'admin_backUser', 'action_name' => 'MP'),
  72. array('control_name' => 'admin_platform_bulletin', 'action_name' => 'ReqCreate'), //添加公告
  73. array('control_name' => 'admin_platform_bulletin', 'action_name' => 'PageUp'), //编辑公告
  74. );
  75. foreach ($notCompetences as $tmpCompetence) {
  76. if (lcfirst($tmpCompetence['control_name']) != $requestParams['control_name']) {
  77. continue;
  78. }
  79. if (!$tmpCompetence['action_name']) { //对控制器下的所有行为拥有权限
  80. $is_cpt = false;
  81. break;
  82. }
  83. if (lcfirst($tmpCompetence['action_name']) == $requestParams['action_name']) {
  84. $is_cpt = false;
  85. break;
  86. }
  87. }
  88. return $is_cpt;
  89. }
  90. /**
  91. * 通过用户id获取用户所属的组id集合
  92. * @param int $uid
  93. * @return array
  94. */
  95. public function getsGroupidsByUid($uid) {
  96. if (!Verify::unsignedInt($uid)) {
  97. return array();
  98. }
  99. $groupids = array();
  100. $objDKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  101. $groupids = $objDKifUsergroupRelation->getsGroupids($uid);
  102. return $groupids;
  103. }
  104. /**
  105. * 获取用户组所有的权限设置
  106. * @param array $groupids
  107. * @return array
  108. */
  109. public function getsCompetencesByGroupids($groupids) {
  110. if (!$groupids) {
  111. return array();
  112. }
  113. $competences = array();
  114. $objDKifUsergroupPermission = new \Cas\Dao\KifUsergroupPermission();
  115. $competences = $objDKifUsergroupPermission->getsCompetencesByGroupids($groupids);
  116. return $competences;
  117. }
  118. /**
  119. * 是否超级管理员
  120. * @return boolean
  121. */
  122. static public function isSuperadmin() {
  123. if (!self::$useCompetence) {
  124. return true;
  125. }
  126. $result = \Cas\Module\Permission::isSuperadmin();
  127. return $result;
  128. }
  129. /**
  130. * 输出错误消息
  131. * @param string $msg
  132. */
  133. public function fail_exit_cpt($msg = null) {
  134. $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
  135. $this->tpl = $permission_template_dir . '/admin/permission/prompt_message';
  136. $this->setOutputs(array(
  137. 'type' => 'fail',
  138. 'msg' => $msg,
  139. 'referer' => Request::referer(),
  140. 'header_tpl'=> $permission_template_dir . '/header.html',
  141. 'bottom_tpl'=> $permission_template_dir . '/bottom.html',
  142. ));
  143. $this->render();
  144. exit;
  145. }
  146. /**
  147. * 输出成功消息
  148. * @param string $msg
  149. */
  150. public function success_exit_cpt($msg = null) {
  151. $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
  152. $this->tpl = $permission_template_dir . '/admin/permission/prompt_message';
  153. $this->setOutputs(array(
  154. 'type' => 'success',
  155. 'msg' => $msg,
  156. 'referer' => Request::referer(),
  157. 'header_tpl'=> $permission_template_dir . '/header.html',
  158. 'bottom_tpl'=> $permission_template_dir . '/bottom.html',
  159. ));
  160. $this->render();
  161. exit;
  162. }
  163. public function no_permission_exit() {
  164. $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
  165. $this->tpl = $permission_template_dir . '/admin/permission/prompt_message';
  166. $this->setOutputs(array(
  167. 'type' => 'no_permission',
  168. ));
  169. $this->render();
  170. exit;
  171. }
  172. public function run() {
  173. if (isset(self::$useCompetence) && self::$useCompetence) {
  174. $this->requireCompetence();
  175. }
  176. # 登陆用户
  177. $this->setOutput('backuser', $this->getUser());
  178. # 是否管理员
  179. $IS_ADMIN = false;
  180. if (self::isSuperadmin()) {
  181. $IS_ADMIN = true;
  182. }
  183. $this->setOutput('IS_ADMIN', $IS_ADMIN);
  184. $action = $this->action;
  185. $this->$action();
  186. }
  187. }