Skip to content

Commit 63cce3c

Browse files
committed
正确支持 User login 里面的大小写字母,不再全部转成小写。
之前转小写是错误的配置了 Devise 的 case_insensitive_keys 修正某些字符的用户名主页无法打开的问题;
1 parent a6d0d80 commit 63cce3c

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

app/controllers/users_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def show
3535
protected
3636

3737
def set_user
38-
# 处理 login 有大写字母的情况
39-
if params[:id] != params[:id].downcase
40-
redirect_to request.path.downcase, status: 301
41-
return
38+
@user = User.find_login!(params[:id])
39+
40+
# 转向正确的拼写
41+
if @user.login != params[:id]
42+
redirect_to user_path(@user.login), status: 301
4243
end
4344

44-
@user = User.find_login!(params[:id])
4545
@user_type = @user.user_type
4646
if @user.deleted?
4747
render_404

app/models/user.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,12 @@ def self.find_login!(slug)
212212

213213
def self.find_login(slug)
214214
return nil unless slug =~ ALLOW_LOGIN_CHARS_REGEXP
215-
slug = slug.downcase
216-
fetch_by_uniq_keys(login: slug)
215+
fetch_by_uniq_keys(login: slug) || where("lower(login) = ?", slug.downcase).take
217216
end
218217

219218
def self.find_by_login_or_email(login_or_email)
220219
login_or_email = login_or_email.downcase
221-
fetch_by_uniq_keys(login: login_or_email) || fetch_by_uniq_keys(email: login_or_email)
220+
find_login(login_or_email) || find_by_email(login_or_email)
222221
end
223222

224223
def self.find_for_database_authentication(warden_conditions)

config/initializers/devise.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Configure which authentication keys should be case-insensitive.
2929
# These keys will be downcased upon creating or modifying a user and when used
3030
# to authenticate or find a user. Default is :email.
31-
config.case_insensitive_keys = [:email, :login]
31+
config.case_insensitive_keys = [:email]
3232

3333
# Configure which authentication keys should have whitespace stripped.
3434
# These keys will have whitespace before and after removed upon creating or

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
get 'users/city/:id' => 'users#city', as: 'location_users'
187187
get 'users' => 'users#index', as: 'users'
188188

189-
constraints(id: /[\w\-\.]*/) do
189+
constraints(id: /[a-zA-Z0-9\_\-\.]*/) do
190190
resources :users, path: '', as: 'users' do
191191
member do
192192
# User only

spec/controllers/users_controller_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
get :topics, params: { id: user.login }
3535
expect(response).to be_success
3636
end
37+
38+
it 'should redirect to right spell login' do
39+
get :topics, params: { id: user.login.upcase }
40+
expect(response.status).to eq(301)
41+
end
3742
end
3843

3944
describe ':replies' do

spec/models/user_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@
6666
let(:user) { build(:user, login: 'aaa*11') }
6767
it { expect(user.valid?).to eq false }
6868
end
69+
70+
describe 'Login allow upcase downcase both' do
71+
let(:user1) { create(:user, login: 'ReiIs123') }
72+
73+
it 'should work' do
74+
expect(user1.login).to eq('ReiIs123')
75+
expect(User.find_login('ReiIs123').id).to eq(user1.id)
76+
expect(User.find_login('reiis123').id).to eq(user1.id)
77+
expect(User.find_login('rEIIs123').id).to eq(user1.id)
78+
end
79+
end
6980
end
7081

7182
describe '#read_topic?' do

0 commit comments

Comments
 (0)