diff options
Diffstat (limited to 'Digitigrade/PolicyManager.php')
| -rw-r--r-- | Digitigrade/PolicyManager.php | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Digitigrade/PolicyManager.php b/Digitigrade/PolicyManager.php index 24c2c7f..69e2ab0 100644 --- a/Digitigrade/PolicyManager.php +++ b/Digitigrade/PolicyManager.php @@ -1,8 +1,10 @@ <?php namespace Digitigrade; +use Digitigrade\HttpResponseStatus\PolicyRejected; use Digitigrade\Model\Actor; use Digitigrade\Model\FetchableModel; +use Digitigrade\Model\Instance; use Digitigrade\Model\Interaction; use Digitigrade\Model\Note; @@ -34,6 +36,34 @@ class PolicyManager extends Singleton { }; } + public function checkOrThrow(FetchableModel $object) { + if (!$this->check($object)) { + throw new PolicyRejected(); + } + } + + /** + * Checks if an object is allowed to be sent to a certain Instance by all + * policies, and possibly alters it. + * @param FetchableModel $object the object to check (may be modified in-place) + * @param ?Instance $instance the instance it's being sent to (may be null if unknown) + * @return bool true if allowed, false if the request should be cancelled + */ + public function checkFederation(FetchableModel $object, ?Instance $instance): bool { + return match ($object::class) { + Actor::class => self::arrayAll($this->policies, fn(Policy $p) => $p->checkActorFederation($object, $instance)), + Interaction::class => self::arrayAll($this->policies, fn(Policy $p) => $p->checkInteractionFederation($object, $instance)), + Note::class => self::arrayAll($this->policies, fn(Policy $p) => $p->checkNoteFederation($object, $instance)), + default => true + }; + } + + public function checkFederationOrThrow(FetchableModel $object, ?Instance $instance) { + if (!$this->checkFederation($object, $instance)) { + throw new PolicyRejected(); + } + } + private static function arrayAll(array $arr, callable $test): bool { // i would use array_all but that's php 8.4+ only which i don't have lol foreach ($arr as $item) { |
