summaryrefslogtreecommitdiffstats
path: root/app/acf-util/password-controller.lua
blob: 3cbc98ae4e865ee7ffedf8ab8090b679f1bc0797 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
module(..., package.seeall)

auth=require("authenticator-plaintext")

local list_redir = function (self)
	self.conf.action = "status"
	self.conf.type = "redir"
	error (self.conf)
end

mvc = {}
mvc.on_load = function(self, parent)
	if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then
		self.worker[self.conf.action] = list_redir(self)
	end
end

local function admin_permission()
	if (sessiondata.userinfo) and (sessiondata.userinfo.userid == "alpine") then
		return true
	else
		return false
	end
end

local function config(self,userid)
	local config = {}
	local userinfo = {}
	if (userid) then
		userinfo=auth.get_userinfo(self,userid)
	else
		userinfo.userid = ""
		userinfo.username = ""
		userinfo.roles = {}

	end
	local avail_roles=auth.list_roles()

	config.debug = userid

	config.userid =  cfe({
		name="userid",
		label="User id",
		value=(userinfo.userid or ""),
		})
	config.orguserid = cfe({
		name="orguserid",
		value=(userinfo.userid or ""),
		type="hidden",
		})
	
	config.username =  cfe({
		name="username",
		label="User name",
		value=userinfo.username,
		})
	config.roles =  cfe({
		name="roles",
		label="Roles",
		option=userinfo.roles,
		type="select",
		size=#avail_roles,
		})
	config.password =  cfe({
		name="password",
		label="Password",
		type="passwd",
		disabled="yes",
		})
	config.password_confirm =  cfe({
		name="password_confirm",
		label="Password (confirm)",
		type="passwd",
		disabled="yes",
		})

	config.availableroles =  cfe({
		name="availableroles",
		label="Available roles",
		type="select",
		option=avail_roles,
		})

	return config
end

function status(self)
	local status = {}

	-- Check for admin persmissions - else redirect to personal options
	if not (admin_permission()) then
		self.conf.action = "edit_me"
		return edit_me(self)
	end

	-- Redirect when creating a new account
	if (clientdata.cmdnew) then
		self.conf.action = "administrator"
		self.conf.type = "redir"

		return administrator(self)
	end

	--List all users and their userinfo
	status.users = {}
	local userlist = auth.list_users(self)
	for k,v in pairs(userlist) do
		local userinfo = auth.get_userinfo(self,v)
		status.users[k] = cfe({
			name=v,
			label=v,
--			debug=userinfo,
			value={	userid=cfe ({
					name="userid",
					label="User ID",
					value=userinfo.userid,
					}),
				username=cfe ({
					name="username",
					label="User name",
					value=userinfo.username,
					}),
				roles=cfe ({
					name="roles",
					label="Roles",
					value=table.concat(userinfo.roles," / "),
					option=userinfo.roles,
					type="select",
					}),
				},

			})
	end

	--Create a button for 'New user account'
	status.cmdnew = cfe ({
		name="cmdnew",
		type="submit",
		label="Create new account",
		value="Create",
--		disabled="yes",
		})
	return { status=status }
end

function administrator(self)
	local output = {}

	-- Check for admin persmissions - else redirect to personal options
	if not (admin_permission()) then
		self.conf.action = "edit_me"
		return edit_me(self)
	end

	-- Output userinfo
	output = config(self,self.clientdata.userid)

	--Clear password-field
	output.password.value = ""

	-- Add some buttons
	output.cmdsave = cfe ({
		name="cmdsave",
		type="submit",
		label="Save changes",
		value="Save",
--		disabled="yes",
		})
	output.cmddelete = cfe ({
		name="cmddelete",
		type="submit",
		label="Delete this account",
		value="Delete",
		disabled="yes",
		})

	return {config=output}
end

function edit_me(self)

	--FIXME: Redirect to Welcome or logon if user is not logged on
--	if not ( self.sessiondata.userinfo) then
--		self.conf.action = ""
--		self.conf.type = "redir"
--	end

	-- Output userinfo
	local output = config(self,sessiondata.userinfo.userid)

	--Hide roles/cmddelete for current the user
	output.roles = nil
	output.cmddelete = nil

	--Disable userid
	output.userid.disabled = "yes"

	--Add save-button
	output.cmdsave = cfe ({
		name="cmdsave",
		type="submit",
		label="Save changes",
		value="Save",
		disabled="yes",
		})

	return {config=output}
end

function save(self)

	--FIXME: Check if user is allowed to save settings

	-- We start changing things based on input
	local cmdresult = {}
	cmdresult.debug = {}
	if (clientdata.cmdsave) then
		if (#clientdata.orguserid > 0) then
			local variables="username userid"
			cmdresult.debugs = self.clientdata.orguserid
			for var in string.gmatch(variables, "%S+") do
				if (self.clientdata[var]) then
					cmdresult[var],cmdresult.debug[var] = auth.change_settings(
						self,
						self.clientdata.orguserid, 
						var, self.clientdata[var]
						)
				end
			end
		else
			cmdresult["new"],cmdresult.debug["new"] = auth.new_settings(
				self,
				self.clientdata.userid,
				self.clientdata.username,
				self.clientdata.password,
				self.clientdata.password_confirm )
		end
	end

	cmdresult.clientdata = self.clientdata

	return cmdresult
--[[
	--FIXME: Redirect somewhere when changed settings
	self.conf.action = "status"
	self.conf.type = "redir"
	
	return status(self)
--]]
end