douhuan1648 2019-04-07 06:12
浏览 70
已采纳

如何在laravel中修复没有未定义偏移的检查权限?

The problem is undefined offset when i try to show all posts with permissions. Without Encode and Decode the links this problem never been happened. When i encode and try to pass the encode links to edit post, it's no problem. but when i try to show all post, the problem was happened and show me undefined offset [0]. How to fix this problem??

My controller to edit post

 public function edit($id)
    {
        $key = Hashids::connection('main')->decode($id)[0] ??  abort(404);
        $post = Post::findOrFail($key);
        $tags = Tag::all();
        $tags2 = array();
        foreach($tags as $tag){
            $tags2[$tag->id] = $tag->name;
        }
        return view('dashboard.adminblogpost.editpost', compact('post','tags2'));
    }

my view

@php $parameter = Hashids::connection('main')->encode($post->id); @endphp

@if(check_user_permissions($request, "BlogPost@edit", $parameter))
      <a href="{{route('admins-blogpost.edit', $parameter)}}" class="btn btn-xs btn-warning">
          <i class="fa fa-edit"></i>
      </a>
@else
       <a href="#" class="btn btn-xs btn-warning disabled">
           <i class="fa fa-edit"></i>
       </a>
@endif

and method for check permission

<?php
use Vinkla\Hashids\Facades\Hashids;
function check_user_permissions($request, $actionName = NULL, $id = NULL)
{
    $currentUser = $request->user();
        if($actionName)
        {
            $currentActionName = $actionName;
        }
        else{
            $currentActionName = ($request->route()->getActionName());
        }

        list($controller, $method) = explode('@', $currentActionName);
        $controller = str_replace(["App\\Http\\Controllers\\Backend\\", "Controller"], "", $controller);


        $crudPermissionsMap = [
            'crud' => ['create', 'store', 'edit', 'update', 'destroy', 'restore', 'forceDestroy', 'index', 'view']
        ];

        $classesMap = [
            'BlogPost'       => 'post',
            'CategoriesPost' => 'category',
            'Users'          => 'user'
        ];

        foreach($crudPermissionsMap as $permission => $methods){

            if(in_array($method, $methods) && isset($classesMap[$controller]))
            {
                $classesName = $classesMap[$controller];
                // dd("{$permission}-{$classesName}");
                if($classesName == 'post' && in_array($method, ['edit', 'update', 'restore', 'destroy', 'forceDestroy']))
                {
                    $id = !is_null($id) ? $id : $request->route("admins_blogpost");
                    if( $id
                        && (!$currentUser->can('update-others-post') || !$currentUser->can('delete-others-post')))
                    {

                                $post = \App\Post::withTrashed()->find(Hashids::decode($id)[0]);
                                if($post->author_id != $currentUser->id){

                                return false;

                            }
                    }
                }
                elseif(! $currentUser->can("{$permission}-{$classesName}")){
                    // abort(403, "forbidden access!");
                    return false;
                }
                break;
            }
        }
    return true;
}
</div>
  • 写回答

1条回答 默认 最新

  • drqwbh2150 2019-04-07 06:52
    关注

    sorry it's my fault. i don't check all posts. it's must $post = \App\Post::withTrashed()->find(Hashids::decode($id)[0]?? $id));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,