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
|
|
67 function User.get_by_email(email)
|
|
68 local doc = Db.get_document("user_email:"..lucene_quote(email))
|
|
69 return doc and from_doc(doc)
|
|
70 end
|
|
71
|
|
72 local function get_by_name(name)
|
|
73 local doc = Db.get_document("user_name:"..lucene_quote(name))
|
|
74 return doc and from_doc(doc)
|
|
75 end
|
|
76 User.get_by_name = get_by_name
|
|
77
|
|
78 function User.current()
|
|
79 local name = Http.request.cookies.user
|
|
80 local password = Http.request.cookies.password
|
|
81 if name == nil or password == nil then
|
|
82 return nil
|
|
83 end
|
|
84 local user = get_by_name(name)
|
|
85 if user == nil or user.password ~= password then
|
|
86 return nil
|
|
87 end
|
|
88 return user
|
|
89 end
|
|
90
|
8
|
91 function User.current_required()
|
|
92 local user = User.current()
|
|
93 user or Http.response.send_redirect "/login.html"
|
|
94 return user
|
|
95 end
|
|
96
|
3
|
97 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
98 do
|
|
99 local t = {}
|
|
100 for i in range(1,#password_chars) do
|
|
101 t[#t+1] = sub_string(password_chars,i,i)
|
|
102 end
|
|
103 password_chars = t
|
|
104 end
|
|
105
|
|
106 local function new_password()
|
|
107 local n = #password_chars
|
|
108 local t = {}
|
|
109 for _ in range(1,10) do
|
|
110 t[#t+1] = password_chars[random(n)]
|
|
111 end
|
|
112 return concat(t)
|
|
113 end
|
|
114
|
|
115 function User.get_or_create_by_email(email)
|
|
116 local user = User.get_by_email(email)
|
|
117 if user == nil then
|
|
118 user = User.new{ email=email, password=new_password() }
|
|
119 user.save()
|
|
120 end
|
|
121 return user
|
|
122 end
|
|
123
|
|
124 return User
|