forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_permissions.lua
More file actions
285 lines (200 loc) · 7.55 KB
/
web_permissions.lua
File metadata and controls
285 lines (200 loc) · 7.55 KB
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
-- 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>"}
ins(Row, cWebAdmin:GetHTMLEscapedString(a_GroupName))
ins(Row, "</td><td>")
-- 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>")
-- Third column: operations:
ins(Row, "<form>")
ins(Row, GetFormButton("editpermissions", "Edit permissions", {GroupName = a_GroupName}))
ins(Row, "</form><form>")
ins(Row, GetFormButton("confirmdelgroup", "Delete group", {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><th>Group name:</th><td><input type='text' name='GroupName'/></td></tr><tr><th/><td>")
ins(Page, GetFormButton("addgroup", "Create a new group", {}))
ins(Page, "</td></tr></table></form>")
-- Display a table showing all groups currently known:
ins(Page, "<h4>Groups</h4><table><tr><th>Group name</th><th>Permissions</th><th>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(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 permissions 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(Permission, GroupName)
-- Redirect the user:
return
"<p>Permission added. <a href='/" ..
a_Request.Path ..
"?subpage=editpermissions&GroupName=" ..
cWebAdmin:GetHTMLEscapedString(GroupName) ..
"'>Return to group list</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=editpermissions&GroupName=" ..
cWebAdmin:GetHTMLEscapedString(GroupName) ..
"'>Return to group list</a>.</p>"
end
--- Displays the Edit Permissions page for a single group
local function ShowEditPermissionsPage(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>
<h4>Add a permission</h4>
<form method='POST'><table><tr><th>Permission</th><td><input type='text' name='Permission'/></td></tr>
<tr><th/><td>
]]}
ins(Page, GetFormButton("addpermission", "Add permission", {GroupName = GroupName}))
ins(Page, "</td></tr></table></form>")
-- 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>")
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,
["confirmdelgroup"] = ShowConfirmDelGroupPage,
["delgroup"] = ShowDelGroupPage,
["delpermission"] = ShowDelPermissionPage,
["editpermissions"] = ShowEditPermissionsPage,
}
--- 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