1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

feat(db-refactor): user status now boolean

This commit is contained in:
Torsten Dittmann 2021-07-14 13:02:12 +02:00
parent 3d2888ac70
commit 4633d74fc7
18 changed files with 154 additions and 59 deletions

View file

@ -212,7 +212,7 @@ $collections = [
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Status',
'key' => 'status',
'type' => Database::SYSTEM_VAR_TYPE_NUMERIC,
'type' => Database::SYSTEM_VAR_TYPE_BOOLEAN,
'default' => '',
'required' => true,
'array' => false,

View file

@ -240,7 +240,7 @@ $collections = [
],
[
'$id' => 'status',
'type' => Database::VAR_INTEGER,
'type' => Database::VAR_BOOLEAN,
'format' => '',
'size' => 0,
'signed' => true,

View file

@ -91,7 +91,7 @@ App::post('/v1/account')
'$write' => ['user:'.$userId],
'email' => $email,
'emailVerification' => false,
'status' => Auth::USER_STATUS_UNACTIVATED,
'status' => true,
'password' => Auth::passwordHash($password),
'passwordUpdate' => \time(),
'registration' => \time(),
@ -168,7 +168,7 @@ App::post('/v1/account/sessions')
throw new Exception('Invalid credentials', 401); // Wrong password or username
}
if (Auth::USER_STATUS_BLOCKED == $profile->getAttribute('status')) { // Account is blocked
if (false === $profile->getAttribute('status')) { // Account is blocked
throw new Exception('Invalid credentials. User is blocked', 401); // User is in status blocked
}
@ -472,7 +472,7 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
'$write' => ['user:'.$userId],
'email' => $email,
'emailVerification' => true,
'status' => Auth::USER_STATUS_ACTIVATED, // Email should already be authenticated by OAuth2 provider
'status' => true, // Email should already be authenticated by OAuth2 provider
'password' => Auth::passwordHash(Auth::passwordGenerator()),
'passwordUpdate' => 0,
'registration' => \time(),
@ -491,7 +491,7 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
}
}
if (Auth::USER_STATUS_BLOCKED == $user->getAttribute('status')) { // Account is blocked
if (false === $user->getAttribute('status')) { // Account is blocked
throw new Exception('Invalid credentials. User is blocked', 401); // User is in status blocked
}
@ -524,7 +524,7 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
}
$user
->setAttribute('status', Auth::USER_STATUS_ACTIVATED)
->setAttribute('status', true)
->setAttribute('sessions', $session, Document::SET_TYPE_APPEND)
;
@ -630,7 +630,7 @@ App::post('/v1/account/sessions/anonymous')
'$write' => ['user:'.$userId],
'email' => null,
'emailVerification' => false,
'status' => Auth::USER_STATUS_UNACTIVATED,
'status' => true,
'password' => null,
'passwordUpdate' => \time(),
'registration' => \time(),
@ -1143,7 +1143,7 @@ App::delete('/v1/account')
/** @var Appwrite\Event\Event $events */
$protocol = $request->getProtocol();
$user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('status', Auth::USER_STATUS_BLOCKED));
$user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('status', false));
//TODO delete all tokens or only current session?
//TODO delete all user data according to GDPR. Make sure everything is backed up and backups are deleted later
@ -1374,8 +1374,8 @@ App::post('/v1/account/recovery')
throw new Exception('User not found', 404); // TODO maybe hide this
}
if (Auth::USER_STATUS_BLOCKED == $profile->getAttribute('status')) { // Account is blocked
throw new Exception('Invalid credentials. User is blocked', 401); // User is in status blocked
if (false === $profile->getAttribute('status')) { // Account is blocked
throw new Exception('Invalid credentials. User is blocked', 401);
}
$secret = Auth::tokenGenerator();

View file

@ -297,7 +297,7 @@ App::post('/v1/teams/:teamId/memberships')
'$write' => ['user:'.$userId],
'email' => $email,
'emailVerification' => false,
'status' => Auth::USER_STATUS_UNACTIVATED,
'status' => true,
'password' => Auth::passwordHash(Auth::passwordGenerator()),
/**
* Set the password update time to 0 for users created using

View file

@ -49,7 +49,7 @@ App::post('/v1/users')
'$write' => ['user:'.$userId],
'email' => $email,
'emailVerification' => false,
'status' => Auth::USER_STATUS_UNACTIVATED,
'status' => true,
'password' => Auth::passwordHash($password),
'passwordUpdate' => \time(),
'registration' => \time(),
@ -321,7 +321,7 @@ App::patch('/v1/users/:userId/status')
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_USER)
->param('userId', '', new UID(), 'User unique ID.')
->param('status', '', new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED], true, Validator::TYPE_INTEGER), 'User Status code. To activate the user pass '.Auth::USER_STATUS_ACTIVATED.', to block the user pass '.Auth::USER_STATUS_BLOCKED.' and for disabling the user pass '.Auth::USER_STATUS_UNACTIVATED)
->param('status', null, new Boolean(true), 'User Status. To activate the user pass `true` and to block the user pass `false`')
->inject('response')
->inject('dbForInternal')
->action(function ($userId, $status, $response, $dbForInternal) {
@ -334,7 +334,7 @@ App::patch('/v1/users/:userId/status')
throw new Exception('User not found', 404);
}
$user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('status', (int)$status));
$user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('status', (bool) $status));
$response->dynamic2($user, Response::MODEL_USER);
});

View file

@ -234,7 +234,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $consoleDB
if ($key && $user->isEmpty()) {
$user = new Document([
'$id' => '',
'status' => Auth::USER_STATUS_ACTIVATED,
'status' => true,
'email' => 'app.'.$project->getId().'@service.'.$request->getHostname(),
'password' => '',
'name' => $project->getAttribute('name', 'Untitled'),
@ -278,8 +278,8 @@ App::init(function ($utopia, $request, $response, $console, $project, $consoleDB
throw new Exception($user->getAttribute('email', 'User').' (role: '.\strtolower($roles[$role]['label']).') missing scope ('.$scope.')', 401);
}
if (Auth::USER_STATUS_BLOCKED == $user->getAttribute('status')) { // Account has not been activated
throw new Exception('Invalid credentials. User is blocked', 401); // User is in status blocked
if (false === $user->getAttribute('status')) { // Account is blocked
throw new Exception('Invalid credentials. User is blocked', 401);
}
if ($user->getAttribute('reset')) {

View file

@ -85,15 +85,15 @@ $auth = $this->getParam('auth', []);
<small data-ls-bind="{{user.email}}"></span>
</td>
<td data-title="Status: ">
<span data-ls-if="{{user.emailVerification}} === true">
<span data-ls-if="{{user.emailVerification}} === true && {{user.status}} === true">
<span class="tag green">Verified</span>
</span>
<span data-ls-if="{{user.emailVerification}} !== true">
<span data-ls-if="{{user.emailVerification}} !== true && {{user.status}} === true">
<span class="tag">Unverified</span>
</span>
<span data-ls-if="{{user.status}} === <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>">
<span data-ls-if="{{user.status}} === false">
<span class="tag red">Blocked</span>
</span>
</td>

View file

@ -34,7 +34,7 @@
<li data-state="/console/users/user?id={{router.params.id}}&project={{router.params.project}}">
<h2>Overview</h2>
<div data-ls-if="{{user.status}} === <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" style="display: none" class="box padding-small danger margin-bottom-xxl text-align-center">
<div data-ls-if="{{user.status}} === false" style="display: none" class="box padding-small danger margin-bottom-xxl text-align-center">
This user account is blocked.
</div>
@ -99,11 +99,11 @@
<div class="box danger margin-bottom">
<p>This is the area where you can delete this user.</p>
<p>By deleting this user you will lose all data associated with this user.</p>
<p>PLEASE NOTE: User deletion is irreversible.</p>
<form class="inline"
data-analytics
data-analytics-activity
@ -122,7 +122,7 @@
data-failure-param-alert-text="Failed to delete user"
data-failure-param-alert-classname="error">
<input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
<button class="danger reverse" type="submit">Delete User</button>
</form>
</div>
@ -137,7 +137,7 @@
<li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-json" class="link text-size-small">View as JSON</button></li>
</ul>
<div data-ls-if="{{user.status}} !== <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" style="display: none">
<div data-ls-if="{{user.status}} === true" style="display: none">
<form name="users.updateStatus" class="margin-bottom"
data-analytics
data-analytics-activity
@ -154,11 +154,11 @@
data-failure-param-alert-text="Failed to block user"
data-failure-param-alert-classname="error">
<button name="status" type="submit" class="danger fill" value="<?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" data-cast-to="integer">Block Account</button>
<button name="status" type="submit" class="danger fill" value="false" data-cast-to="boolean">Block Account</button>
</form>
</div>
<div data-ls-if="{{user.status}} === <?php echo \Appwrite\Auth\Auth::USER_STATUS_BLOCKED; ?>" style="display: none">
<div data-ls-if="{{user.status}} === false" style="display: none">
<form name="users.updateStatus" class="margin-bottom"
data-analytics
data-analytics-activity
@ -175,7 +175,7 @@
data-failure-param-alert-text="Failed to activate user"
data-failure-param-alert-classname="error">
<button name="status" type="submit" class="fill" value="<?php echo \Appwrite\Auth\Auth::USER_STATUS_ACTIVATED; ?>" data-cast-to="integer">Activate Account</button>
<button name="status" type="submit" class="fill" value="true" data-cast-to="boolean">Activate Account</button>
</form>
</div>
</div>

View file

@ -6,13 +6,6 @@ use Appwrite\Database\Document;
class Auth
{
/**
* User Status.
*/
const USER_STATUS_UNACTIVATED = 0;
const USER_STATUS_ACTIVATED = 1;
const USER_STATUS_BLOCKED = 2;
/**
* User Roles.
*/

View file

@ -0,0 +1,52 @@
<?php
namespace Appwrite\Migration\Version;
use Appwrite\Migration\Migration;
use Utopia\Config\Config;
use Utopia\CLI\Console;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
class V09 extends Migration
{
public function execute(): void
{
$project = $this->project;
Console::log('Migrating project: ' . $project->getAttribute('name') . ' (' . $project->getId() . ')');
$this->forEachDocument([$this, 'fixDocument']);
}
protected function fixDocument(Document $document)
{
switch ($document->getAttribute('$collection')) {
case Database::SYSTEM_COLLECTION_USERS:
/**
* Remove deprecated user status 0 and replace with boolean.
*/
if ($document->getAttribute('status') === 0 || $document->getAttribute('status') === 1) {
$document->setAttribute('status', true);
}
if ($document->getAttribute('status') === 2) {
$document->setAttribute('status', false);
}
}
foreach ($document as &$attr) {
if ($attr instanceof Document) {
$attr = $this->fixDocument($attr);
}
if (\is_array($attr)) {
foreach ($attr as &$child) {
if ($child instanceof Document) {
$child = $this->fixDocument($child);
}
}
}
}
return $document;
}
}

View file

@ -327,7 +327,7 @@ class V06 extends Filter {
$content['oauth2'.ucfirst($key)] = '';
$content['oauth2'.ucfirst($key).'AccessToken'] = '';
}
$content['status'] = empty($content['status']) ? 0 : $content['status'];
$content['status'] = $content['status'] ? 0 : 2;
$content['roles'] = Authorization::getRoles() ?? [];
return $content;
}

