view src/lib/User.luan @ 11:3ed1e3f3a53a

use bbcode_to_html
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Jun 2022 20:05:11 -0600
parents de0cbf515ef5
children 298c71e0c854
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 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

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