Permission.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. namespace Cas\Controller;
  3. /**
  4. * Permission 权限管理系统
  5. * @author lishumingoo@gmail.com
  6. */
  7. use KIF\Verify;
  8. use KIF\Core\Request;
  9. use Cas\Dao\KifResourceManage;
  10. use KIF\String\Filter;
  11. use Cas\Dao\KifUsergroupPermission;
  12. class Permission extends \Cas\Controller\Admin\Controller {
  13. private $objMPermission;
  14. public function __construct() {
  15. $this->objMPermission = new \Cas\Module\Permission();
  16. }
  17. public function doDefault() {
  18. echo 'xxx';exit;
  19. }
  20. /**
  21. * 用户组管理
  22. */
  23. public function doUsergroup() {
  24. $this->tpl = 'admin/permission/usergroup';
  25. $page = Request::varGetInt('page', 1);
  26. $size = 20;
  27. $offset = ($page - 1) * $size;
  28. $objKifUsergroup = new \Cas\Dao\KifUsergroup();
  29. $objKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  30. $objKifUsergroupManage = new \Cas\Dao\KifUsergroupManage();
  31. $order = 'id asc';
  32. # 获取当前登陆用户所属主
  33. $groupids = $this->objMPermission->getsGroupidsByUid(\KIF\Core\BKController::getUid());
  34. $groups = $objKifUsergroup->gets($groupids);
  35. # 当前登陆用户可以管理的组
  36. $manageGroupids = $this->objMPermission->allowsMangeGroupids(\KIF\Core\BKController::getUid());
  37. $manageGroups = $objKifUsergroup->gets($manageGroupids);
  38. $title = '用户组管理';
  39. $this->setOutput('title', $title);
  40. $this->setOutput('groups', $groups);
  41. $this->setOutput('manageGroups', $manageGroups);
  42. $this->addNavMenu('KIF权限管理');
  43. $this->addNavMenu('用户组管理', Request::url(), '_self');
  44. }
  45. /**
  46. * 添加用户组
  47. */
  48. public function doAddUsergroup() {
  49. $group_name = Request::g('description');
  50. if (!$group_name) {
  51. $this->fail_exit_cpt('请填写要添加的组名');
  52. }
  53. $objDKifUsergroup = new \Cas\Dao\KifUsergroup();
  54. $info = array(
  55. 'description' => $group_name,
  56. );
  57. $rid = $objDKifUsergroup->add($info);
  58. if (!$rid) {
  59. $this->ajax_fail_exit('添加新用户组失败');
  60. }
  61. $objKifUsergroupManage = new \Cas\Dao\KifUsergroupManage();
  62. $parent_groupids = $_GET['parent_groupids'] ? explode(',', $_GET['parent_groupids']) : '';
  63. if ($parent_groupids) {
  64. foreach ($parent_groupids as $tmpGroupid) {
  65. $addResult = $objKifUsergroupManage->add(array(
  66. 'groupid' => $tmpGroupid,
  67. 'rid' => $rid
  68. ));
  69. if (!$addResult) {
  70. $this->ajax_fail_exit('分配管理用户组失败');
  71. }
  72. }
  73. }
  74. $this->ajax_success_exit('添加成功');
  75. }
  76. /**
  77. * 获取当前登陆用户有权分配的组
  78. */
  79. public function doUsergroupsOfAllowAssign() {
  80. $this->tpl = 'admin/permission/usergroupsOfAllowAssign';
  81. # 所在组
  82. $groupids = $this->objMPermission->allowsMangeGroupids(\KIF\Core\BKController::getUid());
  83. $objKifUsergroup = new \Cas\Dao\KifUsergroup();
  84. $groups = $objKifUsergroup->gets($groupids);
  85. $this->setOutput('groups', $groups);
  86. $this->ajax_success_exit($this->render(true));
  87. }
  88. /**
  89. * 编辑用户组 、基础设置
  90. */
  91. public function doEditUsergroup() {
  92. $this->tpl = 'admin/permission/edit_usergroup';
  93. $groupid = Request::r('groupid');
  94. if (!Verify::unsignedInt($groupid)) {
  95. $this->fail_exit_cpt('无效的请求参数');
  96. }
  97. $objDKifUsergroup = new \Cas\Dao\KifUsergroup();
  98. if (!Request::isPost()) {
  99. $group = $objDKifUsergroup->get($groupid);
  100. $this->setOutput('group', $group);
  101. $this->addNavMenu('KIF权限管理');
  102. $this->addNavMenu('用户组管理', Request::schemeDomain() . '?c=permission&a=usergroup', '_self');
  103. $this->addNavMenu('基础设置', Request::url(), '_self');
  104. } else {
  105. $tableInfo = array(
  106. 'id' => $groupid,
  107. 'description' => Request::p('description'),
  108. );
  109. $cas_token = Request::p('cas_token');
  110. $modifyResult = $objDKifUsergroup->modify($tableInfo, null, $cas_token);
  111. if (!$modifyResult->isSuccess()) {
  112. $this->fail_exit_cpt($modifyResult->getData());
  113. }
  114. $this->success_exit_cpt();
  115. }
  116. }
  117. /**
  118. * 删除用户组
  119. * 同时会把想用用户组权限和关联的用户删除掉
  120. */
  121. public function doDelUsergroup() {
  122. $groupid = Request::g('groupid');
  123. if (!Verify::unsignedInt($groupid)) {
  124. $this->fail_exit_cpt('无效的请求参数');
  125. }
  126. $objKifUsergroup = new \Cas\Dao\KifUsergroup();
  127. $objKifUsergroupCompetence = new \Cas\Dao\KifUsergroupCompetence();
  128. $objKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  129. $objKifUsergroupManage = new \Cas\Dao\KifUsergroupManage();
  130. $delUsergroupResult = $objKifUsergroup->delete(array('id' => $groupid));
  131. if (!$delUsergroupResult) {
  132. $this->fail_exit_cpt('删除用户组失败');
  133. }
  134. $delUsergroupCptResult = $objKifUsergroupCompetence->delete(array('groupid' => $groupid));
  135. if (!$delUsergroupCptResult) {
  136. $this->fail_exit_cpt('删除用户组权限失败');
  137. }
  138. $delUsergroupRelation = $objKifUsergroupRelation->delete(array('groupid' => $groupid));
  139. if (!$delUsergroupRelation) {
  140. $this->fail_exit_cpt('删除用户、用户组关联表失败');
  141. }
  142. $delUsergroupManage = $objKifUsergroupManage->delete(array('groupid' => $groupid));
  143. if (!$delUsergroupManage) {
  144. $this->fail_exit_cpt('删除用户组管理表失败');
  145. }
  146. $delUsergroupManage = $objKifUsergroupManage->delete(array('rid' => $groupid));
  147. if (!$delUsergroupManage) {
  148. $this->fail_exit_cpt('删除用户组管理表失败');
  149. }
  150. $this->success_exit_cpt();
  151. }
  152. /**
  153. * 用户管理
  154. */
  155. public function doUser() {
  156. $this->tpl = 'admin/permission/user';
  157. $objKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  158. $relation = $objKifUsergroupRelation->getsAll();
  159. $uids = $groupids = array();
  160. foreach ($relation as $tmpval) {
  161. $uids[] = $tmpval['uid'];
  162. $groupids[] = $tmpval['groupid'];
  163. }
  164. $uids = array_unique($uids);
  165. $groupids = array_unique($groupids);
  166. $members = $this->objMPermission->getsUserByUids($uids);
  167. $objKifUsergroup = new \Cas\Dao\KifUsergroup();
  168. $groups = $objKifUsergroup->gets($groupids);
  169. $data = array();
  170. foreach ($uids as $tmpUid) {
  171. $tmpGroupids = array();
  172. foreach ($relation as $tmpval) {
  173. if ($tmpval['uid'] == $tmpUid) {
  174. $tmpGroupids[] = $tmpval['groupid'];
  175. }
  176. }
  177. $data[$tmpUid] = array(
  178. 'uid' => $tmpUid,
  179. 'groupids' => $tmpGroupids,
  180. );
  181. }
  182. $this->setOutput('data', $data);
  183. $this->setOutput('relation', $relation);
  184. $this->setOutput('members', $members);
  185. $this->setOutput('groups', $groups);
  186. $this->addNavMenu('KIF权限管理');
  187. $this->addNavMenu('用户管理', Request::url(), '_self');
  188. }
  189. /**
  190. * 查询用户权限信息
  191. */
  192. public function doSearchUser() {
  193. $this->tpl = 'admin/permission/search_user';
  194. $username = Request::g('username');
  195. $uid = Request::g('uid');
  196. if ($username) {
  197. $member = $this->objMPermission->getUserByUsername($username);
  198. } elseif ($uid) {
  199. $member = $this->objMPermission->getUserByUid($uid);
  200. }
  201. if (!$member) {
  202. $this->fail_exit_cpt('没有搜索到符合条件的用户');
  203. }
  204. $uid = $member['uid'];
  205. $groupids = array();
  206. $objDKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  207. $groupids = $objDKifUsergroupRelation->getsGroupids($uid);
  208. # 用户所在组集合
  209. $objDKifUsergroup = new \Cas\Dao\KifUsergroup();
  210. $hasGroups = $objDKifUsergroup->gets($groupids);
  211. # 获取当前登陆用户可管理的组
  212. $allowsMangeGroupIds = $this->objMPermission->allowsMangeGroupids(\KIF\Core\BKController::getUid());
  213. $groupsAll = $objDKifUsergroup->gets($allowsMangeGroupIds);
  214. # 在当前登陆用户可管理用户组中已有的权限
  215. $has_groupids = array_intersect($groupids, $allowsMangeGroupIds);
  216. $this->setOutput('member', $member);
  217. $this->setOutput('hasGroups', $hasGroups);
  218. $this->setOutput('groupsAll', $groupsAll);
  219. $this->setOutput('has_groupids', implode(',', $has_groupids));
  220. $this->addNavMenu('KIF权限管理');
  221. $this->addNavMenu('用户管理', Request::schemeDomain() . '?c=permission&a=user', '_self');
  222. $this->addNavMenu('编辑 ' . $member['username'] . ' 用户组', Request::url(), '_self');
  223. }
  224. /**
  225. * 添加用户到用户组
  226. */
  227. public function doAddUserToGroup() {
  228. $groupids = $_POST['groupids'] ? $_POST['groupids'] : array();
  229. $uid = $_POST['uid'];
  230. $hasGroupids = $_POST['hasGroupids'] ? explode(',', $_POST['hasGroupids']) : array();
  231. $objDKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  232. $intersectGroupids = array_intersect($groupids, $hasGroupids);
  233. # 添加权限
  234. $addGroupids = array_diff($groupids, $intersectGroupids);
  235. if ($addGroupids) {
  236. foreach ($addGroupids as $tmpGroupid) {
  237. $addResult = $objDKifUsergroupRelation->add(array(
  238. 'uid' => $uid,
  239. 'groupid' => $tmpGroupid
  240. ));
  241. if (!$addResult) {
  242. $this->fail_exit_cpt('添加权限操作失败');
  243. }
  244. }
  245. }
  246. # 删除权限
  247. $delGroupids = array_diff($hasGroupids, $intersectGroupids);
  248. if ($delGroupids) {
  249. $delResult = $objDKifUsergroupRelation->delete(array(
  250. 'uid' => $uid,
  251. 'groupid' => $delGroupids,
  252. ));
  253. if (!$delResult) {
  254. $this->fail_exit_cpt('删除权限操作失败');
  255. }
  256. }
  257. $this->success_exit_cpt('修改权限操作成功');
  258. }
  259. /**
  260. * 新版资源管理
  261. */
  262. public function doResourceNew() {
  263. $result = $this->objMPermission->getResourceTreeNew(true);
  264. $resource = $result['resource'];
  265. $this->tpl = 'admin/permission/new_resource';
  266. $this->addNavMenu('KIF权限管理');
  267. $this->addNavMenu('资源管理', Request::url(), '_self');
  268. $this->setOutput('resource', json_encode($resource));
  269. }
  270. /**
  271. * 新版资源管理 - 创建资源
  272. */
  273. public function doAddResourceNew() {
  274. $objKifResourceManage = new KifResourceManage();
  275. $parent_id = Request::g('parent_id');
  276. $type = Request::g('type');
  277. $is_end = 0;
  278. $parent_resource = array();
  279. if ($type == 'dir') {
  280. $alias = '目录';
  281. } else {
  282. $alias = '资源';
  283. $parent_resource = $objKifResourceManage->get($parent_id);
  284. if (!$parent_resource) {
  285. $this->fail_exit_cpt("parent_id对于的数据不存在");
  286. }
  287. if ($parent_resource['parent_id']) {
  288. $is_end = 1;
  289. }
  290. }
  291. if (!Request::isPost()) {
  292. $this->tpl = 'admin/permission/new_resource_add_edit';
  293. $title = "创建{$alias}";
  294. $this->addNavMenu('KIF权限管理');
  295. $this->addNavMenu('资源管理', request::schemeDomain() . '/?c=permission&a=resourceNew', '_self');
  296. $this->addNavMenu($title, Request::url(), '_self');
  297. $this->setOutput('type', $type);
  298. $this->setOutput('alias', $alias);
  299. $this->setOutput('parent_resource', $parent_resource);
  300. $this->setOutput('is_end', $is_end);
  301. } else {
  302. $tableInfo = Filter::arrayfilter($_POST);
  303. if (!$tableInfo['name']) {
  304. $this->fail_exit_cpt("请填写{$alias}名称");
  305. }
  306. $tableInfo['app_name'] = lcfirst($tableInfo['app_name']);
  307. $tableInfo['control_name'] = lcfirst($tableInfo['control_name']);
  308. $tableInfo['action_name'] = lcfirst($tableInfo['action_name']);
  309. $tmpResult = $objKifResourceManage->add($tableInfo);
  310. if (!$tmpResult) {
  311. $this->fail_exit_cpt("创建{$alias}失败");
  312. }
  313. $this->success_exit_cpt("创建{$alias}成功");
  314. }
  315. }
  316. /**
  317. * 新版资源管理 - 修改资源
  318. */
  319. public function doEditResourceNew() {
  320. $id = Request::g('id');
  321. $objKifResourceManage = new KifResourceManage();
  322. $resource = $objKifResourceManage->get($id);
  323. $is_end = 0;
  324. $parent_resource = array();
  325. if (!$resource['parent_id']) {
  326. $type = 'dir';
  327. $alias = '目录';
  328. } else {
  329. $type = '';
  330. $alias = '资源';
  331. $parent_resource = $objKifResourceManage->get($resource['parent_id']);
  332. if (!$parent_resource) {
  333. $this->fail_exit_cpt("parent_id对于的数据不存在");
  334. }
  335. if ($parent_resource['parent_id']) {
  336. $is_end = 1;
  337. }
  338. }
  339. if (!Request::isPost()) {
  340. $this->tpl = 'admin/permission/new_resource_add_edit';
  341. $title = "编辑{$alias}";
  342. $this->addNavMenu('KIF权限管理');
  343. $this->addNavMenu('资源管理', request::schemeDomain() . '/?c=permission&a=resourceNew', '_self');
  344. $this->addNavMenu($title, Request::url(), '_self');
  345. $this->setOutput('resource', $resource);
  346. $this->setOutput('parent_resource', $parent_resource);
  347. $this->setOutput('type', $type);
  348. $this->setOutput('alias', $alias);
  349. $this->setOutput('is_end', $is_end);
  350. } else {
  351. $tableInfo = Filter::arrayfilter($_POST);
  352. if (!$tableInfo['name']) {
  353. $this->fail_exit_cpt("请填写{$alias}名称");
  354. }
  355. $tableInfo['app_name'] = lcfirst($tableInfo['app_name']);
  356. $tableInfo['control_name'] = lcfirst($tableInfo['control_name']);
  357. $tableInfo['action_name'] = lcfirst($tableInfo['action_name']);
  358. $cas_token = $tableInfo['cas_token'];
  359. unset($tableInfo['cas_token']);
  360. $tableInfo['id'] = $id;
  361. $tmpModifyResult = $objKifResourceManage->modify($tableInfo, null, $cas_token);
  362. if (!$tmpModifyResult->isSuccess()) {
  363. $this->fail_exit_cpt("修改失败,原因:" . $tmpModifyResult->getData());
  364. }
  365. $this->success_exit_cpt("修改成功");
  366. }
  367. }
  368. /**
  369. * 新版资源管理 - 删除资源
  370. */
  371. public function doDelResourceNew() {
  372. $id = Request::g('id');
  373. if (!Verify::unsignedInt($id)) {
  374. $this->ajax_fail_exit("无效的id");
  375. }
  376. $objKifResourceManage = new KifResourceManage();
  377. $result = $objKifResourceManage->delete(array('id' => $id));
  378. if (!$result) {
  379. $this->ajax_fail_exit("删除失败");
  380. }
  381. $this->ajax_success_exit("删除成功");
  382. }
  383. public function doGetResource() {
  384. $this->ajax_success_exit();
  385. }
  386. /**
  387. * 用户组权限管理
  388. */
  389. public function doUsergroupCpt() {
  390. $this->tpl = 'admin/permission/new_usergroupCpt';
  391. $groupid = Request::g('groupid');
  392. if (!$groupid) {
  393. $this->fail_exit_cpt('无效参数');
  394. }
  395. $objKifUsergroup = new \Cas\Dao\KifUsergroup();
  396. $usergroup = $objKifUsergroup->get($groupid);
  397. if (!$usergroup) {
  398. $this->fail_exit_cpt('获取用户组信息失败');
  399. }
  400. # 获取用户组的权限
  401. $objKifUsergroupPermission = new \Cas\Dao\KifUsergroupPermission();
  402. $usergroupCpt = $objKifUsergroupPermission->getsCompetencesByGroupids(array($groupid));
  403. $result = $this->objMPermission->getResourceTreeNew(true);
  404. # 所有资源
  405. $resource = $result['resource'];
  406. # 当前登陆用户没有权限的resourceid(资源数的id)
  407. $disableTreeIds = $result['disableTreeIds'];
  408. $this->setOutput('groupid', $groupid);
  409. $this->setOutput('resource', json_encode($resource));
  410. $this->setOutput('disableTreeIds', json_encode($disableTreeIds));
  411. $this->setOutput('usergroupCpt', json_encode(array_values($usergroupCpt)));
  412. $title = '权限管理';
  413. $this->setOutput('title', $title);
  414. $this->setOutput('menu_active', array('name' => 'userslist', 'item' => '')); //激活菜单
  415. $this->addNavMenu('帐号管理');
  416. $this->addNavMenu($title);
  417. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  418. }
  419. /**
  420. * 新版编辑用户组权限
  421. */
  422. public function doEditUsergroupCpt() {
  423. $resourceids = $_POST['resourceids'];
  424. $groupid = Request::p('groupid');
  425. if (!Verify::unsignedInt($groupid)) {
  426. $this->ajax_fail_exit('无效用户组id');
  427. }
  428. $objKifUsergroupPermission = new KifUsergroupPermission();
  429. # 先把用户组以前的所有权限删除
  430. if (!$objKifUsergroupPermission->delete(array('groupid' => $groupid))) {
  431. $this->ajax_fail_exit("删除原权限失败");
  432. }
  433. foreach ($resourceids as $tmpResourceId) {
  434. $result = $objKifUsergroupPermission->add(array(
  435. 'groupid' => $groupid,
  436. 'resourceid' => $tmpResourceId,
  437. ));
  438. if (!$result) {
  439. $this->ajax_fail_exit("数据库操作失败");
  440. }
  441. }
  442. $this->ajax_success_exit();
  443. }
  444. /**
  445. * 渲览页面
  446. */
  447. public function display() {
  448. $this->render();
  449. }
  450. }