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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
<?php
/**
* ResourceSearch.php - A search type for resources.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @author Timo Hartge <hartge@data-quest.de>
* @copyright 2019
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class ResourceSearch extends SearchType
{
//Attributes:
/**
* string[]
* Limits the search results to resources where a person has at least
* the permission levels specified in this attribute.
* Defaults to ['user', 'autor', 'tutor', 'admin'].
*/
protected $accepted_permission_levels = ['user', 'autor', 'tutor', 'admin'];
/**
* bool
* Whether to use global permissions when searching for resources
* where a person has permissions on (true) or not. Defaults to true.
*/
protected $use_global_permissions = true;
/**
* string[]
* Additional properties that shall be appended to the name of the resource.
* The array must be one-dimensional.
*/
protected $additional_display_properties = [];
/**
* string
* A format specification for additional properties in the "sprintf syntax".
* This format specification must have only one placeholder named '%s'
* in which the additional properties will be inserted, separated by
* the string ", ".
* This attribute defaults to '[%s]'.
*/
protected $additional_property_format = '[%s]';
/**
* array
* Array of class_names of Resource classes that will be included in the search.
* An empty array will include all resource classes.
*/
protected $searchable_resource_classes = [];
/**
* The setter method for the $accepted_permission_levels attribute.
*
* @param string[] $accepted_permission_levels The new value for the attribute.
*/
public function setAcceptedPermissionLevels($levels = [])
{
//Check the array:
if (!$levels) {
throw new InvalidArgumentException(
_('Es wurde keine gültige Berechtigungsstufe übergeben!')
);
}
if (!is_array($levels)) {
$levels = [$levels];
}
foreach ($levels as $level) {
if (!in_array($level, ['user', 'autor', 'tutor', 'admin'])) {
throw new InvalidArgumentException(
sprintf(
_('Die Berechtigungsstufe %s ist ungültig!'),
$level
)
);
}
}
//The checks are finished: set the new levels:
$this->accepted_permission_levels = array_unique($levels);
}
/**
* The getter method for the $accepted_permission_levels attribute.
*
* @returns string[]
*/
public function getAcceptedPermissionLevels()
{
return $this->accepted_permission_levels;
}
/**
* The setter method for the $use_global_permission attribute.
*
* @param bool $use_global_permissions The new value for the attribute.
*/
public function setUseOfGlobalPermissions($use_global_permissions = true)
{
$this->use_global_permissions = (bool)$use_global_permissions;
}
/**
* The getter method for the $use_global_permission attribute.
*
* @returns bool
*/
public function getUseOfGlobalPermissions()
{
return $this->use_global_permissions;
}
/**
* The setter method for the $additional_display_properties attribute.
*
* @param array $properties The new value for the attribute.
*/
public function setAdditionalDisplayProperties($properties = [])
{
$this->additional_display_properties = $properties;
}
/**
* The getter method for the $additional_display_properties attribute.
*
* @returns array @see $additional_display_properties for the array format.
*/
public function getAdditionalDisplayProperties()
{
return $this->additional_display_properties;
}
/**
* The setter method for the $additional_property_format attribute.
*
* @param string $format The new value for the attribute.
*/
public function setAdditionalPropertyFormat($format = '[%s]')
{
$this->additional_property_format = $format;
}
/**
* The getter method for the $additional_property_format attribute.
*
* @returns string
*/
public function getAdditionalPropertyFormat()
{
return $this->additional_property_format;
}
/**
* The setter method for the $searchable_resource_classes attribute.
*
* @param array $categories The new value for the attribute.
*/
public function setSearchableResourceClasses($classes = [])
{
$this->searchable_resource_classes = $classes;
}
/**
* The getter method for the $searchable_resource_classes attribute.
*
* @returns array
*/
public function getSearchableResourceClasses()
{
return $this->searchable_resource_classes;
}
//SearchType interface implementations:
public function getTitle()
{
return _('Ressourcensuche');
}
/**
* @param string $keyword The name of the resource or a part of it.
* @param array $contextual_data This parameter is not used at the moment.
*/
public function getResults(
$keyword,
$contextual_data = [],
$limit = PHP_INT_MAX,
$offset = 0
)
{
$user = User::findCurrent();
if (!$user) {
return [];
}
$sql = "INNER JOIN resource_categories rc
ON resources.category_id = rc.id
WHERE
resources.name LIKE CONCAT('%', :keyword, '%') ";
$sql_params = [
'keyword' => $keyword
];
//Root users can access everything so that we don't need
//to check for permissions in case the current user is root.
if (!$GLOBALS['perm']->have_perm('root')) {
$global_permission = null;
if ($this->use_global_permissions) {
//Get the global permission level:
$global_permission = ResourceManager::getGlobalResourcePermission(
$user
);
}
if (in_array($global_permission, $this->accepted_permission_levels)) {
//Retrieve all resources where the user has sufficient permissions.
//There may be resources where the user has explicitly lower
//permissions and we must therefore exclude these resources.
$lower_permission_levels = ResourceManager::getLowerPermissionLevels(
$global_permission
);
$sql .= "AND resources.id NOT IN (
SELECT resource_id
FROM
resource_permissions
WHERE
user_id = :user_id
AND
perms IN ( :lower_perms )
UNION
SELECT resource_id
FROM
resource_temporary_permissions
WHERE
user_id = :user_id
AND
perms IN ( :lower_perms )
AND
begin <= :now
AND
end >= :now
)";
$sql_params = array_merge(
$sql_params,
[
'user_id' => $user->id,
'lower_perms' => $lower_permission_levels,
'now' => time()
]
);
} else {
//Only retrieve those resources where the user has direct
//sufficient permissions.
$sql .= "AND resources.id IN (
SELECT resource_id
FROM
resource_permissions
WHERE
user_id = :user_id
AND
perms IN ( :perms )
UNION
SELECT resource_id
FROM
resource_temporary_permissions
WHERE
user_id = :user_id
AND
perms IN ( :perms )
AND
begin <= :now
AND
end >= :now
)";
$sql_params = array_merge(
$sql_params,
[
'user_id' => $user->id,
'perms' => $this->accepted_permission_levels,
'now' => time()
]
);
}
}
$sql .= " ORDER BY resources.name ASC
LIMIT :limit OFFSET :offset ";
$sql_params['limit'] = $limit;
$sql_params['offset'] = $offset;
$resources = Resource::findBySql(
$sql,
$sql_params
);
$results = [];
foreach ($resources as $resource) {
if (!empty($this->searchable_resource_classes)) {
if(!in_array($resource->class_name, $this->searchable_resource_classes)) {
continue;
}
}
$additional_text = '';
if (count($this->additional_display_properties)) {
$additional_values = [];
foreach ($this->additional_display_properties as $ap) {
$additional_values[] = $resource->getProperty($ap);
}
$additional_text = implode(', ', $additional_values);
}
$complete_text = $resource->name;
if ($additional_text) {
$complete_text .= ' ' . sprintf(
$this->additional_property_format,
$additional_text
);
}
$results[] = [
$resource->id,
$complete_text
];
}
return $results;
}
public function includePath()
{
return __FILE__;
}
}
|