【问题标题】:Devise: When is too much customization?设计:什么时候定制太多了?
【发布时间】:2011-06-30 09:33:52
【问题描述】:

我正在为自己编写一个应用程序,所以我并不着急,我唯一的目标就是正确地做事。 对于身份验证,我使用了设计,但结果我定制了很多。 我已经看到 Rails 3.1 中的一些不错的功能可以让我自己更轻松地实现身份验证。

一般来说,Devise 什么时候不再有用并开始妨碍您? 这是我目前拥有的自定义列表,当然除了视图之外,但我仍然希望至少实现对 SEO 友好的 url。

# model/User.rb
  #this method is called by devise to check for "active" state of the model
  def active?
    #remember to call the super
    #then put our own check to determine "active" state using 
    #our own "is_active" column
    super and self.is_active?
  end


protected #====================================================================

  # find in db the user with username or email login
  def self.find_record(login)
    where(attributes).where(["name = :value OR email = :value", { :value => login }]).first
  end

  # allow no case sensitive email
  # (saved downcase, fetched downcase)
  def self.find_for_authentication(conditions)
    conditions[:email].downcase!
    super(conditions)
  end

  # find the user in the db by username or email
  def self.find_for_database_authentication(conditions)
    login = conditions.delete(:login)
    where(conditions).where(["name = :value OR email = :value", { :value => login }]).first
  end

  # Attempt to find a user by it's email. If a record is found, send new
  # password instructions to it. If not user is found, returns a new user
  # with an email not found error.
  def self.send_reset_password_instructions(attributes={})
    recoverable = find_recoverable_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
    recoverable.send_reset_password_instructions if recoverable.persisted?
    recoverable
  end

  def self.find_recoverable_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
    case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }

    attributes = attributes.slice(*required_attributes)
    attributes.delete_if { |key, value| value.blank? }

    if attributes.size == required_attributes.size
      if attributes.has_key?(:login)
        login = attributes.delete(:login)
        record = find_record(login)
      else
        record = where(attributes).first
      end
    end

    unless record
      record = new

      required_attributes.each do |key|
        value = attributes[key]
        record.send("#{key}=", value)
        record.errors.add(key, value.present? ? error : :blank)
      end
    end
    record
  end

  # password not required on edit
  # see: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
  def password_required?
    new_record?
  end


# controllers/registrations_controller.rb
# devise controller for registration
class RegistrationsController < Devise::RegistrationsController

  # update_attributes (with final S) without providing password
  # overrides devise
  # see: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
  def update
    # Devise use update_with_password instead of update_attributes.
    # This is the only change we make.
    if resource.update_attributes(params[resource_name])
      set_flash_message :notice, :updated
      # Line below required if using Devise >= 1.2.0
      sign_in resource_name, resource, :bypass => true
      redirect_to after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      render_with_scope :edit
    end
  end

end

谢谢

【问题讨论】:

    标签: ruby-on-rails devise customization ruby-on-rails-3.1


    【解决方案1】:

    我只是暂时坚持设计,你的变化并不大。但是,我会分叉设计并将您所做的更改提取到新功能中。然后尝试让他们参与设计本身。这样维护它们就不会落在你身上,它可以落在很多人身上。

    维护一个完整的身份验证系统可能会让人头疼,最终还是要重新发明轮子。只需要一个错误就可以让你敞开心扉。

    还有你的新 find_for_authentication 方法,现在已经在设计中得到支持,放入你的设计初始化程序...

    config.case_insensitive_keys = [ :email ]
    

    【讨论】:

    • +1 非常感谢,如果您不使用附带的修正,更新 gem 的目的是什么?
    【解决方案2】:

    好问题 - 我的观点可能是,只要它使事情变得更容易,它就是有用的。您总是可以在 github 上进行设计并在某种程度上将您的定制放在那里 - 这是我在一个项目中所做的。当正确完成身份验证非常重要时,我也有点担心滚动自己的身份验证,特别是如果其他人想看到他们不应该看到的东西。但我很想看看其他人的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 2023-03-06
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多