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));

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!