【问题标题】:Devise: multiple user types设计:多种用户类型
【发布时间】:2016-06-02 02:36:22
【问题描述】:

为了在设计中增加安全性,我需要设置“before_filter”,比如:

before_filter :authenticate_student!, only: [:new, :edit]

这很棒...但是我的应用需要两种用户类型...学生和教师。我如何让控制器只检查其中任何一个是否经过身份验证?

喜欢:

before_filter :authenticate_any!, only: [:new, :edit]

我该如何存档?

我正在使用 Ruby 2.2.0 和 rails 4。

【问题讨论】:

  • 根据您的描述,听起来用户可能有不同的“角色”。用户可以是学生或老师(或两者兼而有之?)。如果是这样,此功能的另一种方法是根据与课程的关系设置角色,并且只有一种类型的用户。然后您可以根据角色而不是身份验证进行限制。
  • 看看这个答案:stackoverflow.com/a/20642860/3033467 似乎正是你想要的。
  • 我的学生/教师表真的很不一样。教师将扮演不同的角色,需要不同的属性。这就是为什么我当时除了学生。

标签: ruby-on-rails devise


【解决方案1】:

只需在应用程序控制器中定义这些方法

class ApplicationController < ActionController::Base
  def authenticate_student!
     authenticate_user! && current_user.student?
  end

  def authenticate_any!
    authenticate_user!
  end
end

您可以填写如何查看student?的代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多