Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Data/Filters/AccessibleResourceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
*/
class AccessibleResourceFilter 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
*
Expand All @@ -24,7 +37,12 @@ class AccessibleResourceFilter implements \LORIS\Data\Filter
public function filter(\User $user, \Loris\Data\DataInstance $resource) : bool
{
if (!($resource instanceof \LORIS\StudyEntities\AccessibleResource)) {
throw new \LorisException("Resource is not an AccessibleResource instance");
if ($this->defaultReturn === null) {
throw new \LorisException(
"Resource is not an AccessibleResource instance"
);
}
return $this->defaultReturn;
}
return $resource->isAccessibleBy($user);
}
Expand Down
44 changes: 44 additions & 0 deletions src/Data/Filters/CompositeANDFilter.php
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;
}
}
44 changes: 44 additions & 0 deletions src/Data/Filters/CompositeORFilter.php
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;
}
}
57 changes: 57 additions & 0 deletions src/Data/Filters/ResourceHasNoSession.php
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
Copy link
Contributor

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

$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;
}
}
39 changes: 39 additions & 0 deletions src/Data/Filters/UserHasAnyPermission.php
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;
}
}
31 changes: 31 additions & 0 deletions src/Data/Filters/UserIsCreator.php
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();
}
}
47 changes: 25 additions & 22 deletions src/Data/Filters/UserProjectMatch.php
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
*
* @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;

/**
Expand All @@ -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
*
Expand All @@ -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';
Expand All @@ -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;
}
}
42 changes: 21 additions & 21 deletions src/Data/Filters/UserSiteMatch.php
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;

/**
Expand All @@ -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
*
Expand All @@ -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;
}
}
Loading