annotate src/lib/User.luan @ 46:42b741a1d5c6

add username
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 28 Feb 2025 19:25:12 -0700
parents 818697418dbe
children 7b339b1ccd11
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
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 ipairs = Luan.ipairs or error()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
4 local range = Luan.range or error()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local to_string = Luan.to_string or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local get_local_only = Luan.get_local_only or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local set_local_only = Luan.set_local_only or error()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
8 local String = require "luan:String.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
9 local sub_string = String.sub or error()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
10 local Math = require "luan:Math.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
11 local random = Math.random or error()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
12 local Table = require "luan:Table.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
13 local concat = Table.concat or error()
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
14 local Html = require "luan:Html.luan"
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
15 local html_encode = Html.encode or error()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local lucene_quote = Lucene.quote or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local Db = require "site:/lib/Db.luan"
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
20 local run_in_transaction = Db.run_in_transaction or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
21 local Chat = require "site:/lib/Chat.luan"
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
22 local chat_search = Chat.search or error()
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
23 local Utils = require "site:/lib/Utils.luan"
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
24 local list_to_set = Utils.list_to_set or error()
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
25 local set_to_list = Utils.set_to_list or error()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local Logging = require "luan:logging/Logging.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local logger = Logging.logger "User"
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
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 local function from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 doc.type == "user" or error "wrong type"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return User.new {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 email = doc.user_email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 password = doc.password
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
38 was_notified = doc.was_notified=="true"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
39 notify_email = doc.notify_email
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
40 multi_notify = doc.multi_notify=="true"
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
41 voice_url = doc.voice_url
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
42 name = doc.name
2
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 function to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 type = "user"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 id = user.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 user_email = user.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 password = user.password
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
52 was_notified = user.was_notified and "true" or nil
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
53 notify_email = user.notify_email
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
54 multi_notify = user.multi_notify and "true" or nil
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
55 voice_url = user.voice_url
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
56 name = user.name
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 function User.new(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 function user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 local doc = to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 Db.save(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 user.id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
68 function user.reload()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
69 return User.get_by_id(user.id) or error(user.id)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
70 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
71
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
72 function user.delete()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
73 run_in_transaction( function()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
74 local id = user.id
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
75 local chats = chat_search("chat_user_ids:"..id)
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
76 for _, chat in ipairs(chats) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
77 chat.delete()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
78 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
79 Db.delete("id:"..id)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
80 end )
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 function user.login()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 local id = to_string(user.id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 Http.response.set_persistent_cookie("user",id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 Http.response.set_persistent_cookie("password",user.password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 Http.request.cookies.user = id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 Http.request.cookies.password = user.password or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
91 function user.last_update()
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
92 local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc", 1 )
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
93 local n = #chats
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
94 if n == 0 then
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
95 return 0
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
96 elseif n == 1 then
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
97 return chats[1].updated
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
98 else error() end
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
99 end
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
100
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
101 function user.chatting_with_ids()
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
102 local my_id = user.id
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
103 local user_ids = list_to_set{}
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
104 local chats = chat_search( "chat_user_ids:"..my_id )
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
105 for _, chat in ipairs(chats) do
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
106 for _, user_id in ipairs(chat.user_ids) do
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
107 user_ids[user_id] = true
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
108 end
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
109 end
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
110 user_ids[my_id] = false
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
111 return set_to_list(user_ids)
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
112 end
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
113
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
114 function user.name_html()
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
115 return html_encode(user.name or user.email)
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
116 end
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
117
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 return user
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 local function get_by_id(id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 local doc = Db.get_document("id:"..id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 return doc and doc.type=="user" and from_doc(doc) or nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 User.get_by_id = get_by_id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
127 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
128 do
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
129 local t = {}
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
130 for i in range(1,#password_chars) do
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
131 t[#t+1] = sub_string(password_chars,i,i)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
132 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
133 password_chars = t
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
134 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
135
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
136 local function new_password()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
137 local n = #password_chars
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
138 local t = {}
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
139 for _ in range(1,10) do
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
140 t[#t+1] = password_chars[random(n)]
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
141 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
142 return concat(t)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
143 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
144
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
145 local function get_by_email(email)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
146 local doc = Db.get_document("user_email:"..lucene_quote(email))
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
147 return doc and from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
148 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
149 User.get_by_email = get_by_email
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
150
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
151 function User.get_or_create_by_email(email)
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
152 return run_in_transaction( function()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
153 local user = get_by_email(email)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
154 if user == nil then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
155 user = User.new{
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
156 email = email
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
157 password = new_password()
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
158 notify_email = email
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
159 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
160 user.save()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
161 end
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
162 return user
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
163 end )
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 function User.search(query,sort,rows)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 rows = rows or 1000000
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 local users = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 local docs = Db.search(query,1,rows,{sort=sort})
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 for _, doc in ipairs(docs) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 users[#users+1] = from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 return users
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
176 local function current()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 local user = get_local_only(User,"current")
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 if user == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 local id = Http.request.cookies.user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 local password = Http.request.cookies.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 if id == nil or password == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 user = "nil"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 user = get_by_id(id)
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
185 if user == nil or user.password ~= password then
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 user = "nil"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 set_local_only(User,"current",user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 return user ~= "nil" and user or nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 end
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
193 User.current = current
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 function User.current_required()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
196 local user = current()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 user or Http.response.send_redirect "/login.html"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 return User