2022-11-30 10:12:04 +01:00
|
|
|
class APITokensController < ApplicationController
|
|
|
|
before_action :authenticate_administrateur!
|
2023-09-12 17:20:00 +02:00
|
|
|
before_action :set_api_token, only: [:update, :destroy]
|
2022-11-30 10:12:04 +01:00
|
|
|
|
|
|
|
def create
|
|
|
|
@api_token, @packed_token = APIToken.generate(current_administrateur)
|
|
|
|
|
2023-09-13 12:05:40 +02:00
|
|
|
render :index
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2023-09-13 10:29:49 +02:00
|
|
|
if become_full_access?
|
|
|
|
@api_token.become_full_access!
|
|
|
|
elsif disallow_procedure_id.present?
|
2023-09-13 10:30:47 +02:00
|
|
|
@api_token.untarget_procedure(disallow_procedure_id.to_i)
|
2023-02-28 16:33:22 +01:00
|
|
|
else
|
|
|
|
@api_token.update!(api_token_params)
|
|
|
|
end
|
2022-11-30 10:12:04 +01:00
|
|
|
|
2023-09-13 12:05:40 +02:00
|
|
|
render :index
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@api_token.destroy
|
|
|
|
|
2023-09-13 12:05:40 +02:00
|
|
|
render :index
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-09-12 17:20:00 +02:00
|
|
|
def set_api_token
|
|
|
|
@api_token = current_administrateur.api_tokens.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2023-09-13 10:29:49 +02:00
|
|
|
def become_full_access?
|
|
|
|
api_token_params[:become_full_access].present?
|
|
|
|
end
|
|
|
|
|
2023-09-13 10:30:47 +02:00
|
|
|
def disallow_procedure_id
|
|
|
|
api_token_params[:disallow_procedure_id]
|
|
|
|
end
|
|
|
|
|
2022-11-30 10:12:04 +01:00
|
|
|
def api_token_params
|
2023-09-13 10:29:49 +02:00
|
|
|
params.require(:api_token).permit(:name, :write_access, :become_full_access, :disallow_procedure_id, allowed_procedure_ids: [])
|
2022-11-30 10:12:04 +01:00
|
|
|
end
|
|
|
|
end
|