diff src/lib/User.luan @ 3:fc3ee39d7764

login
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 19 Jun 2022 20:47:31 -0600
parents
children be36282b556a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/User.luan	Sun Jun 19 20:47:31 2022 -0600
@@ -0,0 +1,118 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local set_metatable = Luan.set_metatable or error()
+local range = Luan.range or error()
+local String = require "luan:String.luan"
+local sub_string = String.sub or error()
+local Table = require "luan:Table.luan"
+local concat = Table.concat or error()
+local Math = require "luan:Math.luan"
+local random = Math.random or error()
+local Time = require "luan:Time.luan"
+local time_now = Time.now or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Lucene = require "luan:lucene/Lucene.luan"
+local lucene_quote = Lucene.quote or error()
+local Http = require "luan:http/Http.luan"
+local Db = require "site:/lib/Db.luan"
+
+
+local User = {}
+
+local function from_doc(doc)
+	doc.type == "user" or error "wrong type"
+	return User.new {
+		id = doc.id
+		email = doc.user_email
+		password = doc.password
+		name = doc.user_name
+		created = doc.created
+	}
+end
+
+local function to_doc(user)
+	local email = user.email
+	return {
+		type = "user"
+		id = user.id
+		user_email = email
+		password = user.password
+		user_name = user.name
+		created = user.created or time_now()
+	}
+end
+
+local metatable = {}
+function metatable.__index(user,key)
+	if key == "name_html" then
+		user.name_html = html_encode(user.name)
+		return user.name_html
+	end
+	return nil
+end
+
+function User.new(user)
+
+	function user.save()
+		local doc = to_doc(user)
+		Db.save(doc)
+		user.id = doc.id
+	end
+
+	set_metatable(user,metatable)
+	return user
+end
+
+function User.get_by_email(email)
+	local doc = Db.get_document("user_email:"..lucene_quote(email))
+	return doc and from_doc(doc)
+end
+
+local function get_by_name(name)
+	local doc = Db.get_document("user_name:"..lucene_quote(name))
+	return doc and from_doc(doc)
+end
+User.get_by_name = get_by_name
+
+function User.current()
+	local name = Http.request.cookies.user
+	local password = Http.request.cookies.password
+	if name == nil or password == nil then
+		return nil
+	end
+	local user = get_by_name(name)
+	if user == nil or user.password ~= password then
+		return nil
+	end
+	return user
+end
+
+local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+do
+	local t = {}
+	for i in range(1,#password_chars) do
+		t[#t+1] = sub_string(password_chars,i,i)
+	end
+	password_chars = t
+end	
+
+local function new_password()
+	local n = #password_chars
+	local t = {}
+	for _ in range(1,10) do
+		t[#t+1] = password_chars[random(n)]
+	end
+	return concat(t)
+end
+
+function User.get_or_create_by_email(email)
+	local user = User.get_by_email(email)
+	if user == nil then
+		user = User.new{ email=email, password=new_password() }
+		user.save()
+	end
+	return user
+end
+
+return User