3
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local set_metatable = Luan.set_metatable or error()
|
|
4 local range = Luan.range or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local sub_string = String.sub or error()
|
|
7 local Table = require "luan:Table.luan"
|
|
8 local concat = Table.concat or error()
|
|
9 local Math = require "luan:Math.luan"
|
|
10 local random = Math.random or error()
|
|
11 local Time = require "luan:Time.luan"
|
|
12 local time_now = Time.now or error()
|
|
13 local Html = require "luan:Html.luan"
|
|
14 local html_encode = Html.encode or error()
|
|
15 local Lucene = require "luan:lucene/Lucene.luan"
|
|
16 local lucene_quote = Lucene.quote or error()
|
|
17 local Http = require "luan:http/Http.luan"
|
|
18 local Db = require "site:/lib/Db.luan"
|
|
19
|
|
20
|
|
21 local User = {}
|
|
22
|
|
23 local function from_doc(doc)
|
|
24 doc.type == "user" or error "wrong type"
|
|
25 return User.new {
|
|
26 id = doc.id
|
|
27 email = doc.user_email
|
|
28 password = doc.password
|
|
29 name = doc.user_name
|
|
30 created = doc.created
|
|
31 }
|
|
32 end
|
|
33
|
|
34 local function to_doc(user)
|
|
35 local email = user.email
|
|
36 return {
|
|
37 type = "user"
|
|
38 id = user.id
|
|
39 user_email = email
|
|
40 password = user.password
|
|
41 user_name = user.name
|
|
42 created = user.created or time_now()
|
|
43 }
|
|
44 end
|
|
45
|
|
46 local metatable = {}
|
|
47 function metatable.__index(user,key)
|
|
48 if key == "name_html" then
|
|
49 user.name_html = html_encode(user.name)
|
|
50 return user.name_html
|
|
51 end
|
|
52 return nil
|
|
53 end
|
|
54
|
|
55 function User.new(user)
|
|
56
|
|
57 function user.save()
|
|
58 local doc = to_doc(user)
|
|
59 Db.save(doc)
|
|
60 user.id = doc.id
|
|
61 end
|
|
62
|
|
63 set_metatable(user,metatable)
|
|
64 return user
|
|
65 end
|
|
66
|
8
|
67 function User.get_by_id(id)
|
|
68 local doc = Db.get_document("id:"..id)
|
|
69 return doc and User.from_doc(doc)
|
|
70 end
|
|
71
|
3
|
72 function User.get_by_email(email)
|
|
73 local doc = Db.get_document("user_email:"..lucene_quote(email))
|
|
74 return doc and from_doc(doc)
|
|
75 end
|
|
76
|
|
77 local function get_by_name(name)
|
|
78 local doc = Db.get_document("user_name:"..lucene_quote(name))
|
|
79 return doc and from_doc(doc)
|
|
80 end
|
|
81 User.get_by_name = get_by_name
|
|
82
|
|
83 function User.current()
|
|
84 local name = Http.request.cookies.user
|
|
85 local password = Http.request.cookies.password
|
|
86 if name == nil or password == nil then
|
|
87 return nil
|
|
88 end
|
|
89 local user = get_by_name(name)
|
|
90 if user == nil or user.password ~= password then
|
|
91 return nil
|
|
92 end
|
|
93 return user
|
|
94 end
|
|
95
|
8
|
96 function User.current_required()
|
|
97 local user = User.current()
|
|
98 user or Http.response.send_redirect "/login.html"
|
|
99 return user
|
|
100 end
|
|
101
|
3
|
102 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
103 do
|
|
104 local t = {}
|
|
105 for i in range(1,#password_chars) do
|
|
106 t[#t+1] = sub_string(password_chars,i,i)
|
|
107 end
|
|
108 password_chars = t
|
|
109 end
|
|
110
|
|
111 local function new_password()
|
|
112 local n = #password_chars
|
|
113 local t = {}
|
|
114 for _ in range(1,10) do
|
|
115 t[#t+1] = password_chars[random(n)]
|
|
116 end
|
|
117 return concat(t)
|
|
118 end
|
|
119
|
|
120 function User.get_or_create_by_email(email)
|
|
121 local user = User.get_by_email(email)
|
|
122 if user == nil then
|
|
123 user = User.new{ email=email, password=new_password() }
|
|
124 user.save()
|
|
125 end
|
|
126 return user
|
|
127 end
|
|
128
|
|
129 return User
|