mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Make policies LIST and GET easy to override
This commit is contained in:
1 parent
8c9d04ecd8
commit
8e0891c00d
3 files changed
+82
-51
No files matched your search
@@ -23,6 +23,9 @@ class Get extends Action
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$policies = $this->getPolicies();
|
||||
$policyIds = \array_keys($policies);
|
||||
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/project/policies/:policyId')
|
||||
@@ -40,31 +43,11 @@ class Get extends Action
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_OK,
|
||||
model: [
|
||||
Response::MODEL_POLICY_PASSWORD_DICTIONARY,
|
||||
Response::MODEL_POLICY_PASSWORD_HISTORY,
|
||||
Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA,
|
||||
Response::MODEL_POLICY_SESSION_ALERT,
|
||||
Response::MODEL_POLICY_SESSION_DURATION,
|
||||
Response::MODEL_POLICY_SESSION_INVALIDATION,
|
||||
Response::MODEL_POLICY_SESSION_LIMIT,
|
||||
Response::MODEL_POLICY_USER_LIMIT,
|
||||
Response::MODEL_POLICY_MEMBERSHIP_PRIVACY,
|
||||
],
|
||||
model: \array_values($policies),
|
||||
)
|
||||
]
|
||||
))
|
||||
->param('policyId', '', new WhiteList([
|
||||
'password-dictionary',
|
||||
'password-history',
|
||||
'password-personal-data',
|
||||
'session-alert',
|
||||
'session-duration',
|
||||
'session-invalidation',
|
||||
'session-limit',
|
||||
'user-limit',
|
||||
'membership-privacy',
|
||||
], true), 'Policy ID. Can be one of: password-dictionary, password-history, password-personal-data, session-alert, session-duration, session-invalidation, session-limit, user-limit, membership-privacy.', enum: new Enum(name: 'ProjectPolicyId'))
|
||||
->param('policyId', '', new WhiteList($policyIds, true), 'Policy ID. Can be one of: ' . \implode(', ', $policyIds) . '.', enum: new Enum(name: 'ProjectPolicyId'))
|
||||
->inject('response')
|
||||
->inject('project')
|
||||
->callback($this->action(...));
|
||||
@@ -75,9 +58,43 @@ class Get extends Action
|
||||
Response $response,
|
||||
Document $project,
|
||||
): void {
|
||||
$resolved = $this->resolvePolicy($policyId, $project);
|
||||
|
||||
if ($resolved === null) {
|
||||
throw new \LogicException('Unknown policy ID: ' . $policyId);
|
||||
}
|
||||
|
||||
[$policy, $model] = $resolved;
|
||||
|
||||
$response->dynamic($policy, $model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function getPolicies(): array
|
||||
{
|
||||
return [
|
||||
'password-dictionary' => Response::MODEL_POLICY_PASSWORD_DICTIONARY,
|
||||
'password-history' => Response::MODEL_POLICY_PASSWORD_HISTORY,
|
||||
'password-personal-data' => Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA,
|
||||
'session-alert' => Response::MODEL_POLICY_SESSION_ALERT,
|
||||
'session-duration' => Response::MODEL_POLICY_SESSION_DURATION,
|
||||
'session-invalidation' => Response::MODEL_POLICY_SESSION_INVALIDATION,
|
||||
'session-limit' => Response::MODEL_POLICY_SESSION_LIMIT,
|
||||
'user-limit' => Response::MODEL_POLICY_USER_LIMIT,
|
||||
'membership-privacy' => Response::MODEL_POLICY_MEMBERSHIP_PRIVACY,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?array<mixed>
|
||||
*/
|
||||
protected function resolvePolicy(string $policyId, Document $project): ?array
|
||||
{
|
||||
$auths = $project->getAttribute('auths', []);
|
||||
|
||||
[$policy, $model] = match ($policyId) {
|
||||
return match ($policyId) {
|
||||
'password-dictionary' => [
|
||||
new Document([
|
||||
'$id' => 'password-dictionary',
|
||||
@@ -145,9 +162,7 @@ class Get extends Action
|
||||
]),
|
||||
Response::MODEL_POLICY_MEMBERSHIP_PRIVACY,
|
||||
],
|
||||
default => throw new \LogicException('Unknown policy ID: ' . $policyId),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$response->dynamic($policy, $model);
|
||||
}
|
||||
}
|
||||
@@ -71,9 +71,30 @@ class XList extends Action
|
||||
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||
}
|
||||
|
||||
$policies = $this->getPolicies($project);
|
||||
|
||||
$total = $includeTotal ? \count($policies) : 0;
|
||||
|
||||
$grouped = Query::groupByType($queries);
|
||||
$offset = $grouped['offset'] ?? 0;
|
||||
$limit = $grouped['limit'] ?? null;
|
||||
|
||||
$policies = \array_slice($policies, $offset, $limit);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'policies' => $policies,
|
||||
'total' => $total,
|
||||
]), Response::MODEL_POLICY_LIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Document>
|
||||
*/
|
||||
protected function getPolicies(Document $project): array
|
||||
{
|
||||
$auths = $project->getAttribute('auths', []);
|
||||
|
||||
$policies = [
|
||||
return [
|
||||
new Document([
|
||||
'$id' => 'password-dictionary',
|
||||
'enabled' => $auths['passwordDictionary'] ?? false,
|
||||
@@ -115,18 +136,5 @@ class XList extends Action
|
||||
'userMFA' => $auths['membershipsMfa'] ?? false,
|
||||
]),
|
||||
];
|
||||
|
||||
$total = $includeTotal ? \count($policies) : 0;
|
||||
|
||||
$grouped = Query::groupByType($queries);
|
||||
$offset = $grouped['offset'] ?? 0;
|
||||
$limit = $grouped['limit'] ?? null;
|
||||
|
||||
$policies = \array_slice($policies, $offset, $limit);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'policies' => $policies,
|
||||
'total' => $total,
|
||||
]), Response::MODEL_POLICY_LIST);
|
||||
}
|
||||
}
|
||||
@@ -17,23 +17,31 @@ class PolicyList extends Model
|
||||
'example' => 9,
|
||||
])
|
||||
->addRule('policies', [
|
||||
'type' => [
|
||||
Response::MODEL_POLICY_PASSWORD_DICTIONARY,
|
||||
Response::MODEL_POLICY_PASSWORD_HISTORY,
|
||||
Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA,
|
||||
Response::MODEL_POLICY_SESSION_ALERT,
|
||||
Response::MODEL_POLICY_SESSION_DURATION,
|
||||
Response::MODEL_POLICY_SESSION_INVALIDATION,
|
||||
Response::MODEL_POLICY_SESSION_LIMIT,
|
||||
Response::MODEL_POLICY_USER_LIMIT,
|
||||
Response::MODEL_POLICY_MEMBERSHIP_PRIVACY,
|
||||
],
|
||||
'type' => $this->getPolicyModels(),
|
||||
'description' => 'List of policies.',
|
||||
'default' => [],
|
||||
'array' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
protected function getPolicyModels(): array
|
||||
{
|
||||
return [
|
||||
Response::MODEL_POLICY_PASSWORD_DICTIONARY,
|
||||
Response::MODEL_POLICY_PASSWORD_HISTORY,
|
||||
Response::MODEL_POLICY_PASSWORD_PERSONAL_DATA,
|
||||
Response::MODEL_POLICY_SESSION_ALERT,
|
||||
Response::MODEL_POLICY_SESSION_DURATION,
|
||||
Response::MODEL_POLICY_SESSION_INVALIDATION,
|
||||
Response::MODEL_POLICY_SESSION_LIMIT,
|
||||
Response::MODEL_POLICY_USER_LIMIT,
|
||||
Response::MODEL_POLICY_MEMBERSHIP_PRIVACY,
|
||||
];
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'Policies List';
|
||||
|
||||
Reference in new issue
Block a user