How do you restrict permissions to a specific acti

2019-07-07 17:23发布

I have set up guardian to encode specific permissions in the token, however, I would like to restrict this in my UserController to just require it on specific actions ([:index, :show, :edit, :update, :delete]). I use the following line at the top of the controller to require the admin: [:cms] permission for the entire controller:

plug Guardian.Plug.EnsurePermissions, handler: Ay.Guardian.ErrorHandler, admin: [:cms]

This works fine but I would like to add a guard like clause to this (e.g. when action in [:index, :show, :edit, :update, :delete]) but I can't seem to get this to work the way I want to.

I realize I can easily check the claims in each action but because of the large amount of controllers I would like a more generic approach.

1条回答
冷血范
2楼-- · 2019-07-07 18:16

Try calling the plug like this:

plug Guardian.Plug.EnsurePermissions, [handler: Ay.Guardian.ErrorHandler, admin: [:cms]] when action in [:index, :show, :edit, :update, :delete]

(the [] are important!)

To make it a bit more readable try alias the plug like:

alias Guardian.Plug.EnsurePermissions

plug EnsurePermissions, [handler: Ay.Guardian.ErrorHandler, admin: [:cms]] when action in [:index, :show]
查看更多
登录 后发表回答