Merge pull request #9564 from adullact/feature-ouidou/admin_creation_delegation_manager_page

Feature ouidou/admin creation delegation manager page
This commit is contained in:
Paul Chavard 2023-10-11 09:50:14 +00:00 committed by GitHub
commit c1d6cafc2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 541 additions and 4 deletions

View file

@ -20,7 +20,25 @@ module Manager
.deliver_later
end
redirect_to manager_groupe_gestionnaires_path(groupe_gestionnaire)
redirect_to manager_groupe_gestionnaire_path(groupe_gestionnaire)
end
def remove_gestionnaire
if !groupe_gestionnaire.root_groupe_gestionnaire? || groupe_gestionnaire.gestionnaires.one?
flash[:alert] = "Suppression impossible : il doit y avoir au moins un gestionnaire dans le groupe racine"
else
gestionnaire = Gestionnaire.find(gestionnaire_id)
if groupe_gestionnaire.remove(gestionnaire)
flash[:notice] = "Le gestionnaire « #{gestionnaire.email} » a été retiré du groupe."
GroupeGestionnaireMailer
.notify_removed_gestionnaire(groupe_gestionnaire, gestionnaire, current_super_admin.email)
.deliver_later
else
flash[:alert] = "Le gestionnaire « #{gestionnaire.email} » nest pas dans le groupe."
end
end
redirect_to manager_groupe_gestionnaire_path(groupe_gestionnaire)
end
private
@ -28,5 +46,9 @@ module Manager
def groupe_gestionnaire
@groupe_gestionnaire ||= GroupeGestionnaire.find(params[:id])
end
def gestionnaire_id
params[:gestionnaire][:id]
end
end
end

View file

@ -1,6 +1,14 @@
class GroupeGestionnaireMailer < ApplicationMailer
layout 'mailers/layout'
def notify_removed_gestionnaire(groupe_gestionnaire, removed_gestionnaire, current_super_admin_email)
@groupe_gestionnaire = groupe_gestionnaire
@current_super_admin_email = current_super_admin_email
subject = "Vous avez été retiré(e) du groupe d'administrateur \"#{groupe_gestionnaire.name}\""
mail(to: removed_gestionnaire.email, subject: subject)
end
def notify_added_gestionnaires(groupe_gestionnaire, added_gestionnaires, current_super_admin_email)
added_gestionnaire_emails = added_gestionnaires.map(&:email)
@groupe_gestionnaire = groupe_gestionnaire

View file

@ -4,10 +4,24 @@ class GroupeGestionnaire < ApplicationRecord
has_many :administrateurs
has_and_belongs_to_many :gestionnaires
def root_groupe_gestionnaire?
groupe_gestionnaire.nil?
end
def add(gestionnaire)
return if gestionnaire.nil?
return if in?(gestionnaire.groupe_gestionnaires)
gestionnaires << gestionnaire
end
def remove(gestionnaire)
return if gestionnaire.nil?
return if !in?(gestionnaire.groupe_gestionnaires)
gestionnaire.groupe_gestionnaires.destroy(self)
end
def add_gestionnaires(ids: [], emails: [])
gestionnaires_to_add, valid_emails, invalid_emails = Gestionnaire.find_all_by_identifier_with_emails(ids:, emails:)
not_found_emails = valid_emails - gestionnaires_to_add.map(&:email)

View file

@ -0,0 +1,16 @@
#breadcrumbs.sub-header
.fr-container.flex.justify-between.align-baseline.column
%nav.fr-breadcrumb.mt-0{ role: "navigation", aria: { label: t('you_are_here', scope: [:layouts, :breadcrumb]) } }
%button.fr-breadcrumb__button{ aria: { expanded: "false", controls: "breadcrumb-1" } }
= t('show', scope: [:layouts, :breadcrumb])
.fr-collapse#breadcrumb-1
%ol.fr-breadcrumb__list
%li= link_to t('root', scope: [:layouts, :breadcrumb]), root_path, class: 'fr-breadcrumb__link'
- steps.each.with_index do |step, i|
- if i == steps.size - 1
%li{ aria: { current: "page" } }
%span.fr-breadcrumb__link= step[0]
- else
%li= link_to step[0], step[1], class: 'fr-breadcrumb__link'

