|
| 1 | +<?php |
| 2 | +namespace LORIS\mri_violations; |
| 3 | + |
| 4 | +/** |
| 5 | + * UserCenterMatchOrNull filters out data for any resource which is not |
| 6 | + * part of one of the user's centers unless the user has one of the |
| 7 | + * given permissions. If the resource has no CenterID defined, it is not |
| 8 | + * excluded from the results. |
| 9 | + * |
| 10 | + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 |
| 11 | + */ |
| 12 | +class UserCenterMatchOrNullOrAnyPermission implements \LORIS\Data\Filter |
| 13 | +{ |
| 14 | + /** |
| 15 | + * {@constructor} |
| 16 | + * |
| 17 | + * @param string[] $permissions - List of possible permissions |
| 18 | + */ |
| 19 | + public function __construct(protected array $permissions) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Implements the \LORIS\Data\Filter interface |
| 25 | + * |
| 26 | + * @param \User $user The user that the data is being |
| 27 | + * filtered for. |
| 28 | + * @param \LORIS\Data\DataInstance $resource The data being filtered. |
| 29 | + * |
| 30 | + * @return bool |
| 31 | + */ |
| 32 | + public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool |
| 33 | + { |
| 34 | + // phan only understands method_exists on simple variables. |
| 35 | + // Assigning to a variable is the a workaround |
| 36 | + // for false positive 'getProjectID doesn't exist errors suggested |
| 37 | + // in https://github.com/phan/phan/issues/2628 |
| 38 | + $res = $resource; |
| 39 | + '@phan-var object $res'; |
| 40 | + |
| 41 | + if ($user->hasAnyPermission($this->permissions)) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + if (method_exists($res, 'getCenterID')) { |
| 46 | + $resourceCenter = $res->getCenterID(); |
| 47 | + if (!is_null($resourceCenter)) { |
| 48 | + return $user->hasCenter( |
| 49 | + new \CenterID(strval($resourceCenter)) |
| 50 | + ); |
| 51 | + } |
| 52 | + return true; |
| 53 | + } |
| 54 | + throw new \LorisException( |
| 55 | + "Can not implement filter on a resource type that has no centers." |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
0 commit comments