2
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
3
|
4 local range = Luan.range or error()
|
2
|
5 local to_string = Luan.to_string or error()
|
|
6 local get_local_only = Luan.get_local_only or error()
|
|
7 local set_local_only = Luan.set_local_only or error()
|
3
|
8 local String = require "luan:String.luan"
|
|
9 local sub_string = String.sub or error()
|
|
10 local Math = require "luan:Math.luan"
|
|
11 local random = Math.random or error()
|
|
12 local Table = require "luan:Table.luan"
|
|
13 local concat = Table.concat or error()
|
2
|
14 local Lucene = require "luan:lucene/Lucene.luan"
|
|
15 local lucene_quote = Lucene.quote or error()
|
|
16 local Http = require "luan:http/Http.luan"
|
|
17 local Db = require "site:/lib/Db.luan"
|
3
|
18 local run_in_transaction = Db.run_in_transaction or error()
|
4
|
19 local Chat = require "site:/lib/Chat.luan"
|
2
|
20 local Logging = require "luan:logging/Logging.luan"
|
|
21 local logger = Logging.logger "User"
|
|
22
|
|
23
|
|
24 local User = {}
|
|
25
|
|
26 local function from_doc(doc)
|
|
27 doc.type == "user" or error "wrong type"
|
|
28 return User.new {
|
|
29 id = doc.id
|
|
30 email = doc.user_email
|
|
31 password = doc.password
|
|
32 }
|
|
33 end
|
|
34
|
|
35 local function to_doc(user)
|
|
36 return {
|
|
37 type = "user"
|
|
38 id = user.id
|
|
39 user_email = user.email
|
|
40 password = user.password
|
|
41 }
|
|
42 end
|
|
43
|
|
44 function User.new(user)
|
|
45
|
|
46 function user.save()
|
|
47 local doc = to_doc(user)
|
|
48 Db.save(doc)
|
|
49 user.id = doc.id
|
|
50 end
|
|
51
|
4
|
52 function user.delete()
|
|
53 run_in_transaction( function()
|
|
54 local id = user.id
|
|
55 local chats = Chat.search("chat_user_ids:"..id)
|
|
56 for _, chat in ipairs(chats) do
|
|
57 chat.delete()
|
|
58 end
|
|
59 Db.delete("id:"..id)
|
|
60 end )
|
2
|
61 end
|
|
62
|
|
63 function user.login()
|
|
64 local id = to_string(user.id)
|
|
65 Http.response.set_persistent_cookie("user",id)
|
|
66 Http.response.set_persistent_cookie("password",user.password)
|
|
67 Http.request.cookies.user = id
|
|
68 Http.request.cookies.password = user.password or error()
|
|
69 end
|
|
70
|
|
71 return user
|
|
72 end
|
|
73
|
|
74 local function get_by_id(id)
|
|
75 local doc = Db.get_document("id:"..id)
|
|
76 return doc and doc.type=="user" and from_doc(doc) or nil
|
|
77 end
|
|
78 User.get_by_id = get_by_id
|
|
79
|
3
|
80 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
81 do
|
|
82 local t = {}
|
|
83 for i in range(1,#password_chars) do
|
|
84 t[#t+1] = sub_string(password_chars,i,i)
|
|
85 end
|
|
86 password_chars = t
|
|
87 end
|
|
88
|
|
89 local function new_password()
|
|
90 local n = #password_chars
|
|
91 local t = {}
|
|
92 for _ in range(1,10) do
|
|
93 t[#t+1] = password_chars[random(n)]
|
|
94 end
|
|
95 return concat(t)
|
|
96 end
|
|
97
|
4
|
98 local function get_by_email(email)
|
|
99 local doc = Db.get_document("user_email:"..lucene_quote(email))
|
|
100 return doc and from_doc(doc)
|
|
101 end
|
|
102 User.get_by_email = get_by_email
|
|
103
|
3
|
104 function User.get_or_create_by_email(email)
|
|
105 return run_in_transaction( function()
|
4
|
106 local user = get_by_email(email)
|
|
107 if user == nil then
|
|
108 user = User.new{
|
3
|
109 email = email
|
|
110 password = new_password()
|
|
111 }
|
|
112 user.save()
|
|
113 end
|
4
|
114 return user
|
3
|
115 end )
|
2
|
116 end
|
|
117
|
|
118 function User.search(query,sort,rows)
|
|
119 rows = rows or 1000000
|
|
120 local users = {}
|
|
121 local docs = Db.search(query,1,rows,{sort=sort})
|
|
122 for _, doc in ipairs(docs) do
|
|
123 users[#users+1] = from_doc(doc)
|
|
124 end
|
|
125 return users
|
|
126 end
|
|
127
|
3
|
128 local function current()
|
2
|
129 local user = get_local_only(User,"current")
|
|
130 if user == nil then
|
|
131 local id = Http.request.cookies.user
|
|
132 local password = Http.request.cookies.password
|
|
133 if id == nil or password == nil then
|
|
134 user = "nil"
|
|
135 else
|
|
136 user = get_by_id(id)
|
3
|
137 if user == nil or user.password ~= password then
|
2
|
138 user = "nil"
|
|
139 end
|
|
140 end
|
|
141 set_local_only(User,"current",user)
|
|
142 end
|
|
143 return user ~= "nil" and user or nil
|
|
144 end
|
3
|
145 User.current = current
|
2
|
146
|
|
147 function User.current_required()
|
3
|
148 local user = current()
|
2
|
149 user or Http.response.send_redirect "/login.html"
|
|
150 return user
|
|
151 end
|
|
152
|
|
153 return User
|