view src/luan/host/Util.luan @ 1611:f67f972bd648

make postgres.luan optional
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 15 May 2021 17:24:07 -0600
parents 04615093b19d
children
line wrap: on
line source
local Luan = require "luan:Luan.luan"
local error = Luan.error
local do_file = Luan.do_file or error()
local ipairs = Luan.ipairs or error()
local stringify = Luan.stringify or error()
local Package = require "luan:Package.luan"
local Io = require "luan:Io.luan"
local String = require "luan:String.luan"
local lower = String.lower or error()
local format = String.format or error()
local to_binary = String.to_binary or error()
local Binary = require "luan:Binary.luan"
local bytes = Binary.byte or error()
local base64_encode = Binary.base64_encode or error()
local digest_message = Binary.digest_message or error()
local Hosted = require "luan:host/Hosted.luan"
local Sql = require "luan:sql/Sql.luan"
local database = Sql.database or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "Util"


local Util = {}

function Util.read_password(domain)
	domain = lower(domain)
	return do_file(Hosted.sites_dir..domain.."/info.luan").password or error()
end

function Util.set_password(domain,password)
	local dir = Hosted.sites_dir..lower(domain)
	local file = Io.schemes.file(dir.."/info.luan")
	file.delete()
	file.write_text("return "..stringify{password=password}.."\n")
end

local pg_admin = Package.load("file:postgres.luan")

function Util.set_postgres_password(domain,password)
	if pg_admin == false then
		return
	end
	local db = database(pg_admin)
	local exists = db.query("select rolname from pg_roles where rolname=?",domain).results() ~= nil;
	--logger.info("exists "..exists)
	if exists then
		db.update( [[alter role "]]..domain..[[" with encrypted password ']]..password..[[']] )
	end
	db.close()
end

function Util.check_postgres_password(domain,password)
	if pg_admin == false then
		return
	end
	local db = database(pg_admin)
	local exists = db.query("select rolname from pg_roles where rolname=?",domain).results() ~= nil;
	db.close()
	if exists then
		db = database{
			class = "org.postgresql.Driver"
			url = "jdbc:postgresql://localhost:5432/"..domain
			user = domain
			password = password
		}
		db.close()
	end
end

return Util