From 443ebac71c4fc74bf9a4d64cd0746c46430fdd84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 11 May 2026 11:56:22 +0200 Subject: [PATCH] Finish project response model --- app/init/models.php | 6 + src/Appwrite/Utopia/Response.php | 3 + .../Utopia/Response/Model/Project.php | 139 ++++-------------- .../Response/Model/ProjectAuthMethod.php | 47 ++++++ .../Utopia/Response/Model/ProjectProtocol.php | 47 ++++++ .../Utopia/Response/Model/ProjectService.php | 47 ++++++ 6 files changed, 175 insertions(+), 114 deletions(-) create mode 100644 src/Appwrite/Utopia/Response/Model/ProjectAuthMethod.php create mode 100644 src/Appwrite/Utopia/Response/Model/ProjectProtocol.php create mode 100644 src/Appwrite/Utopia/Response/Model/ProjectService.php diff --git a/app/init/models.php b/app/init/models.php index 8276e2561e..f983c43f0a 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -173,6 +173,9 @@ use Appwrite\Utopia\Response\Model\PolicySessionLimit; use Appwrite\Utopia\Response\Model\PolicyUserLimit; use Appwrite\Utopia\Response\Model\Preferences; use Appwrite\Utopia\Response\Model\Project; +use Appwrite\Utopia\Response\Model\ProjectAuthMethod; +use Appwrite\Utopia\Response\Model\ProjectProtocol; +use Appwrite\Utopia\Response\Model\ProjectService; use Appwrite\Utopia\Response\Model\Provider; use Appwrite\Utopia\Response\Model\ProviderRepository; use Appwrite\Utopia\Response\Model\ProviderRepositoryFramework; @@ -397,6 +400,9 @@ Response::setModel(new FrameworkAdapter()); Response::setModel(new Deployment()); Response::setModel(new Execution()); Response::setModel(new Project()); +Response::setModel(new ProjectAuthMethod()); +Response::setModel(new ProjectService()); +Response::setModel(new ProjectProtocol()); Response::setModel(new Webhook()); Response::setModel(new Key()); Response::setModel(new EphemeralKey()); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index e02e70b3d8..01f86cb5e8 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -249,6 +249,9 @@ class Response extends SwooleResponse // Project public const MODEL_PROJECT = 'project'; public const MODEL_PROJECT_LIST = 'projectList'; + public const MODEL_PROJECT_AUTH_METHOD = 'projectAuthMethod'; + public const MODEL_PROJECT_SERVICE = 'projectService'; + public const MODEL_PROJECT_PROTOCOL = 'projectProtocol'; public const MODEL_WEBHOOK = 'webhook'; public const MODEL_WEBHOOK_LIST = 'webhookList'; public const MODEL_KEY = 'key'; diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index 132b228ed6..b25fdc51e9 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -174,58 +174,6 @@ class Project extends Model 'array' => true, ]) ; - - // Resource: Auth methods - $auth = Config::getParam('auth', []); - foreach ($auth as $index => $method) { - $name = $method['name'] ?? ''; - $key = $method['key'] ?? ''; - - $this - ->addRule('auth' . ucfirst($key), [ - 'type' => self::TYPE_BOOLEAN, - 'description' => $name . ' auth method status', - 'example' => true, - 'default' => true, - ]) - ; - } - - // Resource: Services - $services = Config::getParam('services', []); - foreach ($services as $service) { - if (!$service['optional']) { - continue; - } - - $name = $service['name'] ?? ''; - $key = $service['key'] ?? ''; - - $this - ->addRule('serviceStatusFor' . ucfirst($key), [ - 'type' => self::TYPE_BOOLEAN, - 'description' => $name . ' service status', - 'example' => true, - 'default' => true, - ]) - ; - } - - // Resource: Protocols - $apis = Config::getParam('protocols', []); - foreach ($apis as $api) { - $name = $api['name'] ?? ''; - $key = $api['key'] ?? ''; - - $this - ->addRule('protocolStatusFor' . ucfirst($key), [ - 'type' => self::TYPE_BOOLEAN, - 'description' => $name . ' protocol status', - 'example' => true, - 'default' => true, - ]) - ; - } } /** @@ -259,7 +207,6 @@ class Project extends Model $this->expandServiceFields($document); $this->expandApiFields($document); $this->expandAuthFields($document); - $this->expandOAuthProviders($document); return $document; } @@ -270,8 +217,8 @@ class Project extends Model return; } - // SMTP $smtp = $document->getAttribute('smtp', []); + $document->setAttribute('smtpEnabled', $smtp['enabled'] ?? false); $document->setAttribute('smtpSenderEmail', $smtp['senderEmail'] ?? ''); $document->setAttribute('smtpSenderName', $smtp['senderName'] ?? ''); @@ -291,16 +238,20 @@ class Project extends Model } $values = $document->getAttribute('services', []); - $services = Config::getParam('services', []); + $services = []; - foreach ($services as $service) { + foreach (Config::getParam('services', []) as $id => $service) { if (!$service['optional']) { continue; } - $key = $service['key'] ?? ''; - $value = $values[$key] ?? true; - $document->setAttribute('serviceStatusFor' . ucfirst($key), $value); + + $services[] = new Document([ + '$id' => $id, + 'enabled' => $values[$service['key']] ?? true, + ]); } + + $document->setAttribute('services', $services); } private function expandApiFields(Document $document): void @@ -310,13 +261,16 @@ class Project extends Model } $values = $document->getAttribute('apis', []); - $apis = Config::getParam('protocols', []); + $protocols = []; - foreach ($apis as $api) { - $key = $api['key'] ?? ''; - $value = $values[$key] ?? true; - $document->setAttribute('protocolStatusFor' . ucfirst($key), $value); + foreach (Config::getParam('protocols', []) as $id => $api) { + $protocols[] = new Document([ + '$id' => $id, + 'enabled' => $values[$api['key']] ?? true, + ]); } + + $document->setAttribute('protocols', $protocols); } private function expandAuthFields(Document $document): void @@ -326,58 +280,15 @@ class Project extends Model } $authValues = $document->getAttribute('auths', []); - $auth = Config::getParam('auth', []); + $authMethods = []; - $document->setAttribute('authLimit', $authValues['limit'] ?? 0); - $document->setAttribute('authDuration', $authValues['duration'] ?? TOKEN_EXPIRATION_LOGIN_LONG); - $document->setAttribute('authSessionsLimit', $authValues['maxSessions'] ?? 0); - $document->setAttribute('authPasswordHistory', $authValues['passwordHistory'] ?? 0); - $document->setAttribute('authPasswordDictionary', $authValues['passwordDictionary'] ?? false); - $document->setAttribute('authPersonalDataCheck', $authValues['personalDataCheck'] ?? false); - $document->setAttribute('authDisposableEmails', $authValues['disposableEmails'] ?? false); - $document->setAttribute('authCanonicalEmails', $authValues['canonicalEmails'] ?? false); - $document->setAttribute('authFreeEmails', $authValues['freeEmails'] ?? false); - $document->setAttribute('authMockNumbers', $authValues['mockNumbers'] ?? []); - $document->setAttribute('authSessionAlerts', $authValues['sessionAlerts'] ?? false); - $document->setAttribute('authMembershipsUserName', $authValues['membershipsUserName'] ?? false); - $document->setAttribute('authMembershipsUserEmail', $authValues['membershipsUserEmail'] ?? false); - $document->setAttribute('authMembershipsMfa', $authValues['membershipsMfa'] ?? false); - $document->setAttribute('authMembershipsUserId', $authValues['membershipsUserId'] ?? false); - $document->setAttribute('authMembershipsUserPhone', $authValues['membershipsUserPhone'] ?? false); - $document->setAttribute('authInvalidateSessions', $authValues['invalidateSessions'] ?? false); - - foreach ($auth as $method) { - $key = $method['key']; - $value = $authValues[$key] ?? true; - $document->setAttribute('auth' . ucfirst($key), $value); - } - } - - private function expandOAuthProviders(Document $document): void - { - if (!$document->isSet('oAuthProviders')) { - return; - } - - $providers = Config::getParam('oAuthProviders', []); - $providerValues = $document->getAttribute('oAuthProviders', []); - $projectProviders = []; - - foreach ($providers as $key => $provider) { - if (!$provider['enabled']) { - // Disabled by Appwrite configuration, exclude from response - continue; - } - - $projectProviders[] = new Document([ - 'key' => $key, - 'name' => $provider['name'] ?? '', - 'appId' => $providerValues[$key . 'Appid'] ?? '', - 'secret' => '', // Write-only: never expose the stored value - 'enabled' => $providerValues[$key . 'Enabled'] ?? false, + foreach (Config::getParam('auth', []) as $id => $method) { + $authMethods[] = new Document([ + '$id' => $id, + 'enabled' => $authValues[$method['key']] ?? true ]); } - - $document->setAttribute('oAuthProviders', $projectProviders); + + $document->setAttribute('authMethods', $authMethods); } } diff --git a/src/Appwrite/Utopia/Response/Model/ProjectAuthMethod.php b/src/Appwrite/Utopia/Response/Model/ProjectAuthMethod.php new file mode 100644 index 0000000000..b4e9820c54 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/ProjectAuthMethod.php @@ -0,0 +1,47 @@ +addRule('$id', [ + 'type' => self::TYPE_STRING, + 'description' => 'Auth method ID.', + 'default' => '', + 'example' => 'email-password', + ]) + ->addRule('enabled', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Auth method status.', + 'example' => false, + 'default' => true, + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'ProjectAuthMethod'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_PROJECT_AUTH_METHOD; + } +} diff --git a/src/Appwrite/Utopia/Response/Model/ProjectProtocol.php b/src/Appwrite/Utopia/Response/Model/ProjectProtocol.php new file mode 100644 index 0000000000..51addc5366 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/ProjectProtocol.php @@ -0,0 +1,47 @@ +addRule('$id', [ + 'type' => self::TYPE_STRING, + 'description' => 'Protocol ID.', + 'default' => '', + 'example' => 'email-password', + ]) + ->addRule('enabled', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Protocol status.', + 'example' => false, + 'default' => true, + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'ProjectProtocol'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_PROJECT_PROTOCOL; + } +} diff --git a/src/Appwrite/Utopia/Response/Model/ProjectService.php b/src/Appwrite/Utopia/Response/Model/ProjectService.php new file mode 100644 index 0000000000..654936ffb4 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/ProjectService.php @@ -0,0 +1,47 @@ +addRule('$id', [ + 'type' => self::TYPE_STRING, + 'description' => 'Service ID.', + 'default' => '', + 'example' => 'email-password', + ]) + ->addRule('enabled', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Service status.', + 'example' => false, + 'default' => true, + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'ProjectService'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_PROJECT_SERVICE; + } +}