[add] added public mode

This commit is contained in:
2021-03-07 16:39:38 -05:00
parent 8bbd3e02f3
commit c4cb76fe0b
4 changed files with 109 additions and 2 deletions

View File

@@ -3,11 +3,12 @@
require 'resolv'
class ClientsController < ApplicationController
before_action :parse_api_key_header, except: [:show_public_ipv4]
before_action :authenticate_client, except: [:show_public_ipv4]
before_action :parse_api_key_header, except: %i[show_public_ipv4 create_public_ipv4]
before_action :authenticate_client, except: %i[show_public_ipv4 create_public_ipv4]
before_action :authorize_perm_r, only: [:show_ipv4]
before_action :authorize_perm_w, only: %i[update_ipv4 destroy_ipv4]
before_action :authorize_perm_x, only: %i[create update index show destroy]
before_action :authorize_public, only: [:create_public_ipv4] if ENV['RAILS_API_PUBLIC']
before_action :client_authorized, except: [:show_public_ipv4]
before_action :demo, only: %i[create update destroy] if ENV['RAILS_API_DEMO']
@@ -129,6 +130,29 @@ class ClientsController < ApplicationController
json_response(client_read_object(client))
end
# POST /public/:id
def create_public_ipv4
ipv4 = request_ip
# return 406 if body with invalid ipv4 is present
# TODO: move this into the model (update)
return head(406) unless ipv4
client_params = params.permit(:name)
client = Client.find_by name: params[:name]
status_code = client ? 200 : 201
# create new client or update existing
if client.nil?
client ||= Client.new_client(client_params)
return head(429) if client.nil? # reached max client count
end
client.public_ip = true
client.ipv4 = ipv4
client.save
json_response(client_read_object(client), status_code)
end
private
# helper methods
@@ -215,6 +239,10 @@ class ClientsController < ApplicationController
@authorized = @requesting_client.admin?
end
def authorize_public
@authorized = true
end
# check if request is authorized
def client_authorized
return head(403) unless @authorized