-
Notifications
You must be signed in to change notification settings - Fork 192
[Core] New Filters and Logic #9548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
74dc8a1
[Core] New Filters and Logic
ridz1208 e1f7adc
phpcs
ridz1208 196a12a
cs again
ridz1208 3da432e
dave comments
ridz1208 578d3c2
Update src/Data/Filters/AccessibleResourceFilter.php
ridz1208 b20912b
Update src/Data/Filters/UserProjectMatch.php
ridz1208 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
| * The AND Filter class allows for the combination of multiple filters using an AND | ||
| * operand to determine the final result. | ||
| * | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| */ | ||
| class CompositeANDFilter implements \LORIS\Data\Filter | ||
| { | ||
|
|
||
| protected array $filters; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param \LORIS\Data\Filter $filters An array of filter objects. | ||
| */ | ||
| public function __construct(\LORIS\Data\Filter ...$filters) | ||
| { | ||
| $this->filters = $filters; | ||
| } | ||
|
|
||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
| * @param \User $user The user that the data is being | ||
| * filtered for. | ||
| * @param \LORIS\Data\DataInstance $resource The data being filtered. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | ||
| { | ||
| foreach ($this->filters as $filter) { | ||
| if (!$filter->filter($user, $resource)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
| * The OR Filter class allows for the combination of multiple filters using an OR | ||
| * operand to determine the final result. | ||
| * | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| */ | ||
| class CompositeORFilter implements \LORIS\Data\Filter | ||
| { | ||
|
|
||
| protected array $filters; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param \LORIS\Data\Filter $filters An array of filter objects. | ||
| */ | ||
| public function __construct(\LORIS\Data\Filter ...$filters) | ||
| { | ||
| $this->filters = $filters; | ||
| } | ||
|
|
||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
| * @param \User $user The user that the data is being | ||
| * filtered for. | ||
| * @param \LORIS\Data\DataInstance $resource The data being filtered. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | ||
| { | ||
| foreach ($this->filters as $filter) { | ||
| if ($filter->filter($user, $resource)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
| * ResourceHasNoSession filters out data for any resource which does has a session. | ||
| * | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| */ | ||
| class ResourceHasNoSession implements \LORIS\Data\Filter | ||
| { | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param ?bool $defaultReturn The default return value to return instead of | ||
| * throwing an exception when an exception is | ||
| * indesirable. | ||
| * | ||
| */ | ||
| public function __construct(protected ?bool $defaultReturn = null) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
| * @param \User $user The user that the data is being | ||
| * filtered for. | ||
| * @param \LORIS\Data\DataInstance $resource The data being filtered. | ||
| * | ||
| * @return bool true if the resource has no session affiliated with it | ||
| */ | ||
| public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | ||
| { | ||
| // phan only understands method_exists on simple variables, not | ||
| // Assigning to a variable is the a workaround | ||
| // for false positive 'getCenterIDs doesn't exist errors suggested | ||
| // in https://github.com/phan/phan/issues/2628 | ||
| $res = $resource; | ||
| '@phan-var object $res'; | ||
|
|
||
| if (!method_exists($res, 'getSessionID')) { | ||
| if ($this->defaultReturn === null) { | ||
| throw new \LorisException( | ||
| "ResourceHasNoSession filter requires a getSessionID() method" | ||
| ); | ||
| } | ||
| return $this->defaultReturn; | ||
| } | ||
|
|
||
| if ($res->getSessionID() === null) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
| * This class will filters data based on the user having or not the permissions | ||
| * provided to the constructor | ||
| * | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| */ | ||
| class UserHasAnyPermission implements \LORIS\Data\Filter | ||
| { | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param string[] $permissions An array of permission objects. | ||
| */ | ||
| public function __construct(protected array $permissions) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
| * @param \User $user The user that the data is being | ||
| * filtered for. | ||
| * @param \LORIS\Data\DataInstance $resource The data being filtered. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | ||
| { | ||
| if ($user->hasAnyPermission($this->permissions)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
| * This class filters data based on whether the user is the creator of the resource. | ||
| * | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| */ | ||
| class UserIsCreator implements \LORIS\Data\Filter | ||
| { | ||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
| * @param \User $user The user that the data is being | ||
| * filtered for. | ||
| * @param \LORIS\Data\DataInstance $resource The data being filtered. | ||
| * | ||
| * @return bool true if the user is the creator of the resource | ||
| */ | ||
| public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | ||
| { | ||
| // phan only understands method_exists on simple variables, not | ||
| // Assigning to a variable is the a workaround | ||
| // for false positive 'CreatedBy()' doesn't exist errors suggested | ||
| // in https://github.com/phan/phan/issues/2628 | ||
| $res = $resource; | ||
| '@phan-var object $res'; | ||
| return $res->createdBy()->getId() === $user->getId(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,5 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file provides an implementation of the UserProjectMatch filter. | ||
| * | ||
| * PHP Version 7 | ||
skarya22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @category Data | ||
| * @package Main | ||
| * @subpackage Data | ||
| * @author Dave MacFarlane <[email protected]> | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| * @link https://www.github.com/aces/Loris/ | ||
| */ | ||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
|
|
@@ -22,15 +10,23 @@ | |
| * filtered out unless the User is a member of at least one project that the resource | ||
| * DataInstance is a member of. | ||
| * | ||
| * @category Data | ||
| * @package Main | ||
| * @subpackage Data | ||
| * @author Dave MacFarlane <[email protected]> | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| * @link https://www.github.com/aces/Loris/ | ||
| */ | ||
| class UserProjectMatch implements \LORIS\Data\Filter | ||
| { | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param ?bool $defaultReturn The default return value to return instead of | ||
| * throwing an exception when an exception is | ||
| * undesirable. | ||
| * | ||
| */ | ||
| public function __construct(protected ?bool $defaultReturn = null) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
|
|
@@ -44,7 +40,7 @@ public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | |
| { | ||
| // phan only understands method_exists on simple variables, not | ||
| // Assigning to a variable is the a workaround | ||
| // for false positive 'getCenterIDs doesn't exist errors suggested | ||
| // for false positive 'getProjectIDs doesn't exist errors suggested | ||
| // in https://github.com/phan/phan/issues/2628 | ||
| $res = $resource; | ||
| '@phan-var object $res'; | ||
|
|
@@ -69,10 +65,17 @@ public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | |
| // We don't know if the resource thought a null ProjectID | ||
| // should mean "no one can access it" or "anyone can access | ||
| // it", so throw an exception. | ||
| throw new \LorisException("getProjectID on resource returned null"); | ||
| if ($this->defaultReturn === null) { | ||
| throw new \LorisException("getProjectID on resource returned null"); | ||
| } | ||
| return $this->defaultReturn; | ||
| } | ||
| if ($this->defaultReturn === null) { | ||
| throw new \LorisException( | ||
| "Can not implement UserProjectMatch on a ". | ||
| "resource type that has no projects." | ||
| ); | ||
| } | ||
| throw new \LorisException( | ||
| "Can not implement UserProjectMatch on a resource type that has no projects." | ||
| ); | ||
| return $this->defaultReturn; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,5 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file provides an implementation of the UserSiteMatch filter. | ||
| * | ||
| * PHP Version 7 | ||
| * | ||
| * @category Data | ||
| * @package Main | ||
| * @subpackage Data | ||
| * @author Dave MacFarlane <[email protected]> | ||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| * @link https://www.github.com/aces/Loris/ | ||
| */ | ||
| namespace LORIS\Data\Filters; | ||
|
|
||
| /** | ||
|
|
@@ -21,16 +9,24 @@ | |
| * (or array) of CenterIDs that the data belongs to. The data will be filtered out | ||
| * unless the User is a member of at least one site that the resource DataInstance | ||
| * is a member of. | ||
| * | ||
| * @category Data | ||
| * @package Main | ||
| * @subpackage Data | ||
| * @author Dave MacFarlane <[email protected]> | ||
|
|
||
| * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 | ||
| * @link https://www.github.com/aces/Loris/ | ||
| */ | ||
| class UserSiteMatch implements \LORIS\Data\Filter | ||
| { | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param ?bool $defaultReturn The default return value to return instead of | ||
| * throwing an exception when an exception is | ||
| * indesirable. | ||
| * | ||
| */ | ||
| public function __construct(protected ?bool $defaultReturn = null) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Implements the \LORIS\Data\Filter interface | ||
| * | ||
|
|
@@ -54,8 +50,12 @@ public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool | |
| } elseif ($resource instanceof \LORIS\StudyEntities\SiteHaver) { | ||
| return $user->hasCenter($resource->getCenterID()); | ||
| } | ||
| throw new \LorisException( | ||
| "Can not implement UserSiteMatch on a resource type that has no sites." | ||
| ); | ||
|
|
||
| if ($this->defaultReturn === null) { | ||
| throw new \LorisException( | ||
| "Can not implement UserSiteMatch on a resource type that has no sites." | ||
| ); | ||
| } | ||
| return $this->defaultReturn; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just adding this for documentation, #7309 was involved in this too