Merge pull request #4742 from betagouv/4532-supprime-email-column-de-la-table-admin
#4532 supprime la colonne email de la table admin
This commit is contained in:
commit
4954f798a5
18 changed files with 56 additions and 23 deletions
|
@ -79,7 +79,7 @@ class Admin::ProceduresController < AdminController
|
||||||
end
|
end
|
||||||
|
|
||||||
def transfer
|
def transfer
|
||||||
admin = Administrateur.find_by(email: params[:email_admin].downcase)
|
admin = Administrateur.by_email(params[:email_admin].downcase)
|
||||||
|
|
||||||
if admin.nil?
|
if admin.nil?
|
||||||
render '/admin/procedures/transfer', formats: 'js', status: 404
|
render '/admin/procedures/transfer', formats: 'js', status: 404
|
||||||
|
|
|
@ -48,9 +48,9 @@ module Manager
|
||||||
end
|
end
|
||||||
|
|
||||||
def pending_demandes
|
def pending_demandes
|
||||||
already_approved_emails = Administrateur
|
already_approved_emails = Administrateur.eager(:user)
|
||||||
.where(email: demandes.map { |d| d[:email] })
|
.where(users: { email: demandes.map { |d| d[:email] } })
|
||||||
.pluck(:email)
|
.map(&:email)
|
||||||
|
|
||||||
demandes.reject { |demande| already_approved_emails.include?(demande[:email]) }
|
demandes.reject { |demande| already_approved_emails.include?(demande[:email]) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,7 +29,7 @@ module Manager
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_administrateur
|
def add_administrateur
|
||||||
administrateur = Administrateur.find_by(email: params[:email])
|
administrateur = Administrateur.by_email(params[:email])
|
||||||
if administrateur
|
if administrateur
|
||||||
procedure.administrateurs << administrateur
|
procedure.administrateurs << administrateur
|
||||||
flash[:notice] = "L'administrateur \"#{params[:email]}\" est ajouté à la démarche."
|
flash[:notice] = "L'administrateur \"#{params[:email]}\" est ajouté à la démarche."
|
||||||
|
|
|
@ -9,7 +9,7 @@ module NewAdministrateur
|
||||||
email = params.require(:administrateur)[:email]&.strip&.downcase
|
email = params.require(:administrateur)[:email]&.strip&.downcase
|
||||||
|
|
||||||
# Find the admin
|
# Find the admin
|
||||||
administrateur = Administrateur.find_by(email: email)
|
administrateur = Administrateur.by_email(email)
|
||||||
if administrateur.nil?
|
if administrateur.nil?
|
||||||
flash.alert = "L’administrateur « #{email} » n’existe pas. Invitez-le à demander un compte administrateur à l’addresse <a href=#{new_demande_url}>#{new_demande_url}</a>."
|
flash.alert = "L’administrateur « #{email} » n’existe pas. Invitez-le à demander un compte administrateur à l’addresse <a href=#{new_demande_url}>#{new_demande_url}</a>."
|
||||||
return
|
return
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Users::PasswordsController < Devise::PasswordsController
|
||||||
def create
|
def create
|
||||||
# Check the credentials associated to the mail to generate a correct reset link
|
# Check the credentials associated to the mail to generate a correct reset link
|
||||||
email = params[:user][:email]
|
email = params[:user][:email]
|
||||||
if Administrateur.find_by(email: email)
|
if Administrateur.by_email(email)
|
||||||
@devise_mapping = Devise.mappings[:administrateur]
|
@devise_mapping = Devise.mappings[:administrateur]
|
||||||
params[:administrateur] = params[:user]
|
params[:administrateur] = params[:user]
|
||||||
# uncomment to check password complexity for Instructeur
|
# uncomment to check password complexity for Instructeur
|
||||||
|
@ -56,7 +56,7 @@ class Users::PasswordsController < Devise::PasswordsController
|
||||||
|
|
||||||
def try_to_authenticate_administrateur
|
def try_to_authenticate_administrateur
|
||||||
if user_signed_in?
|
if user_signed_in?
|
||||||
administrateur = Administrateur.find_by(email: current_user.email)
|
administrateur = Administrateur.by_email(current_user.email)
|
||||||
|
|
||||||
if administrateur
|
if administrateur
|
||||||
sign_in(administrateur.user)
|
sign_in(administrateur.user)
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
class Administrateur < ApplicationRecord
|
class Administrateur < ApplicationRecord
|
||||||
self.ignored_columns = ['features', 'encrypted_password', 'reset_password_token', 'reset_password_sent_at', 'remember_created_at', 'sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip', 'failed_attempts', 'unlock_token', 'locked_at']
|
self.ignored_columns = ['email', 'features', 'encrypted_password', 'reset_password_token', 'reset_password_sent_at', 'remember_created_at', 'sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip', 'failed_attempts', 'unlock_token', 'locked_at']
|
||||||
include EmailSanitizableConcern
|
|
||||||
include ActiveRecord::SecureToken
|
include ActiveRecord::SecureToken
|
||||||
|
|
||||||
has_and_belongs_to_many :instructeurs
|
has_and_belongs_to_many :instructeurs
|
||||||
|
@ -10,11 +9,19 @@ class Administrateur < ApplicationRecord
|
||||||
|
|
||||||
has_one :user, dependent: :nullify
|
has_one :user, dependent: :nullify
|
||||||
|
|
||||||
before_validation -> { sanitize_email(:email) }
|
default_scope { eager_load(:user) }
|
||||||
|
|
||||||
scope :inactive, -> { joins(:user).where(users: { last_sign_in_at: nil }) }
|
scope :inactive, -> { joins(:user).where(users: { last_sign_in_at: nil }) }
|
||||||
scope :with_publiees_ou_closes, -> { joins(:procedures).where(procedures: { aasm_state: [:publiee, :close, :depubliee] }) }
|
scope :with_publiees_ou_closes, -> { joins(:procedures).where(procedures: { aasm_state: [:publiee, :close, :depubliee] }) }
|
||||||
|
|
||||||
|
def self.by_email(email)
|
||||||
|
Administrateur.find_by(users: { email: email })
|
||||||
|
end
|
||||||
|
|
||||||
|
def email
|
||||||
|
user.email
|
||||||
|
end
|
||||||
|
|
||||||
# validate :password_complexity, if: Proc.new { |a| Devise.password_length.include?(a.password.try(:size)) }
|
# validate :password_complexity, if: Proc.new { |a| Devise.password_length.include?(a.password.try(:size)) }
|
||||||
|
|
||||||
def password_complexity
|
def password_complexity
|
||||||
|
|
|
@ -381,7 +381,7 @@ class Dossier < ApplicationRecord
|
||||||
update(hidden_at: deleted_dossier.deleted_at)
|
update(hidden_at: deleted_dossier.deleted_at)
|
||||||
|
|
||||||
if en_construction?
|
if en_construction?
|
||||||
administration_emails = followers_instructeurs.present? ? followers_instructeurs.map(&:email) : procedure.administrateurs.pluck(:email)
|
administration_emails = followers_instructeurs.present? ? followers_instructeurs.map(&:email) : procedure.administrateurs.map(&:email)
|
||||||
administration_emails.each do |email|
|
administration_emails.each do |email|
|
||||||
DossierMailer.notify_deletion_to_administration(deleted_dossier, email).deliver_later
|
DossierMailer.notify_deletion_to_administration(deleted_dossier, email).deliver_later
|
||||||
end
|
end
|
||||||
|
|
|
@ -82,7 +82,7 @@ class User < ApplicationRecord
|
||||||
user = User.create_or_promote_to_instructeur(email, password)
|
user = User.create_or_promote_to_instructeur(email, password)
|
||||||
|
|
||||||
if user.valid? && user.administrateur_id.nil?
|
if user.valid? && user.administrateur_id.nil?
|
||||||
user.create_administrateur!(email: email)
|
user.create_administrateur!
|
||||||
end
|
end
|
||||||
|
|
||||||
user
|
user
|
||||||
|
|
|
@ -119,7 +119,7 @@ class AdministrateurUsageStatisticsService
|
||||||
end
|
end
|
||||||
|
|
||||||
def nb_instructeurs_by_administrateur_id
|
def nb_instructeurs_by_administrateur_id
|
||||||
@nb_instructeurs_by_administrateur_id ||= with_default(0, Administrateur.joins(:instructeurs).group(:administrateur_id).count)
|
@nb_instructeurs_by_administrateur_id ||= with_default(0, Administrateur.joins(:instructeurs).group('administrateurs.id').count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state
|
def nb_dossiers_by_administrateur_id_and_procedure_id_and_synthetic_state
|
||||||
|
|
|
@ -56,4 +56,4 @@
|
||||||
%td.flex
|
%td.flex
|
||||||
= link_to('Consulter', apercu_procedure_path(id: procedure.id), target: "_blank", rel: "noopener", class: 'button small')
|
= link_to('Consulter', apercu_procedure_path(id: procedure.id), target: "_blank", rel: "noopener", class: 'button small')
|
||||||
= link_to('Cloner', admin_procedure_clone_path(procedure.id, from_new_from_existing: true), 'data-method' => :put, class: 'button small primary')
|
= link_to('Cloner', admin_procedure_clone_path(procedure.id, from_new_from_existing: true), 'data-method' => :put, class: 'button small primary')
|
||||||
= link_to('Contacter', "mailto:#{procedure.administrateurs.pluck(:email) * ","}", class: 'button small')
|
= link_to('Contacter', "mailto:#{procedure.administrateurs.map(&:email) * ","}", class: 'button small')
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
= form_for procedure.administrateurs.new,
|
= form_for procedure.administrateurs.new(user: User.new),
|
||||||
url: { controller: 'procedure_administrateurs' },
|
url: { controller: 'procedure_administrateurs' },
|
||||||
html: { class: 'form', id: "procedure-#{procedure.id}-new_administrateur" } ,
|
html: { class: 'form', id: "procedure-#{procedure.id}-new_administrateur" } ,
|
||||||
remote: true do |f|
|
remote: true do |f|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
%th= 'Enregistré le'
|
%th= 'Enregistré le'
|
||||||
%th= 'État'
|
%th= 'État'
|
||||||
%tbody{ id: "procedure-#{@procedure.id}-administrateurs" }
|
%tbody{ id: "procedure-#{@procedure.id}-administrateurs" }
|
||||||
= render partial: 'administrateur', collection: @procedure.administrateurs.order(:email)
|
= render partial: 'administrateur', collection: @procedure.administrateurs.order('users.email')
|
||||||
%tfoot
|
%tfoot
|
||||||
%tr
|
%tr
|
||||||
%th{ colspan: 4 }
|
%th{ colspan: 4 }
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
class RemoveUniqueConstraintOnAdministrateurEmails < ActiveRecord::Migration[5.2]
|
||||||
|
def up
|
||||||
|
# Drop the index entirely
|
||||||
|
remove_index :administrateurs, :email
|
||||||
|
# Add the index again, without the unicity constraint
|
||||||
|
add_index :administrateurs, :email
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
remove_index :administrateurs, :email
|
||||||
|
add_index :administrateurs, :email, unique: true
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2020_01_14_113700) do
|
ActiveRecord::Schema.define(version: 2020_01_30_165328) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -53,7 +53,7 @@ ActiveRecord::Schema.define(version: 2020_01_14_113700) do
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
t.boolean "active", default: false
|
t.boolean "active", default: false
|
||||||
t.string "encrypted_token"
|
t.string "encrypted_token"
|
||||||
t.index ["email"], name: "index_administrateurs_on_email", unique: true
|
t.index ["email"], name: "index_administrateurs_on_email"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "administrateurs_instructeurs", id: false, force: :cascade do |t|
|
create_table "administrateurs_instructeurs", id: false, force: :cascade do |t|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
sequence(:administrateur_email) { |n| "admin#{n}@admin.com" }
|
sequence(:administrateur_email) { |n| "admin#{n}@admin.com" }
|
||||||
factory :administrateur do
|
factory :administrateur do
|
||||||
email { generate(:administrateur_email) }
|
|
||||||
|
|
||||||
transient do
|
transient do
|
||||||
|
email { generate(:administrateur_email) }
|
||||||
password { 'mon chien aime les bananes' }
|
password { 'mon chien aime les bananes' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ require 'spec_helper'
|
||||||
feature 'As an administrateur', js: true do
|
feature 'As an administrateur', js: true do
|
||||||
let(:administration) { create(:administration) }
|
let(:administration) { create(:administration) }
|
||||||
let(:admin_email) { 'new_admin@gouv.fr' }
|
let(:admin_email) { 'new_admin@gouv.fr' }
|
||||||
let(:new_admin) { Administrateur.find_by(email: admin_email) }
|
let(:new_admin) { Administrateur.by_email(admin_email) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
perform_enqueued_jobs do
|
perform_enqueued_jobs do
|
||||||
|
|
|
@ -59,4 +59,18 @@ feature 'Administrateurs can edit procedures', js: true do
|
||||||
expect(page).to have_field('procedure_libelle', with: 'Ma petite démarche')
|
expect(page).to have_field('procedure_libelle', with: 'Ma petite démarche')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario 'the administrator can add another administrator' do
|
||||||
|
another_administrateur = create(:administrateur)
|
||||||
|
visit admin_procedure_path(procedure)
|
||||||
|
click_on 'Administrateurs'
|
||||||
|
|
||||||
|
fill_in('administrateur_email', with: another_administrateur.email)
|
||||||
|
|
||||||
|
click_on 'Ajouter comme administrateur'
|
||||||
|
|
||||||
|
within('.alert-success') do
|
||||||
|
expect(page).to have_content(another_administrateur.email)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,6 +41,6 @@ class AdministrationMailerPreview < ActionMailer::Preview
|
||||||
end
|
end
|
||||||
|
|
||||||
def administrateur
|
def administrateur
|
||||||
Administrateur.new(id: 111, email: "chef.de.service@administration.gouv.fr")
|
Administrateur.new(id: 111, user: User.new(email: "chef.de.service@administration.gouv.fr"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue