dongyouzhi7218 2019-06-10 12:35
浏览 322
已采纳

Laravel 5.8 - 授权无需在AuthServicerProvider.php中注册ProjectPolicy即可运行


I am following Jeffrey Way's laracasts from scratch and he mentions registering the ProjectPolicy.php in AuthServiceProvider.php. However, I tried refreshing my auth page to check on some other account without doing so, and it still works.

This is a weird question because I think I'm wasting time on something which works and I shouldnt be worried about. Below is the code snippet.

Ive tried commenting quite a few LoCs which I thought could be used by the framework to authorize the pages-

ProjectsController.php

public function __construct(){

      // $this->middleware('auth'); 

    }

unedited version for the question, here.
show() method in ProjectsController.php

  public function show(Project $project, Twitter $twitter)
    {
        // $twitter = app('twitter');
        // dd($twitter);

        // abort_if($project->owner_id !== auth()->id(),403);
        //abort_unless();
        $this->authorize('view',$project);

        return view('project.show',compact('project'));
    }

ProjectPolicy.php


    public function view(User $user, Project $project)
    {
         return $project->owner_id == $user->id;//works even if I remove this
    }// works even if I remove the complete method.

ProjectsController.php

public function show(Project $project, Twitter $twitter)//edited
    {
        $this->authorize('view',$project);//the authorization is enabled just by this loc.

        return view('project.show',compact('project'));
    }

I am so confused so as to how this is still working.

How is the framework picking up authorize('view',$project); even when Im removing the view() method?

Edit: Found this on the documentations page.

Instead of manually registering model policies, Laravel can auto-discover policies as long as the model and policy follow standard Laravel naming conventions. Specifically, the policies must be in a Policies directory below the directory that contains the models. So, for example, the models may be placed in the app directory while the policies may be placed in the app/Policies directory. In addition, the policy name must match the model name and have a Policy suffix. So, a User model would correspond to a UserPolicy class.

  • 写回答

1条回答 默认 最新

  • dongliang9682 2019-06-10 12:43
    关注

    You need to register your policy to fit your model, more informations :

    https://laravel.com/docs/5.8/authorization#registering-policies

    In your app/Providers/AuthServiceProvider.php file, just add :

    protected $policies = [
        Project::class => ProjectPolicy::class,
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)