View file

@ -29,10 +29,10 @@ class User extends Model
'example' => 1592981250,
])
->addRule('status', [
'type' => self::TYPE_INTEGER,
'description' => 'User status. 0 for Unactivated, 1 for active and 2 is blocked.',
'default' => 0,
'example' => 0,
'type' => self::TYPE_BOOLEAN,
'description' => 'User status. Pass `true` for enabled and `false` for disabled.',
'default' => true,
'example' => true,
])
->addRule('passwordUpdate', [
'type' => self::TYPE_INTEGER,

View file

@ -102,7 +102,7 @@ class AccountCustomClientTest extends Scope
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'status' => 2,
'status' => false,
]);
$this->assertEquals($response['headers']['status-code'], 200);

View file

@ -23,7 +23,7 @@ trait UsersBase
$this->assertEquals($user['headers']['status-code'], 201);
$this->assertEquals($user['body']['name'], 'Project User');
$this->assertEquals($user['body']['email'], 'users.service@example.com');
$this->assertEquals($user['body']['status'], 0);
$this->assertEquals($user['body']['status'], true);
$this->assertGreaterThan(0, $user['body']['registration']);
return ['userId' => $user['body']['$id']];
@ -45,7 +45,7 @@ trait UsersBase
$this->assertEquals($user['headers']['status-code'], 200);
$this->assertEquals($user['body']['name'], 'Project User');
$this->assertEquals($user['body']['email'], 'users.service@example.com');
$this->assertEquals($user['body']['status'], 0);
$this->assertEquals($user['body']['status'], true);
$this->assertGreaterThan(0, $user['body']['registration']);
$sessions = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'] . '/sessions', array_merge([
@ -105,11 +105,11 @@ trait UsersBase
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'status' => 2,
'status' => false,
]);
$this->assertEquals($user['headers']['status-code'], 200);
$this->assertEquals($user['body']['status'], 2);
$this->assertEquals($user['body']['status'], false);
$user = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'], array_merge([
'content-type' => 'application/json',
@ -117,7 +117,7 @@ trait UsersBase
], $this->getHeaders()));
$this->assertEquals($user['headers']['status-code'], 200);
$this->assertEquals($user['body']['status'], 2);
$this->assertEquals($user['body']['status'], false);
return $data;
}

