PermissionsController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Permission;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Models\Permission;
  5. use App\Models\Role;
  6. use App\Repositories\Eloquent\PermissionRepositoryEloquent;
  7. use App\Validators\PermissionValidator;
  8. use Illuminate\Contracts\Routing\ResponseFactory;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * Class PermissionsController.
  13. *
  14. * @package namespace App\Http\Controllers;
  15. */
  16. class PermissionsController extends AdminBaseController
  17. {
  18. /**
  19. * PermissionsController constructor.
  20. *
  21. * @param PermissionRepositoryEloquent $repository
  22. * @param PermissionValidator $validator
  23. */
  24. public function __construct(PermissionRepositoryEloquent $repository, PermissionValidator $validator)
  25. {
  26. parent::__construct($repository, $validator);
  27. }
  28. /**
  29. * @param Request $request
  30. * @param ResponseFactory $response
  31. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
  32. * @throws \Prettus\Repository\Exceptions\RepositoryException
  33. *
  34. */
  35. protected function index(Request $request, ResponseFactory $response)
  36. {
  37. if( $request->isMethod('post') && $request->ajax() ){
  38. $roleID = $request->input('id'); // roleID
  39. // 查询关联信息
  40. $datum = DB::table('role_has_permissions as rhp')
  41. ->select(['rhp.permission_id', 'rhp.role_id', 'p.name', 'p.routes', 'p.created_at', 'p.updated_at'])
  42. ->RightJoin('permissions as p', 'p.id', '=', 'rhp.permission_id')
  43. ->where('rhp.role_id', '=', $roleID)
  44. ->paginate($request->pageSize);
  45. foreach ( $datum as &$data ) {
  46. $data->id = "{$data->permission_id}-{$data->role_id}";
  47. }
  48. unset($data);
  49. if( $request->wantsJson() ){
  50. return $response->json([
  51. "total" => $datum->total(), // $datum->total() 返回总行数
  52. "rows" => $datum->items(), // 返回当前查询出来的数据
  53. "data" => $datum, // 返回当前对象
  54. ]);
  55. }
  56. }
  57. $params = array();
  58. $params['id'] = request()->input('id', 0);
  59. $params['role'] = Role::where('id', $params['id'])->first();
  60. return view("admins.{$this->tables}.index", (array)$params);
  61. }
  62. // 会员编辑
  63. protected function _storeGet($request)
  64. {
  65. $roleID = $request->input('id', 0); // 角色ID
  66. // 查询会员绑定的权限
  67. $permissions = DB::table('role_has_permissions')
  68. ->where('role_id', '=', $roleID)
  69. ->get();
  70. $permissionIDS = array(); // 已绑定权限
  71. foreach ( $permissions as $permission ) {
  72. $permissionIDS[] = $permission->permission_id;
  73. }
  74. // 查询当前
  75. // $notBindingPermission = DB::table('permissions')->whereNotIn('id', $permissionIDS)->get();
  76. $list= Permission::where('p_id','0')->where('type',1)->where('is_del','0')->select(['id','name as label'])->orderBy('sort','asc')->get()->toArray();
  77. foreach ($list as &$item){
  78. $item['children']=Permission::where('p_id',$item['id'])->where('type',2)->where('is_del','0')->select(['id','name as label'])->orderBy('sort','asc')->get()->toArray();
  79. foreach ($item['children'] as &$value){
  80. $child=Permission::where('p_id',$value['id'])->where('type',3)->where('is_del','0')->select(['id','name as label'])->orderBy('sort','asc')->get()->toArray();
  81. if($child)$value['children']=$child;
  82. }
  83. }
  84. // $checkboxs = array();
  85. // foreach ( $notBindingPermission as $key=>$value ) {
  86. // $checkboxs[] = array(
  87. // 'value' => $value->id,
  88. // 'label' => $value->name,
  89. // 'disable' => "",
  90. // 'checked' => false,
  91. // );
  92. // }
  93. ////
  94. // $params['checkboxs'] = json_encode($checkboxs);
  95. $params['list'] = json_encode($list);
  96. $params['select_ids'] = $permissionIDS;
  97. $params['role_id'] = $roleID;
  98. return $params;
  99. }
  100. protected function _storePost($request)
  101. {
  102. $ids = $request->input('ids', []);
  103. $roleID = $request->input('role_id', 0);
  104. if(is_string($ids)){
  105. $ids=explode(',',$ids);
  106. $ids=array_filter(array_unique($ids));
  107. }
  108. if ( empty($ids) ) {
  109. $this->errorMsg = "没有需要更新的权限";
  110. return false;
  111. }
  112. $role = Role::where('id', '=', $roleID)->first();
  113. if ( empty($role) ) {
  114. $this->errorMsg = "会员角色不存在";
  115. return false;
  116. }
  117. // 添加会员权限
  118. foreach ( $ids as $id ) {
  119. $count = DB::table('role_has_permissions')
  120. ->where('permission_id', '=', $id)
  121. ->where('role_id', '=', $roleID)
  122. ->count();
  123. if ( $count <= 0 ) {
  124. DB::table('role_has_permissions')->insert([
  125. 'permission_id' => $id,
  126. 'role_id' => $roleID,
  127. ]);
  128. }
  129. }
  130. $deleted = DB::table('role_has_permissions')
  131. ->whereNotIn('permission_id', $ids)
  132. ->where('role_id', '=', $roleID)->delete();
  133. return true;
  134. }
  135. /**
  136. * @param Request $request
  137. * @param ResponseFactory $response
  138. * @param $id
  139. * @return \Illuminate\Http\JsonResponse
  140. *
  141. * 删除一行数据
  142. */
  143. protected function destroy(Request $request,ResponseFactory $response,$id = null)
  144. {
  145. if( $request->isMethod('post') && $request->ajax() ){
  146. list($permission_id, $role_id) = explode("-", $id);
  147. $deleted = DB::table('role_has_permissions')
  148. ->where('permission_id', '=', $permission_id)
  149. ->where('role_id', '=', $role_id)->delete();
  150. $deleted === false ? $data = [
  151. 'status' => 1,
  152. 'message'=> self::ERROR_MSG
  153. ]: $data = [
  154. 'status' => 0,
  155. 'message' => ''
  156. ];
  157. if( $request->wantsJson() ){
  158. return $response->json($data);
  159. }
  160. }
  161. }
  162. }