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