summaryrefslogtreecommitdiffstats
path: root/app/acf-util/logon-model.lua
blob: dbd8522a28cc3031297fb45abe3a2af465e784ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- Logon / Logoff model functions

module (..., package.seeall)

local sess = require ("session")

local pvt = {}


-- return a sessionid if username / password is valid, false
-- /etc/acf/passwd should be lines of userid:passwd:user name:role1[,role2[,role3]]
pvt.logon = function (self, id, passwd )
	-- if we already have sessionid... then you are already logged in
	if self.session.id then
		return false
	end

	id = id or ""
	passwd = passwd or ""

	-- open our hokey password file,
	local f = io.open(self.conf.confdir .. "/passwd" )
	if f then
		m = f:read("*all") .. "\n"
		f:close()
	
		for l in string.gmatch(m, "(%C*)\n") do
			local userid, password, username, roles =
				string.match(l, "([^:]*):([^:]*):([^:]*):(.*)")
			if userid == id and password == passwd then
				self.session.id = sess.random_hash(512)
				self.session.name = username
				self.session.roles = roles
				break
			end
		end
	end
	if self.session.id then 
	        local x = require("session")
		 x.save_session(self.conf.sessiondir, self.session.id, self.session)
		x=nil
		return self.session.id 
	else
		return false
	end
end

-- invalidate the session, or return false if the session wasn't valid
pvt.logout = function (self, sessionid)

	sess.invalidate_session ( self.conf.sessiondir, sessionid)
	self.session = {}

end

-------------------------------------------------------------------------
-- Public Methods
-------------------------------------------------------------------------

logon = pvt.logon
logout = pvt.logout