From 8e0891c00d0258091bc7ced7ddd343abfd70ed46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 30 May 2026 20:28:07 +0200 Subject: [PATCH] Make policies LIST and GET easy to override --- .../Project/Http/Project/Policies/Get.php | 67 ++++++++++++------- .../Project/Http/Project/Policies/XList.php | 36 ++++++---- .../Utopia/Response/Model/PolicyList.php | 30 ++++++--- 3 files changed, 82 insertions(+), 51 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php index 7505cf62c4..c0c247936d 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/Get.php @@ -23,6 +23,9 @@ class Get extends Action public function __construct() { + $policies = $this->getPolicies(); + $policyIds = \array_keys($policies); + $this ->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) ->setHttpPath('/v1/project/policies/:policyId') @@ -40,31 +43,11 @@ class Get extends Action responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, - model: [ - Response::MODEL_POLICY_PASSWORD_DICTIONARY, - Response::MODEL_POLICY_PASSWORD_HISTORY, - Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA, - Response::MODEL_POLICY_SESSION_ALERT, - Response::MODEL_POLICY_SESSION_DURATION, - Response::MODEL_POLICY_SESSION_INVALIDATION, - Response::MODEL_POLICY_SESSION_LIMIT, - Response::MODEL_POLICY_USER_LIMIT, - Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, - ], + model: \array_values($policies), ) ] )) - ->param('policyId', '', new WhiteList([ - 'password-dictionary', - 'password-history', - 'password-personal-data', - 'session-alert', - 'session-duration', - 'session-invalidation', - 'session-limit', - 'user-limit', - 'membership-privacy', - ], true), 'Policy ID. Can be one of: password-dictionary, password-history, password-personal-data, session-alert, session-duration, session-invalidation, session-limit, user-limit, membership-privacy.', enum: new Enum(name: 'ProjectPolicyId')) + ->param('policyId', '', new WhiteList($policyIds, true), 'Policy ID. Can be one of: ' . \implode(', ', $policyIds) . '.', enum: new Enum(name: 'ProjectPolicyId')) ->inject('response') ->inject('project') ->callback($this->action(...)); @@ -75,9 +58,43 @@ class Get extends Action Response $response, Document $project, ): void { + $resolved = $this->resolvePolicy($policyId, $project); + + if ($resolved === null) { + throw new \LogicException('Unknown policy ID: ' . $policyId); + } + + [$policy, $model] = $resolved; + + $response->dynamic($policy, $model); + } + + /** + * @return array + */ + protected function getPolicies(): array + { + return [ + 'password-dictionary' => Response::MODEL_POLICY_PASSWORD_DICTIONARY, + 'password-history' => Response::MODEL_POLICY_PASSWORD_HISTORY, + 'password-personal-data' => Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA, + 'session-alert' => Response::MODEL_POLICY_SESSION_ALERT, + 'session-duration' => Response::MODEL_POLICY_SESSION_DURATION, + 'session-invalidation' => Response::MODEL_POLICY_SESSION_INVALIDATION, + 'session-limit' => Response::MODEL_POLICY_SESSION_LIMIT, + 'user-limit' => Response::MODEL_POLICY_USER_LIMIT, + 'membership-privacy' => Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, + ]; + } + + /** + * @return ?array + */ + protected function resolvePolicy(string $policyId, Document $project): ?array + { $auths = $project->getAttribute('auths', []); - [$policy, $model] = match ($policyId) { + return match ($policyId) { 'password-dictionary' => [ new Document([ '$id' => 'password-dictionary', @@ -145,9 +162,7 @@ class Get extends Action ]), Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, ], - default => throw new \LogicException('Unknown policy ID: ' . $policyId), + default => null, }; - - $response->dynamic($policy, $model); } } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php index 3020fa79dd..7057803c8a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/XList.php @@ -71,9 +71,30 @@ class XList extends Action throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); } + $policies = $this->getPolicies($project); + + $total = $includeTotal ? \count($policies) : 0; + + $grouped = Query::groupByType($queries); + $offset = $grouped['offset'] ?? 0; + $limit = $grouped['limit'] ?? null; + + $policies = \array_slice($policies, $offset, $limit); + + $response->dynamic(new Document([ + 'policies' => $policies, + 'total' => $total, + ]), Response::MODEL_POLICY_LIST); + } + + /** + * @return array + */ + protected function getPolicies(Document $project): array + { $auths = $project->getAttribute('auths', []); - $policies = [ + return [ new Document([ '$id' => 'password-dictionary', 'enabled' => $auths['passwordDictionary'] ?? false, @@ -115,18 +136,5 @@ class XList extends Action 'userMFA' => $auths['membershipsMfa'] ?? false, ]), ]; - - $total = $includeTotal ? \count($policies) : 0; - - $grouped = Query::groupByType($queries); - $offset = $grouped['offset'] ?? 0; - $limit = $grouped['limit'] ?? null; - - $policies = \array_slice($policies, $offset, $limit); - - $response->dynamic(new Document([ - 'policies' => $policies, - 'total' => $total, - ]), Response::MODEL_POLICY_LIST); } } diff --git a/src/Appwrite/Utopia/Response/Model/PolicyList.php b/src/Appwrite/Utopia/Response/Model/PolicyList.php index 09548fedcf..11704cdbff 100644 --- a/src/Appwrite/Utopia/Response/Model/PolicyList.php +++ b/src/Appwrite/Utopia/Response/Model/PolicyList.php @@ -17,23 +17,31 @@ class PolicyList extends Model 'example' => 9, ]) ->addRule('policies', [ - 'type' => [ - Response::MODEL_POLICY_PASSWORD_DICTIONARY, - Response::MODEL_POLICY_PASSWORD_HISTORY, - Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA, - Response::MODEL_POLICY_SESSION_ALERT, - Response::MODEL_POLICY_SESSION_DURATION, - Response::MODEL_POLICY_SESSION_INVALIDATION, - Response::MODEL_POLICY_SESSION_LIMIT, - Response::MODEL_POLICY_USER_LIMIT, - Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, - ], + 'type' => $this->getPolicyModels(), 'description' => 'List of policies.', 'default' => [], 'array' => true, ]); } + /** + * @return array + */ + protected function getPolicyModels(): array + { + return [ + Response::MODEL_POLICY_PASSWORD_DICTIONARY, + Response::MODEL_POLICY_PASSWORD_HISTORY, + Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA, + Response::MODEL_POLICY_SESSION_ALERT, + Response::MODEL_POLICY_SESSION_DURATION, + Response::MODEL_POLICY_SESSION_INVALIDATION, + Response::MODEL_POLICY_SESSION_LIMIT, + Response::MODEL_POLICY_USER_LIMIT, + Response::MODEL_POLICY_MEMBERSHIP_PRIVACY, + ]; + } + public function getName(): string { return 'Policies List';