| @@ -58,11 +58,55 @@ class Admins::UsersController < Admins::BaseController | |||||
| render_ok | render_ok | ||||
| end | 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 | def update_params | ||||
| params.require(:user).permit(%i[lastname nickname gender identity technical_title student_id is_shixun_marker | 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 | mail phone location location_city school_id department_id admin business is_test | ||||
| password professional_certification authentication]) | password professional_certification authentication]) | ||||
| end | 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 | end | ||||
| @@ -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 | |||||
| @@ -64,12 +64,12 @@ | |||||
| <%= f.label :identity, label: '职业' %> | <%= f.label :identity, label: '职业' %> | ||||
| <%= select_tag('user[identity]', [], class: 'form-control identity-select optional', 'data-value': @user.user_extension&.identity, 'data-first-title': '请选择') %> | <%= select_tag('user[identity]', [], class: 'form-control identity-select optional', 'data-value': @user.user_extension&.identity, 'data-first-title': '请选择') %> | ||||
| </div> | </div> | ||||
| <div class="form-group technical-title-select-wrapper optional col-md-1" style="<%= @user.user_extension.student? ? 'display:none;' : '' %>"> | |||||
| <div class="form-group technical-title-select-wrapper optional col-md-1" style="<%= @user.user_extension&.student? ? 'display:none;' : '' %>"> | |||||
| <%= f.label :technical_title, label: '职称' %> | <%= f.label :technical_title, label: '职称' %> | ||||
| <%= select_tag('user[technical_title]', [], class: 'form-control technical-title-select optional', 'data-value': @user.technical_title) %> | <%= select_tag('user[technical_title]', [], class: 'form-control technical-title-select optional', 'data-value': @user.technical_title) %> | ||||
| </div> | </div> | ||||
| <%= 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' } %> | |||||
| </div> | </div> | ||||
| <div class="form-row"> | <div class="form-row"> | ||||
| @@ -31,6 +31,7 @@ | |||||
| <%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %> | <%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %> | ||||
| <% end %> | <% 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'} %> | <%#= javascript_void_link '导入用户', class: 'btn btn-secondary btn-sm', data: { toggle: 'modal', target: '.admin-import-user-modal'} %> | ||||
| <% | <% | ||||
| =begin%> | =begin%> | ||||
| @@ -0,0 +1,37 @@ | |||||
| <% | |||||
| define_admin_breadcrumbs do | |||||
| add_admin_breadcrumb('用户管理', admins_users_path) | |||||
| add_admin_breadcrumb('新增用户') | |||||
| end | |||||
| %> | |||||
| <div class="box user-edit-container"> | |||||
| <%= simple_form_for([:admins, User.new], url: admins_users_path ) do |f| %> | |||||
| <%= f.error_notification %> | |||||
| <div class="form-group px-2"> | |||||
| <div class="form-row"> | |||||
| <%= f.input :login, label: '登录名', required: true, wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-md-11' } %> | |||||
| </div> | |||||
| <div class="form-row"> | |||||
| <%= f.input :nickname, label: '昵称', wrapper_html: { class: 'col-md-3' }, input_html: { class: 'col-md-11' } %> | |||||
| </div> | |||||
| <div class="form-row"> | |||||
| <%= 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' } %> | |||||
| </div> | |||||
| </div> | |||||
| <div class="form-row"> | |||||
| <%= 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' } %> | |||||
| </div> | |||||
| <div class="form-row mt-4"> | |||||
| <%= f.button :submit, value: '保存', class: 'btn-primary mr-3 px-4' %> | |||||
| <%= link_to '取消', admins_users_path, class: 'btn btn-secondary px-4' %> | |||||
| </div> | |||||
| <% end %> | |||||
| </div> | |||||
| @@ -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: | |||||
| @@ -641,7 +641,7 @@ Rails.application.routes.draw do | |||||
| get :contrast, on: :collection | get :contrast, on: :collection | ||||
| end | end | ||||
| resources :users, only: [:index, :edit, :update, :destroy] do | |||||
| resources :users do | |||||
| member do | member do | ||||
| post :reward_grade | post :reward_grade | ||||
| post :lock | post :lock | ||||