move api_tokens to admin module

This commit is contained in:
simon lehericey 2024-01-25 11:13:47 +01:00
parent 49f0caba7e
commit 588e58195c
13 changed files with 154 additions and 149 deletions

View file

@ -4,7 +4,7 @@
%p %p
= t('.first_paragraph_html', application_name: APPLICATION_NAME, api_doc_url: API_DOC_URL) = t('.first_paragraph_html', application_name: APPLICATION_NAME, api_doc_url: API_DOC_URL)
= link_to t('.create_token'), nom_api_tokens_path, class: "fr-btn fr-btn--secondary fr-mt-2w" = link_to t('.create_token'), nom_admin_api_tokens_path, class: "fr-btn fr-btn--secondary fr-mt-2w"
%ul.fr-mt-4w %ul.fr-mt-4w
= render Profile::APITokenComponent.with_collection(api_tokens) = render Profile::APITokenComponent.with_collection(api_tokens)

View file

@ -10,7 +10,7 @@
%div= use_and_expiration %div= use_and_expiration
%div %div
= link_to 'Supprimer', = link_to 'Supprimer',
api_token_path(@api_token), admin_api_token_path(@api_token),
method: :delete, method: :delete,
class: 'fr-btn fr-btn--tertiary-no-outline fr-btn--sm fr-btn--icon-left fr-icon-delete-line', class: 'fr-btn fr-btn--tertiary-no-outline fr-btn--sm fr-btn--icon-left fr-icon-delete-line',
data: { confirm: "Confirmez-vous la suppression du jeton « #{@api_token.name} » ?" } data: { confirm: "Confirmez-vous la suppression du jeton « #{@api_token.name} » ?" }

View file

@ -0,0 +1,112 @@
module Administrateurs
class APITokensController < AdministrateurController
before_action :authenticate_administrateur!
before_action :set_api_token, only: [:destroy]
def nom
@name = name
end
def autorisations
@name = name
@libelle_id_procedures = current_administrateur
.procedures
.order(:libelle)
.pluck(:libelle, :id)
.map { |libelle, id| ["#{id} - #{libelle}", id] }
end
def securite
end
def create
if params[:networkFiltering] == "customNetworks" && invalid_network?
return redirect_to securite_admin_api_tokens_path(all_params.merge(invalidNetwork: true))
end
@api_token, @packed_token = APIToken.generate(current_administrateur)
@api_token.update!(name:, write_access:,
allowed_procedure_ids:, authorized_networks:, expires_at:)
end
def destroy
@api_token.destroy
redirect_to profil_path
end
private
def all_params
[:name, :access, :target, :targets, :networkFiltering, :networks, :lifetime, :customLifetime]
.index_with { |param| params[param] }
end
def authorized_networks
if params[:networkFiltering] == "customNetworks"
networks
else
[]
end
end
def invalid_network?
params[:networks]
.split
.any? do
begin
IPAddr.new(_1)
false
rescue
true
end
end
end
def networks
params[:networks]
.split
.map { begin IPAddr.new(_1) rescue nil end }
.compact
end
def set_api_token
@api_token = current_administrateur.api_tokens.find(params[:id])
end
def name
params[:name]
end
def write_access
params[:access] == "read_write"
end
def allowed_procedure_ids
if params[:target] == "custom"
current_administrateur
.procedure_ids
.intersection(params[:targets].map(&:to_i))
else
nil
end
end
def expires_at
case params[:lifetime]
in 'oneWeek'
1.week.from_now.to_date
in 'custom'
[
Date.parse(params[:customLifetime]),
1.year.from_now
].min
in 'infinite' if authorized_networks.present?
nil
else
1.week.from_now.to_date
end
end
end
end

View file

