Permission.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Verify;
  4. use KIF\Core\Config;
  5. use Cas\Dao\KifResourceManage;
  6. use Cas\Dao\BackUser;
  7. /**
  8. * Permission 权限管理系统
  9. * @author li.shuming@kimiss.com
  10. */
  11. class Permission {
  12. /**
  13. * 新版 获取 Permission 资源树数据
  14. * @param Boolean $open 资源树是否打开,默认 true 打开,false 关闭
  15. * @return array(
  16. * 'resource' = array(//所有资源树
  17. * 'id' => '', //资源id
  18. * 'pId' => '', //资源父级id
  19. * 'name' => '', //资源描述
  20. * 'isParent' => '', //是否目录,既控制器
  21. * 'open' => '', //目录是否打开
  22. * ),
  23. * 'disableNodes' => array(), //当前用户没有权限的资源id集
  24. * );
  25. */
  26. public function getResourceTreeNew($open = true) {
  27. $objKifResourceManage = new KifResourceManage();
  28. $resource = $disableTreeIds = array();
  29. $resources = $objKifResourceManage->getsAll('create_time asc');
  30. foreach ($resources as $tmpResource) {
  31. $tree_id = $tmpResource['id'];
  32. $parent_id = $tmpResource['parent_id'];
  33. $isParent = $tmpResource['is_end'] ? false : true;
  34. //$open = $parent_id ? false : true;
  35. $chkDisabled = $parent_id ? false : true;
  36. $resource[] = array( //app节点
  37. 'id' => $tree_id,
  38. 'pId' => $parent_id,
  39. 'name' => $tmpResource['name'],
  40. 'isParent' => $isParent,
  41. 'open' => $open,
  42. 'chkDisabled' => $chkDisabled,
  43. );
  44. if (!$this->checkLoginUserCpt($tmpResource['app_name'], $tmpResource['control_name'], $tmpResource['action_name'])) {
  45. $disableTreeIds[] = $tree_id;
  46. }
  47. }
  48. return array(
  49. 'resource' => $resource, //资源树
  50. 'disableTreeIds' => $disableTreeIds, //当前用户没有权限操作的节点
  51. );
  52. }
  53. /**
  54. * 检查当前登陆对指定资源是否有权限
  55. * @param string $app_name
  56. * @param string $control_name
  57. * @param string $action_name
  58. * @return boolean
  59. */
  60. public function checkLoginUserCpt($app_name, $control_name, $action_name) {
  61. # 超级管理员啥权限都有
  62. if (self::isSuperadmin()) {
  63. return true;
  64. }
  65. # 获取当前用户所有权限
  66. $objKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  67. $objKifUsergroupPermission = new \Cas\Dao\KifUsergroupPermission();
  68. $groupids = $objKifUsergroupRelation->getsGroupids(\KIf\Core\BKController::getUid());
  69. $groupCompetences = $objKifUsergroupPermission->getsCompetencesByGroupids($groupids);
  70. foreach ($groupCompetences as $tmpCpt) {
  71. $tmpAppName = $tmpCpt['app_name'];
  72. $tmpControlName = $tmpCpt['control_name'];
  73. $tmpActionName = $tmpCpt['action_name'];
  74. # 对action拥有权限
  75. if ($tmpAppName && $tmpControlName && $tmpActionName) {
  76. if ($tmpAppName == $app_name && $tmpControlName == $control_name && $tmpActionName == $action_name) {
  77. return true;
  78. }
  79. }
  80. # 对control拥有权限
  81. if ($tmpAppName && $tmpControlName && $tmpActionName == '') {
  82. if ($tmpAppName == $app_name && $tmpControlName == $control_name) {
  83. return true;
  84. }
  85. }
  86. # 对app拥有权限
  87. if ($tmpAppName && $tmpControlName == '' && $tmpActionName == '') {
  88. if ($tmpAppName == $app_name) {
  89. return true;
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * 通过用户名查询用户信息
  97. * @param string $username
  98. * @return array
  99. */
  100. public function getUserByUsername($username) {
  101. if (!$username) {
  102. return array();
  103. }
  104. $member = array();
  105. $objBackUser = new BackUser();
  106. $member = $objBackUser->fetchOne(array('name' => $username));
  107. if ($member) {
  108. $member['username'] = $member['name'];
  109. }
  110. return $member;
  111. }
  112. /**
  113. * 通过用户id获取用户信息
  114. * @param int $uid
  115. * @return array
  116. */
  117. public function getUserByUid($uid) {
  118. $members = $this->getsUserByUids(array($uid));
  119. if (!$members) {
  120. return array();
  121. }
  122. return array_pop($members);
  123. }
  124. /**
  125. * 批量获取用户信息
  126. * @param array $uids
  127. * @return multitype:|array
  128. */
  129. public function getsUserByUids($uids) {
  130. if (!$uids) {
  131. return array();
  132. }
  133. $members = array();
  134. $objBackUser = new BackUser();
  135. $members = $objBackUser->findBy(array('uid' => $uids), 'uid');
  136. foreach ($members as $tmpkey => $tmpval) {
  137. $members[$tmpkey]['username'] = $tmpval['name'];
  138. }
  139. return $members;
  140. }
  141. /**
  142. * 当前用户是否超级管理员
  143. * @return boolean
  144. */
  145. static public function isSuperadmin() {
  146. $objBackUser = new BackUser();
  147. $user = $objBackUser->get(\KIF\Core\BKController::getUid());
  148. if ($user['permission'] == 'admin') {
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * 获取超级管理员uid集
  155. * @return array
  156. */
  157. static public function getSuperadminUids() {
  158. $config = Config::getInstance()->current();
  159. return isset($config['superadmin']) ? $config['superadmin'] : array();
  160. }
  161. /**
  162. * 获取指定用户所属组
  163. * @param $uid
  164. */
  165. public function getsGroupidsByUid($uid) {
  166. if (!Verify::unsignedInt($uid)) {
  167. return array();
  168. }
  169. $objKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  170. $result = $objKifUsergroupRelation->findBy(array('uid' => $uid), null, null, 'groupid', 'id asc');
  171. if (!$result) {
  172. return array();
  173. }
  174. $groupids = array();
  175. foreach ($result as $tmpval) {
  176. $groupids[] = $tmpval['groupid'];
  177. }
  178. return array_unique($groupids);
  179. }
  180. /**
  181. * 获取指定用户可管理的用户组
  182. * @param int $uid
  183. */
  184. public function allowsMangeGroupids($uid) {
  185. # 如果是登陆用户是超管,返回所有用户组
  186. if (in_array($uid, self::getSuperadminUids())) {
  187. $objKifUsergroup = new \Cas\Dao\KifUsergroup();
  188. return $objKifUsergroup->getsIdsAll('id asc');
  189. }
  190. $groupids = $this->getsGroupidsByUid($uid);
  191. # 可管理的组
  192. $manageGroupids = array();
  193. $objKifUsergroupManage = new \Cas\Dao\KifUsergroupManage();
  194. $usergroupManages = $objKifUsergroupManage->findBy(array('groupid' => $groupids));
  195. foreach ($usergroupManages as $tmpManage) {
  196. $manageGroupids[] = $tmpManage['rid'];
  197. }
  198. return array_unique(array_merge($groupids, $manageGroupids));
  199. }
  200. }