Skip to content

Commit

Permalink
Merge branch '4.13' of https://github.com/craftcms/cms into 5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Oct 23, 2024
2 parents b3c33e4 + 701f159 commit 1a4ac19
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- Added `craft\events\DefineAddressCountriesEvent`. ([#15711](https://github.com/craftcms/cms/pull/15711))
- Added `craft\filters\BasicHttpAuthLogin`. ([#15720](https://github.com/craftcms/cms/pull/15720))
- Added `craft\filters\BasicHttpAuthStatic`. ([#15720](https://github.com/craftcms/cms/pull/15720))
- Added `craft\filters\ConditionalFilterTrait`. ([#15948](https://github.com/craftcms/cms/pull/15948))
- Added `craft\filters\SiteFilterTrait::$enabled`. ([#15720](https://github.com/craftcms/cms/pull/15720))
- Added `craft\filters\UtilityAccess`.
- Added `craft\helpers\Console::indent()`.
Expand Down
39 changes: 39 additions & 0 deletions src/filters/ConditionalFilterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\filters;

/**
* Filter for ensuring the user should be able to access the configured utility.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.5.0
*/
trait ConditionalFilterTrait
{
/**
* @var callable|null A PHP callable that determines when this filter should be applied.
*/
public mixed $when = null;

/**
* @inheritdoc
*/
protected function isActive(mixed $action): bool
{
// Retain only/except logic
if (!parent::isActive($action)) {
return false;
}

if (isset($this->when) && !call_user_func($this->when, $action)) {
return false;
}

return true;
}
}
12 changes: 5 additions & 7 deletions src/filters/SiteFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@
*/
trait SiteFilterTrait
{
/**
* @var bool Whether the filter should be enabled.
* @since 5.5.0
*/
public bool $enabled = true;
use ConditionalFilterTrait {
isActive as conditionalFilterTraitIsActive;
}

private null|array $siteIds = null;

protected function isActive(mixed $action): bool
{
if (!parent::isActive($action)) {
if (!$this->conditionalFilterTraitIsActive($action)) {
return false;
}

return $this->enabled && $this->isCurrentSiteActive();
return $this->isCurrentSiteActive();
}

protected function setSite(null|array|int|string|Site $value): void
Expand Down
11 changes: 2 additions & 9 deletions src/filters/UtilityAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,19 @@
*/
class UtilityAccess extends ActionFilter
{
use ConditionalFilterTrait;

/**
* @var string The utility class
* @phpstan-var class-string<UtilityInterface>
*/
public string $utility;

/**
* @var callable|null A PHP callable that determines when this filter should be applied.
*/
public mixed $when = null;

/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (isset($this->when) && !call_user_func($this->when, $action)) {
return true;
}

if (!Craft::$app->getUtilities()->checkAuthorization($this->utility)) {
throw new ForbiddenHttpException('User is not authorized to perform this action.');
}
Expand Down

0 comments on commit 1a4ac19

Please sign in to comment.