aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/PolicyManager.php
diff options
context:
space:
mode:
authorwinter2025-02-24 18:29:51 +0000
committerwinter2025-02-24 18:29:51 +0000
commita88283c526a779389efa2b819201a2331de0f2b2 (patch)
tree684e6aa432d6062373941e3018dfc744adc03ff5 /Digitigrade/PolicyManager.php
parent261ab1364aa40384b0315be60e78cd89ea7fe661 (diff)
implement federation policies and instance info substitution policy
Diffstat (limited to 'Digitigrade/PolicyManager.php')
-rw-r--r--Digitigrade/PolicyManager.php30
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) {