【问题标题】:Whats the best approach for using sorcery(auth) with multiple user classes?将巫术(auth)与多个用户类一起使用的最佳方法是什么?
【发布时间】:2011-11-25 17:37:23
【问题描述】:

我正在寻找创建两个模型:培训师和客户。

注册时,这两种模型共享基本的身份验证信息,例如电子邮件和密码。

因此我想使用 Sorcery 为我做身份验证,gem 默认创建一个 User 模型。

通过 StackOverflow 进行搜索我知道我可以使用单表继承,但大多数人都认为这是有问题的。

对于这两种类型的用户是否有更好/更简单的解决方案来共享基本身份验证信息,但它们是包含其角色特定数据的单独模型?


如果我把事情搞混了,我很抱歉。

【问题讨论】:

    标签: ruby-on-rails class inheritance


    【解决方案1】:

    您的两个用户拥有什么样的“角色特定数据”?

    我的情况与您在我仍在开发的应用程序中的情况非常相似。我选择通过 CanCan 使用基于角色的方法。

    class User < ActiveRecord::Base
      has_one :client_profile # or whatever a client has here
      has_one :trainer_profile # or whatever a trainer has here
    end
    

    然后,你将定义你的能力

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        user ||= User.new # anonymous user
        if user.is? :trainer
          can :create, TrainerProfile
          # some other trainer specific roles here, like editing his/her profile
        elseif user.is? :client
          can :create, ClientProfile
          # some other client specific roles here, like editing his/her profile
        end
      end
    end
    

    当然,上面的代码假设是? User 类上的方法来检查用户角色。

    有关 CanCan 的更多信息,请访问 CanCan wikiRailscast on CanCan

    【讨论】:

    • 首先感谢您的回复!具体数据例如:一个培训师有一个专业化、max_clients、vanity_url 等,而一个客户可能有一个 trainer_id、starting_weight、goal_weight 等。关系 Im looking to create between them is has_many and belongs_to. Hope this clarifies it a little bit. Later today Ill 坐下来查看 CanCan。但是对于我的情况,我正在考虑构建一个自定义身份验证系统,以便培训师和客户可以创建单独的帐户等。不是很干,但可能会起作用。让我知道你的想法,再次感谢。
    • 那样的话,我不会太害怕单表继承。另一个更好的建议是创建两个模型,TrainerClient,它们都调用 authenticates_with_sorcery!
    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多