diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 90f596eed8..a58e4037c6 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -5,6 +5,7 @@ namespace Utopia\Migration\Destinations; use Appwrite\AppwriteException; use Appwrite\Client; use Appwrite\Enums\Adapter; +use Appwrite\Enums\AuthMethod; use Appwrite\Enums\BuildRuntime; use Appwrite\Enums\Compression; use Appwrite\Enums\Framework; @@ -14,6 +15,7 @@ use Appwrite\Enums\SmtpEncryption; use Appwrite\InputFile; use Appwrite\Services\Functions; use Appwrite\Services\Messaging; +use Appwrite\Services\Project; use Appwrite\Services\Sites; use Appwrite\Services\Storage; use Appwrite\Services\Teams; @@ -37,6 +39,7 @@ use Utopia\Database\Validator\UID; use Utopia\Migration\Destination; use Utopia\Migration\Exception; use Utopia\Migration\Resource; +use Utopia\Migration\Resources\Auth\AuthMethods; use Utopia\Migration\Resources\Auth\Hash; use Utopia\Migration\Resources\Auth\Membership; use Utopia\Migration\Resources\Auth\Team; @@ -91,12 +94,13 @@ class Appwrite extends Destination ]; protected Client $client; - protected string $project; + protected string $projectId; protected string $key; private Functions $functions; private Messaging $messaging; + private Project $project; private Sites $sites; private Storage $storage; private Teams $teams; @@ -170,7 +174,7 @@ class Appwrite extends Destination protected OnDuplicate $onDuplicate = OnDuplicate::Fail, ?callable $getDatabaseDSN = null, ) { - $this->project = $project; + $this->projectId = $project; $this->endpoint = $endpoint; $this->key = $key; @@ -181,6 +185,7 @@ class Appwrite extends Destination $this->functions = new Functions($this->client); $this->messaging = new Messaging($this->client); + $this->project = new Project($this->client); $this->sites = new Sites($this->client); $this->storage = new Storage($this->client); $this->teams = new Teams($this->client); @@ -2160,6 +2165,10 @@ class Appwrite extends Destination userId: $user->getId(), ); break; + case Resource::TYPE_AUTH_METHODS: + /** @var AuthMethods $resource */ + $this->createAuthMethods($resource); + break; } $resource->setStatus(Resource::STATUS_SUCCESS); @@ -3175,7 +3184,7 @@ class Appwrite extends Destination '$id' => ID::unique(), '$permissions' => $resource->getPermissions(), 'projectInternalId' => $this->projectInternalId, - 'projectId' => $this->project, + 'projectId' => $this->projectId, 'name' => $resource->getWebhookName(), 'events' => $resource->getEvents(), 'url' => $resource->getUrl(), @@ -3194,7 +3203,7 @@ class Appwrite extends Destination return false; } - $this->dbForPlatform->purgeCachedDocument('projects', $this->project); + $this->dbForPlatform->purgeCachedDocument('projects', $this->projectId); return true; } @@ -3205,7 +3214,7 @@ class Appwrite extends Destination protected function createPlatform(Platform $resource): bool { $existing = $this->dbForPlatform->findOne('platforms', [ - Query::equal('projectId', [$this->project]), + Query::equal('projectId', [$this->projectId]), Query::equal('type', [$resource->getType()]), Query::equal('name', [$resource->getPlatformName()]), ]); @@ -3223,7 +3232,7 @@ class Appwrite extends Destination '$id' => ID::unique(), '$permissions' => $resource->getPermissions(), 'projectInternalId' => $this->projectInternalId, - 'projectId' => $this->project, + 'projectId' => $this->projectId, 'type' => $resource->getType(), 'name' => $resource->getPlatformName(), 'key' => $resource->getKey(), @@ -3237,7 +3246,32 @@ class Appwrite extends Destination return false; } - $this->dbForPlatform->purgeCachedDocument('projects', $this->project); + $this->dbForPlatform->purgeCachedDocument('projects', $this->projectId); + + return true; + } + + /** + * Flip each auth-method flag on the destination project via the SDK. Seven + * single-field updates rather than one bulk write — the SDK only exposes + * per-flag setters, and the destination needs to honor any server-side + * validation per flag (e.g. provider-specific guards). + */ + protected function createAuthMethods(AuthMethods $resource): bool + { + $flags = [ + [AuthMethod::EMAILPASSWORD(), $resource->getEmailPassword()], + [AuthMethod::MAGICURL(), $resource->getMagicURL()], + [AuthMethod::EMAILOTP(), $resource->getEmailOtp()], + [AuthMethod::ANONYMOUS(), $resource->getAnonymous()], + [AuthMethod::INVITES(), $resource->getInvites()], + [AuthMethod::JWT(), $resource->getJwt()], + [AuthMethod::PHONE(), $resource->getPhone()], + ]; + + foreach ($flags as [$method, $enabled]) { + $this->project->updateAuthMethod($method, $enabled); + } return true; } @@ -3264,7 +3298,7 @@ class Appwrite extends Destination '$id' => ID::unique(), '$permissions' => $resource->getPermissions(), 'resourceInternalId' => $this->projectInternalId, - 'resourceId' => $this->project, + 'resourceId' => $this->projectId, 'resourceType' => 'projects', 'name' => $resource->getApiKeyName(), 'scopes' => $resource->getScopes(), @@ -3280,7 +3314,7 @@ class Appwrite extends Destination return false; } - $this->dbForPlatform->purgeCachedDocument('projects', $this->project); + $this->dbForPlatform->purgeCachedDocument('projects', $this->projectId); return true; } diff --git a/src/Migration/Resource.php b/src/Migration/Resource.php index 9dc936ac74..dbd8474496 100644 --- a/src/Migration/Resource.php +++ b/src/Migration/Resource.php @@ -69,6 +69,8 @@ abstract class Resource implements \JsonSerializable public const TYPE_HASH = 'hash'; + public const TYPE_AUTH_METHODS = 'auth-methods'; + public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable'; // Integrations @@ -117,6 +119,7 @@ abstract class Resource implements \JsonSerializable self::TYPE_ENVIRONMENT_VARIABLE, self::TYPE_TEAM, self::TYPE_MEMBERSHIP, + self::TYPE_AUTH_METHODS, self::TYPE_PLATFORM, self::TYPE_API_KEY, self::TYPE_PROJECT_VARIABLE, diff --git a/src/Migration/Resources/Auth/AuthMethods.php b/src/Migration/Resources/Auth/AuthMethods.php new file mode 100644 index 0000000000..bc1adbe419 --- /dev/null +++ b/src/Migration/Resources/Auth/AuthMethods.php @@ -0,0 +1,113 @@ +id = $id; + $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; + } + + /** + * @param array $array + */ + public static function fromArray(array $array): self + { + return new self( + $array['id'], + (bool) ($array['emailPassword'] ?? true), + (bool) ($array['magicURL'] ?? true), + (bool) ($array['emailOtp'] ?? true), + (bool) ($array['anonymous'] ?? true), + (bool) ($array['invites'] ?? true), + (bool) ($array['jwt'] ?? true), + (bool) ($array['phone'] ?? true), + createdAt: $array['createdAt'] ?? '', + updatedAt: $array['updatedAt'] ?? '', + ); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'emailPassword' => $this->emailPassword, + 'magicURL' => $this->magicURL, + 'emailOtp' => $this->emailOtp, + 'anonymous' => $this->anonymous, + 'invites' => $this->invites, + 'jwt' => $this->jwt, + 'phone' => $this->phone, + 'createdAt' => $this->createdAt, + 'updatedAt' => $this->updatedAt, + ]; + } + + public static function getName(): string + { + return Resource::TYPE_AUTH_METHODS; + } + + public function getGroup(): string + { + return Transfer::GROUP_AUTH; + } + + public function getEmailPassword(): bool + { + return $this->emailPassword; + } + + public function getMagicURL(): bool + { + return $this->magicURL; + } + + public function getEmailOtp(): bool + { + return $this->emailOtp; + } + + public function getAnonymous(): bool + { + return $this->anonymous; + } + + public function getInvites(): bool + { + return $this->invites; + } + + public function getJwt(): bool + { + return $this->jwt; + } + + public function getPhone(): bool + { + return $this->phone; + } +} diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index fd03b7d044..ef74e24100 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -19,6 +19,7 @@ use Utopia\Database\DateTime as UtopiaDateTime; use Utopia\Database\Document as UtopiaDocument; use Utopia\Migration\Exception; use Utopia\Migration\Resource; +use Utopia\Migration\Resources\Auth\AuthMethods; use Utopia\Migration\Resources\Auth\Hash; use Utopia\Migration\Resources\Auth\Membership; use Utopia\Migration\Resources\Auth\Team; @@ -359,6 +360,11 @@ class Appwrite extends Source )->total; } } + + if (\in_array(Resource::TYPE_AUTH_METHODS, $resources)) { + // Singleton — there is exactly one auth-methods config per project. + $report[Resource::TYPE_AUTH_METHODS] = 1; + } } /** @@ -599,6 +605,48 @@ class Appwrite extends Source previous: $e )); } + + try { + if (\in_array(Resource::TYPE_AUTH_METHODS, $resources)) { + $this->exportAuthMethods(); + } + } catch (\Throwable $e) { + $this->addError(new Exception( + Resource::TYPE_AUTH_METHODS, + Transfer::GROUP_AUTH, + message: $e->getMessage(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, + previous: $e + )); + } + } + + /** + * Read auth-method flags from the source project document. + * No SDK Project.get() exists; using raw HTTP against /v1/project. + */ + private function exportAuthMethods(): void + { + $response = $this->call('GET', '/v1/project'); + + if (!\is_array($response)) { + return; + } + + $authMethods = new AuthMethods( + $this->projectId, + (bool) ($response['authEmailPassword'] ?? true), + (bool) ($response['authUsersAuthMagicURL'] ?? true), + (bool) ($response['authEmailOtp'] ?? true), + (bool) ($response['authAnonymous'] ?? true), + (bool) ($response['authInvites'] ?? true), + (bool) ($response['authJWT'] ?? true), + (bool) ($response['authPhone'] ?? true), + createdAt: $response['$createdAt'] ?? '', + updatedAt: $response['$updatedAt'] ?? '', + ); + + $this->callback([$authMethods]); } /** diff --git a/src/Migration/Transfer.php b/src/Migration/Transfer.php index 05c61f407d..bdc3c2a948 100644 --- a/src/Migration/Transfer.php +++ b/src/Migration/Transfer.php @@ -34,7 +34,8 @@ class Transfer Resource::TYPE_USER, Resource::TYPE_TEAM, Resource::TYPE_MEMBERSHIP, - Resource::TYPE_HASH + Resource::TYPE_HASH, + Resource::TYPE_AUTH_METHODS, ]; public const GROUP_STORAGE_RESOURCES = [ @@ -114,6 +115,7 @@ class Transfer Resource::TYPE_USER, Resource::TYPE_TEAM, Resource::TYPE_MEMBERSHIP, + Resource::TYPE_AUTH_METHODS, Resource::TYPE_FILE, Resource::TYPE_BUCKET, Resource::TYPE_FUNCTION,