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