RolesController.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 小梦科技资源nanodreamtech.com
  5. *
  6. * ==========================================================================
  7. * @link https://www.nanodreamtech.com/
  8. * @copyright Copyright (c) 2015 liofis.com.
  9. * @license https://www.nanodreamtech.com/license.html License
  10. * ==========================================================================
  11. *
  12. * @author fish
  13. *
  14. */
  15. class RolesController extends CommonController {
  16. /**
  17. * 角色管理列表
  18. * @return
  19. */
  20. protected function _initialize(){
  21. parent::_initialize();
  22. $this->breadcrumb1='权限管理';
  23. $this->breadcrumb2='角色管理';
  24. }
  25. public function index() {
  26. $roles = D('Role', 'Service')->getRoles();
  27. $this->assign('roles', $roles);
  28. $this->assign('rows_count', count($roles));
  29. $this->display();
  30. }
  31. /**
  32. * 添加角色
  33. * @return
  34. */
  35. public function add() {
  36. $this->assign('roles', D('Role', 'Service')->getRoles());
  37. $this->display();
  38. }
  39. /**
  40. * 创建角色
  41. * @return
  42. */
  43. public function create() {
  44. if (!isset($_POST['role'])) {
  45. return $this->errorReturn('无效的操作!');
  46. }
  47. $result = D('Role', 'Service')->addRole($_POST['role']);
  48. if (!$result['status']) {
  49. $status = array('status'=>'back','message'=>$result['data']['error']);
  50. $this->osc_alert($status);
  51. }
  52. $status = array('status'=>'success','message'=>'添加角色成功!','jump'=>U('Roles/index'));
  53. $this->osc_alert($status);
  54. }
  55. /**
  56. * 编辑角色信息
  57. * @return
  58. */
  59. public function edit() {
  60. $roleService = D('Role', 'Service');
  61. if (!isset($_GET['id']) || !$roleService->existRole($_GET['id'])) {
  62. return $this->error('需要编辑的角色不存在!');
  63. }
  64. $role = M('Role')->getById($_GET['id']);
  65. $this->assign('role', $role);
  66. $this->assign('roles', $roleService->getRoles());
  67. $this->assign('sids', $roleService->getSonRoleIds($role['id']));
  68. $this->display();
  69. }
  70. /**
  71. * 更新角色信息
  72. * @return
  73. */
  74. public function update() {
  75. $roleService = D('Role', 'Service');
  76. if (!isset($_POST['role'])
  77. || !$roleService->existRole($_POST['role']['id'])) {
  78. $status = array('status'=>'back','message'=>'无效的操作!');
  79. $this->osc_alert($status);
  80. }
  81. $result = $roleService->updateRole($_POST['role']);
  82. if (!$result['status']) {
  83. $status = array('status'=>'back','message'=>$result['data']['error']);
  84. $this->osc_alert($status);
  85. }
  86. $status = array('status'=>'success','message'=>'更新角色信息成功!','jump'=>U('Roles/index'));
  87. $this->osc_alert($status);
  88. }
  89. /**
  90. * 权限分配
  91. * @return
  92. */
  93. public function assignAccess() {
  94. $roleService = D('Role', 'Service');
  95. if (!isset($_GET['id'])
  96. || !$roleService->existRole($_GET['id'])) {
  97. return $this->errorReturn('需要分配权限的角色不存在!');
  98. }
  99. $role = M('Role')->getById($_GET['id']);
  100. if (0 == $role['pid'] && 0 == $_SESSION['oscshop_admin_s']['user_auth']['is_super']) {
  101. return $this->error('您无权限进行该操作!');
  102. }
  103. $access = D('Access')->relation(true)
  104. ->where("role_id={$role['id']}")
  105. ->select();
  106. $rAccess = array();
  107. foreach ($access as $key => $item) {
  108. $rAccess[$key]['val'] = "{$item['node_id']}:"
  109. . "{$item['node']['level']}:"
  110. . "{$item['node']['pid']}";
  111. }
  112. $role['access'] = json_encode($rAccess);
  113. $this->assign('role', $role);
  114. $this->assign('nodes', D('Node', 'Service')->getLevelNodes());
  115. $this->display('assign_access');
  116. }
  117. /**
  118. * 处理分配权限
  119. * @return
  120. */
  121. public function doAssignAccess() {
  122. $roleService = D('Role', 'Service');
  123. if (!isset($_POST['id']) || !$roleService->existRole($_POST['id'])) {
  124. $data = array('state' => 0);
  125. $data['err']='需要分配权限的角色不存在!';
  126. $this->ajaxReturn($data);
  127. //return $this->errorReturn('需要分配权限的角色不存在!');
  128. }
  129. if (empty($_POST['access'])) {
  130. $_POST['access'] = array();
  131. }
  132. $result = $roleService->assignAccess($_POST['id'], $_POST['access']);
  133. if (!$result['status']) {
  134. $data = array('state' => 0);
  135. $data['err'] = $result['data']['error'];
  136. $this->ajaxReturn($data);
  137. //return $this->errorReturn($result['data']['error']);
  138. }
  139. if (!empty($result['data'])) {
  140. $data = array('state' => 1);
  141. $data['msg'] = $result['data'];
  142. $this->ajaxReturn($data);
  143. //return $this->successReturn($result['data']);
  144. }
  145. $data = array('state' => 1);
  146. $data['msg'] = '分配权限成功!';
  147. $this->ajaxReturn($data);
  148. }
  149. }