2022-11-30 10:12:04 +01:00
|
|
|
class APITokensController < ApplicationController
|
|
|
|
before_action :authenticate_administrateur!
|
2024-01-17 11:02:26 +01:00
|
|
|
before_action :set_api_token, only: [:destroy]
|
2022-11-30 10:12:04 +01:00
|
|
|
|
2024-01-17 11:05:56 +01:00
|
|
|
def nom
|
2024-01-17 11:11:38 +01:00
|
|
|
@name = name
|
2024-01-17 11:05:56 +01:00
|
|
|
end
|
|
|
|
|
2024-01-17 11:07:48 +01:00
|
|
|
def autorisations
|
2024-01-17 11:11:38 +01:00
|
|
|
@name = name
|
2024-01-17 11:07:48 +01:00
|
|
|
@libelle_id_procedures = current_administrateur
|
|
|
|
.procedures
|
|
|
|
.order(:libelle)
|
|
|
|
.pluck(:libelle, :id)
|
|
|
|
.map { |libelle, id| ["#{id} - #{libelle}", id] }
|
|
|
|
end
|
|
|
|
|
2024-01-17 11:10:39 +01:00
|
|
|
def securite
|
|
|
|
end
|
|
|
|
|
2022-11-30 10:12:04 +01:00
|
|
|
def create
|
|
|
|
@api_token, @packed_token = APIToken.generate(current_administrateur)
|
|
|
|
|
2024-01-17 11:11:38 +01:00
|
|
|
@api_token.update!(name:, write_access:,
|
|
|
|
allowed_procedure_ids:, authorized_networks:, expires_at:)
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@api_token.destroy
|
|
|
|
|
2024-01-17 10:44:09 +01:00
|
|
|
redirect_to profil_path
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-01-17 11:11:38 +01:00
|
|
|
def authorized_networks
|
|
|
|
if params[:networkFiltering] == "customNetworks"
|
|
|
|
networks
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def networks
|
|
|
|
params[:networks]
|
|
|
|
.split
|
|
|
|
.map { begin IPAddr.new(_1) rescue nil end }
|
|
|
|
.compact
|
|
|
|
end
|
|
|
|
|
2023-09-12 17:20:00 +02:00
|
|
|
def set_api_token
|
|
|
|
@api_token = current_administrateur.api_tokens.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2024-01-17 11:11:38 +01:00
|
|
|
def name
|
|
|
|
params[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_access
|
|
|
|
params[:access] == "read_write"
|
2023-09-13 10:29:49 +02:00
|
|
|
end
|
|
|
|
|
2024-01-17 11:11:38 +01:00
|
|
|
def allowed_procedure_ids
|
|
|
|
if params[:target] == "custom"
|
|
|
|
current_administrateur
|
|
|
|
.procedure_ids
|
|
|
|
.intersection(params[:targets].map(&:to_i))
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2023-09-13 10:30:47 +02:00
|
|
|
end
|
|
|
|
|
2024-01-17 11:11:38 +01:00
|
|
|
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
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
end
|