View file

@ -50,7 +50,7 @@ class WebhooksCustomClientTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], $name);
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 0);
$this->assertEquals($webhook['data']['status'], true);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], []);
@ -119,7 +119,7 @@ class WebhooksCustomClientTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], $name);
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 2);
$this->assertEquals($webhook['data']['status'], false);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], []);
@ -389,7 +389,7 @@ class WebhooksCustomClientTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], $newName);
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 0);
$this->assertEquals($webhook['data']['status'], true);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], []);
@ -433,7 +433,7 @@ class WebhooksCustomClientTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], 'New Name');
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 0);
$this->assertEquals($webhook['data']['status'], true);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], []);
@ -479,7 +479,7 @@ class WebhooksCustomClientTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], 'New Name');
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 0);
$this->assertEquals($webhook['data']['status'], true);
$this->assertEquals($webhook['data']['email'], $newEmail);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], []);
@ -526,7 +526,7 @@ class WebhooksCustomClientTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], 'New Name');
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 0);
$this->assertEquals($webhook['data']['status'], true);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], [

View file

@ -194,7 +194,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], $name);
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 0);
$this->assertEquals($webhook['data']['status'], true);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs'], []);
@ -250,7 +250,7 @@ class WebhooksCustomServerTest extends Scope
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'status' => 2,
'status' => false,
]);
$this->assertEquals($user['headers']['status-code'], 200);
@ -269,7 +269,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], $data['name']);
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 2);
$this->assertEquals($webhook['data']['status'], false);
$this->assertEquals($webhook['data']['email'], $data['email']);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs']['a'], 'b');
@ -305,7 +305,7 @@ class WebhooksCustomServerTest extends Scope
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertEquals($webhook['data']['name'], $data['name']);
$this->assertIsInt($webhook['data']['registration']);
$this->assertEquals($webhook['data']['status'], 2);
$this->assertEquals($webhook['data']['status'], false);
$this->assertEquals($webhook['data']['email'], $data['email']);
$this->assertEquals($webhook['data']['emailVerification'], false);
$this->assertEquals($webhook['data']['prefs']['a'], 'b');