View file

@ -0,0 +1,17 @@
= render partial: 'gestionnaires/breadcrumbs',
locals: { steps: [['Groupes d\'administrateurs', gestionnaire_groupe_gestionnaires_path]] }
#groupe_gestionnaires-index.container
%h1.fr-h1 Liste des groupes d'administrateurs
%table.fr-table.width-100.mt-3
%thead
%tr
%th{ scope: "col" }
Nom
%tbody
- @groupe_gestionnaires.each do |groupe_gestionnaire|
%tr
%td
= groupe_gestionnaire.name

View file

@ -0,0 +1,6 @@
%p= t(:hello, scope: [:views, :shared, :greetings])
%p
= t(".email_body", groupe_gestionnaire_name: @groupe_gestionnaire.name, email: @current_super_admin_email, application_name: APPLICATION_NAME)
= render partial: "layouts/mailers/signature"

View file

@ -41,3 +41,16 @@
<% end %>
</dd>
</dl>
<dl>
<dt class="attribute-label" id="meta-usager">
Gestionnaire
</dt>
<dd class="attribute-data attribute-data--meta-usager">
<% if user.gestionnaire.present? %>
<%= link_to('Voir son compte gestionnaire', manager_gestionnaire_path(user.gestionnaire)) %>
<% else %>
Pas gestionnaire !
<% end %>
</dd>
</dl>

View file

@ -0,0 +1,17 @@
<% if existing_action?(collection_presenter.resource_name, :edit) %>
<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
) if accessible_action?(resource, :edit) %></td>
<% end %>
<% if existing_action?(collection_presenter.resource_name, :destroy) %>
<td><%= link_to(
t("administrate.actions.destroy"),
[namespace, resource],
class: "text-color-red",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) if accessible_action?(resource, :destroy) %></td>
<% end %>

View file

@ -0,0 +1,27 @@
<% content_for(:title) do %>
<%= display_resource_name(page.resource_name) %>
<% end %>
<header class="main-content__header">
<h1 class="main-content__page-title" id="page-title">
<%= content_for(:title) %>
</h1>
<% if show_search_bar %>
<%= render(
"search",
search_term: search_term,
resource_name: display_resource_name(page.resource_name)
) %>
<% end %>
<div>
<%= link_to(
t(
"manager.gestionnaires.manage_root_groupe_gestionnaire"
),
manager_groupe_gestionnaires_path,
class: "button",
) %>
</div>
</header>

View file

@ -0,0 +1,58 @@
<%#
# Show
This view is the template for the show page.
It renders the attributes of a resource,
as well as a link to its edit page.
## Local variables:
- `page`:
An instance of [Administrate::Page::Show][1].
Contains methods for accessing the resource to be displayed on the page,
as well as helpers for describing how each attribute of the resource
should be displayed.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Show
%>
<% content_for(:title) { t("administrate.actions.show_resource", name: page.page_title) } %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to(
t("administrate.actions.edit_resource", name: page.page_title),
[:edit, namespace, page.resource],
class: "button",
) if accessible_action?(page.resource, :edit) %>
<%= link_to(
t("administrate.actions.destroy"),
[namespace, page.resource],
class: "button button--danger",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) if accessible_action?(page.resource, :destroy) %>
</div>
</header>
<section class="main-content__body">
<%= render partial: 'manager/application/user_meta', locals: {user: page.resource&.user} %>
<dl>
<% page.attributes.each do |attribute| %>
<dt class="attribute-label" id="<%= attribute.name %>">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: page.resource.class.human_attribute_name(attribute.name),
) %>
</dt>
<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
><%= render_field attribute, page: page %></dd>
<% end %>
</dl>
</section>

View file

