tiki-list_object_permissions.php 10.5 KB
Newer Older
1
<?php
2
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3
//
changi67's avatar
changi67 committed
4 5
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
6
// $Id$
7

rjsmelo's avatar
rjsmelo committed
8
include_once('tiki-setup.php');
9
$access->check_permission('tiki_p_admin');
10 11
$all_perms = $userlib->get_permissions();

12 13 14 15 16
/**
 * @param $permName
 * @param $objectType
 * @return bool
 */
changi67's avatar
changi67 committed
17 18
function is_perm($permName, $objectType)
{
19 20
	global $all_perms, $tikilib;
	$permGroup = $tikilib->get_permGroup_from_objectType($objectType);
21
	foreach ($all_perms['data'] as $perm) {
22 23 24 25 26 27
		if ($perm['permName'] == $permName) {
			return $permGroup == $perm['type'];
		}
	}
	return false;
}
28 29 30 31 32 33 34 35

/**
 * @param $objectId
 * @param $objectType
 * @param $objectName
 * @param string $filterGroup
 * @return array
 */
rjsmelo's avatar
rjsmelo committed
36
function list_perms($objectId, $objectType, $objectName, $filterGroup = '')
changi67's avatar
changi67 committed
37
{
38 39
	global $prefs;
	$userlib = TikiLib::lib('user');
rjsmelo's avatar
rjsmelo committed
40 41
	$ret = [];
	$cats = [];
42
	$perms = $userlib->get_object_permissions($objectId, $objectType);
rjsmelo's avatar
rjsmelo committed
43
	if (! empty($perms)) {
44
		foreach ($perms as $perm) {
45
			if (empty($filterGroup) || in_array($perm['groupName'], $filterGroup)) {
rjsmelo's avatar
rjsmelo committed
46 47 48
				$json = json_encode(['group' => $perm['groupName'], 'perm' => $perm['permName'], 'objectId' => $objectId, 'objectType' => $objectType]);
				$ret[] = ['group' => $perm['groupName'], 'perm' => $perm['permName'], 'reason' => 'Object',
						   'objectId' => $objectId, 'objectType' => $objectType, 'objectName' => $objectName, 'json' => $json];
49
			}
50
		}
51 52
	}
	if ($prefs['feature_categories'] == 'y') {
53
		$categlib = TikiLib::lib('categ');
54
		$categs = $categlib->get_object_categories($objectType, $objectId);
rjsmelo's avatar
rjsmelo committed
55
		if (! empty($categs)) {
56
			foreach ($categs as $categId) {
57
				$category_perms = $userlib->get_object_permissions($categId, 'category');
rjsmelo's avatar
rjsmelo committed
58
				if (! empty($category_perms)) {
59
					foreach ($category_perms as $category_perm) {
60
						if (is_perm($category_perm['permName'], $objectType) && (empty($filterGroup) || in_array($category_perm['groupName'], $filterGroup))) {
rjsmelo's avatar
rjsmelo committed
61
							$cats[] = ['group' => $category_perm['groupName'], 'perm' => $category_perm['permName'],
62
									'reason' => 'Category', 'objectId' => $categId, 'objectType' => 'category',
rjsmelo's avatar
rjsmelo committed
63
									'objectName' => $categlib->get_category_name($categId)];
64
						}
65 66 67 68 69
					}
				}
			}
		}
	}
rjsmelo's avatar
rjsmelo committed
70
	return ['objectId' => $objectId, 'special' => $ret, 'category' => $cats];
71
}
72

