annotate src/lib/User.luan @ 3:2c63b10781e1

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