简体   繁体   English

具有Devise&Pundit的Rails 4-@user或@current_user

[英]Rails 4 with Devise & Pundit - @user or @current_user

I am trying to make an app with Rails 4 and devise and pundit (with rolify for roles) 我正在尝试使用Rails 4制作一个应用程序并进行设计和审查(以角色扮演角色)

In the: 在里面:

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

According to this Go Rails video: https://gorails.com/episodes/authorization-with-pundit?autoplay=1 根据这段Go Rails视频: https : //gorails.com/episodes/authorization-with-pundit?autoplay=1

This attr_reader section in the application policy covers all of the rest of the policies - so you don't need to repeat in each one. 应用程序策略中的attr_reader部分涵盖了所有其他策略-因此您无需在每个策略中都重复。

However, my current question is given that I use devise, should I change the application policy to use current_user instead of user? 但是,当前的问题是我使用devise,是否应该将应用程序策略更改为使用current_user而不是user? Eg: 例如:

class ApplicationPolicy
      attr_reader :current_user, :record

      def initialize(current_user, record)
        @current_user = current_user
        @record = record
      end 

None of the examples I've found do it this way, but I don't understand why not. 我发现的所有示例都没有这种方式,但是我不明白为什么不这样做。

I was hoping to figure out if I'm off the the right start before I start writing rules for each policy. 我希望在开始为每个策略编写规则之前先弄清楚我是否正确。 Does every controller action that i make a policy for need to refer to user or should I change all of them to current_user? 我制定策略的每个控制器操作是否都需要引用用户,还是应该将它们全部更改为current_user?

You can use whatever you want to do the authorization logic. 您可以使用任何您想做的授权逻辑。

Pundit will send in current_user from your controller when you call something like authorize @object. 当您调用诸如authorize @object之类的命令时,Pundit将从您的控制器发送current_user。

From in your class you will just have to do your logic with current_user instead of user. 在您的类中,您只需要使用current_user而不是user来进行逻辑处理。

Why would you want to change it? 您为什么要更改它? From your applications perspective, you are really authorizing a user to do something, not necessarily a current_user. 从应用程序的角度来看,您实际上是在授权用户执行某项操作,而不一定要是current_user。 So keeping it as user follows conventions 因此,保持用户遵循惯例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM