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