문제

I believe Refinery uses Devise, and I found this guide to allow uppercase usernames in Devise

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

However, even with

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

it still forced the username to lowercase.

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

I saw this question, but it did not help

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

Refinery 2.1.1, Devise 2.2.8, Rails 3.2.14

도움이 되었습니까?

해결책

It is in the Refinery::User model. There's a before_validation filter that downcases usernames:

...
before_validation :downcase_username, :strip_username
...
private

  def downcase_username
    self.username = self.username.downcase if self.username?
  end

You could decorate the Refinery::User model:

Refinery::User.class_eval do

  private

    def downcase_username
      self.username if self.username?
    end

end

다른 팁

Found it

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top