不同型号如何在导轨

时间:2017-06-26 06:47:47

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

这是我的第一个rails应用程序,我对rails很新。我使用pundit创建了一个带有设计的用户模型,为用户模型添加了角色(admin,owner)。

user.rb

    class User < ApplicationRecord
  has_many :owners, dependent: :destroy

  enum role: [:user, :owner, :agent, :admin]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :user
  end

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

userpolicy:

    class UserPolicy
  attr_reader :current_user, :model

  def initialize(current_user, model)
    @current_user = current_user
    @user = model
  end

  def index?
    @current_user.admin?
  end

  def show?
    @current_user.admin? || @current_user == @user
  end

  def update?
    @current_user.admin?
  end

  def destroy?
    return false if @current_user == user
    @current_user.admin?
  end 
end

我创建了一个所有者模型。新所有者仅由管理员角色创建。管理员仅将所有者添加到所有者模型。管理员添加所有者后,所有者电子邮件和密码将发送给所有者电子邮件。使用给定的凭据所有者登录到他的页面。在这里,我想要一个登录页面供所有者登录他的页面。我尝试使用用户的登录页面由所有者登录但我收到错误的用户名和密码无效。是否有可能使用用户的登录为所有者,所以我只有一个登录管理员/所有者登录他们的页面或我应该为所有者创建另一个登录页面?

owner.rb:

    class Owner < ApplicationRecord
has_many :customers, dependent: :destroy
has_many :agents, dependent: :destroy
belongs_to :user

enum role: [:user, :owner]
after_initialize :set_default_role, :if => :new_record?

def set_default_role
  self.role ||= :owner
end

  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
  validates :mobile, presence: true, length: { maximum: 10 }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true

  def Owner.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

end

我的owner_policy:

    class OwnerPolicy
  attr_reader :current_user, :owner

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

  def index?
    @current_user.admin?
  end

  def new?
    @current_user.admin?
  end

  def create?
    @current_user.admin?
  end

  def show?
    @current_user.admin? || @current_user == @owner
  end

  def edit?
    @current_user.admin? || @current_user == @owner
  end

  def update?
    @current_user.admin? || @current_user == @owner
  end

  def destroy?
    return false if @current_user == owner
    @current_user.admin?
  end
end

我用谷歌搜索并尝试了很多答案来实现,但这一切都结束了一团糟。通过尝试不同的答案,它带我到兔子洞。有人,请帮帮我。提前致谢

1 个答案:

答案 0 :(得分:1)

以下是我认为你应该实现尝试实现的目标:

class User < ApplicationRecord
  # This is an **abstract** base class.
  # You should not create a `User` directly;
  # Only create Admin, Owner and Agent records

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  before_save { self.email = email.downcase }

  validates :name, presence: true,
                   length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true,
                    length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :mobile, presence: true,
                     length: { maximum: 10 }
  has_secure_password
  validates :password, presence: true,
                       length: { minimum: 6 },
                       allow_nil: true

  def self.digest(string)
    cost = if ActiveModel::SecurePassword.min_cost
             BCrypt::Engine::MIN_COST
           else
             BCrypt::Engine.cost
           end
    BCrypt::Password.create(string, cost: cost)
  end
end

class Admin < User
  # admin.type == 'Admin'
  # -- use the default column name for Single Table Inheritance.

  has_many :owners, dependent: :destroy
end

class Owner < User
  # owner.type == 'Owner'

  belongs_to :admin
  has_many :customers, dependent: :destroy
  has_many :agents, dependent: :destroy
end

class Agent < User
  # agent.type == 'Agent'

  # Add any agent-specific logic in here.
  # (This wasn't discussed in your original question)
end

您现在可以在其他地方继续继承模式 - 例如在您的政策中:

class OwnerPolicy < UserPolicy

登录时,您始终可以引用User模型 - 因为所有用户类型都是从此继承的。