@ -0,0 +1,81 @@
<% if attribute.resources.any? %>
<% order = attribute.order_from_params(params.fetch(attribute.name, {})) %>
<% page_number = params.fetch(attribute.name, {}).fetch(:page, nil) %>
<table aria-labelledby="<%= attribute.name %>">
<thead>
<tr>
<% attribute.associated_collection(order).attribute_types.select{ |attr_name, attr_type| [:id, :user].include?(attr_name) }.each do |attr_name, attr_type| %>
<th class="cell-label
cell-label--<%= attr_type.html_class %>
cell-label--<%= attribute.associated_collection(order).ordered_html_class(attr_name) %>
cell-label--<%= "#{attribute.associated_collection(order).resource_name}_#{attr_name}" %>"
scope="col"
aria-sort="<%= sort_order(attribute.associated_collection(order).ordered_html_class(attr_name)) %>">
<%= link_to(sanitized_order_params(page, attribute.name).merge(
attribute.associated_collection(order).order_params_for(attr_name, key: attribute.name)
)) do %>
<%= t(
"helpers.label.#{attribute.associated_collection(order).resource_name}.#{attr_name}",
default: attribute.associated_class.human_attribute_name(attr_name).titleize,
) %>
<% if attribute.associated_collection(order).ordered_by?(attr_name) %>
<span class="cell-label__sort-indicator cell-label__sort-indicator--<%= attribute.associated_collection(order).ordered_html_class(attr_name) %>">
<svg aria-hidden="true">
<use xlink:href="#icon-up-caret" />
</svg>
</span>
<% end %>
<% end %>
</th>
<% end %>
<% [false && existing_action?(attribute.associated_collection(order).resource_name, :edit),
existing_action?(attribute.associated_collection(order).resource_name, :destroy)].count(true).times do %>
<th scope="col"></th>
<% end %>
</tr>
</thead>
<tbody>
<% attribute.resources(page_number, order).each do |resource| %>
<tr class="js-table-row">
<% attribute.associated_collection(order).attributes_for(resource).select{ |field| ["id", "user"].include?(field.name) }.each do |field| %>
<td class="cell-data cell-data--<%= field.html_class %>">
<%= render_field field %>
</td>
<% end %>
<% if false %>
<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
) if accessible_action?(resource, :edit) %></td>
<% end %>
<% if existing_action?(attribute.associated_collection(order).resource_name, :destroy) %>
<td class="actions"><%= button_to 'Retirer',
{ action: :remove_gestionnaire, id: page.resource.id },
{ method: :delete,
data: { confirm: t("administrate.actions.confirm") },
params: { gestionnaire: { id: resource.id }},
class: 'fr-btn fr-btn--secondary' }
%></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% if attribute.more_than_limit? %>
<%= render("pagination", resources: attribute.resources(page_number), param_name: "#{attribute.name}[page]") %>
<% end %>
<% else %>
<%= t("administrate.fields.has_many.none", default: "") %>
<% end %>
<%= form_tag(add_gestionnaire_manager_groupe_gestionnaire_path(page.resource), style: 'margin-top: 1rem;') do %>
<%= email_field_tag(:emails, '', placeholder: 'Emails', autocapitalize: 'off', autocorrect: 'off', spellcheck: 'false', style: 'margin-bottom: 1rem;width:24rem;') %>
<button>Ajouter un gestionnaire</button>
<% end %>

View file

@ -0,0 +1,67 @@
<%#
# Show
This view is the template for the show page.
It renders the attributes of a resource,
as well as a link to its edit page.
## Local variables:
- `page`:
An instance of [Administrate::Page::Show][1].
Contains methods for accessing the resource to be displayed on the page,
as well as helpers for describing how each attribute of the resource
should be displayed.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Show
%>
<% content_for(:title) { t("administrate.actions.show_resource", name: page.page_title) } %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to(
t("administrate.actions.edit_resource", name: page.page_title),
[:edit, namespace, page.resource],
class: "button",
) if accessible_action?(page.resource, :edit) %>
<%= link_to(
t("administrate.actions.destroy"),
[namespace, page.resource],
class: "button button--danger",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) if accessible_action?(page.resource, :destroy) %>
</div>
</header>
<section class="main-content__body">
<dl>
<% page.attributes.each do |attribute| %>
<dt class="attribute-label" id="<%= attribute.name %>">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: page.resource.class.human_attribute_name(attribute.name),
) %>
</dt>
<dd class="attribute-data attribute-data--<%=attribute.html_class%>">
<% if attribute.name == 'gestionnaires' %>
<%= render(
"collection_gestionnaires",
page: page,
attribute: attribute
) %>
<% else %>
<%= render_field attribute, page: page %>
<% end %>
</dd>
<% end %>
</dl>
</section>

