From a88283c526a779389efa2b819201a2331de0f2b2 Mon Sep 17 00:00:00 2001 From: winter Date: Mon, 24 Feb 2025 18:29:51 +0000 Subject: implement federation policies and instance info substitution policy --- Digitigrade/PolicyManager.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'Digitigrade/PolicyManager.php') 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 @@ 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) { -- cgit v1.3