Mercurial Hosting > freedit
annotate 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 |
| 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 | |
| 42 } | |
| 56 | 43 set_local_only(users_by_id,user.id,user) |
| 43 | 44 return user |
| 3 | 45 end |
| 46 | |
| 47 local function to_doc(user) | |
| 48 local email = user.email | |
| 49 return { | |
| 50 type = "user" | |
| 51 id = user.id | |
| 52 user_email = email | |
| 53 password = user.password | |
| 54 user_name = user.name | |
| 55 created = user.created or time_now() | |
| 56 } | |
| 57 end | |
| 58 | |
| 59 local metatable = {} | |
| 60 function metatable.__index(user,key) | |
| 61 if key == "name_html" then | |
| 62 user.name_html = html_encode(user.name) | |
| 63 return user.name_html | |
| 64 end | |
| 65 return nil | |
| 66 end | |
| 67 | |
| 68 function User.new(user) | |
| 69 | |
| 70 function user.save() | |
| 71 local doc = to_doc(user) | |
| 72 Db.save(doc) | |
| 73 user.id = doc.id | |
| 74 end | |
| 75 | |
| 54 | 76 function user.reload() |
| 77 return User.get_by_id(user.id) or error(user.id) | |
| 78 end | |
| 79 | |
| 80 function user.login() | |
| 56 | 81 local id = to_string(user.id) |
| 82 Http.response.set_persistent_cookie("user",id) | |
| 54 | 83 Http.response.set_persistent_cookie("password",user.password) |
| 56 | 84 Http.request.cookies.user = id |
| 54 | 85 Http.request.cookies.password = user.password or error() |
| 86 end | |
| 87 | |
| 3 | 88 set_metatable(user,metatable) |
| 89 return user | |
| 90 end | |
| 91 | |
| 56 | 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 | |
| 54 | 102 local doc = Db.get_document("id:"..id) |
| 103 return doc and from_doc(doc) | |
| 104 end | |
| 56 | 105 User.get_by_id = get_by_id |
| 54 | 106 |
| 3 | 107 function User.get_by_email(email) |
| 108 local doc = Db.get_document("user_email:"..lucene_quote(email)) | |
| 109 return doc and from_doc(doc) | |
| 110 end | |
| 111 | |
| 56 | 112 function User.get_by_name(name) |
| 3 | 113 local doc = Db.get_document("user_name:"..lucene_quote(name)) |
| 114 return doc and from_doc(doc) | |
| 115 end | |
| 116 | |
| 117 function User.current() | |
| 56 | 118 local id = Http.request.cookies.user |
| 3 | 119 local password = Http.request.cookies.password |
| 56 | 120 if id == nil or password == nil then |
| 3 | 121 return nil |
| 122 end | |
| 56 | 123 local user = get_by_id(id) |
| 3 | 124 if user == nil or user.password ~= password then |
| 125 return nil | |
| 126 end | |
| 127 return user | |
| 128 end | |
| 129 | |
| 8 | 130 function User.current_required() |
| 131 local user = User.current() | |
| 132 user or Http.response.send_redirect "/login.html" | |
| 133 return user | |
| 134 end | |
| 135 | |
| 3 | 136 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 137 do | |
| 138 local t = {} | |
| 139 for i in range(1,#password_chars) do | |
| 140 t[#t+1] = sub_string(password_chars,i,i) | |
| 141 end | |
| 142 password_chars = t | |
| 143 end | |
| 144 | |
| 145 local function new_password() | |
| 146 local n = #password_chars | |
| 147 local t = {} | |
| 148 for _ in range(1,10) do | |
| 149 t[#t+1] = password_chars[random(n)] | |
| 150 end | |
| 151 return concat(t) | |
| 152 end | |
| 153 | |
|
55
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
154 function User.get_or_create_by_email(email,change_password) |
| 3 | 155 local user = User.get_by_email(email) |
| 156 if user == nil then | |
| 56 | 157 run_in_transaction( function() |
| 158 user = User.new{ email=email, password=new_password() } | |
| 159 user.save() | |
| 160 end ) | |
|
55
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
161 elseif change_password then |
|
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
162 run_in_transaction( function() |
|
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
163 user = user.reload() |
|
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
164 user.password = new_password() |
|
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
165 user.save() |
|
c57b84f461ae
login and registration work
Franklin Schmidt <fschmidt@gmail.com>
parents:
54
diff
changeset
|
166 end ) |
| 3 | 167 end |
| 168 return user | |
| 169 end | |
| 170 | |
| 56 | 171 User.name_regex = regex "^[a-zA-Z0-9_-]+$" |
| 172 | |
| 3 | 173 return User |
