123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace App\Http\Controllers\Permission;
- use App\Http\Controllers\AdminBaseController;
- use App\Models\Permission;
- use App\Models\Role;
- use App\Repositories\Eloquent\PermissionRepositoryEloquent;
- use App\Validators\PermissionValidator;
- use Illuminate\Contracts\Routing\ResponseFactory;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- /**
- * Class PermissionsController.
- *
- * @package namespace App\Http\Controllers;
- */
- class PermissionsController extends AdminBaseController
- {
- /**
- * PermissionsController constructor.
- *
- * @param PermissionRepositoryEloquent $repository
- * @param PermissionValidator $validator
- */
- public function __construct(PermissionRepositoryEloquent $repository, PermissionValidator $validator)
- {
- parent::__construct($repository, $validator);
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- *
- */
- protected function index(Request $request, ResponseFactory $response)
- {
- if( $request->isMethod('post') && $request->ajax() ){
- $roleID = $request->input('id'); // roleID
- // 查询关联信息
- $datum = DB::table('role_has_permissions as rhp')
- ->select(['rhp.permission_id', 'rhp.role_id', 'p.name', 'p.routes', 'p.created_at', 'p.updated_at'])
- ->RightJoin('permissions as p', 'p.id', '=', 'rhp.permission_id')
- ->where('rhp.role_id', '=', $roleID)
- ->paginate($request->pageSize);
- foreach ( $datum as &$data ) {
- $data->id = "{$data->permission_id}-{$data->role_id}";
- }
- unset($data);
- if( $request->wantsJson() ){
- return $response->json([
- "total" => $datum->total(), // $datum->total() 返回总行数
- "rows" => $datum->items(), // 返回当前查询出来的数据
- "data" => $datum, // 返回当前对象
- ]);
- }
- }
- $params = array();
- $params['id'] = request()->input('id', 0);
- $params['role'] = Role::where('id', $params['id'])->first();
- return view("admins.{$this->tables}.index", (array)$params);
- }
- // 会员编辑
- protected function _storeGet($request)
- {
- $roleID = $request->input('id', 0); // 角色ID
- // 查询会员绑定的权限
- $permissions = DB::table('role_has_permissions')
- ->where('role_id', '=', $roleID)
- ->get();
- $permissionIDS = array(); // 已绑定权限
- foreach ( $permissions as $permission ) {
- $permissionIDS[] = $permission->permission_id;
- }
- // 查询当前
- // $notBindingPermission = DB::table('permissions')->whereNotIn('id', $permissionIDS)->get();
- $list= Permission::where('p_id','0')->where('type',1)->where('is_del','0')->select(['id','name as label'])->orderBy('sort','asc')->get()->toArray();
- foreach ($list as &$item){
- $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();
- foreach ($item['children'] as &$value){
- $child=Permission::where('p_id',$value['id'])->where('type',3)->where('is_del','0')->select(['id','name as label'])->orderBy('sort','asc')->get()->toArray();
- if($child)$value['children']=$child;
- }
- }
- // $checkboxs = array();
- // foreach ( $notBindingPermission as $key=>$value ) {
- // $checkboxs[] = array(
- // 'value' => $value->id,
- // 'label' => $value->name,
- // 'disable' => "",
- // 'checked' => false,
- // );
- // }
- ////
- // $params['checkboxs'] = json_encode($checkboxs);
- $params['list'] = json_encode($list);
- $params['select_ids'] = $permissionIDS;
- $params['role_id'] = $roleID;
- return $params;
- }
- protected function _storePost($request)
- {
- $ids = $request->input('ids', []);
- $roleID = $request->input('role_id', 0);
- if(is_string($ids)){
- $ids=explode(',',$ids);
- $ids=array_filter(array_unique($ids));
- }
- if ( empty($ids) ) {
- $this->errorMsg = "没有需要更新的权限";
- return false;
- }
- $role = Role::where('id', '=', $roleID)->first();
- if ( empty($role) ) {
- $this->errorMsg = "会员角色不存在";
- return false;
- }
- // 添加会员权限
- foreach ( $ids as $id ) {
- $count = DB::table('role_has_permissions')
- ->where('permission_id', '=', $id)
- ->where('role_id', '=', $roleID)
- ->count();
- if ( $count <= 0 ) {
- DB::table('role_has_permissions')->insert([
- 'permission_id' => $id,
- 'role_id' => $roleID,
- ]);
- }
- }
- $deleted = DB::table('role_has_permissions')
- ->whereNotIn('permission_id', $ids)
- ->where('role_id', '=', $roleID)->delete();
- return true;
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @param $id
- * @return \Illuminate\Http\JsonResponse
- *
- * 删除一行数据
- */
- protected function destroy(Request $request,ResponseFactory $response,$id = null)
- {
- if( $request->isMethod('post') && $request->ajax() ){
- list($permission_id, $role_id) = explode("-", $id);
- $deleted = DB::table('role_has_permissions')
- ->where('permission_id', '=', $permission_id)
- ->where('role_id', '=', $role_id)->delete();
- $deleted === false ? $data = [
- 'status' => 1,
- 'message'=> self::ERROR_MSG
- ]: $data = [
- 'status' => 0,
- 'message' => ''
- ];
- if( $request->wantsJson() ){
- return $response->json($data);
- }
- }
- }
- }
|