From a188d28f1d09d4aac6208b610fc1ed4855fce8fd Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 27 Apr 2024 23:10:01 +0200 Subject: [PATCH] Ownership is now managed. --- src/requests/ownership.cr | 8 ++--- src/storage.cr | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/requests/ownership.cr b/src/requests/ownership.cr index 224ad1d..c6076e0 100644 --- a/src/requests/ownership.cr +++ b/src/requests/ownership.cr @@ -9,7 +9,7 @@ class DNSManager::Request user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user - Response::Error.new "Not implemented." + dnsmanagerd.storage.ask_share_token user.uid, @domain end end DNSManager.requests << AskShareToken @@ -24,7 +24,7 @@ class DNSManager::Request user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user - Response::Error.new "Not implemented." + dnsmanagerd.storage.ask_transfer_token user.uid, @domain end end DNSManager.requests << AskTransferToken @@ -39,7 +39,7 @@ class DNSManager::Request user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user - Response::Error.new "Not implemented." + dnsmanagerd.storage.ask_unshare_domain user.uid, @domain end end DNSManager.requests << AskUnShareDomain @@ -54,7 +54,7 @@ class DNSManager::Request user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user - Response::Error.new "Not implemented." + dnsmanagerd.storage.gain_ownership user.uid, @uuid end end DNSManager.requests << GainOwnership diff --git a/src/storage.cr b/src/storage.cr index 913a2b4..08dd05e 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -155,6 +155,73 @@ class DNSManager::Storage Response::DomainAdded.new domain end + def ask_share_token(user_id : UserDataID, domain_name : String) + user_must_exist! user_id + user_should_own! user_id, domain_name + + domain = @domains_by_name.get domain_name + if domain.share_key.nil? + domain.share_key = UUID.random.to_s + @domains_by_name.update_or_create domain_name, domain + Response::DomainChanged.new domain + else + Response::Error.new "The domain already have a share key." + end + end + + def ask_transfer_token(user_id : UserDataID, domain_name : String) + user_must_exist! user_id + user_should_own! user_id, domain_name + + domain = @domains_by_name.get domain_name + if domain.transfer_key.nil? + domain.transfer_key = UUID.random.to_s + @domains_by_name.update_or_create domain_name, domain + Response::DomainChanged.new domain + else + Response::Error.new "The domain already have a transfer key." + end + end + + # Check the domain owners. + # In case there's only the requesting user, allow him to gain full control. + def ask_unshare_domain(user_id : UserDataID, domain_name : String) + user_must_exist! user_id + user_should_own! user_id, domain_name + + domain = @domains_by_name.get domain_name + if domain.owners.size == 1 && domain.owners[0] == user_id + domain.share_key = nil + @domains_by_name.update_or_create domain_name, domain + Response::DomainChanged.new domain + else + Response::Error.new "You are't the only owner." + end + end + + def gain_ownership(user_id : UserDataID, uuid : String) + user_must_exist! user_id + + if domain = @domains_by_share_key.get uuid + if domain.owners.includes? user_id + return Response::Error.new "You already own this domain." + end + domain.owners << user_id + @domains_by_name.update_or_create domain.name, domain + Response::DomainChanged.new domain + elsif domain = @domains_by_transfer_key.get uuid + if domain.owners.includes? user_id + return Response::Error.new "You already own this domain." + end + domain.transfer_key = nil + domain.owners = [user_id] + @domains_by_name.update_or_create domain.name, domain + Response::DomainChanged.new domain + else + Response::Error.new "There is no key with this UUID." + end + end + def add_or_update_zone(user_id : UserDataID, zone : Zone) : IPC::JSON user_must_exist! user_id