From de9e1b102d7c4a53b92644ae778cbbb301b13cbd Mon Sep 17 00:00:00 2001 From: fosterfarrell9 <28628554+fosterfarrell9@users.noreply.github.com> Date: Sat, 29 Apr 2023 10:54:55 +0200 Subject: [PATCH] refactor controller --- app/controllers/users_controller.rb | 21 +++++++++++---------- app/views/users/_basics.html.erb | 3 +-- config/routes.rb | 4 ---- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index efc7e9a65..598a17bc1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,8 @@ -# TermsController +# UsersController class UsersController < ApplicationController before_action :set_elevated_users, only: [:index, :list_generic_users] + before_action :set_user, only: [:edit, :update, :destroy] + layout 'administration' def current_ability @@ -13,12 +15,10 @@ def index end def edit - @user = User.find_by_id(params[:id]) authorize! :edit, @user end def update - @user = User.find_by_id(params[:id]) authorize! :update, @user old_image_data = @user.image_data @user.update(user_params) @@ -48,20 +48,14 @@ def elevate end end - def list - search = User.search { fulltext params[:term] } - @users = search.results - end - def list_generic_users - authorize! :list_generic_user, @user + authorize! :list_generic_users, User.new result = User.where.not(id: @elevated_users.pluck(:id)) .values_for_select render json: result end def destroy - @user = User.find_by_id(params[:id]) authorize! :destroy, @user @user.destroy unless @user.admin || @user.editor? || @user.teacher? redirect_to users_path @@ -79,6 +73,7 @@ def teacher end def fill_user_select + authorize! :fill_user_select, User.new if params[:q] result = User.preferred_name_or_email_like(params[:q]) .values_for_select @@ -104,6 +99,12 @@ def user_params :current_lecture_id,:image) end + def set_user + @user = User.find_by_id(params[:id]) + return unless @user.nil? + redirect_to :root, alert: I18n.t('controllers.no_medium') + end + def set_elevated_users @elevated_users = User.where(admin: true).or(User.proper_editors) .or(User.teachers) diff --git a/app/views/users/_basics.html.erb b/app/views/users/_basics.html.erb index 6ccc6cf71..617ee86ea 100644 --- a/app/views/users/_basics.html.erb +++ b/app/views/users/_basics.html.erb @@ -1,8 +1,7 @@
<%= f.label :name, t('basics.display_name') %> <%= helpdesk(t('admin.user.info.display_name'), false) %> - <%= f.text_field :name, { class: 'form-control', - disabled: true } %> + <%= f.text_field :name, { class: 'form-control' } %>
diff --git a/config/routes.rb b/config/routes.rb index 3b871df55..46bc8c68d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -775,10 +775,6 @@ to: 'users#fill_user_select', as: 'fill_user_select' - get 'users/list', - to: 'users#list', - as: 'list_users' - get 'users/delete_account', to: 'users#delete_account', as: 'delete_account'