From 8ee089b82dce63a97a51465a13cff9541ca91b41 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 4 Jun 2021 18:01:03 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E5=90=8E=E5=8F=B0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=20=E6=B7=BB=E5=8A=A0=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admins/users_controller.rb | 46 ++++++++++++++++++- app/forms/users/admin_create_user_form.rb | 32 +++++++++++++ app/views/admins/users/edit.html.erb | 4 +- app/views/admins/users/index.html.erb | 1 + app/views/admins/users/new.html.erb | 37 +++++++++++++++ .../forms/admin_create_user_form.zh-CN.yml | 14 ++++++ config/routes.rb | 2 +- 7 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 app/forms/users/admin_create_user_form.rb create mode 100644 app/views/admins/users/new.html.erb create mode 100644 config/locales/forms/admin_create_user_form.zh-CN.yml diff --git a/app/controllers/admins/users_controller.rb b/app/controllers/admins/users_controller.rb index 98f0a6bf..31946257 100644 --- a/app/controllers/admins/users_controller.rb +++ b/app/controllers/admins/users_controller.rb @@ -58,11 +58,55 @@ class Admins::UsersController < Admins::BaseController render_ok end - private + def new + @user = User.new + end + + def create + logger.info "---validate_create_params: #{validate_create_params}" + Users::AdminCreateUserForm.new(validate_create_params).validate! + user = User.new(create_params) + user.type = 'User' + ActiveRecord::Base.transaction do + if user.save! + UserExtension.create!(user_id: user.id) + interactor = Gitea::RegisterInteractor.call({username: user.login, email: user.mail, password: create_params[:password]}) + if interactor.success? + gitea_user = interactor.result + result = Gitea::User::GenerateTokenService.call(user.login, create_params[:password]) + user.gitea_token = result['sha1'] + user.gitea_uid = gitea_user[:body]['id'] + user.save! + end + end + end + + flash[:success] = '保存成功' + redirect_to admins_users_path + rescue ActiveRecord::RecordInvalid => e + logger.info "------------ #{e.message}" + puts e.message + flash.now[:danger] = e.message + render 'new' + rescue Exception => ex + flash.now[:danger] = ex.message + render 'new' + end + + + private def update_params params.require(:user).permit(%i[lastname nickname gender identity technical_title student_id is_shixun_marker mail phone location location_city school_id department_id admin business is_test password professional_certification authentication]) end + + def create_params + params.require(:user).permit(%i[login nickname gender mail phone location location_city password professional_certification]) + end + + def validate_create_params + create_params.slice(:login, :mail, :phone, :password) + end end diff --git a/app/forms/users/admin_create_user_form.rb b/app/forms/users/admin_create_user_form.rb new file mode 100644 index 00000000..06b9dc21 --- /dev/null +++ b/app/forms/users/admin_create_user_form.rb @@ -0,0 +1,32 @@ +class Users::AdminCreateUserForm + include ActiveModel::Model + + + attr_accessor :mail, :login, :phone, :password + + validates :login, presence: true + validates :mail, presence: true, format: { with: CustomRegexp::EMAIL, message: "邮箱格式错误." } + validates :phone, presence: true, format: { with: CustomRegexp::PHONE, message: "手机号格式错误" } + validates :password, presence: true, length: { minimum: 8, maximum: 16 }, format: { with: CustomRegexp::PASSWORD, message: "8~16位密码,支持字母数字和符号" } + + validate :check_login, :check_mail + + private + def check_mail + return if mail.blank? + if User.exists?(mail: mail) + raise "邮箱 #{mail} 已使用." + errors.add(:mail, :not_exist) + end + end + + def check_login + return if login.blank? + if User.exists?(login: login) + raise "手机号 #{login} 已使用." + errors.add(:login, :not_exist) + end + end + +end + diff --git a/app/views/admins/users/edit.html.erb b/app/views/admins/users/edit.html.erb index 5abc8271..7cf32331 100644 --- a/app/views/admins/users/edit.html.erb +++ b/app/views/admins/users/edit.html.erb @@ -64,12 +64,12 @@ <%= f.label :identity, label: '职业' %> <%= select_tag('user[identity]', [], class: 'form-control identity-select optional', 'data-value': @user.user_extension&.identity, 'data-first-title': '请选择') %> -
+
<%= f.label :technical_title, label: '职称' %> <%= select_tag('user[technical_title]', [], class: 'form-control technical-title-select optional', 'data-value': @user.technical_title) %>
- <%= f.input :student_id, as: :tel, label: '学号', wrapper_html: { class: 'col-md-2', style: @user.user_extension.student? ? '' : 'display:none;' }, input_html: { class: 'student-id-input' } %> + <%= f.input :student_id, as: :tel, label: '学号', wrapper_html: { class: 'col-md-2', style: @user.user_extension&.student? ? '' : 'display:none;' }, input_html: { class: 'student-id-input' } %>
diff --git a/app/views/admins/users/index.html.erb b/app/views/admins/users/index.html.erb index be09cdf2..dd58129a 100644 --- a/app/views/admins/users/index.html.erb +++ b/app/views/admins/users/index.html.erb @@ -31,6 +31,7 @@ <%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %> <% end %> + <%= link_to '新增', new_admins_user_path, class: 'btn btn-primary ml-3' %> <%#= javascript_void_link '导入用户', class: 'btn btn-secondary btn-sm', data: { toggle: 'modal', target: '.admin-import-user-modal'} %> <% =begin%> diff --git a/app/views/admins/users/new.html.erb b/app/views/admins/users/new.html.erb new file mode 100644 index 00000000..02e64a4e --- /dev/null +++ b/app/views/admins/users/new.html.erb @@ -0,0 +1,37 @@ +<% + define_admin_breadcrumbs do + add_admin_breadcrumb('用户管理', admins_users_path) + add_admin_breadcrumb('新增用户') + end +%> + +
+ <%= simple_form_for([:admins, User.new], url: admins_users_path ) do |f| %> + <%= f.error_notification %> +
+
+ <%= f.input :login, label: '登录名', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-md-11' } %> +
+ +
+ <%= f.input :nickname, label: '昵称', wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-md-11' } %> +
+ +
+ <%= f.input :mail, as: :email, label: '邮箱地址', required: true, error_html: { id: 'password_error'}, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11' } %> + <%= f.input :phone, as: :tel, label: '手机号', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11', autocomplete: 'off' } %> +
+ +
+ +
+ <%= f.input :password, as: :password, required: true, label: '密码', wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11', autocomplete: 'new-password' } %> + <%= f.input :password_confirmation, as: :password, required: true, label: '确认密码', wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-sm-11', autocomplete: 'new-password' } %> +
+ +
+ <%= f.button :submit, value: '保存', class: 'btn-primary mr-3 px-4' %> + <%= link_to '取消', admins_users_path, class: 'btn btn-secondary px-4' %> +
+ <% end %> +
diff --git a/config/locales/forms/admin_create_user_form.zh-CN.yml b/config/locales/forms/admin_create_user_form.zh-CN.yml new file mode 100644 index 00000000..cdf84c04 --- /dev/null +++ b/config/locales/forms/admin_create_user_form.zh-CN.yml @@ -0,0 +1,14 @@ +'zh-CN': + activemodel: + attributes: + users/admin_create_user_form: + login: '登录名' + password: '密码' + mail: '邮箱' + phone: '手机号' + errors: + models: + users/admin_create_user_form: + attributes: + + diff --git a/config/routes.rb b/config/routes.rb index b955d791..f7f652aa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -641,7 +641,7 @@ Rails.application.routes.draw do get :contrast, on: :collection end - resources :users, only: [:index, :edit, :update, :destroy] do + resources :users do member do post :reward_grade post :lock