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

default_action = "editme"

function status(self)
	return self.model.get_users(self)
end

function editme(self)
	local output = self.model.read_user(self, self.sessiondata.userinfo.userid)
	if clientdata.Save then
		-- just to make sure can't modify any other user from this action
		self.clientdata.userid = self.sessiondata.userinfo.userid
		
		-- As a special case for update_user, settings that don't change are nil
		self.clientdata.roles = nil
		-- if password is blank, don't update it or require it
		if self.clientdata.password == "" then self.clientdata.password = nil end
		if self.clientdata.password_confirm == "" then self.clientdata.password_confirm = nil end

		for name,value in pairs(output.value) do
			value.value = self.clientdata[name]
		end

		-- Update userinfo
		output = self.model.update_user(self, output)
		if not output.errtxt then
			output.descr = "Saved user"
		end
		output = self:redirect_to_referrer(output)
	else
		output = self:redirect_to_referrer() or output
	end

	-- Don't allow changing of roles for yourself
	output.value.roles = nil

	output.type = "form"
	output.label = "Edit My Settings"
	output.option = "Save"
	return output
end

function edituser(self)
	local output = self.model.read_user(self, self.clientdata.userid)
	if self.clientdata.Save then
		-- As a special case for update_user, settings that don't change are nil
		-- if password is blank, don't update it or require it
		if self.clientdata.password == "" then self.clientdata.password = nil end
		if self.clientdata.password_confirm == "" then self.clientdata.password_confirm = nil end

		for name,value in pairs(output.value) do
			value.value = self.clientdata[name]
		end

		-- Update userinfo
		output = self.model.update_user(self, output)
		if not output.errtxt then
			redirect(self, "status")
		end
		output = self:redirect_to_referrer(output)
	else
		output = self:redirect_to_referrer() or output
	end

	output.type = "form"
	output.label = "Edit User Settings"
	output.option = "Save"
	return output
end

function newuser(self)
	local output = self.model.read_user(self)
	if self.clientdata.Save then
		for name,value in pairs(output.value) do
			if self.clientdata[name] then
				value.value = self.clientdata[name]
			end
		end

		-- Update userinfo
		output = self.model.create_user(self, output)
		if not output.errtxt then
			redirect(self, "status")
		end
		output = self:redirect_to_referrer(output)
	else
		output = self:redirect_to_referrer() or output
	end

	output.type = "form"
	output.label = "New User Settings"
	output.option = "Save"
	return output
end

function deleteuser(self)
	return self:redirect_to_referrer(self.model.delete_user(self, self.clientdata.userid))
end