rjsmelo's avatar
rjsmelo committed
73 74 75 76
$filterGroup = empty($_REQUEST['filterGroup']) ? [] : $_REQUEST['filterGroup'];
$feedbacks = [];
$del = ! empty($_REQUEST['delete']) && $_REQUEST['delete'] === 'delete';
$dup = ! empty($_REQUEST['duplicate']) && $_REQUEST['duplicate'] === 'duplicate';
77
if ($del || $dup) {
78
	$access->check_authenticity();
rjsmelo's avatar
rjsmelo committed
79
	if (! empty($_REQUEST['groupPerm'])) {
80 81 82 83
		foreach ($_REQUEST['groupPerm'] as $perm) {
			$perm = json_decode($perm, true);
			if ($del) {
				$userlib->remove_permission_from_group($perm['perm'], $perm['group']);
rjsmelo's avatar
rjsmelo committed
84 85
				$feedbacks[] = tra('Remove permission %0 from %1', '', false, [$perm['perm'], $perm['group']]);
			} elseif (! empty($_REQUEST['toGroup']) && $userlib->group_exists($_REQUEST['toGroup'])) {
86
				$userlib->assign_permission_to_group($perm['perm'], $_REQUEST['toGroup']);
rjsmelo's avatar
rjsmelo committed
87
				$feedbacks[] = tra('Assign permission %0 to %1', '', false, [$perm['perm'], $_REQUEST['toGroup']]);
88 89 90
			}
		}
	}
rjsmelo's avatar
rjsmelo committed
91
	if (! empty($_REQUEST['objectPerm'])) {
92 93 94 95
		foreach ($_REQUEST['objectPerm'] as $perm) {
			$perm = json_decode($perm, true);
			if ($del) {
				$userlib->remove_object_permission($perm['group'], $perm['objectId'], $perm['objectType'], $perm['perm']);
rjsmelo's avatar
rjsmelo committed
96 97
				$feedbacks[] = tra('Remove permission %0 from %1', '', false, [$perm['perm'], $perm['group']]);
			} elseif (! empty($_REQUEST['toGroup']) && $userlib->group_exists($_REQUEST['toGroup'])) {
98
				$userlib->assign_object_permission($_REQUEST['toGroup'], $perm['objectId'], $perm['objectType'], $perm['perm']);
rjsmelo's avatar
rjsmelo committed
99
				$feedbacks[] = tra('Assign permission %0 to %1', '', false, [$perm['perm'], $_REQUEST['toGroup']]);
100 101 102
			}
		}
	}
rjsmelo's avatar
rjsmelo committed
103
	if (! empty($feedbacks) && $dup && ! empty($_REQUEST['toGroup']) && ! empty($filterGroup) && ! in_array($_REQUEST['toGroup'], $filterGroup)) {
104 105
		$filterGroup[] = $_REQUEST['toGroup'];
	}
106 107
}

rjsmelo's avatar
rjsmelo committed
108
$types = ['wiki page', 'file gallery', 'tracker', 'forum', 'group', 'articles', 'blog', 'calendar', 'sheet'];
109
$commentslib = TikiLib::lib('comments');
110
$all_groups = $userlib->list_all_groups();
rjsmelo's avatar
rjsmelo committed
111
$res = [];
112
foreach ($types as $type) {
rjsmelo's avatar
rjsmelo committed
113
	$res[$type]['default'] = [];
114
	$type_perms = $userlib->get_permissions(0, -1, 'permName_asc', '', $tikilib->get_permGroup_from_objectType($type));
115
	foreach ($all_groups as $gr) {
116
		$perms = $userlib->get_group_permissions($gr);
117
		foreach ($type_perms['data'] as $type_perm) {
118
			if (in_array($type_perm['permName'], $perms) && (empty($filterGroup) || in_array($gr, $filterGroup))) {
rjsmelo's avatar
rjsmelo committed
119
				$res[$type]['default'][] = ['group' => $gr, 'perm' => $type_perm['permName']];
120 121 122
			}
		}
	}
rjsmelo's avatar
rjsmelo committed
123 124
	$res[$type]['objects'] = [];
	$res[$type]['category'] = [];
125
	switch ($type) {
changi67's avatar
changi67 committed
126 127
		case 'wiki page':
		case 'wiki':
128
			$objects = $tikilib->list_pageNames();
129
			foreach ($objects['data'] as $object) {
130
				$r = list_perms($object['pageName'], $type, $object['pageName'], $filterGroup);
changi67's avatar
changi67 committed
131
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
132
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectType' => $type];
changi67's avatar
changi67 committed
133 134
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
135
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectType' => $type];
changi67's avatar
changi67 committed
136
				}
changi67's avatar
changi67 committed
137
			}
rjsmelo's avatar
rjsmelo committed
138
			break;
139

changi67's avatar
changi67 committed
140 141
		case 'file galleries':
		case 'file gallery':
142
			$filegallib = TikiLib::lib('filegal');
changi67's avatar
changi67 committed
143
			$objects = $filegallib->list_file_galleries(0, -1, 'name_asc', '', '', $prefs['fgal_root_id']);
144
			foreach ($objects['data'] as $object) {
145
				$r = list_perms($object['id'], $type, $object['name'], $filterGroup);
changi67's avatar
changi67 committed
146
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
147
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['name'], 'objectType' => $type];
changi67's avatar
changi67 committed
148 149
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
150
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['name'], 'objectType' => $type];
changi67's avatar
changi67 committed
151
				}
152
			}
rjsmelo's avatar
rjsmelo committed
153
			break;
