WIP (cannot compile ATM): add, update and remove zones.
This commit is contained in:
		
							parent
							
								
									a7c2096423
								
							
						
					
					
						commit
						6ae06ae839
					
				
					 4 changed files with 101 additions and 26 deletions
				
			
		| 
						 | 
				
			
			@ -11,21 +11,22 @@ class DNSManager::Request
 | 
			
		|||
		def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event)
 | 
			
		||||
			user = dnsmanagerd.get_logged_user event
 | 
			
		||||
			raise NotLoggedException.new if user.nil?
 | 
			
		||||
 | 
			
		||||
			# TODO: test for zone validity.
 | 
			
		||||
			if errors = zone.get_errors?
 | 
			
		||||
				return DNSManager::Response::InvalidZone.new errors
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			# In case there is no error, retrieve the zone in the DB.
 | 
			
		||||
			#z = dnsmanagerd.storage.zones_by_domain.get? zone.domain
 | 
			
		||||
			#if z
 | 
			
		||||
			#else
 | 
			
		||||
			#	dnsmanagerd.storage.zones << @zone
 | 
			
		||||
			#end
 | 
			
		||||
 | 
			
		||||
			Response::Success.new
 | 
			
		||||
			dnsmanagerd.storage.add_or_update_zone user.uid, zone
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	DNSManager.requests << AddOrUpdateZone
 | 
			
		||||
 | 
			
		||||
	IPC::JSON.message DeleteZone, 11 do
 | 
			
		||||
		property domain : String
 | 
			
		||||
 | 
			
		||||
		def initialize(@domain)
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event)
 | 
			
		||||
			user = dnsmanagerd.get_logged_user event
 | 
			
		||||
			raise NotLoggedException.new if user.nil?
 | 
			
		||||
			dnsmanagerd.storage.delete_domain user.uid, @domain
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	DNSManager.requests << DeleteZone
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										13
									
								
								src/responses/user.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/responses/user.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,13 @@
 | 
			
		|||
class DNSManager::Response
 | 
			
		||||
	IPC::JSON.message UnknownUser, 50 do
 | 
			
		||||
		def initialize
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	DNSManager.responses << UnknownUser
 | 
			
		||||
 | 
			
		||||
	IPC::JSON.message NoOwnership, 51 do
 | 
			
		||||
		def initialize
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	DNSManager.responses << NoOwnership
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -2,10 +2,17 @@
 | 
			
		|||
class DNSManager::Response
 | 
			
		||||
	IPC::JSON.message InvalidZone, 10 do
 | 
			
		||||
		# For now, Error is just an alias on String.
 | 
			
		||||
		property errors : Array(DNSManager::Storage::Zone::Error)
 | 
			
		||||
		property errors : Array(Storage::Zone::Error)
 | 
			
		||||
		def initialize(@errors)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	DNSManager.responses << InvalidZone
 | 
			
		||||
 | 
			
		||||
	# Domain of a zone cannot change, for security reasons.
 | 
			
		||||
	IPC::JSON.message DomainChanged, 11 do
 | 
			
		||||
		def initialize
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	DNSManager.responses << DomainChanged
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -44,21 +44,75 @@ class DNSManager::Storage
 | 
			
		|||
		user_data_by_uid.update_or_create user_data.uid.to_s, user_data
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	def new_domain(user_id : Int32, zone : Zone)
 | 
			
		||||
		user_data = user_data_by_uid.get? user_id.to_s
 | 
			
		||||
		if user_data
 | 
			
		||||
			# store the new zone
 | 
			
		||||
			@zones << zone
 | 
			
		||||
	def add_or_update_zone(user_id : Int32, zone : Zone)
 | 
			
		||||
 | 
			
		||||
			# update user data only after ensuring this zone isn't already existing
 | 
			
		||||
			user_data.domains << zone.domain
 | 
			
		||||
			update_user_data user_data
 | 
			
		||||
		else
 | 
			
		||||
			Baguette::Log.error "trying to add zone #{zone.domain} to unknown user #{user_id}"
 | 
			
		||||
		# Test zone validity.
 | 
			
		||||
		if errors = zone.get_errors?
 | 
			
		||||
			Baguette::Log.warning "zone #{zone.domain} update with errors: #{errors}"
 | 
			
		||||
			return DNSManager::Response::InvalidZone.new errors
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		# 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 add -or update- zone #{zone.domain}"
 | 
			
		||||
			return Response::UnknownUser.new
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		# Does the zone already exist?
 | 
			
		||||
		if z = zones_by_domain.get? zone.domain
 | 
			
		||||
			# User must own the zone.
 | 
			
		||||
			unless user_data.domains.includes? zone.domain
 | 
			
		||||
				Baguette::Log.warning "user #{user_id} doesn't own domain #{zone.domain}"
 | 
			
		||||
				return Response::NoOwnership.new
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			# Domain cannot change (for security reasons).
 | 
			
		||||
			unless z.domain == zone.domain
 | 
			
		||||
				Baguette::Log.warning "user #{user_id} tries to change domain #{z.domain} by #{zone.domain}"
 | 
			
		||||
				return Response::DomainChanged.new
 | 
			
		||||
			end
 | 
			
		||||
		else
 | 
			
		||||
			# Add the domain to the user's domain.
 | 
			
		||||
			user_data.domains << zone.domain
 | 
			
		||||
 | 
			
		||||
			# Actually write data on-disk.
 | 
			
		||||
			update_user_data user_data
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		# Add -or replace- the zone.
 | 
			
		||||
		zones_by_domain.update_or_create zone.domain, zone
 | 
			
		||||
 | 
			
		||||
		Response::Success.new
 | 
			
		||||
	rescue e
 | 
			
		||||
		Baguette::Log.error "trying to add zone #{zone.domain} #{e}"
 | 
			
		||||
		Baguette::Log.error "trying to add -or update- zone #{zone.domain}: #{e}"
 | 
			
		||||
		Response::Error.new "error while updating the domain #{zone.domain}"
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	def delete_domain(user_id : Int32, domain : String)
 | 
			
		||||
		# 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 delete domain #{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 delete domain #{domain} doesn't own it"
 | 
			
		||||
			return Response::NoOwnership.new
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		# TODO: remove this domain from the list of user's domains.
 | 
			
		||||
		# TODO: remove the related zone.
 | 
			
		||||
		#unless errors = Storage::Zone.is_domain_valid? domain
 | 
			
		||||
		#	return Response::InvalidZone.new errors
 | 
			
		||||
		#end
 | 
			
		||||
	rescue e
 | 
			
		||||
		Baguette::Log.error "trying to delete a domain #{domain}: #{e}"
 | 
			
		||||
		Response::Error.new "error while deleting the domain #{domain}"
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
require "./storage/*"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue