view src/admin/edit.html.luan @ 12:c560b4e2f056

subdir for repos logs
author Vadim Filimonov <fffilimonov@yandex.ru>
date Thu, 28 Jul 2022 16:16:22 +0300
parents dfc36e7ed22c
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local pairs = Luan.pairs or error()
local ipairs = Luan.ipairs or error()
local stringify = Luan.stringify or error()
local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Shared = require "site:/lib/Shared.luan"
local head = Shared.head or error()
local header = Shared.admin_header or error()
local get_user = Shared.get_user or error()
local text_to_list = Shared.text_to_list or error()
local config = Shared.config or error()
local list_to_set = Shared.list_to_set or error()
local get_raw_config = Shared.get_raw_config or error()
local save_raw_config = Shared.save_raw_config or error()
local admin_return = Shared.admin_return or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "admin/edit.html"


local function response(name,content)
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title>Edit Mercurial Repository</title>
	</head>
	<body>
<%		header() %>
		<div content>
			<h1>Edit Repository <a href="/repo/<%=name%>"><%=name%></a></h1>
			<%=content%>
		</div>
	</body>
</html>
<%
end

local function posted(name)
	local mode = Http.request.parameters.mode or error()
	local users = Http.request.parameters.users or error()
	local admins = Http.request.parameters.admins or error()
	users = text_to_list(users)
	admins = text_to_list(admins)
	if #users == 0 then
		return "Users cannot be empty"
	end
	if #admins == 0 then
		return "Users cannot be empty"
	end
	for _, user in ipairs(users) do
		if config.users[user] == nil then
			return "user "..user.." is not a registered user"
		end
	end
	local user_set = list_to_set(users)
	for _, user in ipairs(admins) do
		if config.users[user] == nil then
			return "admin "..user.." is not a registered user"
		end
		if not user_set[user] then
			return "admin "..user.." is not a user in this repo"
		end
	end
	local raw_config = get_raw_config()
	raw_config.repos[name] = {
		mode = mode
		users = users
		admins = admins
	}
	save_raw_config(raw_config)
	return nil
end

return function()
	local name = Http.request.parameters.repo or error()
	local repo = config.repos[name] or error(name)
	repo.admins[get_user()] or error "not admin"
	Io.stdout = Http.response.text_writer()
	local error_msg = ""
	if Http.request.method == "POST" then
		local error_msg = posted(name)
		if error_msg == nil then
			response(name,[[<p>Repository updated</p>]]..admin_return)
		else
			response(name,[[<p error>]]..error_msg..[[</p>]])
		end
		return
	end
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title>Edit Mercurial Repository</title>
	</head>
	<body>
<%		header() %>
		<div content>
			<h1>Edit Repository <a href="/repo/<%=name%>/"><%=name%></a></h1>
			<form method=post>
				<p>
					<select name=mode>
						<option <%= repo.mode=="public" and "selected" or "" %> >public</option>
						<option <%= repo.mode=="private" and "selected" or "" %> >private</option>
					</select>
					<label prompt>If private then only users access this repo.</label>
				</p>
				<p>
					<label prompt>Users (one per line)</label>
					<textarea name=users>
<%	for user in pairs(repo.users) do %>
<%=user%>
<%	end %>
</textarea>
					<label prompt>Users can push to this repo.</label>
				</p>
				<p>
					<label prompt>Admins (one per line)</label>
					<textarea name=admins>
<%	for user in pairs(repo.admins) do %>
<%=user%>
<%	end %>
</textarea>
					<label prompt>Admins can manage this repo.</label>
				</p>
				<p>
					<input type=submit value="Update Repo">
				</p>
			</form>
		</div>
	</body>
</html>
<%
end