-
Notifications
You must be signed in to change notification settings - Fork 41
/
web_permissions.lua
371 lines (270 loc) · 10.6 KB
/
web_permissions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
-- web_permissions.lua
-- Implements the Permissions tab in the webadmin
--- Maximum number of permissions displayed within a group's row.
-- If there are more permissions than this, a triple-dot is displayed at the end of the list
local MAX_PERMISSIONS = 10
local ins = table.insert
local con = table.concat
--- Returns the HTML for a single group's row in the listing table
local function GetGroupRow(a_GroupName)
-- Check params:
assert(type(a_GroupName) == "string")
-- First column: group name:
local Row = {"<tr><td valign='top'>"}
ins(Row, cWebAdmin:GetHTMLEscapedString(a_GroupName))
ins(Row, "</td><td valign='top'>")
-- Second column: permissions:
local Permissions = cRankManager:GetGroupPermissions(a_GroupName)
table.sort(Permissions)
local NumPermissions = #Permissions
if (NumPermissions <= MAX_PERMISSIONS) then
ins(Row, con(Permissions, "<br/>"))
else
ins(Row, con(Permissions, "<br/>", 1, MAX_PERMISSIONS))
ins(Row, "<br/>...")
end
ins(Row, "</td><td valign='top'>")
-- Third column: restrictions:
local Restrictions = cRankManager:GetGroupRestrictions(a_GroupName)
table.sort(Restrictions)
local NumRestrictions = #Restrictions
if (NumRestrictions <= MAX_PERMISSIONS) then
ins(Row, con(Restrictions, "<br/>"))
else
ins(Row, con(Restrictions, "<br/>", 1, MAX_PERMISSIONS))
ins(Row, "<br/>...")
end
ins(Row, "</td><td width='1px' valign='top'>")
-- Fourth column: operations:
ins(Row, "<form>")
ins(Row, GetFormButton("edit", "Edit", {GroupName = a_GroupName}))
ins(Row, "</form></td><td width='1px' valign='top'><form>")
ins(Row, GetFormButton("confirmdelgroup", "Delete", {GroupName = a_GroupName}))
ins(Row, "</form></td></tr>")
return con(Row)
end
--- Displays the main Permissions page, listing the permission groups and their permissions
local function ShowMainPermissionsPage(a_Request)
local Page = {"<h4>Create a new group</h4>"}
-- Add the header for adding a new group:
ins(Page, "<form method='POST'><table><tr><td>Group name:</td><td width='1px'><input type='text' name='GroupName'/></td><td width='1px'>")
ins(Page, GetFormButton("addgroup", "Create a new group", {}))
ins(Page, "</td><td width='50%'></td></tr></table></form><br/><br/>")
-- Display a table showing all groups currently known:
ins(Page, "<h4>Groups</h4><table><tr><th>Group name</th><th>Permissions</th><th>Restrictions</th><th colspan=2>Actions</th></tr>")
local AllGroups = cRankManager:GetAllGroups()
table.sort(AllGroups)
for _, group in ipairs(AllGroups) do
ins(Page, GetGroupRow(group))
end
ins(Page, "</table>")
return con(Page)
end
--- Handles the AddGroup form in the main page
-- Adds the group and redirects the user back to the group list
local function ShowAddGroupPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
if (GroupName == nil) then
return HTMLError("Bad request, missing parameters.")
end
-- Add the group:
cRankManager:AddGroup(TrimString(GroupName))
-- Redirect the user:
return "<p>Group created. <a href='/" .. a_Request.Path .. "'>Return to group list</a>.</p>"
end
--- Handles the AddPermission form in the Edit group page
-- Adds the permission to the group and redirects the user back to the permission list
local function ShowAddPermissionPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
local Permission = a_Request.PostParams["Permission"]
if ((GroupName == nil) or (Permission == nil)) then
return HTMLError("Bad request, missing parameters.")
end
-- Add the permission:
cRankManager:AddPermissionToGroup(TrimString(Permission), GroupName)
-- Redirect the user:
return
"<p>Permission added. <a href='/" ..
a_Request.Path ..
"?subpage=edit&GroupName=" ..
cWebAdmin:GetHTMLEscapedString(GroupName) ..
"'>Return to group list</a>.</p>"
end
--- Handles the AddRestriction form in the Edit group page
-- Adds the restriction to the group and redirects the user back to the permission list
local function ShowAddRestrictionPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
local Restriction = a_Request.PostParams["Restriction"]
if ((GroupName == nil) or (Restriction == nil)) then
return HTMLError("Bad request, missing parameters.")
end
-- Add the permission:
cRankManager:AddRestrictionToGroup(TrimString(Restriction), GroupName)
-- Redirect the user:
return
"<p>Restriction added. <a href='/" ..
a_Request.Path ..
"?subpage=edit&GroupName=" ..
cWebAdmin:GetHTMLEscapedString(GroupName) ..
"'>Return to group details</a>.</p>"
end
--- Shows a confirmation page for deleting a group
local function ShowConfirmDelGroupPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
if (GroupName == nil) then
return HTMLError("Bad request, missing parameters.")
end
-- Show the confirmation request:
local Page =
{
"<h4>Delete group</h4><p>Are you sure you want to delete group ",
GroupName,
"? It will be removed from all the ranks that are using it.</p>",
"<form method='POST'>",
GetFormButton("delgroup", "Delete the group", {GroupName = GroupName}),
"</form><form method='GET'>",
GetFormButton("", "Do NOT delete", {}),
"</form>"
}
return con(Page)
end
--- Handles the DelGroup button in the ConfirmDelGroup page
-- Removes the group and redirects the user back to the group list
local function ShowDelGroupPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
if (GroupName == nil) then
return HTMLError("Bad request, missing parameters.")
end
-- Remove the group:
cRankManager:RemoveGroup(GroupName)
-- Redirect the user:
return
"<p>Group removed. <a href='/" ..
a_Request.Path ..
"'>Return to group list</a>.</p>"
end
-- Handles the DelPermission form in the Edit permissions page
-- Removes the permission from the group and redirects the user back to the permission list
local function ShowDelPermissionPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
local Permission = a_Request.PostParams["Permission"]
if ((GroupName == nil) or (Permission == nil)) then
return HTMLError("Bad request, missing parameters.")
end
-- Add the permission:
cRankManager:RemovePermissionFromGroup(Permission, GroupName)
-- Redirect the user:
return
"<p>Permission removed. <a href='/" ..
a_Request.Path ..
"?subpage=edit&GroupName=" ..
cWebAdmin:GetHTMLEscapedString(GroupName) ..
"'>Return to group list</a>.</p>"
end
-- Handles the DelRestriction form in the Edit group page
-- Removes the restriction from the group and redirects the user back to the Edit group page
local function ShowDelRestrictionPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
local Restriction = a_Request.PostParams["Restriction"]
if ((GroupName == nil) or (Restriction == nil)) then
return HTMLError("Bad request, missing parameters.")
end
-- Add the permission:
cRankManager:RemoveRestrictionFromGroup(Restriction, GroupName)
-- Redirect the user:
return
"<p>Restriction removed. <a href='/" ..
a_Request.Path ..
"?subpage=edit&GroupName=" ..
cWebAdmin:GetHTMLEscapedString(GroupName) ..
"'>Return to group</a>.</p>"
end
--- Displays the Edit Group page for a single group, allowing the admin to edit permissions and restrictions
local function ShowEditGroupPage(a_Request)
-- Check params:
local GroupName = a_Request.PostParams["GroupName"]
if (GroupName == nil) then
return HTMLError("Bad request, missing parameters.")
end
-- Add the header for adding permissions:
local Page = {[[
<p><a href='/]] .. a_Request.Path .. [['>Return to the group list</a>.</p>
<table><tr><td width='50%' valign='top'>
<h4>Add a permission</h4>
<form method='POST'><table><tr><td>Permission</td><td width='1px'><input type='text' size='40' name='Permission'/></td><td width='1px'>
]]}
ins(Page, GetFormButton("addpermission", "Add permission", {GroupName = GroupName}))
ins(Page, "</td></tr></table></form><br/><br/>")
-- Add the permission list:
local Permissions = cRankManager:GetGroupPermissions(GroupName)
table.sort(Permissions)
ins(Page, "<h4>Group permissions</h4><table>")
for _, permission in ipairs(Permissions) do
ins(Page, "<tr><td>")
ins(Page, cWebAdmin:GetHTMLEscapedString(permission))
ins(Page, "</td><td><form method='POST'>")
ins(Page, GetFormButton("delpermission", "Remove permission", {GroupName = GroupName, Permission = permission}))
ins(Page, "</form></td></tr>")
end
ins(Page, "</table></td><td width='50%' valign='top'>")
-- Add the header for adding restrictions:
ins(Page, [[
<h4>Add a restriction</h4>
<form method='POST'><table><tr><td>Restriction</td><td width='1px'><input type='text' size='40' name='Restriction'/></td><td width='1px'>
]])
ins(Page, GetFormButton("addrestriction", "Add restriction", {GroupName = GroupName}))
ins(Page, "</td></tr></table></form><br/><br/>")
-- Add the restriction list:
local Restrictions = cRankManager:GetGroupRestrictions(GroupName)
table.sort(Restrictions)
ins(Page, "<h4>Group restrictions</h4><table>")
for _, restriction in ipairs(Restrictions) do
ins(Page, "<tr><td>")
ins(Page, cWebAdmin:GetHTMLEscapedString(restriction))
ins(Page, "</td><td><form method='POST'>")
ins(Page, GetFormButton("delrestriction", "Remove restriction", {GroupName = GroupName, Restriction = restriction}))
ins(Page, "</form></td></tr>")
end
ins(Page, "</table></td></tr></table>")
return con(Page)
end
--- Handlers for the individual subpages in this tab
-- Each item maps a subpage name to a handler function that receives a HTTPRequest object and returns the HTML to return
local g_SubpageHandlers =
{
[""] = ShowMainPermissionsPage,
["addgroup"] = ShowAddGroupPage,
["addpermission"] = ShowAddPermissionPage,
["addrestriction"] = ShowAddRestrictionPage,
["confirmdelgroup"] = ShowConfirmDelGroupPage,
["delgroup"] = ShowDelGroupPage,
["delpermission"] = ShowDelPermissionPage,
["delrestriction"] = ShowDelRestrictionPage,
["edit"] = ShowEditGroupPage,
}
--- Handles the web request coming from MCS
-- Returns the entire tab's HTML contents, based on the player's request
function HandleRequest_Permissions(a_Request)
local Subpage = (a_Request.PostParams["subpage"] or "")
local Handler = g_SubpageHandlers[Subpage]
if (Handler == nil) then
return HTMLError("An internal error has occurred, no handler for subpage " .. Subpage .. ".")
end
local PageContent = Handler(a_Request)
--[[
-- DEBUG: Save content to a file for debugging purposes:
local f = io.open("permissions.html", "wb")
if (f ~= nil) then
f:write(PageContent)
f:close()
end
--]]
return PageContent
end