RoleController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Http\Controllers\Admin\Base;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Services\Admin\Role;
  5. use Request;
  6. use App\Services\Admin\Acl;
  7. class RoleController extends Controller
  8. {
  9. private $level;
  10. private $_service;
  11. private $_serviceDepartments;
  12. /**
  13. * 初始化Service
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. if(!$this->_service) $this->_service = new Role();
  19. $this->level = isset($this->_getRoleNode()->level)?$this->_getRoleNode()->level:'';
  20. }
  21. /**
  22. * 列表
  23. */
  24. function index()
  25. {
  26. if($this->_user['is_root']){
  27. $search['level'] = 0;
  28. }else{
  29. $search['level'] = $this->level;
  30. }
  31. $request = Request::all();
  32. $search['keyword'] = Request::input('keyword');
  33. $orderby = array();
  34. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  35. $orderby[$request['sort_field']] = $request['sort_field_by'];
  36. }
  37. $list = $this->_service->search($search, $orderby);
  38. return view('admin.base.role.index', compact('list'));
  39. }
  40. /**
  41. * 创建
  42. */
  43. public function create()
  44. {
  45. if(Request::method() == 'POST'){
  46. if(intval(Request::input('info.level')) < $this->level){
  47. $this->showWarning('你无权创建该等级的角色!', urldecode(Request::input('_referer')));
  48. }
  49. if($this->_service->create(Request::input('info'))){
  50. $this->showMessage('操作成功', U( 'Base/Role/index'));
  51. }else{
  52. $this->showMessage('操作失败', U( 'Base/Role/index'));
  53. }
  54. }
  55. $level = $this->level;
  56. return view('admin.base.role.edit', compact('level', 'Departments'));
  57. }
  58. /**
  59. * 更新
  60. */
  61. public function update()
  62. {
  63. if(Request::method() == 'POST'){
  64. if(intval(Request::input('info.level')) < $this->level){
  65. $this->showWarning('你无权创建该等级的角色!', urldecode(Request::input('_referer')));
  66. }
  67. if($this->_service->update(Request::input('id'), Request::input('info'))){
  68. $this->showMessage('操作成功', urldecode(Request::input('_referer')));
  69. }else{
  70. $this->showWarning('操作失败', urldecode(Request::input('_referer')));
  71. }
  72. }
  73. $data = $this->_service->find(Request::input('id'));
  74. $level = $this->level;
  75. return view('admin.base.role.edit', compact('data', 'level', 'Departments'));
  76. }
  77. /**
  78. * 更新
  79. */
  80. public function auth()
  81. {
  82. $id = Request::input('id');
  83. $objAcl = new Acl();
  84. if(Request::method() == 'POST'){
  85. $menuIds = Request::input('menu_ids');
  86. if($this->_user['is_root']) {
  87. $allMenus = false;
  88. }else{
  89. $allMenus = array();
  90. foreach ($this->_user['menus'] as $value) {
  91. $allMenus[] = $value['id'];
  92. }
  93. }
  94. $ok = $objAcl->setRole($id, $menuIds,$allMenus);
  95. if($ok) {
  96. $arr['status'] = SUCESS_CODE;
  97. }else{
  98. $arr['status'] = SERVER_ERROR;
  99. }
  100. exit(json_encode($arr));
  101. }
  102. $hasPermissions = $objAcl->getAccessIDs($id);
  103. $role = session(LOGIN_MARK_SESSION_KEY);
  104. //为ztree做数据准备
  105. $zTree = []; $all = [];
  106. foreach($role['menus'] as $key => $value)
  107. {
  108. $arr = ['id' => $value['id'], 'pId' => $value['pid'],
  109. 'name' => $value['name'] . " (" . $value['path'] . ")",
  110. 'open' => true];
  111. if(in_array($value['id'], $hasPermissions)) $arr['checked'] = true;
  112. $zTree[] = $arr;
  113. $all[] = $value['id'];
  114. }
  115. $data = $this->_service->find($id);
  116. return view('admin.base.role.auth', compact('data','zTree','all'));
  117. }
  118. /**
  119. * 更新状态
  120. */
  121. public function status()
  122. {
  123. $bool = $this->_service->updateStatus(Request::input('id'), Request::input('status'));
  124. if($bool) {
  125. $this->showMessage('操作成功');
  126. }else{
  127. $this->showWarning('操作失败');
  128. }
  129. }
  130. /**
  131. * 删除
  132. */
  133. public function destroy()
  134. {
  135. $bool = $this->_service->destroy(Request::input('id'));
  136. if($bool) {
  137. $this->showMessage('操作成功');
  138. }else{
  139. $this->showWarning("操作失败");
  140. }
  141. }
  142. /**
  143. * 获取角色权限节点(level越小权限越大)
  144. */
  145. private function _getRoleNode()
  146. {
  147. return $this->_service->getLevelNode($this->_user['admin_role_id']);
  148. }
  149. /**
  150. * 获取树形结构
  151. */
  152. private function _getTreeByDepartmentId()
  153. {
  154. if($this->_user['is_root']){
  155. $department_id = 0;
  156. }else{
  157. $department_id = intval($this->_user['department_id']);
  158. }
  159. return $this->_serviceDepartments->getTreeByDepartmentId($department_id);
  160. }
  161. }