Search code examples
ruby-on-railsdeviserefinerycms

How do I allow uppercase usernames in Refinery CMS?


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


Solution

  • 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