@ -1,110 +0,0 @@
class APITokensController < ApplicationController
before_action :authenticate_administrateur!
before_action :set_api_token, only: [:destroy]
def nom
@name = name
end
def autorisations
@name = name
@libelle_id_procedures = current_administrateur
.procedures
.order(:libelle)
.pluck(:libelle, :id)
.map { |libelle, id| ["#{id} - #{libelle}", id] }
end
def securite
end
def create
if params[:networkFiltering] == "customNetworks" && invalid_network?
return redirect_to securite_api_tokens_path(all_params.merge(invalidNetwork: true))
end
@api_token, @packed_token = APIToken.generate(current_administrateur)
@api_token.update!(name:, write_access:,
allowed_procedure_ids:, authorized_networks:, expires_at:)
end
def destroy
@api_token.destroy
redirect_to profil_path
end
private
def all_params
[:name, :access, :target, :targets, :networkFiltering, :networks, :lifetime, :customLifetime]
.index_with { |param| params[param] }
end
def authorized_networks
if params[:networkFiltering] == "customNetworks"
networks
else
[]
end
end
def invalid_network?
params[:networks]
.split
.any? do
begin
IPAddr.new(_1)
false
rescue
true
end
end
end
def networks
params[:networks]
.split
.map { begin IPAddr.new(_1) rescue nil end }
.compact
end
def set_api_token
@api_token = current_administrateur.api_tokens.find(params[:id])
end
def name
params[:name]
end
def write_access
params[:access] == "read_write"
end
def allowed_procedure_ids
if params[:target] == "custom"
current_administrateur
.procedure_ids
.intersection(params[:targets].map(&:to_i))
else
nil
end
end
def expires_at
case params[:lifetime]
in 'oneWeek'
1.week.from_now.to_date
in 'custom'
[
Date.parse(params[:customLifetime]),
1.year.from_now
].min
in 'infinite' if authorized_networks.present?
nil
else
1.week.from_now.to_date
end
end
end

View file

@ -3,11 +3,11 @@
= render partial: 'administrateurs/breadcrumbs', = render partial: 'administrateurs/breadcrumbs',
locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path], locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path],
[t('users.profil.show.profile'), profil_path], [t('users.profil.show.profile'), profil_path],
[t('api_tokens.nom.new_token')]] } [t('administrateurs.api_tokens.nom.new_token')]] }
.fr-container.fr-mt-2w{ 'data-turbo': 'true' } .fr-container.fr-mt-2w{ 'data-turbo': 'true' }
%h1 Privilèges du jeton « #{@name} » %h1 Privilèges du jeton « #{@name} »
= form_with url: securite_api_tokens_path, = form_with url: securite_admin_api_tokens_path,
method: :get, method: :get,
data: { controller: 'api-token-autorisation' } do |f| data: { controller: 'api-token-autorisation' } do |f|
@ -67,4 +67,4 @@
'data-api-token-autorisation-target': 'continueButton' do 'data-api-token-autorisation-target': 'continueButton' do
= t('.continue') = t('.continue')
%li %li
= link_to t('.cancel'), nom_api_tokens_path(name: @name), class: "fr-btn fr-btn--secondary" = link_to t('.cancel'), nom_admin_api_tokens_path(name: @name), class: "fr-btn fr-btn--secondary"

View file

@ -3,7 +3,7 @@
= render partial: 'administrateurs/breadcrumbs', = render partial: 'administrateurs/breadcrumbs',
locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path], locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path],
[t('users.profil.show.profile'), profil_path], [t('users.profil.show.profile'), profil_path],
[t('api_tokens.nom.new_token')]] } [t('administrateurs.api_tokens.nom.new_token')]] }
.fr-container.fr-mt-2w{ 'data-turbo': 'true' } .fr-container.fr-mt-2w{ 'data-turbo': 'true' }
%h1 Votre jeton est prêt %h1 Votre jeton est prêt

View file

@ -3,11 +3,11 @@
= render partial: 'administrateurs/breadcrumbs', = render partial: 'administrateurs/breadcrumbs',
locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path], locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path],
[t('users.profil.show.profile'), profil_path], [t('users.profil.show.profile'), profil_path],
[t('api_tokens.nom.new_token')]] } [t('administrateurs.api_tokens.nom.new_token')]] }
.fr-container.fr-mt-2w{ 'data-turbo': 'true' } .fr-container.fr-mt-2w{ 'data-turbo': 'true' }
%h1= t('.new_token') %h1= t('.new_token')
= form_with url: autorisations_api_tokens_path, method: :get, html: { class: 'fr-mt-2w' } do |f| = form_with url: autorisations_admin_api_tokens_path, method: :get, html: { class: 'fr-mt-2w' } do |f|
.fr-input-group .fr-input-group
= f.label :name, class: 'fr-label' do = f.label :name, class: 'fr-label' do
= t('.name') = t('.name')

View file

@ -3,7 +3,7 @@
= render partial: 'administrateurs/breadcrumbs', = render partial: 'administrateurs/breadcrumbs',
locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path], locals: { steps: [['Tableau de bord', tableau_de_bord_helper_path],
[t('users.profil.show.profile'), profil_path], [t('users.profil.show.profile'), profil_path],
[t('api_tokens.nom.new_token')]] } [t('administrateurs.api_tokens.nom.new_token')]] }
.fr-container.fr-mt-2w .fr-container.fr-mt-2w
%h1 Sécurité %h1 Sécurité
@ -21,7 +21,7 @@
%b Il est de votre responsabilité de le conserver en sécurité et d'en limiter l'utilisation aux seules personnes habilitées. %b Il est de votre responsabilité de le conserver en sécurité et d'en limiter l'utilisation aux seules personnes habilitées.
%p Pour vous aider, nous vous proposons des fonctionnalités de filtrage réseau et de durée de vie du jeton. %p Pour vous aider, nous vous proposons des fonctionnalités de filtrage réseau et de durée de vie du jeton.
= form_with url: api_tokens_path, = form_with url: admin_api_tokens_path,
method: :post, method: :post,
html: { class: 'fr-mt-2w' }, html: { class: 'fr-mt-2w' },
data: { controller: 'api-token-securite' } do |f| data: { controller: 'api-token-securite' } do |f|
@ -102,4 +102,4 @@
'data-api-token-securite-target': 'continueButton' do 'data-api-token-securite-target': 'continueButton' do
créer le jeton créer le jeton
%li %li
= link_to 'retour', autorisations_api_tokens_path(name: params[:name], access: params[:access], target: params[:target], targets: params[:targets]), class: "fr-btn fr-btn--secondary" = link_to 'retour', autorisations_admin_api_tokens_path(name: params[:name], access: params[:access], target: params[:target], targets: params[:targets]), class: "fr-btn fr-btn--secondary"

View file

@ -1,5 +1,6 @@
--- ---
en: en:
administrateurs:
api_tokens: api_tokens:
nom: nom:
new_token: New token creation new_token: New token creation

View file

@ -1,5 +1,6 @@
--- ---
fr: fr:
administrateurs:
api_tokens: api_tokens:
nom: nom:
new_token: Création d'un nouveau jeton new_token: Création d'un nouveau jeton

View file

@ -208,13 +208,6 @@ Rails.application.routes.draw do
resources :attachments, only: [:show, :destroy] resources :attachments, only: [:show, :destroy]
resources :recherche, only: [:index] resources :recherche, only: [:index]
resources :api_tokens, only: [:create, :destroy] do
collection do
get :nom
get :autorisations
get :securite
end
end
get "patron" => "root#patron" if Rails.env.development? || Rails.env.test? get "patron" => "root#patron" if Rails.env.development? || Rails.env.test?
get "suivi" => "root#suivi" get "suivi" => "root#suivi"
@ -668,6 +661,14 @@ Rails.application.routes.draw do
patch 'add_to_procedure' patch 'add_to_procedure'
end end
end end
resources :api_tokens, only: [:create, :destroy] do
collection do
get :nom
get :autorisations
get :securite
end
end
end end
resources :release_notes, only: [:index] resources :release_notes, only: [:index]

View file

@ -1,4 +1,4 @@
describe APITokensController, type: :controller do describe Administrateurs::APITokensController, type: :controller do
let(:admin) { create(:administrateur) } let(:admin) { create(:administrateur) }
let(:procedure) { create(:procedure, administrateur: admin) } let(:procedure) { create(:procedure, administrateur: admin) }