summaryrefslogtreecommitdiffstats
path: root/modules-available/permissionmanager/install.inc.php
blob: 292a5f522634d547cb3b07ce911f4b4458ee583a (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
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
<?php

$res = array();

$res[] = tableCreate('role', "
	roleid int(10) unsigned NOT NULL AUTO_INCREMENT,
	rolename varchar(200) NOT NULL,
	builtin bool NOT NULL DEFAULT '0',
	roledescription TEXT,
	PRIMARY KEY (roleid)
");

if (tableExists('user_x_role')) {
	if (tableExists('role_x_user')) {
		Database::exec('DROP TABLE user_x_role');
	} else {
		$res[] = tableRename('user_x_role', 'role_x_user');
	}
}
$res[] = tableCreate('role_x_user', "
	userid int(10) unsigned NOT NULL,
	roleid int(10) unsigned NOT NULL,
	PRIMARY KEY (userid, roleid)
");

$res[] = tableCreate('role_x_location', "
	roleid int(10) unsigned NOT NULL,
	locationid int(11),
	CONSTRAINT role_loc UNIQUE (roleid, locationid) 
");

$res[] = tableCreate('role_x_permission', "
	roleid int(10) unsigned NOT NULL,
	permissionid varchar(200) NOT NULL,
	PRIMARY KEY (roleid, permissionid)
");

if (tableHasColumn('role_x_location', 'id')) {
	$cnt = Database::exec('DELETE a FROM role_x_location a, role_x_location b
			WHERE a.roleid = b.roleid AND (a.locationid = b.locationid OR (a.locationid IS NULL AND b.locationid IS NULL))
			AND a.id > b.id');
	$ret = Database::exec('ALTER TABLE role_x_location DROP COLUMN id,
			ADD CONSTRAINT role_loc UNIQUE (roleid, locationid)');
	if ($ret === false) {
		$res[] = UPDATE_NOOP;
	} else {
		$res[] = UPDATE_DONE;
	}

}

if (!tableExists('user') || !tableExists('location')) {
	finalResponse(UPDATE_RETRY, 'Cannot add constraint yet. Please retry.');
} else {
	$c = tableGetConstraints('role_x_user', 'userid', 'user', 'userid');
	if ($c === false) {
		$alter = Database::exec('ALTER TABLE role_x_user ADD FOREIGN KEY (userid) REFERENCES user (userid) ON DELETE CASCADE ON UPDATE CASCADE');
		if ($alter === false)
			finalResponse(UPDATE_FAILED, 'Cannot add userid constraint referencing user table: ' . Database::lastError());
		$res[] = UPDATE_DONE;
	}

	$c = tableGetConstraints('role_x_user', 'roleid', 'role', 'roleid');
	if ($c === false) {
		$alter = Database::exec('ALTER TABLE role_x_user ADD FOREIGN KEY (roleid) REFERENCES role (roleid) ON DELETE CASCADE ON UPDATE CASCADE');
		if ($alter === false)
			finalResponse(UPDATE_FAILED, 'Cannot add roleid constraint referencing role table: ' . Database::lastError());
		$res[] = UPDATE_DONE;
	}

	$c = tableGetConstraints('role_x_location', 'roleid', 'role', 'roleid');
	if ($c === false) {
		$alter = Database::exec('ALTER TABLE role_x_location ADD FOREIGN KEY (roleid) REFERENCES role (roleid) ON DELETE CASCADE ON UPDATE CASCADE');
		if ($alter === false)
			finalResponse(UPDATE_FAILED, 'Cannot add roleid constraint referencing role table: ' . Database::lastError());
		$res[] = UPDATE_DONE;
	}

	$c = tableGetConstraints('role_x_location', 'locationid', 'location', 'locationid');
	if ($c === false) {
		$alter = Database::exec('ALTER TABLE role_x_location ADD FOREIGN KEY (locationid) REFERENCES location (locationid) ON DELETE CASCADE ON UPDATE CASCADE');
		if ($alter === false)
			finalResponse(UPDATE_FAILED, 'Cannot add locationid constraint referencing location table: ' . Database::lastError());
		$res[] = UPDATE_DONE;
	}

	$c = tableGetConstraints('role_x_permission', 'roleid', 'role', 'roleid');
	if ($c === false) {
		$alter = Database::exec('ALTER TABLE role_x_permission ADD FOREIGN KEY (roleid) REFERENCES role (roleid) ON DELETE CASCADE ON UPDATE CASCADE');
		if ($alter === false)
			finalResponse(UPDATE_FAILED, 'Cannot add roleid constraint referencing role table: ' . Database::lastError());
		$res[] = UPDATE_DONE;
	}
}

// 2018-04-13 role description field; add a couple default roles
if (!tableHasColumn('role', 'roledescription')) {
	$alter = Database::exec("ALTER TABLE role ADD roledescription TEXT");
	if ($alter === false)
		finalResponse(UPDATE_FAILED, 'Cannot add roledescription field to table role: ' . Database::lastError());
	$res[] = UPDATE_DONE;
}

// 2020-01-09 flag for builtin roles that can't be edited
if (!tableHasColumn('role', 'builtin')) {
	$alter = Database::exec("ALTER TABLE role ADD builtin bool NOT NULL DEFAULT '0' AFTER rolename");
	if ($alter === false)
		finalResponse(UPDATE_FAILED, 'Cannot add builtin field to table role: ' . Database::lastError());
	$res[] = UPDATE_DONE;
}

if (Database::exec("INSERT INTO `role` (roleid, rolename, builtin, roledescription) VALUES
			(1,'Super-Admin', 1, 'Hat keinerlei Zugriffsbeschränkungen'),
			(2,'Admin', 1, 'Alles bis auf Rechte-/Nutzerverwaltung'),
			(3,'Prüfungsadmin', 1, 'Kann E-Prüfungen verwalten, Prüfungsmodus einschalten, etc.'),
			(4,'Lesezugriff', 1, 'Kann auf die meisten Seiten zugreifen, jedoch keine Änderungen vornehmen')
			ON DUPLICATE KEY UPDATE rolename = VALUES(rolename), builtin = 1, roledescription = VALUES(roledescription)") !== false) {
	// Old ruleset accidentally gave write permissions to the read-only role
	Database::exec("DELETE FROM role_x_permission WHERE roleid = 4 AND permissionid = 'news.*'");
	// Assign roles to location (all)
	Database::exec("DELETE FROM role_x_location WHERE roleid IN (1,2,3,4)");
	Database::exec("INSERT INTO `role_x_location` VALUES (1,NULL),(2,NULL),(3,NULL),(4,NULL)");
	// In case user fiddled around before
	Database::exec("DELETE FROM role_x_permission WHERE roleid IN (1,2,3,4)");
	// Assign permissions to roles
	Database::exec("INSERT IGNORE INTO `role_x_permission` VALUES
			(3,'exams.exams.*'),
			(3,'rebootcontrol.action.*'),
			(3,'statistics.hardware.projectors.view'),
			(3,'statistics.machine.note.*'),
			(3,'statistics.machine.view-details'),
			(3,'statistics.view.*'),
			(3,'syslog.view'),
			
			(1,'*'),
			
			(4,'adduser.user.view-list'),
			(4,'backup.create'),
			(4,'baseconfig.view'),
			(4,'dnbd3.access-page'),
			(4,'dnbd3.refresh'),
			(4,'dnbd3.view.details'),
			(4,'dozmod.actionlog.view'),
			(4,'dozmod.users.view'),
			(4,'eventlog.view'),
			(4,'exams.exams.view'),
			(4,'locationinfo.backend.check'),
			(4,'locationinfo.panel.list'),
			(4,'locations.location.view'),
			(4,'minilinux.view'),
			(4,'news.access-page'),
			(4,'permissionmanager.locations.view'),
			(4,'permissionmanager.roles.view'),
			(4,'permissionmanager.users.view'),
			(4,'runmode.list-all'),
			(4,'serversetup.access-page'),
			(4,'serversetup.download'),
			(4,'statistics.hardware.projectors.view'),
			(4,'statistics.machine.note.view'),
			(4,'statistics.machine.view-details'),
			(4,'statistics.view.*'),
			(4,'statistics_reporting.reporting.download'),
			(4,'statistics_reporting.table.export'),
			(4,'statistics_reporting.table.view.*'),
			(4,'sysconfig.config.view-list'),
			(4,'sysconfig.module.download'),
			(4,'sysconfig.module.view-list'),
			(4,'syslog.view'),
			(4,'systemstatus.show.overview.*'),
			(4,'systemstatus.tab.*'),
			(4,'webinterface.access-page'),
			(4,'rebootcontrol.subnet.view'),
			(4,'rebootcontrol.jumphost.view'),
			
			(2,'adduser.user.view-list'),
			(2,'backup.*'),
			(2,'baseconfig.*'),
			(2,'dnbd3.*'),
			(2,'dozmod.*'),
			(2,'eventlog.view'),
			(2,'exams.exams.*'),
			(2,'locationinfo.*'),
			(2,'locations.*'),
			(2,'minilinux.*'),
			(2,'news.*'),
			(2,'permissionmanager.locations.view'),
			(2,'permissionmanager.roles.view'),
			(2,'permissionmanager.users.view'),
			(2,'rebootcontrol.*'),
			(2,'roomplanner.edit'),
			(2,'runmode.list-all'),
			(2,'serversetup.*'),
			(2,'statistics.*'),
			(2,'statistics_reporting.*'),
			(2,'sysconfig.*'),
			(2,'syslog.*'),
			(2,'systemstatus.*'),
			(2,'vmstore.edit'),
			(2,'webinterface.*')");
	Database::exec("OPTIMIZE TABLE role_x_permission");
	// Assign the first user to the superadmin role (if one exists)
	$num = Database::exec("INSERT IGNORE INTO `role_x_user` VALUES (1,1)");
	if ($num > 0) {
		$res[] = UPDATE_DONE;
	}
}

//
//

if (in_array(UPDATE_DONE, $res)) {
	finalResponse(UPDATE_DONE, 'Tables created successfully');
}
finalResponse(UPDATE_NOOP, 'Everything already up to date');