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