feat(Administrateurs::ProcedureAdministrateurs): ensure admin from super admin can not add administrateur
This commit is contained in:
parent
d1297bc1f5
commit
d1544bc4ae
12 changed files with 95 additions and 34 deletions
|
@ -11,15 +11,15 @@ module Administrateurs
|
|||
redirect_to admin_procedures_path, status: 404
|
||||
end
|
||||
|
||||
def retrieve_procedure_administration
|
||||
def is_administrateur_through_procedure_administration_as_manager?
|
||||
id = params[:procedure_id] || params[:id]
|
||||
|
||||
@procedure_administration = current_administrateur.administrateurs_procedures.find_by(procedure_id: id)
|
||||
current_administrateur.administrateurs_procedures
|
||||
.exists?(procedure_id: id, manager: true)
|
||||
end
|
||||
|
||||
def ensure_not_super_admin!
|
||||
procedure_administration = retrieve_procedure_administration
|
||||
if procedure_administration.manager?
|
||||
if is_administrateur_through_procedure_administration_as_manager?
|
||||
redirect_back fallback_location: root_url, alert: "Interdit aux super admins", status: 403
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
module Administrateurs
|
||||
class ArchivesController < AdministrateurController
|
||||
before_action :retrieve_procedure
|
||||
before_action :retrieve_procedure_administration
|
||||
before_action :ensure_not_super_admin!
|
||||
|
||||
helper_method :create_archive_url
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
module Administrateurs
|
||||
class ProcedureAdministrateursController < AdministrateurController
|
||||
before_action :retrieve_procedure, except: [:new]
|
||||
|
||||
before_action :ensure_not_super_admin!, only: [:create]
|
||||
def index
|
||||
@disabled_as_super_admin = is_administrateur_through_procedure_administration_as_manager?
|
||||
end
|
||||
|
||||
def create
|
||||
|
@ -24,6 +25,7 @@ module Administrateurs
|
|||
# Actually add the admin
|
||||
@procedure.administrateurs << administrateur
|
||||
@administrateur = administrateur
|
||||
@disabled_as_super_admin = is_administrateur_through_procedure_administration_as_manager?
|
||||
flash.notice = "L’administrateur « #{administrateur.email} » a été ajouté à la démarche « #{@procedure.libelle} »."
|
||||
end
|
||||
|
||||
|
|
|
@ -7,15 +7,17 @@ module Instructeurs
|
|||
end
|
||||
|
||||
def ensure_not_super_admin!
|
||||
if is_super_admin_through_assign_tos_as_manager?
|
||||
if is_instructeur_through_assign_tos_as_manager?
|
||||
redirect_back fallback_location: root_url, alert: "Interdit aux super admins", status: 403
|
||||
end
|
||||
end
|
||||
|
||||
def is_super_admin_through_assign_tos_as_manager?
|
||||
def is_instructeur_through_assign_tos_as_manager?
|
||||
procedure_id = params[:procedure_id]
|
||||
|
||||
current_instructeur.assign_to
|
||||
.where(instructeur: current_instructeur,
|
||||
groupe_instructeur: current_instructeur.groupe_instructeurs.where(procedure_id: @procedure.id),
|
||||
groupe_instructeur: current_instructeur.groupe_instructeurs.where(procedure_id: procedure_id),
|
||||
manager: true)
|
||||
.count
|
||||
.positive?
|
||||
|
|
|
@ -5,5 +5,5 @@
|
|||
= f.label :email do
|
||||
Ajouter un administrateur
|
||||
%p.notice Renseignez l’email d’un administrateur déjà enregistré sur #{APPLICATION_NAME} pour lui permettre de modifier « #{procedure.libelle} ».
|
||||
= f.email_field :email, placeholder: 'marie.dupont@exemple.fr', required: true
|
||||
= f.submit 'Ajouter comme administrateur', class: 'button primary send'
|
||||
= f.email_field :email, placeholder: 'marie.dupont@exemple.fr', required: true, disabled: disabled_as_super_admin
|
||||
= f.submit 'Ajouter comme administrateur', class: 'button primary send', disabled: disabled_as_super_admin
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
- if @administrateur.present?
|
||||
= turbo_stream.append "administrateurs", partial: 'administrateur', locals: { procedure: @procedure, administrateur: @administrateur }
|
||||
= turbo_stream.replace "new_administrateur", partial: 'add_admin_form', locals: { procedure: @procedure }
|
||||
= turbo_stream.replace "new_administrateur", partial: 'add_admin_form', locals: { procedure: @procedure, disabled_as_super_admin: @disabled_as_super_admin }
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
'Administrateurs'], preview: false }
|
||||
|
||||
.container
|
||||
%h1 Administrateurs de « #{@procedure.libelle} »
|
||||
%h1 Gérer les administrateurs de « #{@procedure.libelle} »
|
||||
|
||||
%table.table
|
||||
%thead
|
||||
%th= 'Adresse email'
|
||||
|
@ -15,4 +16,4 @@
|
|||
%tfoot
|
||||
%tr
|
||||
%th{ colspan: 4 }
|
||||
= render 'add_admin_form', procedure: @procedure
|
||||
= render 'add_admin_form', procedure: @procedure, disabled_as_super_admin: @disabled_as_super_admin
|
||||
|
|
|
@ -1,14 +1,24 @@
|
|||
describe Administrateurs::ProcedureAdministrateursController, type: :controller do
|
||||
let(:signed_in_admin) { create(:administrateur) }
|
||||
let(:other_admin) { create(:administrateur) }
|
||||
let(:procedure) { create(:procedure, administrateurs: [signed_in_admin, other_admin]) }
|
||||
let!(:administrateurs_procedure) { create(:administrateurs_procedure, administrateur: signed_in_admin, procedure: procedure, manager: manager) }
|
||||
let!(:procedure) { create(:procedure, administrateurs: [other_admin]) }
|
||||
render_views
|
||||
|
||||
before do
|
||||
sign_in(signed_in_admin.user)
|
||||
end
|
||||
|
||||
describe '#create' do
|
||||
context 'as manager' do
|
||||
let(:manager) { true }
|
||||
subject { post :create, params: { procedure_id: procedure.id, administrateur: { email: create(:administrateur).email } }, format: :turbo_stream }
|
||||
it { is_expected.to have_http_status(:forbidden) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#destroy' do
|
||||
let(:manager) { false }
|
||||
subject do
|
||||
delete :destroy, params: { procedure_id: procedure.id, id: admin_to_remove.id }, format: :turbo_stream
|
||||
end
|
||||
|
|
|
@ -468,9 +468,10 @@ describe Instructeurs::ProceduresController, type: :controller do
|
|||
describe '#download_export' do
|
||||
let(:instructeur) { create(:instructeur) }
|
||||
let!(:procedure) { create(:procedure) }
|
||||
let!(:gi_0) { procedure.defaut_groupe_instructeur }
|
||||
let!(:assign_to) { create(:assign_to, instructeur: instructeur, groupe_instructeur: build(:groupe_instructeur, procedure: procedure), manager: manager) }
|
||||
let!(:gi_0) { assign_to.groupe_instructeur }
|
||||
let!(:gi_1) { create(:groupe_instructeur, label: 'gi_1', procedure: procedure, instructeurs: [instructeur]) }
|
||||
|
||||
let(:manager) { false }
|
||||
before { sign_in(instructeur.user) }
|
||||
|
||||
subject do
|
||||
|
@ -535,6 +536,11 @@ describe Instructeurs::ProceduresController, type: :controller do
|
|||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when logged in through super admin' do
|
||||
let(:manager) { true }
|
||||
it { is_expected.to have_http_status(:forbidden) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create_multiple_commentaire' do
|
||||
|
|
6
spec/factories/administrateurs_procedure.rb
Normal file
6
spec/factories/administrateurs_procedure.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
FactoryBot.define do
|
||||
factory :administrateurs_procedure do
|
||||
association :administrateur
|
||||
association :procedure
|
||||
end
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
require 'system/administrateurs/procedure_spec_helper'
|
||||
|
||||
describe 'Administrateurs can manage administrateurs', js: true do
|
||||
include ProcedureSpecHelper
|
||||
|
||||
let(:administrateur) { create(:administrateur) }
|
||||
let!(:procedure) { create(:procedure) }
|
||||
let!(:administrateurs_procedure) { create(:administrateurs_procedure, administrateur: administrateur, procedure: procedure, manager: manager) }
|
||||
let(:manager) { false }
|
||||
before do
|
||||
login_as administrateur.user, scope: :user
|
||||
end
|
||||
|
||||
scenario 'card is clickable' do
|
||||
visit admin_procedure_path(procedure)
|
||||
find('#administrateurs').click
|
||||
expect(page).to have_css(:h1, text: "Administrateurs de « #{procedure.libelle} »")
|
||||
end
|
||||
|
||||
context 'as admin not flagged from manager' do
|
||||
let(:manager) { false }
|
||||
|
||||
scenario 'the administrator can add another administrator' do
|
||||
another_administrateur = create(:administrateur)
|
||||
visit admin_procedure_administrateurs_path(procedure)
|
||||
find('#administrateurs').click
|
||||
|
||||
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
|
||||
|
||||
context 'as admin flagged from manager' do
|
||||
let(:manager) { true }
|
||||
scenario 'the administrator from manager can not add another administrator' do
|
||||
administrateur.administrateurs_procedures.update_all(manager: true)
|
||||
visit admin_procedure_administrateurs_path(procedure)
|
||||
|
||||
find('#administrateurs').click
|
||||
|
||||
expect(page).to have_css("#administrateur_email[disabled=\"disabled\"]")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -58,18 +58,4 @@ describe 'Administrateurs can edit procedures', js: true do
|
|||
expect(page).to have_selector('.breadcrumbs li', text: 'Ma petite démarche')
|
||||
end
|
||||
end
|
||||
|
||||
scenario 'the administrator can add another administrator' do
|
||||
another_administrateur = create(:administrateur)
|
||||
visit admin_procedure_path(procedure)
|
||||
find('#administrateurs').click
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue