From 588e58195c7b953b1bb1df7a02030dacdefb1ddb Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 25 Jan 2024 11:13:47 +0100 Subject: [PATCH] move api_tokens to admin module --- .../api_token_card_component.html.haml | 2 +- .../api_token_component.html.haml | 2 +- .../administrateurs/api_tokens_controller.rb | 112 ++++++++++++++++++ app/controllers/api_tokens_controller.rb | 110 ----------------- .../api_tokens/autorisations.html.haml | 6 +- .../api_tokens/create.html.haml | 2 +- .../api_tokens/index.turbo_stream.haml | 0 .../api_tokens/nom.html.haml | 4 +- .../api_tokens/securite.html.haml | 6 +- config/locales/views/api_tokens.en.yml | 21 ++-- config/locales/views/api_tokens.fr.yml | 21 ++-- config/routes.rb | 15 +-- .../api_tokens_controller_spec.rb | 2 +- 13 files changed, 154 insertions(+), 149 deletions(-) create mode 100644 app/controllers/administrateurs/api_tokens_controller.rb delete mode 100644 app/controllers/api_tokens_controller.rb rename app/views/{ => administrateurs}/api_tokens/autorisations.html.haml (92%) rename app/views/{ => administrateurs}/api_tokens/create.html.haml (96%) rename app/views/{ => administrateurs}/api_tokens/index.turbo_stream.haml (100%) rename app/views/{ => administrateurs}/api_tokens/nom.html.haml (83%) rename app/views/{ => administrateurs}/api_tokens/securite.html.haml (94%) rename spec/controllers/{ => administrateurs}/api_tokens_controller_spec.rb (97%) diff --git a/app/components/profile/api_token_card_component/api_token_card_component.html.haml b/app/components/profile/api_token_card_component/api_token_card_component.html.haml index 0e21770bf..6989751d9 100644 --- a/app/components/profile/api_token_card_component/api_token_card_component.html.haml +++ b/app/components/profile/api_token_card_component/api_token_card_component.html.haml @@ -4,7 +4,7 @@ %p = 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 = render Profile::APITokenComponent.with_collection(api_tokens) diff --git a/app/components/profile/api_token_component/api_token_component.html.haml b/app/components/profile/api_token_component/api_token_component.html.haml index 3ded5095e..279793805 100644 --- a/app/components/profile/api_token_component/api_token_component.html.haml +++ b/app/components/profile/api_token_component/api_token_component.html.haml @@ -10,7 +10,7 @@ %div= use_and_expiration %div = link_to 'Supprimer', - api_token_path(@api_token), + admin_api_token_path(@api_token), method: :delete, 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} » ?" } diff --git a/app/controllers/administrateurs/api_tokens_controller.rb b/app/controllers/administrateurs/api_tokens_controller.rb new file mode 100644 index 000000000..119feae19 --- /dev/null +++ b/app/controllers/administrateurs/api_tokens_controller.rb @@ -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 diff --git a/app/controllers/api_tokens_controller.rb b/app/controllers/api_tokens_controller.rb deleted file mode 100644 index 14ff64fb4..000000000 --- a/app/controllers/api_tokens_controller.rb +++ /dev/null @@ -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 diff --git a/app/views/api_tokens/autorisations.html.haml b/app/views/administrateurs/api_tokens/autorisations.html.haml similarity index 92% rename from app/views/api_tokens/autorisations.html.haml rename to app/views/administrateurs/api_tokens/autorisations.html.haml index cf9a63eab..44d8334f1 100644 --- a/app/views/api_tokens/autorisations.html.haml +++ b/app/views/administrateurs/api_tokens/autorisations.html.haml @@ -3,11 +3,11 @@ = render partial: 'administrateurs/breadcrumbs', locals: { steps: [['Tableau de bord', tableau_de_bord_helper_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' } %h1 Privilèges du jeton « #{@name} » - = form_with url: securite_api_tokens_path, + = form_with url: securite_admin_api_tokens_path, method: :get, data: { controller: 'api-token-autorisation' } do |f| @@ -67,4 +67,4 @@ 'data-api-token-autorisation-target': 'continueButton' do = t('.continue') %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" diff --git a/app/views/api_tokens/create.html.haml b/app/views/administrateurs/api_tokens/create.html.haml similarity index 96% rename from app/views/api_tokens/create.html.haml rename to app/views/administrateurs/api_tokens/create.html.haml index 9e5f4514a..b82663a6e 100644 --- a/app/views/api_tokens/create.html.haml +++ b/app/views/administrateurs/api_tokens/create.html.haml @@ -3,7 +3,7 @@ = render partial: 'administrateurs/breadcrumbs', locals: { steps: [['Tableau de bord', tableau_de_bord_helper_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' } %h1 Votre jeton est prêt diff --git a/app/views/api_tokens/index.turbo_stream.haml b/app/views/administrateurs/api_tokens/index.turbo_stream.haml similarity index 100% rename from app/views/api_tokens/index.turbo_stream.haml rename to app/views/administrateurs/api_tokens/index.turbo_stream.haml diff --git a/app/views/api_tokens/nom.html.haml b/app/views/administrateurs/api_tokens/nom.html.haml similarity index 83% rename from app/views/api_tokens/nom.html.haml rename to app/views/administrateurs/api_tokens/nom.html.haml index 66f56d679..730665ea0 100644 --- a/app/views/api_tokens/nom.html.haml +++ b/app/views/administrateurs/api_tokens/nom.html.haml @@ -3,11 +3,11 @@ = render partial: 'administrateurs/breadcrumbs', locals: { steps: [['Tableau de bord', tableau_de_bord_helper_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' } %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 = f.label :name, class: 'fr-label' do = t('.name') diff --git a/app/views/api_tokens/securite.html.haml b/app/views/administrateurs/api_tokens/securite.html.haml similarity index 94% rename from app/views/api_tokens/securite.html.haml rename to app/views/administrateurs/api_tokens/securite.html.haml index b2b66c93c..e98f62db7 100644 --- a/app/views/api_tokens/securite.html.haml +++ b/app/views/administrateurs/api_tokens/securite.html.haml @@ -3,7 +3,7 @@ = render partial: 'administrateurs/breadcrumbs', locals: { steps: [['Tableau de bord', tableau_de_bord_helper_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 %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. %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, html: { class: 'fr-mt-2w' }, data: { controller: 'api-token-securite' } do |f| @@ -102,4 +102,4 @@ 'data-api-token-securite-target': 'continueButton' do créer le jeton %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" diff --git a/config/locales/views/api_tokens.en.yml b/config/locales/views/api_tokens.en.yml index 6bd55ac19..863ff15f4 100644 --- a/config/locales/views/api_tokens.en.yml +++ b/config/locales/views/api_tokens.en.yml @@ -1,12 +1,13 @@ --- en: - api_tokens: - nom: - new_token: New token creation - name: name of the token - name-hint: 'examples: orus prod, presta' - continue: continue - cancel: back - autorisations: - cancel: back - continue: continue + administrateurs: + api_tokens: + nom: + new_token: New token creation + name: name of the token + name-hint: 'examples: orus prod, presta' + continue: continue + cancel: back + autorisations: + cancel: back + continue: continue diff --git a/config/locales/views/api_tokens.fr.yml b/config/locales/views/api_tokens.fr.yml index 56a850be2..4707678b1 100644 --- a/config/locales/views/api_tokens.fr.yml +++ b/config/locales/views/api_tokens.fr.yml @@ -1,12 +1,13 @@ --- fr: - api_tokens: - nom: - new_token: Création d'un nouveau jeton - name: Nom du jeton - name-hint: 'exemples: prod orus, test presta' - continue: Continuer - cancel: Retour - autorisations: - cancel: Retour - continue: Continuer + administrateurs: + api_tokens: + nom: + new_token: Création d'un nouveau jeton + name: Nom du jeton + name-hint: 'exemples: prod orus, test presta' + continue: Continuer + cancel: Retour + autorisations: + cancel: Retour + continue: Continuer diff --git a/config/routes.rb b/config/routes.rb index bdaed2d31..366de8026 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -208,13 +208,6 @@ Rails.application.routes.draw do resources :attachments, only: [:show, :destroy] 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 "suivi" => "root#suivi" @@ -668,6 +661,14 @@ Rails.application.routes.draw do patch 'add_to_procedure' end end + + resources :api_tokens, only: [:create, :destroy] do + collection do + get :nom + get :autorisations + get :securite + end + end end resources :release_notes, only: [:index] diff --git a/spec/controllers/api_tokens_controller_spec.rb b/spec/controllers/administrateurs/api_tokens_controller_spec.rb similarity index 97% rename from spec/controllers/api_tokens_controller_spec.rb rename to spec/controllers/administrateurs/api_tokens_controller_spec.rb index 07e2066cc..5bf4c769e 100644 --- a/spec/controllers/api_tokens_controller_spec.rb +++ b/spec/controllers/administrateurs/api_tokens_controller_spec.rb @@ -1,4 +1,4 @@ -describe APITokensController, type: :controller do +describe Administrateurs::APITokensController, type: :controller do let(:admin) { create(:administrateur) } let(:procedure) { create(:procedure, administrateur: admin) }