From dfc2edd62d5e2d10260d63475123f337c468e4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxxq250=E2=80=9D?= <“xxq250@qq.com”> Date: Mon, 26 Sep 2022 10:38:28 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fixed=E6=B3=A8=E5=86=8Cgitea=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/concerns/register_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/concerns/register_helper.rb b/app/controllers/concerns/register_helper.rb index 3d5b0fc03..069c0944f 100644 --- a/app/controllers/concerns/register_helper.rb +++ b/app/controllers/concerns/register_helper.rb @@ -12,6 +12,7 @@ module RegisterHelper return unless user.valid? interactor = Gitea::RegisterInteractor.call({username: username, email: email, password: password}) + result ={} if interactor.success? gitea_user = interactor.result result = Gitea::User::GenerateTokenService.call(username, password) @@ -22,7 +23,7 @@ module RegisterHelper result[:user] = {id: user.id, token: user.gitea_token} end else - result[:message] = interactor.error + result[:message] = interactor.result[:message] end result end From fce8166ba7f082c4e4cd0fa3c2adc0dfe7a0d280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxxq250=E2=80=9D?= <“xxq250@qq.com”> Date: Mon, 26 Sep 2022 16:11:34 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fixed=20=E7=94=A8=E6=88=B7=E6=B4=BB?= =?UTF-8?q?=E8=B7=83=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admins/dashboards_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/admins/dashboards_controller.rb b/app/controllers/admins/dashboards_controller.rb index fac44cca9..a3fc230ad 100644 --- a/app/controllers/admins/dashboards_controller.rb +++ b/app/controllers/admins/dashboards_controller.rb @@ -1,9 +1,9 @@ class Admins::DashboardsController < Admins::BaseController def index # 用户活跃数 - day_user_ids = CommitLog.where(created_at: today).pluck(:project_id).uniq - weekly_user_ids = CommitLog.where(created_at: current_week).pluck(:project_id).uniq - month_user_ids = CommitLog.where(created_at: current_month).pluck(:project_id).uniq + day_user_ids = CommitLog.where(created_at: today).pluck(:user_id).uniq + weekly_user_ids = CommitLog.where(created_at: current_week).pluck(:user_id).uniq + month_user_ids = CommitLog.where(created_at: current_month).pluck(:user_id).uniq @active_user_count = User.where(last_login_on: today).or(User.where(id: day_user_ids)).count @weekly_active_user_count = User.where(last_login_on: current_week).or(User.where(id: weekly_user_ids)).count @month_active_user_count = User.where(last_login_on: current_month).or(User.where(id: month_user_ids)).count From 649d9c4ea62862525c8a5ee91875dc03fbb4a1a6 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 28 Sep 2022 16:09:10 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E9=82=AE=E7=AE=B1=E6=8E=A7=E5=88=B6=E5=99=A8=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/users/users_controller.rb | 74 +++++++++++++++++++ app/controllers/concerns/api/user_helper.rb | 5 ++ app/mailers/user_mailer.rb | 4 + app/views/user_mailer/update_email.html.erb | 61 +++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 app/controllers/api/v1/users/users_controller.rb create mode 100644 app/views/user_mailer/update_email.html.erb diff --git a/app/controllers/api/v1/users/users_controller.rb b/app/controllers/api/v1/users/users_controller.rb new file mode 100644 index 000000000..c59e869b5 --- /dev/null +++ b/app/controllers/api/v1/users/users_controller.rb @@ -0,0 +1,74 @@ +class Api::V1::Users::UsersController < Api::V1::BaseController + + before_action :load_observe_user + before_action :check_auth_for_observe_user + + def send_update_email_vefify_code + code = %W(0 1 2 3 4 5 6 7 8 9) + verification_code = code.sample(6).join + mail = params[:email] + + sign = Digest::MD5.hexdigest("#{OPENKEY}#{value}") + tip_exception(501, "请求不合理") if sign != params[:smscode] + + # 60s内不能重复发送 + send_email_limit_cache_key = "send_email_60_second_limit:#{mail}" + tip_exception(-1, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) + send_email_control = LimitForbidControl::SendEmailCode.new(mail) + tip_exception(-1, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? + begin + UserMailer.update_email(mail, verification_code).deliver_now + + Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) + send_email_control.increment! + rescue Exception => e + logger_error(e) + tip_exception(-2,"邮件发送失败,请稍后重试") + end + ver_params = {code_type: send_type, code: code, email: mail} + data = VerificationCode.new(ver_params) + if data.save! + render_ok + else + tip_exception(-1, "创建数据失败") + end + end + + def check_password + password = params[:password] + return render_error("8~16位密码,支持字母数字和符号") unless password =~ CustomRegexp::PASSWORD + return render_error("密码错误") unless @observe_user.check_password?(password) + render_ok + end + + def check_email + mail = strip(params[:email]) + return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL + + exist_owner = Owner.find_by(mail: mail) + return render_error('邮箱已被使用') if exist_owner + render_ok + end + + def check_email_verifi_code + code = strip(params[:code]) + mail = strip(params[:email]) + + return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL + + verifi_code = VerificationCode.where(email: mail, code: code, code_type: 8).last + + return render_error("验证码不正确") if verifi_code&.code != code + return render_error("验证码已失效") if !verifi_code&.effective? + render_ok + end + + def change_email + @result_object = Api::V1::Users::UpdateEmailService.call(@observe_user, params, current_user.gitea_token) + if @result_object + return render_ok + else + return render_error('更改邮箱失败!') + end + end +end \ No newline at end of file diff --git a/app/controllers/concerns/api/user_helper.rb b/app/controllers/concerns/api/user_helper.rb index e6156ea56..b7714be39 100644 --- a/app/controllers/concerns/api/user_helper.rb +++ b/app/controllers/concerns/api/user_helper.rb @@ -16,4 +16,9 @@ module Api::UserHelper end @observe_user end + + # 是否具有查看用户或编辑用户的权限 + def check_auth_for_observe_user + return render_forbidden unless current_user.admin? || @observe_user.id == current_user.id + end end \ No newline at end of file diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index acd34fbbd..21ed5b0d5 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -8,4 +8,8 @@ class UserMailer < ApplicationMailer mail(to: mail, subject: 'Gitink | 注册验证码') end + def update_email(mail, code) + @code = code + mail(to: mail, subject: 'Gitink | 更改邮箱验证码') + end end diff --git a/app/views/user_mailer/update_email.html.erb b/app/views/user_mailer/update_email.html.erb new file mode 100644 index 000000000..c93366e4a --- /dev/null +++ b/app/views/user_mailer/update_email.html.erb @@ -0,0 +1,61 @@ + + + + GitLink-验证码发送 + + + + + +
+
+
+ + <%= image_tag("logo.png", alt: "确实开源", width: '100', :style => "float:left; margin-top: 8px;") %> + +
+
+
+

