mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Add Policies migration
This commit is contained in:
1 parent
2f72707343
commit
eab858e31f
5 files changed
+257
No files matched your search
@@ -44,6 +44,7 @@ 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\Policies;
|
||||
use Utopia\Migration\Resources\Auth\Team;
|
||||
use Utopia\Migration\Resources\Auth\User;
|
||||
use Utopia\Migration\Resources\Database\Attribute;
|
||||
@@ -252,6 +253,7 @@ class Appwrite extends Destination
|
||||
Resource::TYPE_TEAM,
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_AUTH_METHODS,
|
||||
Resource::TYPE_POLICIES,
|
||||
|
||||
// Database
|
||||
Resource::TYPE_DATABASE,
|
||||
@@ -2178,6 +2180,10 @@ class Appwrite extends Destination
|
||||
/** @var AuthMethods $resource */
|
||||
$this->createAuthMethods($resource);
|
||||
break;
|
||||
case Resource::TYPE_POLICIES:
|
||||
/** @var Policies $resource */
|
||||
$this->createPolicies($resource);
|
||||
break;
|
||||
}
|
||||
|
||||
$resource->setStatus(Resource::STATUS_SUCCESS);
|
||||
@@ -3362,6 +3368,33 @@ class Appwrite extends Destination
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replay each project security policy on the destination via the SDK. Nine
|
||||
* single-policy updates rather than one bulk write — each policy endpoint
|
||||
* runs its own validation (history bounds, session-duration window, etc.),
|
||||
* and membership-privacy bundles its five sub-flags into one call.
|
||||
*/
|
||||
protected function createPolicies(Policies $resource): bool
|
||||
{
|
||||
$this->project->updatePasswordHistoryPolicy($resource->getPasswordHistory());
|
||||
$this->project->updateSessionDurationPolicy($resource->getSessionDuration());
|
||||
$this->project->updateSessionLimitPolicy($resource->getSessionsLimit());
|
||||
$this->project->updateUserLimitPolicy($resource->getUserLimit());
|
||||
$this->project->updatePasswordDictionaryPolicy($resource->getPasswordDictionary());
|
||||
$this->project->updatePasswordPersonalDataPolicy($resource->getPersonalDataCheck());
|
||||
$this->project->updateSessionAlertPolicy($resource->getSessionAlerts());
|
||||
$this->project->updateSessionInvalidationPolicy($resource->getSessionInvalidation());
|
||||
$this->project->updateMembershipPrivacyPolicy(
|
||||
userId: $resource->getMembershipsUserId(),
|
||||
userEmail: $resource->getMembershipsUserEmail(),
|
||||
userPhone: $resource->getMembershipsUserPhone(),
|
||||
userName: $resource->getMembershipsUserName(),
|
||||
userMFA: $resource->getMembershipsUserMfa(),
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function createApiKey(ApiKey $resource): bool
|
||||
{
|
||||
$existing = $this->dbForPlatform->findOne('keys', [
|
||||
|
||||
@@ -71,6 +71,8 @@ abstract class Resource implements \JsonSerializable
|
||||
|
||||
public const TYPE_AUTH_METHODS = 'auth-methods';
|
||||
|
||||
public const TYPE_POLICIES = 'policies';
|
||||
|
||||
public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable';
|
||||
|
||||
// Integrations
|
||||
@@ -123,6 +125,7 @@ abstract class Resource implements \JsonSerializable
|
||||
self::TYPE_TEAM,
|
||||
self::TYPE_MEMBERSHIP,
|
||||
self::TYPE_AUTH_METHODS,
|
||||
self::TYPE_POLICIES,
|
||||
self::TYPE_PLATFORM,
|
||||
self::TYPE_API_KEY,
|
||||
self::TYPE_PROJECT_VARIABLE,
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Auth;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
/**
|
||||
* Singleton auth resource carrying the project's security policies:
|
||||
* password rules, session behavior, user limits, membership privacy.
|
||||
*
|
||||
* 9 policies, 13 fields total — MembershipPrivacy alone has 5 sub-flags.
|
||||
*/
|
||||
class Policies extends Resource
|
||||
{
|
||||
public function __construct(
|
||||
string $id,
|
||||
private readonly int $passwordHistory = 0,
|
||||
private readonly int $sessionDuration = 31536000,
|
||||
private readonly int $sessionsLimit = 100,
|
||||
private readonly int $userLimit = 0,
|
||||
private readonly bool $passwordDictionary = false,
|
||||
private readonly bool $personalDataCheck = false,
|
||||
private readonly bool $sessionAlerts = false,
|
||||
private readonly bool $sessionInvalidation = false,
|
||||
private readonly bool $membershipsUserId = true,
|
||||
private readonly bool $membershipsUserEmail = true,
|
||||
private readonly bool $membershipsUserName = true,
|
||||
private readonly bool $membershipsUserMfa = true,
|
||||
private readonly bool $membershipsUserPhone = 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'],
|
||||
(int) ($array['passwordHistory'] ?? 0),
|
||||
(int) ($array['sessionDuration'] ?? 31536000),
|
||||
(int) ($array['sessionsLimit'] ?? 100),
|
||||
(int) ($array['userLimit'] ?? 0),
|
||||
(bool) ($array['passwordDictionary'] ?? false),
|
||||
(bool) ($array['personalDataCheck'] ?? false),
|
||||
(bool) ($array['sessionAlerts'] ?? false),
|
||||
(bool) ($array['sessionInvalidation'] ?? false),
|
||||
(bool) ($array['membershipsUserId'] ?? true),
|
||||
(bool) ($array['membershipsUserEmail'] ?? true),
|
||||
(bool) ($array['membershipsUserName'] ?? true),
|
||||
(bool) ($array['membershipsUserMfa'] ?? true),
|
||||
(bool) ($array['membershipsUserPhone'] ?? true),
|
||||
createdAt: $array['createdAt'] ?? '',
|
||||
updatedAt: $array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'passwordHistory' => $this->passwordHistory,
|
||||
'sessionDuration' => $this->sessionDuration,
|
||||
'sessionsLimit' => $this->sessionsLimit,
|
||||
'userLimit' => $this->userLimit,
|
||||
'passwordDictionary' => $this->passwordDictionary,
|
||||
'personalDataCheck' => $this->personalDataCheck,
|
||||
'sessionAlerts' => $this->sessionAlerts,
|
||||
'sessionInvalidation' => $this->sessionInvalidation,
|
||||
'membershipsUserId' => $this->membershipsUserId,
|
||||
'membershipsUserEmail' => $this->membershipsUserEmail,
|
||||
'membershipsUserName' => $this->membershipsUserName,
|
||||
'membershipsUserMfa' => $this->membershipsUserMfa,
|
||||
'membershipsUserPhone' => $this->membershipsUserPhone,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_POLICIES;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_AUTH;
|
||||
}
|
||||
|
||||
public function getPasswordHistory(): int
|
||||
{
|
||||
return $this->passwordHistory;
|
||||
}
|
||||
|
||||
public function getSessionDuration(): int
|
||||
{
|
||||
return $this->sessionDuration;
|
||||
}
|
||||
|
||||
public function getSessionsLimit(): int
|
||||
{
|
||||
return $this->sessionsLimit;
|
||||
}
|
||||
|
||||
public function getUserLimit(): int
|
||||
{
|
||||
return $this->userLimit;
|
||||
}
|
||||
|
||||
public function getPasswordDictionary(): bool
|
||||
{
|
||||
return $this->passwordDictionary;
|
||||
}
|
||||
|
||||
public function getPersonalDataCheck(): bool
|
||||
{
|
||||
return $this->personalDataCheck;
|
||||
}
|
||||
|
||||
public function getSessionAlerts(): bool
|
||||
{
|
||||
return $this->sessionAlerts;
|
||||
}
|
||||
|
||||
public function getSessionInvalidation(): bool
|
||||
{
|
||||
return $this->sessionInvalidation;
|
||||
}
|
||||
|
||||
public function getMembershipsUserId(): bool
|
||||
{
|
||||
return $this->membershipsUserId;
|
||||
}
|
||||
|
||||
public function getMembershipsUserEmail(): bool
|
||||
{
|
||||
return $this->membershipsUserEmail;
|
||||
}
|
||||
|
||||
public function getMembershipsUserName(): bool
|
||||
{
|
||||
return $this->membershipsUserName;
|
||||
}
|
||||
|
||||
public function getMembershipsUserMfa(): bool
|
||||
{
|
||||
return $this->membershipsUserMfa;
|
||||
}
|
||||
|
||||
public function getMembershipsUserPhone(): bool
|
||||
{
|
||||
return $this->membershipsUserPhone;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ 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\Policies;
|
||||
use Utopia\Migration\Resources\Auth\Team;
|
||||
use Utopia\Migration\Resources\Auth\User;
|
||||
use Utopia\Migration\Resources\Database\Attribute;
|
||||
@@ -179,6 +180,7 @@ class Appwrite extends Source
|
||||
Resource::TYPE_TEAM,
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_AUTH_METHODS,
|
||||
Resource::TYPE_POLICIES,
|
||||
|
||||
// Database
|
||||
Resource::TYPE_DATABASE,
|
||||
@@ -372,6 +374,11 @@ class Appwrite extends Source
|
||||
// Singleton — there is exactly one auth-methods config per project.
|
||||
$report[Resource::TYPE_AUTH_METHODS] = 1;
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_POLICIES, $resources)) {
|
||||
// Singleton — one policies config per project.
|
||||
$report[Resource::TYPE_POLICIES] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -626,6 +633,55 @@ class Appwrite extends Source
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
|
||||
try {
|
||||
if (\in_array(Resource::TYPE_POLICIES, $resources)) {
|
||||
$this->exportPolicies();
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
Resource::TYPE_POLICIES,
|
||||
Transfer::GROUP_AUTH,
|
||||
message: $e->getMessage(),
|
||||
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read project security policies from /v1/project. 9 sub-policies collapse
|
||||
* into a single Policies resource carrying 13 fields (MembershipPrivacy
|
||||
* alone has 5 sub-flags).
|
||||
*/
|
||||
private function exportPolicies(): void
|
||||
{
|
||||
$response = $this->call('GET', '/v1/project');
|
||||
|
||||
if (!\is_array($response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$policies = new Policies(
|
||||
$this->projectId,
|
||||
(int) ($response['authPasswordHistory'] ?? 0),
|
||||
(int) ($response['authDuration'] ?? 31536000),
|
||||
(int) ($response['authSessionsLimit'] ?? 100),
|
||||
(int) ($response['authLimit'] ?? 0),
|
||||
(bool) ($response['authPasswordDictionary'] ?? false),
|
||||
(bool) ($response['authPersonalDataCheck'] ?? false),
|
||||
(bool) ($response['authSessionAlerts'] ?? false),
|
||||
(bool) ($response['authInvalidateSessions'] ?? false),
|
||||
(bool) ($response['authMembershipsUserId'] ?? true),
|
||||
(bool) ($response['authMembershipsUserEmail'] ?? true),
|
||||
(bool) ($response['authMembershipsUserName'] ?? true),
|
||||
(bool) ($response['authMembershipsMfa'] ?? true),
|
||||
(bool) ($response['authMembershipsUserPhone'] ?? true),
|
||||
createdAt: $response['$createdAt'] ?? '',
|
||||
updatedAt: $response['$updatedAt'] ?? '',
|
||||
);
|
||||
|
||||
$this->callback([$policies]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -36,6 +36,7 @@ class Transfer
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_HASH,
|
||||
Resource::TYPE_AUTH_METHODS,
|
||||
Resource::TYPE_POLICIES,
|
||||
];
|
||||
|
||||
public const GROUP_STORAGE_RESOURCES = [
|
||||
@@ -119,6 +120,7 @@ class Transfer
|
||||
Resource::TYPE_TEAM,
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_AUTH_METHODS,
|
||||
Resource::TYPE_POLICIES,
|
||||
Resource::TYPE_FILE,
|
||||
Resource::TYPE_BUCKET,
|
||||
Resource::TYPE_FUNCTION,
|
||||
|
||||
Reference in new issue
Block a user