user.admin在哪里?在rails-devise-pundit启动程序中定义? [英] Where is user.admin? defined in rails-devise-pundit starter app?

查看:240
本文介绍了user.admin在哪里?在rails-devise-pundit启动程序中定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 RailsApps rails-composer 创建一个rails-devise-pundit启动程序应用程序。我还有一点新的轨道上的ruby,设计,专家和轨道4。

I used RailsApps rails-composer to create a rails-devise-pundit starter application. I am still a little new to ruby on rails and newer to devise, pundit and rails 4.

我正在看代码来了解它的工作原理。在控制器和策略类中有很多地方,user.admin?叫做。但我找不到管理员?方法。我会期望它在用户模型,但它不在那里。这是用户类:

I was looking at the code to learn how it works. There are many places in controllers and in policy classes where user.admin? is called. But I can't find the admin? method. I would expect it to be in the User model but it isn't there. Here's the user class:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable
  enum role: [:user, :vip, :admin]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :user
  end
end

在users_controller.rb中部分使用:

Used in part of users_controller.rb:

def show
  @user = User.find(params[:id])
  unless current_user.admin?
    unless @user == current_user
      redirect_to root_path, :alert => "Access denied."
    end
  end
end

pundit 或者以某种方式设计出这种方法?我已经看到它在专利文献中使用,但它只是以它为例。它不表示该方法需要创建或它处理它。是否以某种方式使用角色枚举,它具有:admin作为可能的值?如果有人可以解释这是如何工作的,我会很感激。

Does pundit or devise create this method somehow? I've seen it used in the pundit documentation but it just uses it as an example. It doesn't say the method needs to be created or that it handles it. Is it somehow using the role enum which has :admin as a possible value? If anyone can explain how this works, I'd appreciate it.

我打算尽快添加使用rolify gem来处理角色而不是用户类中的角色枚举。也许我想让角色名称由于某种原因而完全不同。我想确保我了解如何保持一切正常。谢谢。

I'm planning to add in use of the rolify gem soon to handle roles instead of the role enum in the user class. Maybe I'll want to make role names that are completely different for some reason. I want to make sure I understand how to keep everything working. Thanks.

推荐答案

角色定义在 app / models / User.rb 文件( User model)。

Roles are defined in the app/models/User.rb file (the User model).

class User < ActiveRecord::Base
  .
  .
  .
  enum role: [:user, :vip, :admin]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :user
  end

end

应用程序使用ActiveRecord 枚举方法来管理角色。 ActiveRecord提供方便查询角色属性的方法:

The application uses the ActiveRecord enum method to manage roles. ActiveRecord provides convenient methods to query the role attribute:

user.admin! # sets the role to "admin"
user.admin? # => true
user.role  # => "admin"

请参阅 ActiveRecord :: Enum 了解详细信息。 ActiveRecord 枚举方法是Rails 4.1中的新功能。

See documentation for ActiveRecord::Enum for details. The ActiveRecord enum method is new in Rails 4.1.

我更新了 rails-devise-pundit 应用程序以包含此信息。这也在我的 Rails权威教程中介绍。

I've updated the README for the rails-devise-pundit application to include this information. It's also covered in my Rails Pundit Tutorial.

这篇关于user.admin在哪里?在rails-devise-pundit启动程序中定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