annotate src/lib/User.luan @ 22:d87a8f9f43d7

tinymce work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 Jul 2022 21:47:55 -0600
parents de0cbf515ef5
children 298c71e0c854
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local set_metatable = Luan.set_metatable or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local range = Luan.range or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local String = require "luan:String.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local sub_string = String.sub or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Table = require "luan:Table.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local concat = Table.concat or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Math = require "luan:Math.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local random = Math.random or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Time = require "luan:Time.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local time_now = Time.now or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Html = require "luan:Html.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local html_encode = Html.encode or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local lucene_quote = Lucene.quote or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Db = require "site:/lib/Db.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local User = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local function from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 doc.type == "user" or error "wrong type"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return User.new {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 email = doc.user_email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 password = doc.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 name = doc.user_name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 created = doc.created
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local function to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 local email = user.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 return {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 type = "user"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 id = user.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 user_email = email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 password = user.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 user_name = user.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 created = user.created or time_now()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 local metatable = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 function metatable.__index(user,key)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 if key == "name_html" then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 user.name_html = html_encode(user.name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 return user.name_html
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 function User.new(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 local doc = to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 Db.save(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 user.id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 set_metatable(user,metatable)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 function User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 local doc = Db.get_document("user_email:"..lucene_quote(email))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 return doc and from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 local function get_by_name(name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 local doc = Db.get_document("user_name:"..lucene_quote(name))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 return doc and from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 User.get_by_name = get_by_name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 function User.current()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 local name = Http.request.cookies.user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 local password = Http.request.cookies.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if name == nil or password == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 local user = get_by_name(name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if user == nil or user.password ~= password then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
91 function User.current_required()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
92 local user = User.current()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
93 user or Http.response.send_redirect "/login.html"
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
94 return user
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
95 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
96
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 local t = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 for i in range(1,#password_chars) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 t[#t+1] = sub_string(password_chars,i,i)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 password_chars = t
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 local function new_password()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 local n = #password_chars
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 local t = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 for _ in range(1,10) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 t[#t+1] = password_chars[random(n)]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 return concat(t)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 function User.get_or_create_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 local user = User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 if user == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 user = User.new{ email=email, password=new_password() }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 return User