Commit 795bb9cf65daf2bfff7dda20d5fed6719d1b1b1d

Authored by Dmitriy Zaporozhets
2 parents 1496c015 9d92433a

Merge branch 'feature/login_with_username'

app/models/user.rb
... ... @@ -46,6 +46,13 @@ class User < ActiveRecord::Base
46 46  
47 47 attr_accessor :force_random_password
48 48  
  49 + # Virtual attribute for authenticating by either username or email
  50 + attr_accessor :login
  51 +
  52 + # Add login to attr_accessible
  53 + attr_accessible :login
  54 +
  55 +
49 56 #
50 57 # Relations
51 58 #
... ... @@ -140,6 +147,16 @@ class User < ActiveRecord::Base
140 147 # Class methods
141 148 #
142 149 class << self
  150 + # Devise method overriden to allow sing in with email or username
  151 + def find_for_database_authentication(warden_conditions)
  152 + conditions = warden_conditions.dup
  153 + if login = conditions.delete(:login)
  154 + where(conditions).where(["lower(username) = :value OR lower(email) = :value", { value: login.downcase }]).first
  155 + else
  156 + where(conditions).first
  157 + end
  158 + end
  159 +
143 160 def filter filter_name
144 161 case filter_name
145 162 when "admins"; self.admins
... ...
app/views/devise/sessions/new.html.haml
1 1 - if ldap_enable?
2   - = render :partial => 'devise/sessions/new_ldap'
  2 + = render partial: 'devise/sessions/new_ldap'
3 3 - else
4   - = form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => { :class => "login-box" }) do |f|
5   - = image_tag "login-logo.png", :width => "304", :height => "66", :class => "login-logo", :alt => "Login Logo"
6   - = f.email_field :email, :class => "text top", :placeholder => "Email", :autofocus => "autofocus"
7   - = f.password_field :password, :class => "text bottom", :placeholder => "Password"
  4 + = form_for(resource, as: resource_name, url: session_path(resource_name), html: { class: "login-box" }) do |f|
  5 + = image_tag "login-logo.png", width: "304", height: "66", class: "login-logo", alt: "Login Logo"
  6 + = f.text_field :login, class: "text top", placeholder: "Username or Email", autofocus: "autofocus"
  7 + = f.password_field :password, class: "text bottom", placeholder: "Password"
8 8 - if devise_mapping.rememberable?
9 9 .clearfix.inputs-list
10   - %label.checkbox.remember_me{:for => "user_remember_me"}
  10 + %label.checkbox.remember_me{for: "user_remember_me"}
11 11 = f.check_box :remember_me
12 12 %span Remember me
13 13 %br/
14   - = f.submit "Sign in", :class => "btn-create btn"
  14 + = f.submit "Sign in", class: "btn-create btn"
15 15 .pull-right
16   - = link_to "Forgot your password?", new_password_path(resource_name), :class => "btn"
  16 + = link_to "Forgot your password?", new_password_path(resource_name), class: "btn"
17 17 %br/
18 18 - if Gitlab.config.gitlab.signup_enabled
19 19 %hr/
... ...
config/initializers/devise.rb
... ... @@ -23,7 +23,7 @@ Devise.setup do |config|
23 23 # session. If you need permissions, you should implement that in a before filter.
24 24 # You can also supply a hash where the value is a boolean determining whether
25 25 # or not authentication should be aborted when the value is not present.
26   - # config.authentication_keys = [ :email ]
  26 + config.authentication_keys = [ :login ]
27 27  
28 28 # Configure parameters from the request object used for authentication. Each entry
29 29 # given should be a request method and it will automatically be passed to the
... ...
spec/support/login_helpers.rb
... ... @@ -12,7 +12,7 @@ module LoginHelpers
12 12 # user - User instance to login with
13 13 def login_with(user)
14 14 visit new_user_session_path
15   - fill_in "user_email", with: user.email
  15 + fill_in "user_login", with: user.email
16 16 fill_in "user_password", with: "123456"
17 17 click_button "Sign in"
18 18 end
... ...