comparison src/admin/edit.html.luan @ 0:dfc36e7ed22c

init
author Vadim Filimonov <fffilimonov@yandex.ru>
date Thu, 12 May 2022 13:51:59 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfc36e7ed22c
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local pairs = Luan.pairs or error()
4 local ipairs = Luan.ipairs or error()
5 local stringify = Luan.stringify or error()
6 local Io = require "luan:Io.luan"
7 local Http = require "luan:http/Http.luan"
8 local Shared = require "site:/lib/Shared.luan"
9 local head = Shared.head or error()
10 local header = Shared.admin_header or error()
11 local get_user = Shared.get_user or error()
12 local text_to_list = Shared.text_to_list or error()
13 local config = Shared.config or error()
14 local list_to_set = Shared.list_to_set or error()
15 local get_raw_config = Shared.get_raw_config or error()
16 local save_raw_config = Shared.save_raw_config or error()
17 local admin_return = Shared.admin_return or error()
18 local Logging = require "luan:logging/Logging.luan"
19 local logger = Logging.logger "admin/edit.html"
20
21
22 local function response(name,content)
23 %>
24 <!doctype html>
25 <html>
26 <head>
27 <% head() %>
28 <title>Edit Mercurial Repository</title>
29 </head>
30 <body>
31 <% header() %>
32 <div content>
33 <h1>Edit Repository <a href="/repo/<%=name%>"><%=name%></a></h1>
34 <%=content%>
35 </div>
36 </body>
37 </html>
38 <%
39 end
40
41 local function posted(name)
42 local mode = Http.request.parameters.mode or error()
43 local users = Http.request.parameters.users or error()
44 local admins = Http.request.parameters.admins or error()
45 users = text_to_list(users)
46 admins = text_to_list(admins)
47 if #users == 0 then
48 return "Users cannot be empty"
49 end
50 if #admins == 0 then
51 return "Users cannot be empty"
52 end
53 for _, user in ipairs(users) do
54 if config.users[user] == nil then
55 return "user "..user.." is not a registered user"
56 end
57 end
58 local user_set = list_to_set(users)
59 for _, user in ipairs(admins) do
60 if config.users[user] == nil then
61 return "admin "..user.." is not a registered user"
62 end
63 if not user_set[user] then
64 return "admin "..user.." is not a user in this repo"
65 end
66 end
67 local raw_config = get_raw_config()
68 raw_config.repos[name] = {
69 mode = mode
70 users = users
71 admins = admins
72 }
73 save_raw_config(raw_config)
74 return nil
75 end
76
77 return function()
78 local name = Http.request.parameters.repo or error()
79 local repo = config.repos[name] or error(name)
80 repo.admins[get_user()] or error "not admin"
81 Io.stdout = Http.response.text_writer()
82 local error_msg = ""
83 if Http.request.method == "POST" then
84 local error_msg = posted(name)
85 if error_msg == nil then
86 response(name,[[<p>Repository updated</p>]]..admin_return)
87 else
88 response(name,[[<p error>]]..error_msg..[[</p>]])
89 end
90 return
91 end
92 %>
93 <!doctype html>
94 <html>
95 <head>
96 <% head() %>
97 <title>Edit Mercurial Repository</title>
98 </head>
99 <body>
100 <% header() %>
101 <div content>
102 <h1>Edit Repository <a href="/repo/<%=name%>/"><%=name%></a></h1>
103 <form method=post>
104 <p>
105 <select name=mode>
106 <option <%= repo.mode=="public" and "selected" or "" %> >public</option>
107 <option <%= repo.mode=="private" and "selected" or "" %> >private</option>
108 </select>
109 <label prompt>If private then only users access this repo.</label>
110 </p>
111 <p>
112 <label prompt>Users (one per line)</label>
113 <textarea name=users>
114 <% for user in pairs(repo.users) do %>
115 <%=user%>
116 <% end %>
117 </textarea>
118 <label prompt>Users can push to this repo.</label>
119 </p>
120 <p>
121 <label prompt>Admins (one per line)</label>
122 <textarea name=admins>
123 <% for user in pairs(repo.admins) do %>
124 <%=user%>
125 <% end %>
126 </textarea>
127 <label prompt>Admins can manage this repo.</label>
128 </p>
129 <p>
130 <input type=submit value="Update Repo">
131 </p>
132 </form>
133 </div>
134 </body>
135 </html>
136 <%
137 end