【问题标题】:How do I allow uppercase usernames in Refinery CMS?如何在 Refinery CMS 中允许大写用户名?
【发布时间】:2014-03-21 19:36:46
【问题描述】:

我相信 Refinery 使用了 Devise,我发现本指南允许在 Devise 中使用大写用户名

https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-sign-in-using-their-username-or-email-address

然而,即使有

config.authentication_keys = [ :login ]
config.case_insensitive_keys = [:email]

它仍然强制用户名小写。

>  u = User.create username: "UsErNaMe", password: "secret", email: "email@com.com"
 => #<Refinery::User id: 60, username: "username", email: "email@com.com", 

我看到了这个问题,但没有帮助

Devise: Allow users to register as "UsErNaMe" but login with "username"

炼油厂 2.1.1、设计 2.2.8、Rails 3.2.14

【问题讨论】:

    标签: ruby-on-rails devise refinerycms


    【解决方案1】:

    它在Refinery::User model 中。有一个 before_validation 过滤器可以将用户名小写:

    ...
    before_validation :downcase_username, :strip_username
    ...
    private
    
      def downcase_username
        self.username = self.username.downcase if self.username?
      end
    

    你可以装饰 Refinery::User 模型:

    Refinery::User.class_eval do
    
      private
    
        def downcase_username
          self.username if self.username?
        end
    
    end
    

    【讨论】:

    • 我更喜欢这个。有多个控制器保存这个对象。
    【解决方案2】:

    找到了

    intended_username = user_params[:username] # save the username because Refinery converts it to lowercase! https://github.com/refinery/refinerycms/blob/master/authentication/app/models/refinery/user.rb#L28
    intended_username.strip! # trim spaces
    
    if current_refinery_user.update_without_password(user_params)
      current_refinery_user.username = intended_username # restore the username as the user intended with mixed case
      current_refinery_user.save(validate: false) # skip validations
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多