View file

@ -0,0 +1,9 @@
fr:
activerecord:
attributes:
groupe_gestionnaire:
gestionnaires: Gestionnaires
models:
groupe_gestionnaire:
one: Groupe d'administrateurs
other: Groupes d'administrateurs

View file

@ -0,0 +1,4 @@
en:
groupe_gestionnaire_mailer:
notify_removed_gestionnaire:
email_body: "You were removed from the admins group %{groupe_gestionnaire_name} on %{application_name} by « %{email} »"

View file

@ -0,0 +1,4 @@
fr:
groupe_gestionnaire_mailer:
notify_removed_gestionnaire:
email_body: "Vous venez dêtre supprimé(e) du groupe %{groupe_gestionnaire_name} sur %{application_name} par « %{email} »."

View file

@ -0,0 +1,9 @@
en:
manager:
groupe_gestionnaires:
add_gestionnaire:
wrong_address:
one: "%{emails} is not a valid email address"
other: "%{emails} are not valid email addresses"
gestionnaires:
manage_root_groupe_gestionnaire: Root group management

View file

@ -5,3 +5,5 @@ fr:
wrong_address:
one: "%{emails} nest pas une adresse email valide"
other: "%{emails} ne sont pas des adresses emails valides"
gestionnaires:
manage_root_groupe_gestionnaire: Gestion du groupe racine

View file

@ -56,11 +56,12 @@ Rails.application.routes.draw do
delete 'delete', on: :member
end
if ENV['ADMINS_GROUP_ENABLED'] == 'enabled' # can be removed if needed when EVERY PARTS of the feature will be merged / from env.example.optional
resources :gestionnaires, only: [:index, :show, :edit, :update]
if ENV['ADMINS_GROUP_ENABLED'] == 'enabled' || Rails.env.test? # can be removed if needed when EVERY PARTS of the feature will be merged / from env.example.optional
resources :gestionnaires, only: [:index, :show, :edit, :update, :destroy]
resources :groupe_gestionnaires, path: 'groupe_administrateurs', only: [:index, :show, :new, :create, :edit, :update] do
post 'add_gestionnaire', on: :member
delete 'remove_gestionnaire', on: :member
end
end
@ -473,7 +474,7 @@ Rails.application.routes.draw do
end
end
if ENV['ADMINS_GROUP_ENABLED'] == 'enabled' # can be removed if needed when EVERY PARTS of the feature will be merged / from env.example.optional
if ENV['ADMINS_GROUP_ENABLED'] == 'enabled' || Rails.env.test? # can be removed if needed when EVERY PARTS of the feature will be merged / from env.example.optional
#
# Gestionnaire

View file

@ -0,0 +1,12 @@
describe Gestionnaires::GestionnaireController, type: :controller do
describe 'before actions: authenticate_gestionnaire!' do
it 'is present' do
before_actions = Gestionnaires::GestionnaireController
._process_action_callbacks
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:authenticate_gestionnaire!)
end
end
end

View file

@ -0,0 +1,23 @@
describe Gestionnaires::GroupeGestionnairesController, type: :controller do
let(:gestionnaire) { create(:gestionnaire) }
describe "#index" do
subject { get :index }
context "when not logged" do
before { subject }
it { expect(response).to redirect_to(new_user_session_path) }
end
context "when logged in" do
let!(:groupe_gestionnaire) { create(:groupe_gestionnaire, gestionnaires: [gestionnaire]) }
before do
sign_in(gestionnaire.user)
subject
end
it { expect(response).to have_http_status(:ok) }
it { expect(assigns(:groupe_gestionnaires)).to include(groupe_gestionnaire) }
end
end
end

View file

@ -0,0 +1,25 @@
describe Manager::GestionnairesController, type: :controller do
let(:super_admin) { create(:super_admin) }
let(:gestionnaire) { create(:gestionnaire) }
before { sign_in super_admin }
describe '#index' do
render_views
it 'searches admin by email' do
get :index, params: { search: gestionnaire.email }
expect(response).to have_http_status(:success)
end
end
describe '#show' do
render_views
before do
get :show, params: { id: gestionnaire.id }
end
it { expect(response.body).to include(gestionnaire.email) }
end
end

