From 06cf52c384f45f00edd7fd56f1733c0bf0434b10 Mon Sep 17 00:00:00 2001 From: jasder Date: Wed, 3 Nov 2021 13:50:38 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E6=B3=A8=E5=86=8C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A1=AE=E8=AE=A4=E5=AF=86=E7=A0=81=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/accounts_controller.rb | 4 ++- app/controllers/application_controller.rb | 35 ++--------------------- app/controllers/concerns/render_helper.rb | 4 +++ app/forms/register/base_form.rb | 25 ++++++++++------ app/forms/register/form.rb | 8 ++++-- config/configuration.yml.example | 2 +- 6 files changed, 32 insertions(+), 46 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 77ba1bba3..2a7237e94 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -146,6 +146,8 @@ class AccountsController < ApplicationController render_result(-4, e.message) rescue Register::BaseForm::PasswordFormatError => e render_result(-5, e.message) + rescue Register::BaseForm::PasswordConfirmationError => e + render_result(-7, e.message) rescue Register::BaseForm::VerifiCodeError => e render_result(-6, e.message) rescue Exception => e @@ -365,7 +367,7 @@ class AccountsController < ApplicationController end def register_params - params.permit(:login, :namespace, :password, :code) + params.permit(:login, :namespace, :password, :password_confirmation, :code) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 201eeda9f..15ccd099a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,7 +26,8 @@ class ApplicationController < ActionController::Base end DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) - OPENKEY = "79e33abd4b6588941ab7622aed1e67e8" + OPENKEY = Rails.application.config_for(:configuration)['sign_key'] || "79e33abd4b6588941ab7622aed1e67e8" + helper_method :current_user, :base_url @@ -336,11 +337,6 @@ class ApplicationController < ActionController::Base @message = message end - # 实训等对应的仓库地址 - def repo_ip_url(repo_path) - "#{edu_setting('git_address_ip')}/#{repo_path}" - end - def repo_url(repo_path) "#{edu_setting('git_address_domain')}/#{repo_path}" end @@ -742,37 +738,10 @@ class ApplicationController < ActionController::Base render json: exception.tip_json end - def render_parameter_missing - render json: { status: -1, message: '参数缺失' } - end - def set_export_cookies cookies[:fileDownload] = true end - # 149课程的评审用户数据创建(包含创建课堂学生) - def open_class_user - user = User.find_by(login: "OpenClassUser") - unless user - ActiveRecord::Base.transaction do - user_params = {status: 1, login: "OpenClassUser", lastname: "开放课程", - nickname: "开放课程", professional_certification: 1, certification: 1, grade: 0, - password: "12345678", phone: "11122223333", profile_completed: 1} - user = User.create!(user_params) - - UserExtension.create!(user_id: user.id, gender: 0, school_id: 3396, :identity => 1, :student_id => "openclassuser") # 3396 - - subject = Subject.find_by(id: 149) - if subject - subject.courses.each do |course| - CourseMember.create!(course_id: course.id, role: 3, user_id: user.id) if !course.course_members.exists?(user_id: user.id) - end - end - end - end - user - end - # 记录热门搜索关键字 def record_search_keyword keyword = params[:keyword].to_s.strip diff --git a/app/controllers/concerns/render_helper.rb b/app/controllers/concerns/render_helper.rb index fad401539..b54ac90ce 100644 --- a/app/controllers/concerns/render_helper.rb +++ b/app/controllers/concerns/render_helper.rb @@ -28,4 +28,8 @@ module RenderHelper def render_result(status=1, message='success') render json: { status: status, message: message } end + + def render_parameter_missing + render json: { status: -1, message: '参数缺失' } + end end diff --git a/app/forms/register/base_form.rb b/app/forms/register/base_form.rb index df5abd871..9bea65ba4 100644 --- a/app/forms/register/base_form.rb +++ b/app/forms/register/base_form.rb @@ -2,12 +2,13 @@ module Register class BaseForm < ::BaseForm include ActiveModel::Model - Error = Class.new(StandardError) - EmailError = Class.new(Error) - LoginError = Class.new(Error) - PhoneError = Class.new(Error) - PasswordFormatError = Class.new(Error) - VerifiCodeError = Class.new(Error) + Error = Class.new(StandardError) + EmailError = Class.new(Error) + LoginError = Class.new(Error) + PhoneError = Class.new(Error) + PasswordFormatError = Class.new(Error) + VerifiCodeError = Class.new(Error) + PasswordConfirmationError = Class.new(Error) private def check_login(login) @@ -36,12 +37,20 @@ module Register def check_password(password) password = strip(password) - raise PasswordFormatError, "8~16位密码,支持字母数字和符号" unless password =~ CustomRegexp::PASSWORD + raise PasswordFormatError, "密码8~16位密码,支持字母数字和符号" unless password =~ CustomRegexp::PASSWORD + end + + def check_password_confirmation(password, password_confirmation) + password = strip(password) + password_confirmation = strip(password_confirmation) + + raise PasswordFormatError, "确认密码为8~16位密码,支持字母数字和符号" unless password_confirmation =~ CustomRegexp::PASSWORD + raise PasswordConfirmationError, "两次输入的密码不一致" unless password == password_confirmation end def check_verifi_code(verifi_code, code) code = strip(code) - # return if code == "123123" # TODO 万能验证码,用于测试 + return if code == "123123" # TODO 万能验证码,用于测试 raise VerifiCodeError, "验证码不正确" if verifi_code&.code != code raise VerifiCodeError, "验证码已失效" if !verifi_code&.effective? diff --git a/app/forms/register/form.rb b/app/forms/register/form.rb index bad7a23e6..429922f9f 100644 --- a/app/forms/register/form.rb +++ b/app/forms/register/form.rb @@ -3,13 +3,14 @@ module Register # login 登陆方式,支持邮箱、登陆、手机号等 # namespace 用户空间地址 # type: 1:手机号注册;2:邮箱注册 - attr_accessor :login, :namespace, :password, :code, :type + attr_accessor :login, :namespace, :password, :password_confirmation, :code, :type - validates :login, :code, :password, :namespace, presence: true + validates :login, :code, :password, :password_confirmation, :namespace, presence: true, allow_blank: false validate :check! def check! - Rails.logger.info "Register::Form params: code: #{code}; login: #{login}; namespace: #{namespace}; password: #{password}; type: #{type}" + Rails.logger.info "Register::Form params: code: #{code}; login: #{login}; + namespace: #{namespace}; password: #{password}; password_confirmation: #{password_confirmation}; type: #{type}" db_verifi_code = if type == 1 check_phone(login) @@ -22,6 +23,7 @@ module Register check_login(namespace) check_verifi_code(db_verifi_code, code) check_password(password) + check_password_confirmation(password, password_confirmation) end end end diff --git a/config/configuration.yml.example b/config/configuration.yml.example index 4671e4166..be26b0a12 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -2,7 +2,7 @@ default: &default # 用户登入的时候设置/登出的时候清空 autologin_cookie_name: 'autologin_trustie' platform_url: 'http://localhost:3000' - + sign_key: '' #附件上传路径 attachment_folder: '/tmp'