ADD educoder api

This commit is contained in:
jasder 2021-03-18 17:27:18 +08:00 committed by vilet.yy
parent edeaaa5db7
commit be9c01bf1d
5 changed files with 112 additions and 3 deletions

View File

@ -4,7 +4,7 @@ class UsersController < ApplicationController
before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users] before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users]
before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users] before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users]
before_action :require_login, only: %i[me list] before_action :require_login, only: %i[me list change_password change_email]
before_action :connect_to_ci_db, only: [:get_user_info] before_action :connect_to_ci_db, only: [:get_user_info]
skip_before_action :check_sign, only: [:attachment_show] skip_before_action :check_sign, only: [:attachment_show]
@ -233,6 +233,64 @@ class UsersController < ApplicationController
render_ok render_ok
end end
# TODO: For Educoder
def change_password
user = User.find_by_login params[:login]
return render_error("用户 #{rq_params[:login]} 不存在.") unless user === current_user
form_params= {
login: params[:login],
email: user&.mail,
password: params[:password],
old_password: params[:old_password],
user: user
}
Gitea::User::ChangePasswordForm.new(form_params).validate!
sync_params = {
email: user&.mail,
password: params[:password]
}
if sync_params.present?
interactor = Gitea::User::UpdateInteractor.call(user.login, sync_params)
if interactor.success?
user.update!(password: params[:password])
render_ok
else
render_error(interactor.error)
end
end
end
# TODO: For Educoder
def change_email
user = User.find_by_login params[:login]
return render_error("用户 #{rq_params[:login]} 不存在.") unless user === current_user
form_params= {
login: params[:login],
email: user&.mail,
user: user
}
Gitea::User::ChangeEmailForm.new(form_params).validate!
sync_params = {
email: params[:email]
}
if sync_params.present?
interactor = Gitea::User::UpdateInteractor.call(user.login, sync_params)
if interactor.success?
user.update!(mail: params[:email])
render_ok
else
render_error(interactor.error)
end
end
end
private private
def load_user def load_user
@user = User.find_by_login(params[:id]) || User.find_by(id: params[:id]) @user = User.find_by_login(params[:id]) || User.find_by(id: params[:id])

View File

@ -0,0 +1,9 @@
class Gitea::User::ChangeEmailForm
include ActiveModel::Model
attr_accessor :user
attr_accessor :email, :login
validates :email, presence: true, format: { with: CustomRegexp::EMAIL }
validates :login, presence: true
end

View File

@ -0,0 +1,23 @@
class Gitea::User::ChangePasswordForm
include ActiveModel::Model
attr_accessor :user
attr_accessor :email, :login, :old_password, :password
validates :email, presence: true, format: { with: CustomRegexp::EMAIL }
validates :login, presence: true
validates :old_password, presence: true
validates :password, presence: true
validate :check_old_password
def check_old_password
return if user.check_password?(old_password.to_s)
errors.add(:old_password, :password_error)
end
# def check_user!
# user === current_user
# end
end

View File

@ -0,0 +1,17 @@
'zh-CN':
activemodel:
attributes:
gitea/user/change_password_form:
user: ''
email: 邮箱
login: 用户名
old_password: 旧密码
password: 新密码
errors:
models:
gitea/user/change_password_form:
attributes:
old_password:
password_error: 错误
user:
not_exist: 不存在

View File

@ -219,6 +219,8 @@ Rails.application.routes.draw do
post :sync_salt post :sync_salt
get :trustie_projects get :trustie_projects
get :trustie_related_projects get :trustie_related_projects
post :change_password
post :change_email
scope '/ci', module: :ci do scope '/ci', module: :ci do
scope do scope do