View file

@ -0,0 +1,82 @@
describe Manager::GroupeGestionnairesController, type: :controller do
let(:super_admin) { create(:super_admin) }
let(:groupe_gestionnaire) { create(:groupe_gestionnaire) }
before { sign_in super_admin }
describe '#index' do
render_views
before do
groupe_gestionnaire
get :index
end
it { expect(response.body).to include(groupe_gestionnaire.name) }
end
describe '#show' do
render_views
before do
get :show, params: { id: groupe_gestionnaire.id }
end
it { expect(response.body).to include(groupe_gestionnaire.name) }
end
describe '#add_gestionnaire' do
before do
post :add_gestionnaire,
params: {
id: groupe_gestionnaire.id,
emails: new_gestionnaire_email
}
end
context 'of a new gestionnaire' do
let(:new_gestionnaire_email) { 'new_gestionnaire@mail.com' }
it { expect(groupe_gestionnaire.gestionnaires.map(&:email)).to include(new_gestionnaire_email) }
it { expect(flash.notice).to be_present }
it { expect(response).to redirect_to(manager_groupe_gestionnaire_path(groupe_gestionnaire)) }
end
end
describe '#remove_gestionnaire' do
let(:gestionnaire) { create(:gestionnaire) }
let(:new_gestionnaire) { create(:gestionnaire) }
before do
groupe_gestionnaire.gestionnaires << gestionnaire << new_gestionnaire
end
def remove_gestionnaire(gestionnaire)
delete :remove_gestionnaire,
params: {
id: groupe_gestionnaire.id,
gestionnaire: { id: gestionnaire.id }
}
end
context 'when there are many gestionnaires' do
before { remove_gestionnaire(new_gestionnaire) }
it { expect(groupe_gestionnaire.gestionnaires).to include(gestionnaire) }
it { expect(groupe_gestionnaire.reload.gestionnaires.count).to eq(1) }
it { expect(response).to redirect_to(manager_groupe_gestionnaire_path(groupe_gestionnaire)) }
end
context 'when there is only one gestionnaire' do
before do
remove_gestionnaire(new_gestionnaire)
remove_gestionnaire(gestionnaire)
end
it { expect(groupe_gestionnaire.gestionnaires).to include(gestionnaire) }
it { expect(groupe_gestionnaire.gestionnaires.count).to eq(1) }
it { expect(flash.alert).to eq('Suppression impossible : il doit y avoir au moins un gestionnaire dans le groupe racine') }
it { expect(response).to redirect_to(manager_groupe_gestionnaire_path(groupe_gestionnaire)) }
end
end
end

View file

@ -1,4 +1,17 @@
RSpec.describe GroupeGestionnaireMailer, type: :mailer do
describe '#notify_removed_gestionnaire' do
let(:groupe_gestionnaire) { create(:groupe_gestionnaire) }
let(:gestionnaire_to_remove) { create(:gestionnaire, email: 'int3@g') }
let(:current_super_admin_email) { 'toto@email.com' }
subject { described_class.notify_removed_gestionnaire(groupe_gestionnaire, gestionnaire_to_remove, current_super_admin_email) }
it { expect(subject.body).to include('Vous venez dêtre supprimé(e) du groupe') }
it { expect(subject.to).to match_array(['int3@g']) }
end
describe '#notify_added_gestionnaires' do
let(:groupe_gestionnaire) { create(:groupe_gestionnaire) }

View file

@ -1,4 +1,11 @@
class GroupeGestionnaireMailerPreview < ActionMailer::Preview
def notify_removed_gestionnaire
groupe_gestionnaire = GroupeGestionnaire.new(name: 'un groupe d\'admin')
current_super_admin_email = 'admin@dgfip.com'
gestionnaire = Gestionnaire.new(user: user)
GroupeGestionnaireMailer.notify_removed_gestionnaire(groupe_gestionnaire, gestionnaire, current_super_admin_email)
end
def notify_added_gestionnaires
groupe_gestionnaire = GroupeGestionnaire.new(name: 'un groupe d\'admin')
current_super_admin_email = 'admin@dgfip.com'