如何在Refinery CMS中允许大写用户名?

时间:2014-03-21 19:36:47

标签: ruby-on-rails devise refinerycms

我相信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

2 个答案:

答案 0 :(得分:2)

它位于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

答案 1 :(得分:0)

找到它

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