Mercurial Hosting > freedit
annotate src/lib/User.luan @ 55:c57b84f461ae
login and registration work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 28 Nov 2022 20:33:47 -0700 |
parents | 260abd8f8565 |
children | 7ce54f6d93f2 |
rev | line source |
---|---|
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() | |
43 | 5 local set_local_only = Luan.set_local_only or error() |
6 local get_local_only = Luan.get_local_only or error() | |
3 | 7 local String = require "luan:String.luan" |
8 local sub_string = String.sub or error() | |
9 local Table = require "luan:Table.luan" | |
10 local concat = Table.concat or error() | |
11 local Math = require "luan:Math.luan" | |
12 local random = Math.random or error() | |
13 local Time = require "luan:Time.luan" | |
14 local time_now = Time.now or error() | |
15 local Html = require "luan:Html.luan" | |
16 local html_encode = Html.encode or error() | |
17 local Lucene = require "luan:lucene/Lucene.luan" | |
18 local lucene_quote = Lucene.quote or error() | |
19 local Http = require "luan:http/Http.luan" | |
20 local Db = require "site:/lib/Db.luan" | |
55
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
21 local run_in_transaction = Db.run_in_transaction or error() |
3 | 22 |
23 | |
24 local User = {} | |
25 | |
43 | 26 local users_by_name = {} |
27 | |
3 | 28 local function from_doc(doc) |
29 doc.type == "user" or error "wrong type" | |
43 | 30 local user = User.new { |
3 | 31 id = doc.id |
32 email = doc.user_email | |
33 password = doc.password | |
34 name = doc.user_name | |
35 created = doc.created | |
36 } | |
43 | 37 set_local_only(users_by_name,user.name,user) |
38 return user | |
3 | 39 end |
40 | |
41 local function to_doc(user) | |
42 local email = user.email | |
43 return { | |
44 type = "user" | |
45 id = user.id | |
46 user_email = email | |
47 password = user.password | |
48 user_name = user.name | |
49 created = user.created or time_now() | |
50 } | |
51 end | |
52 | |
53 local metatable = {} | |
54 function metatable.__index(user,key) | |
55 if key == "name_html" then | |
56 user.name_html = html_encode(user.name) | |
57 return user.name_html | |
58 end | |
59 return nil | |
60 end | |
61 | |
62 function User.new(user) | |
63 | |
64 function user.save() | |
65 local doc = to_doc(user) | |
66 Db.save(doc) | |
67 user.id = doc.id | |
68 end | |
69 | |
54 | 70 function user.reload() |
71 return User.get_by_id(user.id) or error(user.id) | |
72 end | |
73 | |
74 function user.login() | |
75 Http.response.set_persistent_cookie("user",user.name) | |
76 Http.response.set_persistent_cookie("password",user.password) | |
77 Http.request.cookies.user = user.name or error() | |
78 Http.request.cookies.password = user.password or error() | |
79 end | |
80 | |
3 | 81 set_metatable(user,metatable) |
82 return user | |
83 end | |
84 | |
54 | 85 function User.get_by_id(id) |
86 local doc = Db.get_document("id:"..id) | |
87 return doc and from_doc(doc) | |
88 end | |
89 | |
3 | 90 function User.get_by_email(email) |
91 local doc = Db.get_document("user_email:"..lucene_quote(email)) | |
92 return doc and from_doc(doc) | |
93 end | |
94 | |
95 local function get_by_name(name) | |
43 | 96 local user = get_local_only(users_by_name,name) |
97 if user ~= nil then return user end | |
3 | 98 local doc = Db.get_document("user_name:"..lucene_quote(name)) |
99 return doc and from_doc(doc) | |
100 end | |
101 User.get_by_name = get_by_name | |
102 | |
103 function User.current() | |
104 local name = Http.request.cookies.user | |
105 local password = Http.request.cookies.password | |
106 if name == nil or password == nil then | |
107 return nil | |
108 end | |
109 local user = get_by_name(name) | |
110 if user == nil or user.password ~= password then | |
111 return nil | |
112 end | |
113 return user | |
114 end | |
115 | |
8 | 116 function User.current_required() |
117 local user = User.current() | |
118 user or Http.response.send_redirect "/login.html" | |
119 return user | |
120 end | |
121 | |
3 | 122 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
123 do | |
124 local t = {} | |
125 for i in range(1,#password_chars) do | |
126 t[#t+1] = sub_string(password_chars,i,i) | |
127 end | |
128 password_chars = t | |
129 end | |
130 | |
131 local function new_password() | |
132 local n = #password_chars | |
133 local t = {} | |
134 for _ in range(1,10) do | |
135 t[#t+1] = password_chars[random(n)] | |
136 end | |
137 return concat(t) | |
138 end | |
139 | |
55
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
140 function User.get_or_create_by_email(email,change_password) |
3 | 141 local user = User.get_by_email(email) |
142 if user == nil then | |
143 user = User.new{ email=email, password=new_password() } | |
144 user.save() | |
55
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
145 elseif change_password then |
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
146 run_in_transaction( function() |
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
147 user = user.reload() |
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
148 user.password = new_password() |
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
149 user.save() |
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
150 end ) |
3 | 151 end |
152 return user | |
153 end | |
154 | |
155 return User |