Get a zone.

master
Philippe Pittoli 2023-05-07 21:05:53 +02:00
parent 1df35ad4c3
commit 9449ba336f
6 changed files with 82 additions and 2 deletions

View File

@ -46,6 +46,13 @@ class DNSManager::Client < IPC
parse_message [ Response::Success ], read
end
# Get a zone.
def user_zone_get(domain : String)
request = DNSManager::Request::GetZone.new domain
send_now request
parse_message [ Response::Zone ], read
end
# Get user domain list.
def user_domain_list()
request = DNSManager::Request::UserDomains.new

View File

@ -37,8 +37,10 @@ class Actions
# Maintenance
@the_call["admin-maintenance"] = ->admin_maintenance
@the_call["user-zone-add"] = ->user_zone_add
@the_call["user-zone-del"] = ->user_zone_del
@the_call["user-zone-get"] = ->user_zone_get
@the_call["user-domain-list"] = ->user_domain_list
end
@ -93,6 +95,18 @@ class Actions
end
end
def user_zone_get
domains = Context.args.not_nil!
domains.each do |domain|
begin
pp! domain
pp! @dnsmanagerd.user_zone_get domain
rescue e
puts "error for user_zone_get: #{e.message}"
end
end
end
def user_domain_list
response = @dnsmanagerd.user_domain_list
case response

View File

@ -128,6 +128,13 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth)
unrecognized_args_to_context_args.call parser, nil, 1
end
parser.on("get", "Get a zone.") do
Baguette::Log.info "get zone."
Context.command = "user-zone-get"
parser.banner = "COMMAND: user zone get domain [domain...]"
unrecognized_args_to_context_args.call parser, nil, 1
end
parser.on("list", "List all domains.") do
Baguette::Log.info "List domains."
Context.command = "user-domain-list"

View File

@ -31,7 +31,21 @@ class DNSManager::Request
end
DNSManager.requests << DeleteZone
IPC::JSON.message UserDomains, 12 do
IPC::JSON.message GetZone, 12 do
property domain : String
def initialize(@domain)
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
user = dnsmanagerd.get_logged_user event
raise NotLoggedException.new if user.nil?
dnsmanagerd.storage.get_zone user.uid, @domain
end
end
DNSManager.requests << GetZone
IPC::JSON.message UserDomains, 13 do
def initialize()
end

View File

@ -15,7 +15,20 @@ class DNSManager::Response
end
DNSManager.responses << DomainChanged
IPC::JSON.message DomainList, 12 do
IPC::JSON.message Zone, 12 do
property zone : Storage::Zone
def initialize(@zone)
end
end
DNSManager.responses << Zone
IPC::JSON.message UnknownZone, 13 do
def initialize()
end
end
DNSManager.responses << UnknownZone
IPC::JSON.message DomainList, 14 do
property domains : Array(String)
def initialize(@domains)
end

View File

@ -121,6 +121,31 @@ class DNSManager::Storage
Response::Error.new "error while deleting the domain #{domain}"
end
def get_zone(user_id : Int32, domain : String) : IPC::JSON
# User must exist.
user_data = user_data_by_uid.get? user_id.to_s
unless user_data
Baguette::Log.warning "unknown user #{user_id} tries to get zone #{domain}"
return Response::UnknownUser.new
end
# User must own the domain.
unless user_data.domains.includes? domain
Baguette::Log.warning "user #{user_id} tries to get zone #{domain} but doesn't own it"
return Response::NoOwnership.new
end
zone = zones_by_domain.get? domain
unless zone
return Response::UnknownZone.new
end
Response::Zone.new zone
rescue e
Baguette::Log.error "trying to get a zone #{domain}: #{e}"
Response::Error.new "error while accessing a zone #{domain}"
end
def user_domains(user_id : Int32) : IPC::JSON
# User must exist.
user_data = user_data_by_uid.get? user_id.to_s