diff --git a/app/models/user.rb b/app/models/user.rb index c73353bf..16d91675 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,6 +46,13 @@ class User < ActiveRecord::Base attr_accessor :force_random_password + # Virtual attribute for authenticating by either username or email + attr_accessor :login + + # Add login to attr_accessible + attr_accessible :login + + # # Relations # @@ -140,6 +147,16 @@ class User < ActiveRecord::Base # Class methods # class << self + # Devise method overriden to allow sing in with email or username + def find_for_database_authentication(warden_conditions) + conditions = warden_conditions.dup + if login = conditions.delete(:login) + where(conditions).where(["lower(username) = :value OR lower(email) = :value", { value: login.downcase }]).first + else + where(conditions).first + end + end + def filter filter_name case filter_name when "admins"; self.admins diff --git a/app/views/devise/sessions/new.html.haml b/app/views/devise/sessions/new.html.haml index d904e701..5e93ab18 100644 --- a/app/views/devise/sessions/new.html.haml +++ b/app/views/devise/sessions/new.html.haml @@ -1,19 +1,19 @@ - if ldap_enable? - = render :partial => 'devise/sessions/new_ldap' + = render partial: 'devise/sessions/new_ldap' - else - = form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => { :class => "login-box" }) do |f| - = image_tag "login-logo.png", :width => "304", :height => "66", :class => "login-logo", :alt => "Login Logo" - = f.email_field :email, :class => "text top", :placeholder => "Email", :autofocus => "autofocus" - = f.password_field :password, :class => "text bottom", :placeholder => "Password" + = form_for(resource, as: resource_name, url: session_path(resource_name), html: { class: "login-box" }) do |f| + = image_tag "login-logo.png", width: "304", height: "66", class: "login-logo", alt: "Login Logo" + = f.text_field :login, class: "text top", placeholder: "Username or Email", autofocus: "autofocus" + = f.password_field :password, class: "text bottom", placeholder: "Password" - if devise_mapping.rememberable? .clearfix.inputs-list - %label.checkbox.remember_me{:for => "user_remember_me"} + %label.checkbox.remember_me{for: "user_remember_me"} = f.check_box :remember_me %span Remember me %br/ - = f.submit "Sign in", :class => "btn-create btn" + = f.submit "Sign in", class: "btn-create btn" .pull-right - = link_to "Forgot your password?", new_password_path(resource_name), :class => "btn" + = link_to "Forgot your password?", new_password_path(resource_name), class: "btn" %br/ - if Gitlab.config.gitlab.signup_enabled %hr/ diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 9c397633..5714407f 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -23,7 +23,7 @@ Devise.setup do |config| # session. If you need permissions, you should implement that in a before filter. # You can also supply a hash where the value is a boolean determining whether # or not authentication should be aborted when the value is not present. - # config.authentication_keys = [ :email ] + config.authentication_keys = [ :login ] # Configure parameters from the request object used for authentication. Each entry # given should be a request method and it will automatically be passed to the diff --git a/spec/support/login_helpers.rb b/spec/support/login_helpers.rb index 4579c971..d423ccf8 100644 --- a/spec/support/login_helpers.rb +++ b/spec/support/login_helpers.rb @@ -12,7 +12,7 @@ module LoginHelpers # user - User instance to login with def login_with(user) visit new_user_session_path - fill_in "user_email", with: user.email + fill_in "user_login", with: user.email fill_in "user_password", with: "123456" click_button "Sign in" end