3
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local String = require "luan:String.luan"
|
|
4 local trim = String.trim or error()
|
|
5 local Io = require "luan:Io.luan"
|
|
6 local Http = require "luan:http/Http.luan"
|
|
7 local Shared = require "site:/lib/Shared.luan"
|
|
8 local head = Shared.head or error()
|
|
9 local header = Shared.header or error()
|
|
10 local footer = Shared.footer or error()
|
|
11 local Forum = require "site:/lib/Forum.luan"
|
|
12 local forum_title = Forum.title or error()
|
|
13 local User = require "site:/lib/User.luan"
|
56
|
14 local name_regex = User.name_regex
|
3
|
15 local Db = require "site:/lib/Db.luan"
|
|
16 local run_in_transaction = Db.run_in_transaction or error()
|
|
17
|
|
18
|
|
19 local function page(contents)
|
|
20 Io.stdout = Http.response.text_writer()
|
|
21 %>
|
|
22 <!doctype html>
|
|
23 <html>
|
|
24 <head>
|
|
25 <% head() %>
|
56
|
26 <title><%=forum_title%> - Change Name</title>
|
3
|
27 </head>
|
|
28 <body>
|
|
29 <% header() %>
|
|
30 <div content>
|
56
|
31 <h1>Change Name</h1>
|
3
|
32 <%
|
|
33 contents()
|
|
34 %>
|
|
35 </div>
|
|
36 <% footer() %>
|
|
37 </body>
|
|
38 </html>
|
|
39 <%
|
|
40 end
|
|
41
|
|
42 return function()
|
56
|
43 local user = User.current_required()
|
57
|
44 if user==nil then return end
|
56
|
45 local name = user.name or error()
|
54
|
46 local error_message = nil
|
|
47 if Http.request.method == "POST" then
|
56
|
48 name = Http.request.parameters.name or error()
|
3
|
49 name = trim(name)
|
29
|
50 name_regex.matches(name) or error "invalid name"
|
3
|
51 run_in_transaction( function()
|
54
|
52 user = user.reload()
|
3
|
53 if user.name ~= name and User.get_by_name(name) ~= nil then
|
|
54 error_message = "Name already in use"
|
|
55 else
|
|
56 user.name = name
|
|
57 user.save()
|
|
58 end
|
|
59 end )
|
54
|
60 if error_message == nil then
|
3
|
61 page(function()
|
|
62 %>
|
56
|
63 <p>Your name has been changed.</p>
|
3
|
64 <%
|
|
65 end)
|
54
|
66 return
|
3
|
67 end
|
|
68 end
|
54
|
69 page(function()
|
|
70 if error_message ~= nil then %>
|
|
71 <p error>Error: <%= error_message %></p>
|
|
72 <% end %>
|
56
|
73 <form method=post>
|
54
|
74 <label>User name for <%= user.email %></label>
|
|
75 <input type="text" name="name" value="<%= name or "" %>" autofocus required pattern="[a-zA-Z0-9_-]+">
|
56
|
76 <input type="submit">
|
54
|
77 </form>
|
|
78 <%
|
|
79 end)
|
3
|
80 end
|