Commit 795bb9cf65daf2bfff7dda20d5fed6719d1b1b1d
Exists in
master
and in
4 other branches
Merge branch 'feature/login_with_username'
Showing
4 changed files
with
27 additions
and
10 deletions
Show diff stats
app/models/user.rb
@@ -46,6 +46,13 @@ class User < ActiveRecord::Base | @@ -46,6 +46,13 @@ class User < ActiveRecord::Base | ||
46 | 46 | ||
47 | attr_accessor :force_random_password | 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 | # Relations | 57 | # Relations |
51 | # | 58 | # |
@@ -140,6 +147,16 @@ class User < ActiveRecord::Base | @@ -140,6 +147,16 @@ class User < ActiveRecord::Base | ||
140 | # Class methods | 147 | # Class methods |
141 | # | 148 | # |
142 | class << self | 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 | def filter filter_name | 160 | def filter filter_name |
144 | case filter_name | 161 | case filter_name |
145 | when "admins"; self.admins | 162 | when "admins"; self.admins |
app/views/devise/sessions/new.html.haml
1 | - if ldap_enable? | 1 | - if ldap_enable? |
2 | - = render :partial => 'devise/sessions/new_ldap' | 2 | + = render partial: 'devise/sessions/new_ldap' |
3 | - else | 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 | - if devise_mapping.rememberable? | 8 | - if devise_mapping.rememberable? |
9 | .clearfix.inputs-list | 9 | .clearfix.inputs-list |
10 | - %label.checkbox.remember_me{:for => "user_remember_me"} | 10 | + %label.checkbox.remember_me{for: "user_remember_me"} |
11 | = f.check_box :remember_me | 11 | = f.check_box :remember_me |
12 | %span Remember me | 12 | %span Remember me |
13 | %br/ | 13 | %br/ |
14 | - = f.submit "Sign in", :class => "btn-create btn" | 14 | + = f.submit "Sign in", class: "btn-create btn" |
15 | .pull-right | 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 | %br/ | 17 | %br/ |
18 | - if Gitlab.config.gitlab.signup_enabled | 18 | - if Gitlab.config.gitlab.signup_enabled |
19 | %hr/ | 19 | %hr/ |
config/initializers/devise.rb
@@ -23,7 +23,7 @@ Devise.setup do |config| | @@ -23,7 +23,7 @@ Devise.setup do |config| | ||
23 | # session. If you need permissions, you should implement that in a before filter. | 23 | # session. If you need permissions, you should implement that in a before filter. |
24 | # You can also supply a hash where the value is a boolean determining whether | 24 | # You can also supply a hash where the value is a boolean determining whether |
25 | # or not authentication should be aborted when the value is not present. | 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 | # Configure parameters from the request object used for authentication. Each entry | 28 | # Configure parameters from the request object used for authentication. Each entry |
29 | # given should be a request method and it will automatically be passed to the | 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,7 +12,7 @@ module LoginHelpers | ||
12 | # user - User instance to login with | 12 | # user - User instance to login with |
13 | def login_with(user) | 13 | def login_with(user) |
14 | visit new_user_session_path | 14 | visit new_user_session_path |
15 | - fill_in "user_email", with: user.email | 15 | + fill_in "user_login", with: user.email |
16 | fill_in "user_password", with: "123456" | 16 | fill_in "user_password", with: "123456" |
17 | click_button "Sign in" | 17 | click_button "Sign in" |
18 | end | 18 | end |