summaryrefslogtreecommitdiffstats
path: root/app/acf-util/password-model.lua
blob: e3d58f1a5d60f5527175b4633666eb21da0deb9b (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
module(..., package.seeall)

local auth=require("authenticator-plaintext")

function update_user(self, clientdata, newuser)
	local config = {}
	local errtxt
	local descr
	local errormessage = {}

	-- Try to write new or update existing data
	-- if clientdata.username exists, then pretty sure trying to write
	if newuser == true then
		if clientdata.username then
			result, errormessage = auth.new_settings(self, clientdata.userid, clientdata.username, clientdata.password, clientdata.password_confirm, clientdata.roles)
			if result == true then
				descr = "Created new user"
			else
				errtxt = "Failed to create new user"
			end
		end
	else
		result, errormessage = auth.change_settings(self, clientdata.userid, clientdata.username, clientdata.password, clientdata.password_confirm, clientdata.roles)
		if result == true and clientdata.username then
			descr = "Saved changes"
		elseif result == false and clientdata.username then
			errtxt = "Failed to save changes"
		elseif result == false then
			errtxt = "Bad user id"
		end
	end
	
	-- Now, read the updated / existing data
	local userinfo
	if ( errtxt == nil ) and clientdata.userid and (#clientdata.userid > 0) then
		userinfo = auth.get_userinfo(self,clientdata.userid)
	end
	userinfo = userinfo or {}

	config.userid =  cfe({
		label="User id",
		value=(userinfo.userid or clientdata.userid or ""),
		errtxt = errormessage.userid
		})
	config.username =  cfe({
		label="Real name",
		value=(userinfo.username or clientdata.username or ""),
		errtxt = errormessage.username
		})
	config.roles =  cfe({
		label="Roles",
		value=(userinfo.roles or clientdata.roles or {}),
		type="multi",
		option=auth.list_roles(),
		errtxt = errormessage.roles
		})
	config.password =  cfe({
		label="Password",
		errtxt = errormessage.password
		})
	config.password_confirm =  cfe({
		label="Password (confirm)",
		errtxt = errormessage.password_confirm
		})

	return cfe({ type="form", value=config, errtxt = errtxt, descr = descr, label="User Config" })
end

function get_users(self)
	--List all users and their userinfo
	local users = {}
	local userlist = auth.list_users(self)
	
	for x,user in pairs(userlist) do
		local userinfo = auth.get_userinfo(self,user)
		users[user] = cfe({
			type="group",
			label=user,
			value={	userid=cfe ({
					label="User ID",
					value=userinfo.userid,
					}),
				username=cfe ({
					label="Real name",
					value=userinfo.username,
					}),
				roles=cfe ({
					label="Roles",
					value=userinfo.roles,
					option=auth.list_roles(),
					type="multi",
					}),
				},

			})
	end

	return cfe({ type="group", value=users, label="User Configs" })
end

function delete_user(self, userid)
	auth.delete_user(self, userid)
end