Merge pull request #5582 from betagouv/revert-user-discard
Revert "Merge pull request #5576 from tchak/soft-delete-users"
This commit is contained in:
commit
6f5547cafa
5 changed files with 5 additions and 67 deletions
|
@ -1,10 +1,5 @@
|
|||
module Manager
|
||||
class UsersController < Manager::ApplicationController
|
||||
def scoped_resource
|
||||
# Don't display discarded users
|
||||
User.kept
|
||||
end
|
||||
|
||||
def update
|
||||
user = User.find(params[:id])
|
||||
new_email = params[:user][:email]
|
||||
|
@ -41,10 +36,10 @@ module Manager
|
|||
|
||||
def delete
|
||||
user = User.find(params[:id])
|
||||
if !user.can_be_discarded?
|
||||
fail "Impossible de supprimer cet utilisateur. Il est instructeur ou administrateur."
|
||||
if !user.can_be_deleted?
|
||||
fail "Impossible de supprimer cet utilisateur. Il a des dossiers en instruction ou il est administrateur."
|
||||
end
|
||||
user.delete_or_discard!(current_administration)
|
||||
user.delete_and_keep_track_dossiers(current_administration)
|
||||
|
||||
logger.info("L'utilisateur #{user.id} est supprimé par #{current_administration.id}")
|
||||
flash[:notice] = "L'utilisateur #{user.id} est supprimé"
|
||||
|
|
|
@ -8,8 +8,6 @@
|
|||
# confirmed_at :datetime
|
||||
# current_sign_in_at :datetime
|
||||
# current_sign_in_ip :string
|
||||
# discard_reason :string
|
||||
# discarded_at :datetime
|
||||
# email :string default(""), not null
|
||||
# encrypted_password :string default(""), not null
|
||||
# failed_attempts :integer default(0), not null
|
||||
|
@ -31,7 +29,6 @@
|
|||
#
|
||||
class User < ApplicationRecord
|
||||
include EmailSanitizableConcern
|
||||
include Discard::Model
|
||||
|
||||
enum loged_in_with_france_connect: {
|
||||
particulier: 'particulier',
|
||||
|
@ -59,10 +56,6 @@ class User < ApplicationRecord
|
|||
|
||||
# Override of Devise::Models::Confirmable#send_confirmation_instructions
|
||||
def send_confirmation_instructions
|
||||
if discarded?
|
||||
return
|
||||
end
|
||||
|
||||
unless @raw_confirmation_token
|
||||
generate_confirmation_token!
|
||||
end
|
||||
|
@ -146,12 +139,8 @@ class User < ApplicationRecord
|
|||
last_sign_in_at.present?
|
||||
end
|
||||
|
||||
def can_be_discarded?
|
||||
administrateur.nil? && instructeur.nil?
|
||||
end
|
||||
|
||||
def can_be_deleted?
|
||||
can_be_discarded? && dossiers.with_discarded.state_instruction_commencee.empty?
|
||||
administrateur.nil? && instructeur.nil? && dossiers.with_discarded.state_instruction_commencee.empty?
|
||||
end
|
||||
|
||||
def delete_and_keep_track_dossiers(administration)
|
||||
|
@ -166,32 +155,6 @@ class User < ApplicationRecord
|
|||
destroy!
|
||||
end
|
||||
|
||||
def discard_and_anonymize!(reason)
|
||||
if !can_be_discarded?
|
||||
raise "Cannot discard this user because they are also instructeur or administrateur"
|
||||
end
|
||||
|
||||
discard!
|
||||
update_columns(
|
||||
discard_reason: reason,
|
||||
email: "#{SecureRandom.hex}@anonymous.org",
|
||||
encrypted_password: SecureRandom.hex,
|
||||
unconfirmed_email: nil,
|
||||
current_sign_in_at: nil,
|
||||
current_sign_in_ip: nil,
|
||||
last_sign_in_at: nil,
|
||||
last_sign_in_ip: nil
|
||||
)
|
||||
end
|
||||
|
||||
def delete_or_discard!(administration)
|
||||
if can_be_deleted?
|
||||
delete_and_keep_track_dossiers(administration)
|
||||
else
|
||||
discard_and_anonymize!("Discarded by Manager##{administration.id}")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def link_invites!
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
class AddDiscardReasonAndDiscardedAtToUsers < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :users, :discard_reason, :string
|
||||
add_column :users, :discarded_at, :datetime
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2020_09_16_113507) do
|
||||
ActiveRecord::Schema.define(version: 2020_09_02_103047) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -650,8 +650,6 @@ ActiveRecord::Schema.define(version: 2020_09_16_113507) do
|
|||
t.datetime "locked_at"
|
||||
t.bigint "instructeur_id"
|
||||
t.bigint "administrateur_id"
|
||||
t.string "discard_reason"
|
||||
t.datetime "discarded_at"
|
||||
t.index ["administrateur_id"], name: "index_users_on_administrateur_id"
|
||||
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
|
||||
t.index ["email"], name: "index_users_on_email", unique: true
|
||||
|
|
|
@ -293,16 +293,4 @@ describe User, type: :model do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#discard_and_anonymize!' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before { user.discard_and_anonymize!('HS1234') }
|
||||
|
||||
it 'should discard user and make it anonymous' do
|
||||
expect(user.discarded?).to be_truthy
|
||||
expect(user.email).to end_with '@anonymous.org'
|
||||
expect(user.discard_reason).to eq('HS1234')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue