summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/pages/users.inc.php
blob: 50f0f76383c9e1dc24f1fc212e05cff5f1dab4ba (plain) (blame)
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
<?php

class SubPage
{

	public static function doPreprocess()
	{
		// Currently there's only one view, actions are ajax
		User::assertPermission('users.view');
	}

	public static function doRender()
	{
		self::listUsers();
		self::listOrganizations();
	}

	public static function doAjax()
	{
		User::load();

		$action = Request::post('action', '', 'string');
		if ($action === 'setmail' || $action === 'setsu' || $action == 'setlogin') {
			if (User::hasPermission("users.".$action)) {
				self::setUserOption($action);
			}
		} elseif ($action === 'setorglogin') {
			if (User::hasPermission("users.setorglogin")) {
				self::setOrgOption($action);
			}
		} else {
			die('No such action');
		}
	}

	// Helpers

	private static function listUsers()
	{
		$res = Database::simpleQuery('SELECT userid, firstname, lastname, email, lastlogin, user.canlogin, issuperuser, emailnotifications,'
			. ' organization.displayname AS orgname FROM sat.user'
			. ' LEFT JOIN sat.organization USING (organizationid)'
			. ' ORDER BY lastname ASC, firstname ASC');
		$rows = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			settype($row['lastlogin'], 'int');
			$row['canlogin'] = self::checked($row['canlogin']);
			$row['issuperuser'] = self::checked($row['issuperuser']);
			$row['emailnotifications'] = self::checked($row['emailnotifications']);
			if ($row['lastlogin'] !== 0) {
				$row['lastlogin_s'] = date('d.m.Y', $row['lastlogin']);
			}
			$rows[] = $row;
		}
		Render::addTemplate('userlist', array(
			'users' => $rows,
			'nameTag' => User::hasPermission('actionlog.view') ? 'a' : 'span',
		));
	}

	private static function listOrganizations()
	{
		$res = Database::simpleQuery('SELECT organizationid, displayname, canlogin FROM sat.organization'
			. ' ORDER BY displayname ASC');
		$rows = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$row['canlogin'] = self::checked($row['canlogin']);
			$rows[] = $row;
		}
		Render::addTemplate('orglist', array('organizations' => $rows));
	}

	private static function checked($val)
	{
		if ($val)
			return 'checked="checked"';
		return '';
	}

	private static function setUserOption($option)
	{
		$val = (string) Request::post('value', '-');
		if ($val !== '1' && $val !== '0')
			die('Nein');
		if ($option === 'setmail') {
			$field = 'emailnotifications';
		} elseif ($option === 'setsu') {
			$field = 'issuperuser';
		} elseif ($option === 'setlogin') {
			$field = 'canlogin';
		} else {
			die('Unknown');
		}
		$user = (string) Request::post('userid', '?');
		$ret = Database::exec("UPDATE sat.user SET $field = :onoff WHERE userid = :userid", array(
			'userid' => $user,
			'onoff' => $val
		));
		error_log("Setting $field to $val for $user - affected: $ret");
		if ($ret === false)
			die('Error');
		if ($ret === 0)
			die(1 - $val);
		die($val);
	}

	private static function setOrgOption($option)
	{
		$val = (string) Request::post('value', '-');
		if ($val !== '1' && $val !== '0')
			die('Nein');
		if ($option === 'setorglogin') {
			$field = 'canlogin';
		} else {
			die('Unknown');
		}
		$ret = Database::exec("UPDATE sat.organization SET $field = :onoff WHERE organizationid = :organizationid", array(
			'organizationid' => (string) Request::post('organizationid', ''),
			'onoff' => $val
		));
		if ($ret === false)
			die('Error');
		if ($ret === 0)
			die(1 - $val);
		die($val);
	}

}