diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 11a03f25e..b80282798 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -1,5 +1,5 @@ class AccountsController < ApplicationController - before_action :require_login, only: [:simple_update] + before_action :require_login, only: [:login_check, :simple_update] include ApplicationHelper #skip_before_action :check_account, :only => [:logout] @@ -334,6 +334,11 @@ class AccountsController < ApplicationController Register::CheckColumnsForm.new(check_params).validate! render_ok end + + def login_check + Register::LoginCheckColumnsForm.new(check_params.merge(user: current_user)).validate! + render_ok + end private diff --git a/app/forms/register/base_form.rb b/app/forms/register/base_form.rb index 150fef73a..8824fc23c 100644 --- a/app/forms/register/base_form.rb +++ b/app/forms/register/base_form.rb @@ -3,28 +3,40 @@ module Register include ActiveModel::Model private - def check_login(login) + def check_login(login, user=nil) login = strip(login) raise LoginError, "登录名格式有误" unless login =~ CustomRegexp::LOGIN login_exist = Owner.exists?(login: login) || ReversedKeyword.check_exists?(login) - raise LoginError, '登录名已被使用' if login_exist + if user.present? + raise LoginError, '登录名已被使用' if login_exist && login != user&.login + else + raise LoginError, '登录名已被使用' if login_exist + end end - def check_mail(mail) + def check_mail(mail, user=nil) mail = strip(mail) raise EmailError, "邮件格式有误" unless mail =~ CustomRegexp::EMAIL mail_exist = Owner.exists?(mail: mail) - raise EmailError, '邮箱已被使用' if mail_exist + if user.present? + raise EmailError, '邮箱已被使用' if mail_exist && mail != user&.mail + else + raise EmailError, '邮箱已被使用' if mail_exist + end end - def check_phone(phone) + def check_phone(phone, user=nil) phone = strip(phone) raise PhoneError, "手机号格式有误" unless phone =~ CustomRegexp::PHONE phone_exist = Owner.exists?(phone: phone) - raise PhoneError, '手机号已被使用' if phone_exist + if user.present? + raise PhoneError, '手机号已被使用' if phone_exist && phone != user&.phone + else + raise PhoneError, '手机号已被使用' if phone_exist + end end end end diff --git a/app/forms/register/login_check_columns_form.rb b/app/forms/register/login_check_columns_form.rb new file mode 100644 index 000000000..0c6a93af3 --- /dev/null +++ b/app/forms/register/login_check_columns_form.rb @@ -0,0 +1,19 @@ +module Register + class LoginCheckColumnsForm < Register::BaseForm + attr_accessor :type, :value, :user + + validates :type, presence: true, numericality: true + validates :value, presence: true + validate :check! + + def check! + # params[:type] 为事件类型 1:登录名(login) 2:email(邮箱) 3:phone(手机号) + case strip(type).to_i + when 1 then check_login(strip(value), user) + when 2 then check_mail(strip(value), user) + when 3 then check_phone(strip(value), user) + else raise("type值无效") + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 6579f6704..ed81d6c88 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -214,6 +214,7 @@ Rails.application.routes.draw do post :remote_password post :change_password post :check + post :login_check post :simple_update end end