154 155 156

		case 'tracker':
		case 'trackers':
157
			$objects = TikiLib::lib('trk')->list_trackers();
158
			foreach ($objects['data'] as $object) {
159
				$r = list_perms($object['trackerId'], $type, $object['name'], $filterGroup);
changi67's avatar
changi67 committed
160
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
161
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['name'], 'objectType' => $type];
changi67's avatar
changi67 committed
162
				}
163
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
164
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['name'], 'objectType' => $type];
changi67's avatar
changi67 committed
165
				}
166
			}
rjsmelo's avatar
rjsmelo committed
167
			break;
168 169 170 171

		case 'forum':
		case 'forums':
			$objects = $commentslib->list_forums();
172
			foreach ($objects['data'] as $object) {
173
				$r = list_perms($object['forumId'], $type, $object['name'], $filterGroup);
174
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
175
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['name'], 'objectType' => $type];
changi67's avatar
changi67 committed
176 177
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
178
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['name'], 'objectType' => $type];
changi67's avatar
changi67 committed
179
				}
changi67's avatar
changi67 committed
180
			}
rjsmelo's avatar
rjsmelo committed
181
			break;
changi67's avatar
changi67 committed
182

183 184
		case 'group':
		case 'groups':
185
			foreach ($all_groups as $object) {
186
				$r = list_perms($object, $type, '', $filterGroup);
187
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
188
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectType' => $type];
changi67's avatar
changi67 committed
189 190
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
191
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectType' => $type];
changi67's avatar
changi67 committed
192
				}
193
			}
rjsmelo's avatar
rjsmelo committed
194 195
			break;

196 197 198 199 200 201
		case 'calendar':
			$calendarlib = TikiLib::lib('calendar');
			$objects = $calendarlib->list_calendars();
			foreach ($objects['data'] as $object) {
				$r = list_perms($object['calendarId'], $type, $object['name'], $filterGroup);
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
202
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['name'], 'objectType' => $type];
203 204
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
205
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['name'], 'objectType' => $type];
206 207
				}
			}
rjsmelo's avatar
rjsmelo committed
208 209
			break;

210 211 212 213 214 215
		case 'articles':
			$artlib = TikiLib::lib('art');
			$objects = $artlib->list_articles();
			foreach ($objects['data'] as $object) {
				$r = list_perms($object['articleId'], $type, $object['title'], $filterGroup);
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
216
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['title'], 'objectType' => $type];
217 218
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
219
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['title'], 'objectType' => $type];
220 221
				}
			}
rjsmelo's avatar
rjsmelo committed
222 223
			break;

224 225 226
		case 'blog':
			$bloglib = TikiLib::lib('blog');
			$objects = $bloglib->list_blogs();
rjsmelo's avatar
rjsmelo committed
227

228
			foreach ($objects['data'] as $object) {
229
				$r = list_perms($object['blogId'], $type, isset($object['name']) ? $object['name'] : null, $filterGroup);
230
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
231
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['name'], 'objectType' => $type];
232 233
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
234
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['name'], 'objectType' => $type];
235 236
				}
			}
rjsmelo's avatar
rjsmelo committed
237 238
			break;

239 240 241 242
		case 'sheet':
			$sheetlib = TikiLib::lib('sheet');
			$objects = $sheetlib->list_sheets();
			foreach ($objects['data'] as $object) {
243
				$r = list_perms($object['sheetId'], $type, isset($object['name']) ? $object['name'] : null, $filterGroup);
244
				if (count($r['special']) > 0) {
rjsmelo's avatar
rjsmelo committed
245
					$res[$type]['objects'][] = ['objectId' => $r['objectId'], 'special' => $r['special'], 'objectName' => $object['name'], 'objectType' => $type];
246 247
				}
				if (count($r['category']) > 0) {
rjsmelo's avatar
rjsmelo committed
248
					$res[$type]['category'][] = ['objectId' => $r['objectId'], 'category' => $r['category'], 'objectName' => $object['name'], 'objectType' => $type];
249 250
				}
			}
rjsmelo's avatar
rjsmelo committed
251
			break;
252

253
		default:
rjsmelo's avatar
rjsmelo committed
254
			break;
255 256
	}
}
257
Feedback::note(['mes' => $feedbacks]);
258
$smarty->assign_by_ref('res', $res);
259 260
$smarty->assign_by_ref('filterGroup', $filterGroup);
$smarty->assign_by_ref('all_groups', $all_groups);
261

262 263
$smarty->assign('mid', 'tiki-list_object_permissions.tpl');
$smarty->display('tiki.tpl');