comparison src/lib/User.luan @ 3:fc3ee39d7764

login
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 19 Jun 2022 20:47:31 -0600
parents
children be36282b556a
comparison
equal deleted inserted replaced
2:fc2383eb48a9 3:fc3ee39d7764
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
91 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
92 do
93 local t = {}
94 for i in range(1,#password_chars) do
95 t[#t+1] = sub_string(password_chars,i,i)
96 end
97 password_chars = t
98 end
99
100 local function new_password()
101 local n = #password_chars
102 local t = {}
103 for _ in range(1,10) do
104 t[#t+1] = password_chars[random(n)]
105 end
106 return concat(t)
107 end
108
109 function User.get_or_create_by_email(email)
110 local user = User.get_by_email(email)
111 if user == nil then
112 user = User.new{ email=email, password=new_password() }
113 user.save()
114 end
115 return user
116 end
117
118 return User