annotate src/lib/User.luan @ 54:260abd8f8565

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