Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor User Migrations #33

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions bin/MigrationCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ function (array $resources) {
$this->drawFrame();
}
);

var_dump('Complete');
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,19 +522,15 @@ public function importAuthResource(Resource $resource): Resource
switch ($resource->getName()) {
case Resource::TYPE_USER:
/** @var User $resource */
if (in_array(User::TYPE_PASSWORD, $resource->getTypes())) {
if (! empty($resource->getPasswordHash())) {
$this->importPasswordUser($resource);
} elseif (in_array(User::TYPE_OAUTH, $resource->getTypes())) {
$resource->setStatus(Resource::STATUS_WARNING, 'OAuth users cannot be imported.');

return $resource;
} else {
$userService->create(
$resource->getId(),
$resource->getEmail(),
in_array(User::TYPE_PHONE, $resource->getTypes()) ? $resource->getPhone() : null,
$resource->getPhone(),
null,
$resource->getName()
$resource->getUsername()
);
}

Expand Down
51 changes: 9 additions & 42 deletions src/Migration/Resources/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,13 @@

class User extends Resource
{
public const TYPE_PASSWORD = 'password';
protected ?string $email = null;

public const TYPE_PHONE = 'phone';

public const TYPE_ANONYMOUS = 'anonymous';

public const TYPE_MAGIC = 'magic';

public const TYPE_OAUTH = 'oauth';

protected string $email = '';

protected string $username = '';
protected ?string $username = null;

protected ?Hash $passwordHash = null;

protected string $phone = '';

protected array $types = [self::TYPE_ANONYMOUS];
protected ?string $phone = null;

protected array $labels = [];

Expand All @@ -41,11 +29,10 @@ class User extends Resource

public function __construct(
string $id,
string $email = '',
string $username = '',
?string $email = null,
?string $username = null,
?Hash $passwordHash = null,
string $phone = '',
array $types = [self::TYPE_ANONYMOUS],
?string $phone = null,
array $labels = [],
string $oauthProvider = '',
bool $emailVerified = false,
Expand All @@ -58,7 +45,6 @@ public function __construct(
$this->username = $username;
$this->passwordHash = $passwordHash;
$this->phone = $phone;
$this->types = $types;
$this->labels = $labels;
$this->oauthProvider = $oauthProvider;
$this->emailVerified = $emailVerified;
Expand All @@ -78,7 +64,7 @@ public static function getName(): string
/**
* Get Email
*/
public function getEmail(): string
public function getEmail(): ?string
{
return $this->email;
}
Expand All @@ -96,7 +82,7 @@ public function setEmail(string $email): self
/**
* Get Username
*/
public function getUsername(): string
public function getUsername(): ?string
{
return $this->username;
}
Expand Down Expand Up @@ -132,7 +118,7 @@ public function setPasswordHash(Hash $passwordHash): self
/**
* Get Phone
*/
public function getPhone(): string
public function getPhone(): ?string
{
return $this->phone;
}
Expand All @@ -147,24 +133,6 @@ public function setPhone(string $phone): self
return $this;
}

/**
* Get Type
*/
public function getTypes(): array
{
return $this->types;
}

/**
* Set Types
*/
public function setTypes(string $types): self
{
$this->types = $types;

return $this;
}

/**
* Get Labels
*/
Expand Down Expand Up @@ -289,7 +257,6 @@ public function asArray(): array
'username' => $this->username,
'passwordHash' => $this->passwordHash ? $this->passwordHash->asArray() : null,
'phone' => $this->phone,
'types' => $this->types,
'oauthProvider' => $this->oauthProvider,
'emailVerified' => $this->emailVerified,
'phoneVerified' => $this->phoneVerified,
Expand Down
26 changes: 3 additions & 23 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,10 @@ private function exportUsers(int $batchSize)
foreach ($response['users'] as $user) {
$users[] = new User(
$user['$id'],
$user['email'],
$user['name'],
empty($user['email']) ? null : $user['email'],
empty($user['name']) ? null : $user['name'],
$user['password'] ? new Hash($user['password'], algorithm: $user['hash']) : null,
$user['phone'],
$this->calculateTypes($user),
empty($user['phone']) ? null : $user['phone'],
$user['labels'] ?? [],
'',
$user['emailVerification'] ?? false,
Expand Down Expand Up @@ -943,25 +942,6 @@ private function exportIndexes(int $batchSize)
}
}

private function calculateTypes(array $user): array
{
if (empty($user['email']) && empty($user['phone'])) {
return [User::TYPE_ANONYMOUS];
}

$types = [];

if (! empty($user['email']) && ! empty($user['password'])) {
$types[] = User::TYPE_PASSWORD;
}

if (! empty($user['phone'])) {
$types[] = User::TYPE_PHONE;
}

return $types;
}

protected function exportGroupStorage(int $batchSize, array $resources)
{
try {
Expand Down
26 changes: 0 additions & 26 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ private function exportUsers(int $batchSize)
$user['displayName'] ?? $user['email'] ?? '',
new Hash($user['passwordHash'] ?? '', $user['salt'] ?? '', Hash::ALGORITHM_SCRYPT_MODIFIED, $hashConfig['saltSeparator'] ?? '', $hashConfig['signerKey'] ?? ''),
$user['phoneNumber'] ?? '',
$this->calculateUserType($user['providerUserInfo'] ?? []),
[],
'',
$user['emailVerified'] ?? false,
Expand All @@ -233,31 +232,6 @@ private function exportUsers(int $batchSize)
}
}

private function calculateUserType(array $providerData): array
{
if (count($providerData) === 0) {
return [User::TYPE_ANONYMOUS];
}

$types = [];

foreach ($providerData as $provider) {
switch ($provider['providerId']) {
case 'password':
$types[] = User::TYPE_PASSWORD;
break;
case 'phone':
$types[] = User::TYPE_PHONE;
break;
default:
$types[] = User::TYPE_OAUTH;
break;
}
}

return $types;
}

protected function exportGroupDatabases(int $batchSize, array $resources)
{
// Check if Firestore is enabled
Expand Down
20 changes: 0 additions & 20 deletions src/Migration/Sources/NHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ private function exportUsers(int $batchSize)
$user['display_name'] ?? '',
new Hash($user['password_hash'], '', Hash::ALGORITHM_BCRYPT),
$user['phone_number'] ?? '',
$this->calculateUserTypes($user),
[],
'',
$user['email_verified'] ?? false,
Expand Down Expand Up @@ -590,25 +589,6 @@ private function convertIndex(array $index, Collection $collection): Index|false
}
}

private function calculateUserTypes(array $user): array
{
if (empty($user['password_hash']) && empty($user['phone_number'])) {
return [User::TYPE_ANONYMOUS];
}

$types = [];

if (! empty($user['password_hash'])) {
$types[] = User::TYPE_PASSWORD;
}

if (! empty($user['phone_number'])) {
$types[] = User::TYPE_PHONE;
}

return $types;
}

protected function exportGroupStorage(int $batchSize, array $resources)
{
try {
Expand Down
20 changes: 0 additions & 20 deletions src/Migration/Sources/Supabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ private function exportUsers(int $batchSize)
'',
new Hash($user['encrypted_password'], '', Hash::ALGORITHM_BCRYPT),
$user['phone'] ?? '',
$this->calculateAuthTypes($user),
[],
'',
! empty($user['email_confirmed_at']),
Expand All @@ -410,25 +409,6 @@ private function convertMimes(array $mimes): array
return $extensions;
}

private function calculateAuthTypes(array $user): array
{
if (empty($user['encrypted_password']) && empty($user['phone'])) {
return [User::TYPE_ANONYMOUS];
}

$types = [];

if (! empty($user['encrypted_password'])) {
$types[] = User::TYPE_PASSWORD;
}

if (! empty($user['phone'])) {
$types[] = User::TYPE_PHONE;
}

return $types;
}

protected function exportGroupStorage(int $batchSize, array $resources)
{
try {
Expand Down
1 change: 0 additions & 1 deletion tests/Migration/E2E/Sources/NHostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public function testValidateUserTransfer($state): void
$this->assertEquals('$2a$10$ARQ/f.K6OmCjZ8XF0U.6fezPMlxDqsmcl0Rs6xQVkvj62u7gcSzOW', $foundUser->getPasswordHash()->getHash());
$this->assertEquals('bcrypt', $foundUser->getPasswordHash()->getAlgorithm());
$this->assertEquals('[email protected]', $foundUser->getUsername());
$this->assertEquals(['password'], $foundUser->getTypes());
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/Migration/E2E/Sources/SupabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public function testValidateUserTransfer($state): void
$this->assertEquals('success', $foundUser->getStatus());
$this->assertEquals('$2a$10$NGZAAOfXeheUoH9V3dnRoeR.r3J5ynnSZ6KjvHxOUlV8XUrulJzQa', $foundUser->getPasswordHash()->getHash());
$this->assertEquals('bcrypt', $foundUser->getPasswordHash()->getAlgorithm());
$this->assertEquals(['password'], $foundUser->getTypes());
}

/**
Expand Down
Loading