【问题标题】:What is the proper way to validate that a User's admin attribute is set to true in your controllers?验证控制器中用户的管理属性是否设置为 true 的正确方法是什么?
【发布时间】:2014-12-21 04:22:24
【问题描述】:

我有一个 Devise User 模型,其属性 admin 类型为布尔值。如果 admin = true,我如何在我的控制器中指定只希望某些操作可用?以下授权方法是否有效?

def authorize
redirect_to login_url, alert:"Not authorized" if current_user.admin == false
end

这是我的 Users 表,admin 是一个布尔值。

create_table "users", force: true do |t|
t.string   "email",                  default: "",    null: false
t.string   "encrypted_password",     default: "",    null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,     null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean  "admin",                  default: false
t.string   "image_file_name"
t.string   "image_content_type"
t.integer  "image_file_size"
t.datetime "image_updated_at"
t.string   "name"
end

使用 Pry 的 authorize_admin 方法中 current_user 的输出:

NameError: undefined local variable or method `current_user' for :authorize_admin:Symbol

我正在使用设计,所以 current_user 不应该工作吗?

这是控制台中 User.first 的输出:

#<User id: 1, email: "email@gmail.com", encrypted_password:    

"$2a$10$RCgzx7PJho4vegbf8Z04eumTGB1RyDl5YvDeCAMz7g3...", reset_password_token: nil,   
 reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 5,   
 current_sign_in_at: "2014-10-25 23:42:17", last_sign_in_at: "2014-10-23 00:57:07", 
 current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at:   
 "2014-10-10   
19:24:39", updated_at: "2014-10-25 23:42:17", admin: false, image_file_name: nil,   
image_content_type: nil, image_file_size: nil, image_updated_at: nil, name: nil>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    如何在我的控制器中指定只希望某些操作在 admin = true 时可用?

    您可以使用rails before filter 来实现它。 您可以在应用程序控制器中定义您的授权方法,这将使该方法在您的所有控制器中都可用。

    class ApplicationController < ActionController::Base
      private
      def authorize
        redirect_to login_url, alert:"Not authorized" if current_user.try(:admin) == false
      end
    end
    

    现在在控制器内部,您可以在过滤器之前设置您想要授权的方法。

    class HomeController < ApplicationController
      before_action :authorize, only: [:your_action_name]
      def your_action_name
        #some action
      end
    end
    

    如果您的代码有很多依赖于管理员和普通用户的自定义,那么您也可以使用cancancan

    【讨论】:

    • 谢谢。为什么它应该是私有的?
    • 我刚刚收到“未定义的方法管理员”错误。我只是仔细检查了我的架构,admin 确实是用户表的布尔属性,其默认设置为 false。我用 Devise 生成了我的用户模型,如果这会影响什么?
    • @JoeSmith checkout this thread 使用私有方法。 undefined method admin 错误表明您没有 current_user 或基本上 users 表的管理字段。您可以从 rails 控制台或您的架构发布 current_user 的输出吗?
    • 我刚刚将我的用户表编辑到问题中。我知道这通常意味着什么,但管理员在那里,所以我很困惑。
    • @JoeSmith 啊!那很奇怪。你可以在使用调试器或撬后发布 current_user 的输出吗
    【解决方案2】:

    我使用Declarative Authorization gem 来控制用户对控制器操作的访问并强烈推荐它。它允许您设置用户类别,然后控制哪个组可以访问哪些控制器操作。它功能强大、灵活且易于设置。

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2011-12-18
      相关资源
      最近更新 更多