+ 您好! +

+

+ 你正在进行GitLink邮箱更改操作,如非本人操作,请忽略。 +

+
+
+

<%= @code %>

+
+ + 此邮件为系统所发,请勿直接回复。
+ 要解决问题或了解您的帐户详情,您可以访问 帮助中心。 +
+
+

+ 如果您并未发过此请求,则可能是因为其他用户在注册时误输了您的邮件地址,而使您收到了这封邮件,那么您可以放心的忽略此邮件,无需进一步采取任何操作。 +

+
+
+ www.gitlink.org.cn +
+
+
+ + From d68e7f38a3718bdcb8a5440c31870766ad52a00d Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 29 Sep 2022 09:30:33 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E9=82=AE=E7=AE=B1service=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/users/users_controller.rb | 8 ++- .../api/v1/users/update_email_service.rb | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 app/services/api/v1/users/update_email_service.rb diff --git a/app/controllers/api/v1/users/users_controller.rb b/app/controllers/api/v1/users/users_controller.rb index c59e869b5..f62d4f86e 100644 --- a/app/controllers/api/v1/users/users_controller.rb +++ b/app/controllers/api/v1/users/users_controller.rb @@ -3,10 +3,11 @@ class Api::V1::Users::UsersController < Api::V1::BaseController before_action :load_observe_user before_action :check_auth_for_observe_user - def send_update_email_vefify_code + def send_vefify_code code = %W(0 1 2 3 4 5 6 7 8 9) verification_code = code.sample(6).join mail = params[:email] + code_type = params[:code_type] sign = Digest::MD5.hexdigest("#{OPENKEY}#{value}") tip_exception(501, "请求不合理") if sign != params[:smscode] @@ -25,7 +26,7 @@ class Api::V1::Users::UsersController < Api::V1::BaseController logger_error(e) tip_exception(-2,"邮件发送失败,请稍后重试") end - ver_params = {code_type: send_type, code: code, email: mail} + ver_params = {code_type: code_type, code: code, email: mail} data = VerificationCode.new(ver_params) if data.save! render_ok @@ -53,10 +54,11 @@ class Api::V1::Users::UsersController < Api::V1::BaseController def check_email_verifi_code code = strip(params[:code]) mail = strip(params[:email]) + code_type = params[:code_type] return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL - verifi_code = VerificationCode.where(email: mail, code: code, code_type: 8).last + verifi_code = VerificationCode.where(email: mail, code: code, code_type: code_type).last return render_error("验证码不正确") if verifi_code&.code != code return render_error("验证码已失效") if !verifi_code&.effective? diff --git a/app/services/api/v1/users/update_email_service.rb b/app/services/api/v1/users/update_email_service.rb new file mode 100644 index 000000000..976f2195b --- /dev/null +++ b/app/services/api/v1/users/update_email_service.rb @@ -0,0 +1,63 @@ +class Api::V1::Users::UpdateEmailService < ApplicationService + include ActiveModel::Model + + attr_reader :user, :token, :password, :mail, :old_mail, :code, :verify_code + attr_accessor :gitea_token + + validates :mail + + def initialize(user, params, token =nil) + @user = user + @token = token + @password = params[:password] + @mail = params[:email] + @old_mail = user.mail + @code = params[:code] + @verify_code = VerificationCode.where(email: @mail, code: @code, code_type: params[:code_type]).last + end + + def call + raise Error, errors.full_messages.join(",") unless valid? + raise Error, "密码不正确." if @user.check_password?(@password) + raise Error, "验证码不正确." if @verifi_code&.code != @code + raise Error, "验证码已失效." if !verifi_code&.effective? + + ActiveRecord::Base.transaction do + change_user_email + excute_data_to_gitea + excute_remove_email_from_gitea + end + + return gitea_data + + rescue + raise Error, "服务器错误,请联系系统管理员!" + end + + private + def request_params + { + access_token: token + } + end + + def request_body + { + email: @email, + login_name: @user.login, + source_id: 0 + } + end + + def change_user_email + @user.update_attributes!({mail: @mail}) + end + + def excute_data_to_gitea + @gitea_token = $gitea_client.patch_admin_users_by_username(@user.login, {body: request_body.to_json}) + end + + def excute_remove_email_from_gitea + @gitea_token = $gitea_client.delete_user_emails({body: {emails: [@old_mail]}, query: request_params}) + end +end \ No newline at end of file From 2cde13eabbb77fc715c46454b2375e9c8c2393f3 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 29 Sep 2022 12:05:57 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=B5=8B=E8=AF=95=E4=BB=A5=E5=8F=8A=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/users/users_controller.rb | 76 ------------------- app/controllers/api/v1/users_controller.rb | 76 ++++++++++++++++++- app/controllers/concerns/api/user_helper.rb | 4 + .../api/v1/users/update_email_service.rb | 44 ++++++----- config/initializers/gitea_client.rb | 4 +- config/routes/api.rb | 10 ++- 6 files changed, 114 insertions(+), 100 deletions(-) delete mode 100644 app/controllers/api/v1/users/users_controller.rb diff --git a/app/controllers/api/v1/users/users_controller.rb b/app/controllers/api/v1/users/users_controller.rb deleted file mode 100644 index f62d4f86e..000000000 --- a/app/controllers/api/v1/users/users_controller.rb +++ /dev/null @@ -1,76 +0,0 @@ -class Api::V1::Users::UsersController < Api::V1::BaseController - - before_action :load_observe_user - before_action :check_auth_for_observe_user - - def send_vefify_code - code = %W(0 1 2 3 4 5 6 7 8 9) - verification_code = code.sample(6).join - mail = params[:email] - code_type = params[:code_type] - - sign = Digest::MD5.hexdigest("#{OPENKEY}#{value}") - tip_exception(501, "请求不合理") if sign != params[:smscode] - - # 60s内不能重复发送 - send_email_limit_cache_key = "send_email_60_second_limit:#{mail}" - tip_exception(-1, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) - send_email_control = LimitForbidControl::SendEmailCode.new(mail) - tip_exception(-1, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? - begin - UserMailer.update_email(mail, verification_code).deliver_now - - Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) - send_email_control.increment! - rescue Exception => e - logger_error(e) - tip_exception(-2,"邮件发送失败,请稍后重试") - end - ver_params = {code_type: code_type, code: code, email: mail} - data = VerificationCode.new(ver_params) - if data.save! - render_ok - else - tip_exception(-1, "创建数据失败") - end - end - - def check_password - password = params[:password] - return render_error("8~16位密码,支持字母数字和符号") unless password =~ CustomRegexp::PASSWORD - return render_error("密码错误") unless @observe_user.check_password?(password) - render_ok - end - - def check_email - mail = strip(params[:email]) - return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL - - exist_owner = Owner.find_by(mail: mail) - return render_error('邮箱已被使用') if exist_owner - render_ok - end - - def check_email_verifi_code - code = strip(params[:code]) - mail = strip(params[:email]) - code_type = params[:code_type] - - return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL - - verifi_code = VerificationCode.where(email: mail, code: code, code_type: code_type).last - - return render_error("验证码不正确") if verifi_code&.code != code - return render_error("验证码已失效") if !verifi_code&.effective? - render_ok - end - - def change_email - @result_object = Api::V1::Users::UpdateEmailService.call(@observe_user, params, current_user.gitea_token) - if @result_object - return render_ok - else - return render_error('更改邮箱失败!') - end - end -end \ No newline at end of file diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 55f5cfb22..37a41ff75 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,6 +1,78 @@ class Api::V1::UsersController < Api::V1::BaseController - def index - render_ok + before_action :load_observe_user + before_action :check_auth_for_observe_user + + def send_email_vefify_code + code = %W(0 1 2 3 4 5 6 7 8 9) + verification_code = code.sample(6).join + mail = params[:email] + code_type = params[:code_type] + + sign = Digest::MD5.hexdigest("#{OPENKEY}#{mail}") + Rails.logger.info sign + + tip_exception(501, "请求不合理") if sign != params[:smscode] + + # 60s内不能重复发送 + send_email_limit_cache_key = "send_email_60_second_limit:#{mail}" + tip_exception(-1, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) + send_email_control = LimitForbidControl::SendEmailCode.new(mail) + tip_exception(-1, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? + begin + UserMailer.update_email(mail, verification_code).deliver_now + + Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) + send_email_control.increment! + rescue Exception => e + logger_error(e) + tip_exception(-2,"邮件发送失败,请稍后重试") + end + ver_params = {code_type: code_type, code: verification_code, email: mail} + data = VerificationCode.new(ver_params) + if data.save! + render_ok + else + tip_exception(-1, "创建数据失败") + end + end + + def check_password + password = params[:password] + return render_error("8~16位密码,支持字母数字和符号") unless password =~ CustomRegexp::PASSWORD + return render_error("密码错误") unless @observe_user.check_password?(password) + render_ok + end + + def check_email + mail = strip(params[:email]) + return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL + + exist_owner = Owner.find_by(mail: mail) + return render_error('邮箱已被使用') if exist_owner + render_ok + end + + def check_email_verify_code + code = strip(params[:code]) + mail = strip(params[:email]) + code_type = params[:code_type] + + return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL + + verifi_code = VerificationCode.where(email: mail, code: code, code_type: code_type).last + + return render_error("验证码不正确") if verifi_code&.code != code + return render_error("验证码已失效") if !verifi_code&.effective? + render_ok + end + + def update_email + @result_object = Api::V1::Users::UpdateEmailService.call(@observe_user, params, current_user.gitea_token) + if @result_object + return render_ok + else + return render_error('更改邮箱失败!') + end end end \ No newline at end of file diff --git a/app/controllers/concerns/api/user_helper.rb b/app/controllers/concerns/api/user_helper.rb index b7714be39..4c7b713fc 100644 --- a/app/controllers/concerns/api/user_helper.rb +++ b/app/controllers/concerns/api/user_helper.rb @@ -21,4 +21,8 @@ module Api::UserHelper def check_auth_for_observe_user return render_forbidden unless current_user.admin? || @observe_user.id == current_user.id end + + def strip(str) + str.to_s.strip.presence + end end \ No newline at end of file diff --git a/app/services/api/v1/users/update_email_service.rb b/app/services/api/v1/users/update_email_service.rb index 976f2195b..133693777 100644 --- a/app/services/api/v1/users/update_email_service.rb +++ b/app/services/api/v1/users/update_email_service.rb @@ -1,10 +1,11 @@ class Api::V1::Users::UpdateEmailService < ApplicationService include ActiveModel::Model - attr_reader :user, :token, :password, :mail, :old_mail, :code, :verify_code - attr_accessor :gitea_token + attr_reader :user, :token, :password, :mail, :old_mail, :code, :code_type, :verify_code + attr_accessor :gitea_data - validates :mail + validates :password, :code, :code_type, presence: true + validates :mail, presence: true, format: { with: CustomRegexp::EMAIL } def initialize(user, params, token =nil) @user = user @@ -13,25 +14,28 @@ class Api::V1::Users::UpdateEmailService < ApplicationService @mail = params[:email] @old_mail = user.mail @code = params[:code] - @verify_code = VerificationCode.where(email: @mail, code: @code, code_type: params[:code_type]).last + @code_type = params[:code_type] + @verify_code = VerificationCode.where(email: @mail, code: @code, code_type: @code_type).last end def call raise Error, errors.full_messages.join(",") unless valid? - raise Error, "密码不正确." if @user.check_password?(@password) - raise Error, "验证码不正确." if @verifi_code&.code != @code - raise Error, "验证码已失效." if !verifi_code&.effective? + raise Error, "密码不正确." unless @user.check_password?(@password) + raise Error, "验证码不正确." if @verify_code&.code != @code + raise Error, "验证码已失效." if !@verify_code&.effective? - ActiveRecord::Base.transaction do - change_user_email - excute_data_to_gitea - excute_remove_email_from_gitea - end + # begin + ActiveRecord::Base.transaction do + change_user_email + excute_data_to_gitea + excute_change_email_from_gitea + end - return gitea_data + return gitea_data - rescue - raise Error, "服务器错误,请联系系统管理员!" + # rescue + # raise Error, "服务器错误,请联系系统管理员!" + # end end private @@ -43,7 +47,7 @@ class Api::V1::Users::UpdateEmailService < ApplicationService def request_body { - email: @email, + email: @mail, login_name: @user.login, source_id: 0 } @@ -54,10 +58,12 @@ class Api::V1::Users::UpdateEmailService < ApplicationService end def excute_data_to_gitea - @gitea_token = $gitea_client.patch_admin_users_by_username(@user.login, {body: request_body.to_json}) + Rails.logger.info request_body + @gitea_data = $gitea_client.patch_admin_users_by_username(@user.login, {body: request_body.to_json}) end - def excute_remove_email_from_gitea - @gitea_token = $gitea_client.delete_user_emails({body: {emails: [@old_mail]}, query: request_params}) + def excute_change_email_from_gitea + $gitea_client.delete_user_emails({body: {emails: [@old_mail]}.to_json, query: request_params}) + $gitea_client.post_user_emails({body: {emails: [@mail]}.to_json, query: request_params}) end end \ No newline at end of file diff --git a/config/initializers/gitea_client.rb b/config/initializers/gitea_client.rb index 92706ffd8..c909cebf4 100644 --- a/config/initializers/gitea_client.rb +++ b/config/initializers/gitea_client.rb @@ -6,6 +6,6 @@ gitea_config = config[:gitea].symbolize_keys! $gitea_client = Gitea::Api::Client.new({ domain: gitea_config[:domain], base_url: gitea_config[:base_url], - username: gitea_config[:username], - password: gitea_config[:password] + username: gitea_config[:access_key_id], + password: gitea_config[:access_key_secret] }) \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index d5f82ff7b..ab098b1dc 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -2,7 +2,15 @@ defaults format: :json do namespace :api do namespace :v1 do scope ':owner' do - resource :users, path: '/', only: [:show, :update, :edit, :destroy] + resource :users, path: '/', only: [:show, :update, :edit, :destroy] do + collection do + get :send_email_vefify_code + post :check_password + post :check_email + post :check_email_verify_code + patch :update_email + end + end scope module: :users do resources :projects, only: [:index] end From 55170c7693dcba4aa2124fcf416db276e94142ad Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 29 Sep 2022 13:58:14 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E9=82=AE=E7=AE=B1=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/docs/slate/source/includes/_users.md | 185 ++++++++++++++ .../api/v1/users/update_email_service.rb | 7 +- public/docs/api.html | 239 ++++++++++++++++++ 3 files changed, 427 insertions(+), 4 deletions(-) diff --git a/app/docs/slate/source/includes/_users.md b/app/docs/slate/source/includes/_users.md index 318c7930a..e16a52033 100644 --- a/app/docs/slate/source/includes/_users.md +++ b/app/docs/slate/source/includes/_users.md @@ -2304,4 +2304,189 @@ await octokit.request('GET /api/users/:login/applied_projects/:id/refuse.json') "created_at": "2021-06-09 16:41", "time_ago": "7分钟前" } +``` + + +## 用户发送邮件验证码 +用户发送邮件验证码 + +> 示例: + +```shell +curl -X GET http://localhost:3000/api/v1/yystopf/send_email_vefify_code.json +``` + +```javascript +await octokit.request('GET /api/v1/:login/send_email_vefify_code.json') +``` + +### HTTP 请求 +`GET /api/v1/:login/send_email_vefify_code.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|login |string |用户标识 | +|code_type |int |10: 更新邮箱| +|email |string |邮箱| +|smscode |string |邮箱md5加密值| + +### 返回字段说明: + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + + +## 用户验证邮件验证码 +用户验证邮件验证码 + +> 示例: + +```shell +curl -X POST http://localhost:3000/api/v1/yystopf/check_email_verify_code.json +``` + +```javascript +await octokit.request('POST /api/v1/:login/check_email_verify_code.json') +``` + +### HTTP 请求 +`POST /api/v1/:login/check_email_verify_code.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|login |string |用户标识 | +|code_type |int |10: 更新邮箱| +|email |string |邮箱| +|code |string |邮箱验证码| + +### 返回字段说明: + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + + +## 用户验证密码 +用户验证密码,检查是否和用户密码一致 + +> 示例: + +```shell +curl -X POST http://localhost:3000/api/v1/yystopf/check_password.json +``` + +```javascript +await octokit.request('POST /api/v1/:login/check_password.json') +``` + +### HTTP 请求 +`POST /api/v1/:login/check_password.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|login |string |用户标识 | +|password |string |用户密码| + +### 返回字段说明: + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + + +## 用户验证邮箱 +用户验证邮箱是否符合规范以及是否已被使用 + +> 示例: + +```shell +curl -X POST http://localhost:3000/api/v1/yystopf/check_email.json +``` + +```javascript +await octokit.request('POST /api/v1/:login/check_email.json') +``` + +### HTTP 请求 +`POST /api/v1/:login/check_email.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|login |string |用户标识 | +|email |string |邮箱地址| + +### 返回字段说明: + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + + +## 用户更改邮箱 +用户更改一个新的邮箱 + +> 示例: + +```shell +curl -X PATCH http://localhost:3000/api/v1/yystopf/update_email.json +``` + +```javascript +await octokit.request('PATCH /api/v1/:login/update_email.json') +``` + +### HTTP 请求 +`PATCH /api/v1/:login/update_email.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|login |string |用户标识 | +|password |string |用户密码| +|email |string |邮箱地址| +|code |string |邮箱验证码| + + +> 请求的JSON示例: + +```json +{ + "password": "Aa19960425.", + "code": "657134", + "email": "yystopf@163.com" +} +``` + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} ``` \ No newline at end of file diff --git a/app/services/api/v1/users/update_email_service.rb b/app/services/api/v1/users/update_email_service.rb index 133693777..e11dd2f61 100644 --- a/app/services/api/v1/users/update_email_service.rb +++ b/app/services/api/v1/users/update_email_service.rb @@ -1,10 +1,10 @@ class Api::V1::Users::UpdateEmailService < ApplicationService include ActiveModel::Model - attr_reader :user, :token, :password, :mail, :old_mail, :code, :code_type, :verify_code + attr_reader :user, :token, :password, :mail, :old_mail, :code, :verify_code attr_accessor :gitea_data - validates :password, :code, :code_type, presence: true + validates :password, :code, presence: true validates :mail, presence: true, format: { with: CustomRegexp::EMAIL } def initialize(user, params, token =nil) @@ -14,8 +14,7 @@ class Api::V1::Users::UpdateEmailService < ApplicationService @mail = params[:email] @old_mail = user.mail @code = params[:code] - @code_type = params[:code_type] - @verify_code = VerificationCode.where(email: @mail, code: @code, code_type: @code_type).last + @verify_code = VerificationCode.where(email: @mail, code: @code, code_type: 10).last end def call diff --git a/public/docs/api.html b/public/docs/api.html index b41272321..3b0558cd4 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -423,6 +423,21 @@
  • 用户拒绝申请
  • +
  • + 用户发送邮件验证码 +
  • +
  • + 用户验证邮件验证码 +
  • +
  • + 用户验证密码 +
  • +
  • + 用户验证邮箱 +
  • +
  • + 用户更改邮箱 +
  • @@ -4708,6 +4723,230 @@ Success — a happy kitten is an authenticated kitten! "created_at": "2021-06-09 16:41", "time_ago": "7分钟前" } +

    用户发送邮件验证码

    +

    用户发送邮件验证码

    + +
    +

    示例:

    +
    +
    curl -X GET http://localhost:3000/api/v1/yystopf/send_email_vefify_code.json
    +
    await octokit.request('GET /api/v1/:login/send_email_vefify_code.json')
    +

    HTTP 请求

    +

    GET /api/v1/:login/send_email_vefify_code.json

    +

    请求字段说明:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    loginstring用户标识
    code_typeint10: 更新邮箱
    emailstring邮箱
    smscodestring邮箱md5加密值
    +

    返回字段说明:

    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +

    用户验证邮件验证码

    +

    用户验证邮件验证码

    + +
    +

    示例:

    +
    +
    curl -X POST http://localhost:3000/api/v1/yystopf/check_email_verify_code.json
    +
    await octokit.request('POST /api/v1/:login/check_email_verify_code.json')
    +

    HTTP 请求

    +

    POST /api/v1/:login/check_email_verify_code.json

    +

    请求字段说明:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    loginstring用户标识
    code_typeint10: 更新邮箱
    emailstring邮箱
    codestring邮箱验证码
    +

    返回字段说明:

    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +

    用户验证密码

    +

    用户验证密码,检查是否和用户密码一致

    + +
    +

    示例:

    +
    +
    curl -X POST http://localhost:3000/api/v1/yystopf/check_password.json
    +
    await octokit.request('POST /api/v1/:login/check_password.json')
    +

    HTTP 请求

    +

    POST /api/v1/:login/check_password.json

    +

    请求字段说明:

    + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    loginstring用户标识
    passwordstring用户密码
    +

    返回字段说明:

    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +

    用户验证邮箱

    +

    用户验证邮箱是否符合规范以及是否已被使用

    + +
    +

    示例:

    +
    +
    curl -X POST http://localhost:3000/api/v1/yystopf/check_email.json
    +
    await octokit.request('POST /api/v1/:login/check_email.json')
    +

    HTTP 请求

    +

    POST /api/v1/:login/check_email.json

    +

    请求字段说明:

    + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    loginstring用户标识
    emailstring邮箱地址
    +

    返回字段说明:

    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +

    用户更改邮箱

    +

    用户更改一个新的邮箱

    + +
    +

    示例:

    +
    +
    curl -X PATCH http://localhost:3000/api/v1/yystopf/update_email.json
    +
    await octokit.request('PATCH /api/v1/:login/update_email.json')
    +

    HTTP 请求

    +

    PATCH /api/v1/:login/update_email.json

    +

    请求字段说明:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    loginstring用户标识
    passwordstring用户密码
    emailstring邮箱地址
    codestring邮箱验证码
    + +
    +

    请求的JSON示例:

    +
    +
    {
    +    "password": "Aa19960425.",
    +    "code": "657134",
    +    "email": "yystopf@163.com"
    +}
    +
    +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
     

    Projects

    获取项目邀请链接(项目管理员)

    当前登录(管理员)用户获取项目邀请链接的接口(第一次请求会默认生成role类型为developer和is_apply为true的链接)