view src/lib/User.luan @ 54:260abd8f8565

login and register
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 27 Nov 2022 23:46:27 -0700
parents 298c71e0c854
children c57b84f461ae
line wrap: on
line source

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 set_local_only = Luan.set_local_only or error()
local get_local_only = Luan.get_local_only 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 users_by_name = {}

local function from_doc(doc)
	doc.type == "user" or error "wrong type"
	local user = User.new {
		id = doc.id
		email = doc.user_email
		password = doc.password
		name = doc.user_name
		created = doc.created
	}
	set_local_only(users_by_name,user.name,user)
	return user
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

	function user.reload()
		return User.get_by_id(user.id) or error(user.id)
	end

	function user.login()
		Http.response.set_persistent_cookie("user",user.name)
		Http.response.set_persistent_cookie("password",user.password)
		Http.request.cookies.user = user.name or error()
		Http.request.cookies.password = user.password or error()
	end

	set_metatable(user,metatable)
	return user
end

function User.get_by_id(id)
	local doc = Db.get_document("id:"..id)
	return doc and from_doc(doc)
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 user = get_local_only(users_by_name,name)
	if user ~= nil then return user end
	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

function User.current_required()
	local user = User.current()
	user or Http.response.send_redirect "/login.html"
	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