Update anon login limits and tests

This commit is contained in:
Eldad Fux
2021-04-03 11:56:32 +03:00
parent f070eb0a3a
commit d9528ef1ce
2 changed files with 26 additions and 2 deletions
+18 -2
View File
@@ -613,9 +613,10 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
App::post('/v1/account/sessions/anonymous')
->desc('Create Anonymous Session')
->groups(['api', 'account'])
->groups(['api', 'account', 'auth'])
->label('event', 'account.sessions.create')
->label('scope', 'public')
->label('auth.type', 'anonymous')
->label('sdk.platform', [APP_PLATFORM_CLIENT])
->label('sdk.namespace', 'account')
->label('sdk.method', 'createAnonymousSession')
@@ -649,6 +650,22 @@ App::post('/v1/account/sessions/anonymous')
throw new Exception('Failed to create anonymous user.', 401);
}
$limit = $project->getAttribute('usersAuthLimit', 0);
if ($limit !== 0) {
$projectDB->getCollection([ // Count users
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
],
]);
$sum = $projectDB->getSum();
if($sum >= $limit) {
throw new Exception('Project registration is restricted. Contact your administrator for more information.', 501);
}
}
Authorization::disable();
try {
$user = $projectDB->createDocument([
@@ -669,7 +686,6 @@ App::post('/v1/account/sessions/anonymous')
} catch (Exception $th) {
throw new Exception('Failed saving user to DB', 500);
}
Authorization::reset();
if (false === $user) {
@@ -422,6 +422,14 @@ class ProjectsConsoleClientTest extends Scope
$this->assertEquals($response['headers']['status-code'], 501);
$response = $this->client->call(Client::METHOD_POST, '/account/anonymous', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $id,
]), []);
$this->assertEquals($response['headers']['status-code'], 501);
// Cleanup
foreach ($auth as $index => $method) {