Resource如何使用Policy认证

本文探讨了在Laravel框架中如何为资源控制器正确应用策略,以实现更优雅的权限控制。作者分享了使用$this->authorizeResource遇到的问题,并通过社区讨论找到了解决方案,包括正确注册策略和使用类型提示。

https://laracasts.com/discuss/channels/laravel/how-to-apply-policy-to-a-resource-controller?page=1

 

How to apply policy to a resource controller?

Hello. I have a basic resource controller and placing $this->authorize in every method isn't very good I think(maybe I am wrong). Also I have some custom methods(upload, develop) and I'd like to apply policy to those too. I found this method: $this->authorizeResource, but it always shows me "This action is unauthorized.". My model name is snake-cased(EmailList). Also my resource controller methods doesn't require model instance. Here is what I mean:

public function show($id) //--- As you see, no model instance here
    {
        $list = EmailList::find($id);
        //$this->authorize('view', $list);  //--- This works perfectly, by the way..
        return view('dispatch.lists.exact')->with('list', $list);
    }

Awaiting for your reply, thanks in advance!

 

tykus

Level 50

tykusJan 9, 2018

  •  
  •  
  •  
  •  
  •  
  •  

Why can you not typehint the model? If you have a resource route with a hyphen, then the wildcard parameter will be snake_case...

 

...and in the controller, you can use camelCase for the typehinted variable name:

public function show(EmailList $emailList)
{
    return view('dispatch.lists.exact')->with('list', $emailList);
}

martinbean

Level 50

martinbeanJan 9, 2018

  •  
  •  
  •  
  •  
  •  

@Void If you’re extending the base controller in Laravel, then you can use the authorizeResource() method:

class ArticleController extends Controller
{
    public function __construct()
    {
        $this->authorizeResource(Article::class);
    }

    public function index()
    {
        //
    }

    public function create()
    {
        // Will call ArticlePolicy::create()
    }

    public function store()
    {
        // Will call ArticlePolicy::create()
    }

    public function show()
    {
        // Will call ArticlePolicy::view()
    }

    public function edit()
    {
        // Will call ArticlePolicy::update()
    }

    public function create()
    {
        // Will call ArticlePolicy::update()
    }

    public function create()
    {
        // Will call ArticlePolicy::delete()
    }
}

3

tykus

Level 50

tykusJan 9, 2018

  •  
  •  
  •  
  •  
  •  
  •  

@martinbean nice!

1

7f26ec6df9639f8b3580acadbd8b9076?s=100&d=https%3A%2F%2Fs3.amazonaws.com%2Flaracasts%2Fimages%2Fforum%2Favatars%2Favatar-7.pnguploading.gif转存失败重新上传取消Void

Level 1

VoidJan 10, 2018

Well, I did some changes to the code:

class EmailListsController extends Controller
{
    public function __construct()
        {
            $this->authorizeResource(EmailList::class);
        }
    ....
    public function show(EmailList $list)
        {
            return view('dispatch.lists.exact')->with('list', $list);
        }
    ....
}

But it always returns "This action is unauthorized.". Here is my view method in EmailListPolicy file:

....
    public function view(User $user, EmailList $list)
    {
        return $user->id === $list->user_id;
    }
....

And my route:

Route::resource('lists', 'Dispatch\EmailListsController');

What`s wrong?

ehsanhoushmand

Level 1

ehsanhoushmandJun 7, 2018

Do you register your policy in AuthServiceProvider?

martinbean

Level 50

martinbeanJun 7, 2018

  •  
  •  
  •  
  •  
  •  

@ehsanhoushmand Did you see the last reply was four months ago?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值