View file

@ -141,7 +141,7 @@ class WebhooksTest extends Scope
$this->assertNotEmpty($webhook['data']);
$this->assertNotEmpty($webhook['data']['$id']);
$this->assertIsNumeric($webhook['data']['status']);
$this->assertIsBool($webhook['data']['status']);
$this->assertIsNumeric($webhook['data']['registration']);
$this->assertEquals($webhook['data']['email'], $email);
$this->assertEquals($webhook['data']['name'], $name);

View file

@ -0,0 +1,50 @@
<?php
namespace Appwrite\Tests;
use ReflectionClass;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Migration\Version\V09;
class MigrationV09Test extends MigrationTest
{
public function setUp(): void
{
$this->pdo = new \PDO('sqlite::memory:');
$this->migration = new V09($this->pdo);
$reflector = new ReflectionClass('Appwrite\Migration\Version\V09');
$this->method = $reflector->getMethod('fixDocument');
$this->method->setAccessible(true);
}
public function testMigration()
{
$document = $this->fixDocument(new Document([
'$id' => uniqid(),
'$collection' => Database::SYSTEM_COLLECTION_USERS,
'status' => 0
]));
$this->assertIsBool($document->getAttribute('status'));
$this->assertEquals(true, $document->getAttribute('env', false));
$document = $this->fixDocument(new Document([
'$id' => uniqid(),
'$collection' => Database::SYSTEM_COLLECTION_USERS,
'status' => 1
]));
$this->assertIsBool($document->getAttribute('status'));
$this->assertEquals(true, $document->getAttribute('env', false));
$document = $this->fixDocument(new Document([
'$id' => uniqid(),
'$collection' => Database::SYSTEM_COLLECTION_USERS,
'status' => 2
]));
$this->assertIsBool($document->getAttribute('status'));
$this->assertEquals(false, $document->getAttribute('env', false));
}
}