From 1df35ad4c3937a81ccb24c6e756e1da1119a27e1 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 7 May 2023 20:50:56 +0200 Subject: [PATCH] List all user domains. --- src/client/lib/dnsmanager-client.cr | 7 +++++++ src/client/main.cr | 15 +++++++++++++++ src/client/parser.cr | 7 +++++++ src/requests/zone.cr | 12 ++++++++++++ src/responses/zone.cr | 7 +++++++ src/storage.cr | 13 +++++++++++++ 6 files changed, 61 insertions(+) diff --git a/src/client/lib/dnsmanager-client.cr b/src/client/lib/dnsmanager-client.cr index 7c7d5a1..f1b806f 100644 --- a/src/client/lib/dnsmanager-client.cr +++ b/src/client/lib/dnsmanager-client.cr @@ -46,6 +46,13 @@ class DNSManager::Client < IPC parse_message [ Response::Success ], read end + # Get user domain list. + def user_domain_list() + request = DNSManager::Request::UserDomains.new + send_now request + parse_message [ Response::DomainList ], read + end + # # Admin stuff. # diff --git a/src/client/main.cr b/src/client/main.cr index b0fac5f..2dff6d0 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -39,6 +39,7 @@ class Actions @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-domain-list"] = ->user_domain_list end def admin_maintenance @@ -92,6 +93,20 @@ class Actions end end + def user_domain_list + response = @dnsmanagerd.user_domain_list + case response + when DNSManager::Response::DomainList + response.domains.each do |domain| + puts "- #{domain}" + end + else + pp! response + end + rescue e + puts "error for user_zone_del: #{e.message}" + end + end def main diff --git a/src/client/parser.cr b/src/client/parser.cr index 82ff5d0..565f57c 100644 --- a/src/client/parser.cr +++ b/src/client/parser.cr @@ -127,6 +127,13 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth) parser.banner = "COMMAND: user zone del 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" + parser.banner = "COMMAND: user zone list" + unrecognized_args_to_context_args.call parser, 0, nil + end end end diff --git a/src/requests/zone.cr b/src/requests/zone.cr index 8e6235b..23e6f2b 100644 --- a/src/requests/zone.cr +++ b/src/requests/zone.cr @@ -30,4 +30,16 @@ class DNSManager::Request end end DNSManager.requests << DeleteZone + + IPC::JSON.message UserDomains, 12 do + def initialize() + 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.user_domains user.uid + end + end + DNSManager.requests << UserDomains end diff --git a/src/responses/zone.cr b/src/responses/zone.cr index 927ca7a..5e27305 100644 --- a/src/responses/zone.cr +++ b/src/responses/zone.cr @@ -14,5 +14,12 @@ class DNSManager::Response end end DNSManager.responses << DomainChanged + + IPC::JSON.message DomainList, 12 do + property domains : Array(String) + def initialize(@domains) + end + end + DNSManager.responses << DomainList end diff --git a/src/storage.cr b/src/storage.cr index b9e3b3e..464f6ed 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -121,6 +121,19 @@ class DNSManager::Storage Response::Error.new "error while deleting the domain #{domain}" end + def user_domains(user_id : Int32) : 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 list its domains" + return Response::UnknownUser.new + end + + Response::DomainList.new user_data.domains + rescue e + Baguette::Log.error "while listing all user #{user_id} domains: #{e}" + Response::Error.new "error while listing domains" + end end require "./storage/*"