Add AuthMethods migration

Singleton resource per project carrying the 7 auth-method flags
(email/password, magic URL, email OTP, anonymous, invites, JWT, phone).
Source reads via raw GET /v1/project (no SDK get() method exposed);
destination flips each flag via Project::updateAuthMethod().

Renames destination $project (string) -> $projectId so $project can
hold the Project SDK service, matching the source-side convention.
This commit is contained in:
Prem Palanisamy committed 2026-05-18 12:34:08 +01:00
1 parent ab731ca82d
commit 0c70a49ce7
5 files changed
+210 -10

No files matched your search

+43 -9
View File
@@ -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;
}
+3
View File
@@ -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,
@@ -0,0 +1,113 @@
<?php
namespace Utopia\Migration\Resources\Auth;
use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;
/**
* Singleton resource representing the project's auth-method flags.
* One per project; the destination updates the project doc, not a new row.
*/
class AuthMethods extends Resource
{
public function __construct(
string $id,
private readonly bool $emailPassword = true,
private readonly bool $magicURL = true,
private readonly bool $emailOtp = true,
private readonly bool $anonymous = true,
private readonly bool $invites = true,
private readonly bool $jwt = true,
private readonly bool $phone = true,
string $createdAt = '',
string $updatedAt = '',
) {
$this->id = $id;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}
/**
* @param array<string, mixed> $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<string, mixed>
*/
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;
}
}
+48
View File
@@ -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]);
}
/**
+3 -1
View File
@@ -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,