annotate src/lib/User.luan @ 58:31c895b73bd0

improve change email
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Nov 2022 16:09:17 -0700
parents 169ac5fdb320
children 02d8876dc41d
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()
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
4 local type = Luan.type or error()
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
5 local to_string = Luan.to_string or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local range = Luan.range or error()
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
7 local set_local_only = Luan.set_local_only or error()
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
8 local get_local_only = Luan.get_local_only or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local String = require "luan:String.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local sub_string = String.sub or error()
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
11 local to_number = String.to_number or error()
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
12 local regex = String.regex or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Table = require "luan:Table.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local concat = Table.concat or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Math = require "luan:Math.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local random = Math.random or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Time = require "luan:Time.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local time_now = Time.now or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local Html = require "luan:Html.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local html_encode = Html.encode or error()
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
21 local Number = require "luan:Number.luan"
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
22 local long = Number.long or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local lucene_quote = Lucene.quote or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local Db = require "site:/lib/Db.luan"
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
27 local run_in_transaction = Db.run_in_transaction or error()
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 local User = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
32 local users_by_id = {}
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
33
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local function from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 doc.type == "user" or error "wrong type"
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
36 local user = User.new {
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 email = doc.user_email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 password = doc.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 name = doc.user_name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 created = doc.created
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
42 hidden_password_1 = doc.hidden_password_1
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
43 hidden_password_2 = doc.hidden_password_2
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
45 set_local_only(users_by_id,user.id,user)
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
46 return user
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 local function to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 local email = user.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 return {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 type = "user"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 id = user.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 user_email = email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 password = user.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 user_name = user.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 created = user.created or time_now()
58
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
58 hidden_password_1 = user.hidden_password_1
31c895b73bd0 improve change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
59 hidden_password_2 = user.hidden_password_2
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
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 local metatable = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 function metatable.__index(user,key)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 if key == "name_html" then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 user.name_html = html_encode(user.name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 return user.name_html
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 return nil
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 function User.new(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 function user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 local doc = to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 Db.save(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 user.id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
80 function user.reload()
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
81 return User.get_by_id(user.id) or error(user.id)
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
82 end
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
83
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
84 function user.login()
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
85 local id = to_string(user.id)
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
86 Http.response.set_persistent_cookie("user",id)
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
87 Http.response.set_persistent_cookie("password",user.password)
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
88 Http.request.cookies.user = id
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
89 Http.request.cookies.password = user.password or error()
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
90 end
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
91
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 set_metatable(user,metatable)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
96 local function get_by_id(id)
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
97 if type(id) == "string" then
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
98 id = to_number(id)
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
99 if id == nil then return nil end
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
100 end
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
101 id = long(id)
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
102 if not Db.is_in_transaction() then
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
103 local user = get_local_only(users_by_id,id)
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
104 if user ~= nil then return user end
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
105 end
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
106 local doc = Db.get_document("id:"..id)
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
107 return doc and from_doc(doc)
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
108 end
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
109 User.get_by_id = get_by_id
54
260abd8f8565 login and register
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
110
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 function User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 local doc = Db.get_document("user_email:"..lucene_quote(email))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 return doc and from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
116 function User.get_by_name(name)
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 local doc = Db.get_document("user_name:"..lucene_quote(name))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 return doc and from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 function User.current()
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
122 local id = Http.request.cookies.user
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 local password = Http.request.cookies.password
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
124 if id == nil or password == nil then
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 end
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
127 local user = get_by_id(id)
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 if user == nil or user.password ~= password then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
134 function User.current_required()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
135 local user = User.current()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
136 user or Http.response.send_redirect "/login.html"
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
137 return user
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
138 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
139
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 local t = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 for i in range(1,#password_chars) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 t[#t+1] = sub_string(password_chars,i,i)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 password_chars = t
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 local function new_password()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 local n = #password_chars
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 local t = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 for _ in range(1,10) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 t[#t+1] = password_chars[random(n)]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 return concat(t)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 end
57
169ac5fdb320 add change email
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
157 User.new_password = new_password
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
159 function User.get_or_create_by_email(email,change_password)
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 local user = User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 if user == nil then
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
162 run_in_transaction( function()
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
163 user = User.new{ email=email, password=new_password() }
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
164 user.save()
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
165 end )
55
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
166 elseif change_password then
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
167 run_in_transaction( function()
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
168 user = user.reload()
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
169 user.password = new_password()
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
170 user.save()
c57b84f461ae login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
171 end )
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175
56
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
176 User.name_regex = regex "^[a-zA-Z0-9_-]+$"
7ce54f6d93f2 add change name
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
177
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 return User