From e9da21bccfd389d19ea978a4d158a3eabc292f76 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 21 Apr 2023 09:23:30 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Aopenkylin=5Fsign?= =?UTF-8?q?=20=E7=AD=BE=E7=BD=B2=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/users/openkylin_sign_controller.rb | 23 ++++++++++ app/controllers/api/v1/users_controller.rb | 4 +- app/models/openkylin_sign_detail.rb | 24 ++++++++++ .../v1/users/openkylin_sign/create_service.rb | 45 +++++++++++++++++++ config/routes/api.rb | 5 +++ ...420092835_create_openkylin_sign_details.rb | 14 ++++++ 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/v1/users/openkylin_sign_controller.rb create mode 100644 app/models/openkylin_sign_detail.rb create mode 100644 app/services/api/v1/users/openkylin_sign/create_service.rb create mode 100644 db/migrate/20230420092835_create_openkylin_sign_details.rb diff --git a/app/controllers/api/v1/users/openkylin_sign_controller.rb b/app/controllers/api/v1/users/openkylin_sign_controller.rb new file mode 100644 index 000000000..d37e76363 --- /dev/null +++ b/app/controllers/api/v1/users/openkylin_sign_controller.rb @@ -0,0 +1,23 @@ +class Api::V1::Users::OpenkylinSignController < Api::V1::BaseController + + before_action :load_observe_user + + def competitions + @competition_ids = EduSetting.get("openkylin_sign_competitions").split(",") rescue [] + render :json => {data: @competition_ids} + end + + def create + @object_result = Api::V1::Users::OpenkylinSign::CreateService.call(@observe_user, create_params) + if @result_object + return render_ok + else + return render_error('签署失败!') + end + end + + private + def create_params + params.permit(:login, :email, :nickname, :phone, :address) + 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 2204ff89f..47087c523 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,7 +1,7 @@ class Api::V1::UsersController < Api::V1::BaseController - before_action :load_observe_user, except: [:check_user_id] - before_action :check_auth_for_observe_user, except: [:check_user_id] + before_action :load_observe_user, except: [:check_user_id, :check_user_login] + before_action :check_auth_for_observe_user, except: [:check_user_id, :check_user_login] def check_user_id return tip_exception(-1, "用户ID不存在") unless params[:user_id].present? && User.exists?(id: params[:user_id]) diff --git a/app/models/openkylin_sign_detail.rb b/app/models/openkylin_sign_detail.rb new file mode 100644 index 000000000..7f9000cf5 --- /dev/null +++ b/app/models/openkylin_sign_detail.rb @@ -0,0 +1,24 @@ +# == Schema Information +# +# Table name: openkylin_sign_details +# +# id :integer not null, primary key +# user_id :integer +# login :string(255) +# email :string(255) +# nickname :string(255) +# phone :string(255) +# address :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_openkylin_sign_details_on_user_id (user_id) +# + +class OpenkylinSignDetail < ApplicationRecord + + belongs_to :user + +end diff --git a/app/services/api/v1/users/openkylin_sign/create_service.rb b/app/services/api/v1/users/openkylin_sign/create_service.rb new file mode 100644 index 000000000..e9703d9dc --- /dev/null +++ b/app/services/api/v1/users/openkylin_sign/create_service.rb @@ -0,0 +1,45 @@ +class Api::V1::Users::OpenkylinSign::CreateService < ApplicationService + include ActiveModel::Model + + attr_reader :observe_user, :login, :email, :nickname, :phone, :address + + # validates :login, format: {with: CustomRegexp::LOGIN} + validates :email, format: {with: CustomRegexp::EMAIL} + validates :nickname, length: { maximum: 32 } + validates :phone, format: {with: CustomRegexp::PHONE} + validates :address, length: { maximum: 100 } + + def initialize(observe_user, params={}) + @observe_user = observe_user + @login = observe_user.login + @email = params[:email] + @nickname = params[:nickname] + @phone = params[:phone] + @address = params[:address] + end + + def call + raise Error, errors.full_messages.join(",") unless valid? + raise Error, '用户已经签署CLA协议!' if @observe_user.sign_cla + begin + ActiveRecord::Base.transaction do + create_openkylin_sign_detail + update_user_sign_cla + end + + return true + rescue + raise Error, "服务器错误,请联系系统管理员!" + end + end + + private + def create_openkylin_sign_detail + OpenkylinSignDetail.create!(user_id: @observe_user.id, login: @login, email: @email, nickname: @nickname, phone: @phone, address: @address) + end + + def update_user_sign_cla + @observe_user.update_attributes!(sign_cla: true) + end + +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 20c56138c..4d5547683 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -24,6 +24,11 @@ defaults format: :json do scope module: :users do resources :projects, only: [:index] resources :feedbacks, only: [:create] + resources :openkylin_sign, only: [:create] do + collection do + get :competitions + end + end end scope ':repo', constraints: { repo: /[^\/]+/ } do diff --git a/db/migrate/20230420092835_create_openkylin_sign_details.rb b/db/migrate/20230420092835_create_openkylin_sign_details.rb new file mode 100644 index 000000000..e282a5f43 --- /dev/null +++ b/db/migrate/20230420092835_create_openkylin_sign_details.rb @@ -0,0 +1,14 @@ +class CreateOpenkylinSignDetails < ActiveRecord::Migration[5.2] + def change + create_table :openkylin_sign_details do |t| + t.references :user + t.string :login + t.string :email + t.string :nickname + t.string :phone + t.string :address + + t.timestamps + end + end +end From 1387e1d17ec31005cae13b9f3a55b8120bfec683 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 21 Apr 2023 14:07:31 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9Areadme=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=B8=8D=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/repositories_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index afde12d2c..010197a70 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -88,7 +88,7 @@ module RepositoriesHelper unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:") # new_r_content = "#{path}" + new_r_content - new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{path_current}/#{path_last}&ref=#{ref}"].join + new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{path_current}/#{path_last}&ref=#{ref}"].join end content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"").gsub(/src='#{r_content}'/, "src=\"#{new_r_content}\"") rescue @@ -132,7 +132,7 @@ module RepositoriesHelper s_content = File.expand_path(s_content, file_path) s_content = s_content.split("#{Rails.root}/")[1] # content = content.gsub(s[0], "/#{s_content}") - s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join + s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{s_content}&ref=#{ref}"].join case k.to_s when 'ss_src' content = content.gsub("src=\"#{s[0]}\"", "src=\"#{s_content}\"") From 6c26450924e9b4763559cc445be08ee5511b636c Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 21 Apr 2023 14:07:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9Areadme=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=B8=8D=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/repositories_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index afde12d2c..010197a70 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -88,7 +88,7 @@ module RepositoriesHelper unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:") # new_r_content = "#{path}" + new_r_content - new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{path_current}/#{path_last}&ref=#{ref}"].join + new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{path_current}/#{path_last}&ref=#{ref}"].join end content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"").gsub(/src='#{r_content}'/, "src=\"#{new_r_content}\"") rescue @@ -132,7 +132,7 @@ module RepositoriesHelper s_content = File.expand_path(s_content, file_path) s_content = s_content.split("#{Rails.root}/")[1] # content = content.gsub(s[0], "/#{s_content}") - s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join + s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{s_content}&ref=#{ref}"].join case k.to_s when 'ss_src' content = content.gsub("src=\"#{s[0]}\"", "src=\"#{s_content}\"") From 67d7d11c6236860353fd4c1413180a9e0e725739 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 21 Apr 2023 14:22:38 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A&to=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/repositories_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 010197a70..c9be515d2 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -88,7 +88,7 @@ module RepositoriesHelper unless r_content.include?("http://") || r_content.include?("https://") || r_content.include?("mailto:") # new_r_content = "#{path}" + new_r_content - new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{path_current}/#{path_last}&ref=#{ref}"].join + new_r_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{path_current}/#{path_last}?ref=#{ref}"].join end content = content.gsub(/src=\"#{r_content}\"/, "src=\"#{new_r_content}\"").gsub(/src='#{r_content}'/, "src=\"#{new_r_content}\"") rescue @@ -132,7 +132,7 @@ module RepositoriesHelper s_content = File.expand_path(s_content, file_path) s_content = s_content.split("#{Rails.root}/")[1] # content = content.gsub(s[0], "/#{s_content}") - s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{s_content}&ref=#{ref}"].join + s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw/#{s_content}?ref=#{ref}"].join case k.to_s when 'ss_src' content = content.gsub("src=\"#{s[0]}\"", "src=\"#{s_content}\"")