From e422ab5ee4c2d7e217a8756d0786afdea1ed8bbc Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 20 Aug 2024 16:35:57 +0100 Subject: [PATCH] Allo moving deactivated users --- app/controllers/users_controller.rb | 16 +++++++++------- app/policies/user_policy.rb | 14 +++++++------- spec/requests/users_controller_spec.rb | 13 +++++++++---- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bc3eebca4..e9c39e5d1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -56,7 +56,7 @@ class UsersController < ApplicationController def key_contact; end def edit - redirect_to user_path(@user) unless @user.active? + redirect_to user_path(@user) unless @user.active? || current_user.support? end def update @@ -182,9 +182,10 @@ class UsersController < ApplicationController def organisation_change_confirmation authorize @user - if params[:organisation_id].blank? || params[:log_reassignment].blank? || !Organisation.where(id: params[:organisation_id]).exists? - return redirect_to user_path(@user) - end + assigned_to_logs_count = @user.assigned_to_lettings_logs.count + @user.assigned_to_sales_logs.count + + return redirect_to user_path(@user) if params[:organisation_id].blank? || !Organisation.where(id: params[:organisation_id]).exists? + return redirect_to user_path(@user) if params[:log_reassignment].blank? && assigned_to_logs_count.positive? @new_organisation = Organisation.find(params[:organisation_id]) @log_reassignment = params[:log_reassignment] @@ -192,9 +193,10 @@ class UsersController < ApplicationController def confirm_organisation_change authorize @user - if log_reassignment_params[:organisation_id].blank? || log_reassignment_params[:log_reassignment].blank? || !Organisation.where(id: log_reassignment_params[:organisation_id]).exists? - return redirect_to user_path(@user) - end + assigned_to_logs_count = @user.assigned_to_lettings_logs.count + @user.assigned_to_sales_logs.count + + return redirect_to user_path(@user) if log_reassignment_params[:organisation_id].blank? || !Organisation.where(id: log_reassignment_params[:organisation_id]).exists? + return redirect_to user_path(@user) if log_reassignment_params[:log_reassignment].blank? && assigned_to_logs_count.positive? @new_organisation = Organisation.find(log_reassignment_params[:organisation_id]) @log_reassignment = log_reassignment_params[:log_reassignment] diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 3d89e2380..e45614df7 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -10,17 +10,15 @@ class UserPolicy @current_user == @user end - def edit_roles? - (@current_user.data_coordinator? || @current_user.support?) && @user.active? - end - %w[ edit_roles? edit_dpo? edit_key_contact? ].each do |method_name| define_method method_name do - (@current_user.data_coordinator? || @current_user.support?) && @user.active? + return true if @current_user.support? + + @current_user.data_coordinator? && @user.active? end end @@ -30,7 +28,9 @@ class UserPolicy edit_names? ].each do |method_name| define_method method_name do - (@current_user == @user || @current_user.data_coordinator? || @current_user.support?) && @user.active? + return true if @current_user.support? + + (@current_user == @user || @current_user.data_coordinator?) && @user.active? end end @@ -53,7 +53,7 @@ class UserPolicy confirm_organisation_change? ].each do |method_name| define_method method_name do - @current_user.support? && @user.active? + @current_user.support? end end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 11b85fb43..184c678d8 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -1715,10 +1715,11 @@ RSpec.describe UsersController, type: :request do get "/users/#{other_user.id}/edit", headers:, params: {} end - it "redirects to user details page" do - expect(response).to redirect_to("/users/#{other_user.id}") - follow_redirect! - expect(page).not_to have_link("Change") + it "allows editing the user" do + expect(page).to have_field("user[name]") + expect(page).to have_field("user[email]") + expect(page).to have_field("user[role]") + expect(page).to have_field("user[organisation_id]") end end end @@ -2470,6 +2471,10 @@ RSpec.describe UsersController, type: :request do context "when reassignment option is not given" do let(:new_organisation) { create(:organisation, name: "new org") } + before do + create(:lettings_log, assigned_to: other_user) + end + it "redirects to the user page" do get "/users/#{other_user.id}/organisation-change-confirmation?organisation_id=#{new_organisation.id}" expect(response).to redirect_to("/users/#{other_user.id}")