1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00
appwrite/app/controllers/api/users.php

589 lines
22 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2020-06-29 05:31:21 +12:00
use Utopia\App;
2019-05-09 18:54:39 +12:00
use Utopia\Exception;
2020-01-20 09:38:00 +13:00
use Utopia\Validator\Assoc;
2019-05-09 18:54:39 +12:00
use Utopia\Validator\WhiteList;
use Utopia\Validator\Email;
use Utopia\Validator\Text;
use Utopia\Validator\Range;
use Utopia\Audit\Audit;
use Utopia\Audit\Adapters\MySQL as AuditAdapter;
2020-03-29 01:42:16 +13:00
use Utopia\Config\Config;
use Appwrite\Auth\Auth;
use Appwrite\Auth\Validator\Password;
use Appwrite\Database\Database;
use Appwrite\Database\Exception\Duplicate;
use Appwrite\Database\Validator\UID;
2020-06-23 06:38:14 +12:00
use Appwrite\Utopia\Response;
2019-05-09 18:54:39 +12:00
use DeviceDetector\DeviceDetector;
2020-06-29 05:31:21 +12:00
App::post('/v1/users')
2020-02-05 19:31:34 +13:00
->desc('Create User')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2020-02-05 19:31:34 +13:00
->label('scope', 'users.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'users')
->label('sdk.method', 'create')
->label('sdk.description', '/docs/references/users/create-user.md')
2020-09-11 02:40:14 +12:00
->param('email', '', new Email(), 'User email.')
->param('password', '', new Password(), 'User password. Must be between 6 to 32 chars.')
->param('name', '', new Text(128), 'User name. Max length: 128 chars.', true)
2020-06-30 23:09:28 +12:00
->action(function ($email, $password, $name, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
$profile = $projectDB->getCollectionFirst([ // Get user by email address
'limit' => 1,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
'email='.$email,
],
]);
if (!empty($profile)) {
throw new Exception('User already registered', 409);
}
2020-02-05 19:31:34 +13:00
2020-06-30 23:09:28 +12:00
try {
$user = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_USERS,
'$permissions' => [
'read' => ['*'],
'write' => ['user:{self}'],
2020-02-05 19:31:34 +13:00
],
2020-06-30 23:09:28 +12:00
'email' => $email,
'emailVerification' => false,
'status' => Auth::USER_STATUS_UNACTIVATED,
'password' => Auth::passwordHash($password),
'password-update' => \time(),
'registration' => \time(),
'reset' => false,
'name' => $name,
], ['email' => $email]);
} catch (Duplicate $th) {
throw new Exception('Account already exists', 409);
}
2020-02-05 19:31:34 +13:00
2020-06-30 23:09:28 +12:00
$oauth2Keys = [];
2020-02-05 19:31:34 +13:00
2020-06-30 23:09:28 +12:00
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
2020-02-05 19:31:34 +13:00
}
2020-06-30 23:09:28 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
2020-02-05 19:31:34 +13:00
}
2020-06-30 23:09:28 +12:00
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json(\array_merge($user->getArrayCopy(\array_merge([
'$id',
'status',
'email',
'registration',
'emailVerification',
'name',
], $oauth2Keys)), ['roles' => []]));
}, ['response', 'projectDB']);
2020-02-05 19:31:34 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/users')
2019-05-09 18:54:39 +12:00
->desc('List Users')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'list')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/list-users.md')
2020-09-11 02:40:14 +12:00
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
2020-06-30 23:09:28 +12:00
->action(function ($search, $limit, $offset, $orderType, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'orderField' => 'registration',
'orderType' => $orderType,
'orderCast' => 'int',
'search' => $search,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
],
]);
$oauth2Keys = [];
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
2020-06-30 23:09:28 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
$results = \array_map(function ($value) use ($oauth2Keys) { /* @var $value \Database\Document */
return $value->getArrayCopy(\array_merge(
[
2020-02-17 20:16:11 +13:00
'$id',
2019-10-21 19:01:07 +13:00
'status',
'email',
'registration',
2020-02-10 10:37:28 +13:00
'emailVerification',
'name',
],
2020-02-17 00:41:03 +13:00
$oauth2Keys
2020-06-30 23:09:28 +12:00
));
}, $results);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$response->json(['sum' => $projectDB->getSum(), 'users' => $results]);
}, ['response', 'projectDB']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/users/:userId')
2019-05-09 18:54:39 +12:00
->desc('Get User')
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'get')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/get-user.md')
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
2020-06-30 23:09:28 +12:00
->action(function ($userId, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
2020-06-30 23:09:28 +12:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2020-06-30 23:09:28 +12:00
$oauth2Keys = [];
2020-06-30 23:09:28 +12:00
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
2020-06-30 23:09:28 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
}
2020-06-30 23:09:28 +12:00
$response->json(\array_merge($user->getArrayCopy(\array_merge(
[
'$id',
'status',
'email',
'registration',
'emailVerification',
'name',
],
$oauth2Keys
)), ['roles' => []]));
}, ['response', 'projectDB']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/users/:userId/prefs')
2020-01-23 19:27:19 +13:00
->desc('Get User Preferences')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'getPrefs')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/get-user-prefs.md')
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
2020-06-30 23:09:28 +12:00
->action(function ($userId, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$prefs = $user->getAttribute('prefs', '');
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
try {
$prefs = \json_decode($prefs, true);
$prefs = ($prefs) ? $prefs : [];
} catch (\Exception $error) {
throw new Exception('Failed to parse prefs', 500);
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
$response->json($prefs);
}, ['response', 'projectDB']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/users/:userId/sessions')
2019-05-09 18:54:39 +12:00
->desc('Get User Sessions')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'getSessions')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/get-user-sessions.md')
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
2020-07-03 08:24:14 +12:00
->action(function ($userId, $response, $projectDB, $locale, $geodb) {
2020-06-30 23:09:28 +12:00
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Locale\Locale $locale */
2020-07-03 08:24:14 +12:00
/** @var GeoIp2\Database\Reader $geodb */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$tokens = $user->getAttribute('tokens', []);
$sessions = [];
$index = 0;
$countries = $locale->getText('countries');
foreach ($tokens as $token) { /* @var $token Document */
if (Auth::TOKEN_TYPE_LOGIN != $token->getAttribute('type')) {
continue;
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
$userAgent = (!empty($token->getAttribute('userAgent'))) ? $token->getAttribute('userAgent') : 'UNKNOWN';
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$dd = new DeviceDetector($userAgent);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
// OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
// $dd->skipBotDetection();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$dd->parse();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$sessions[$index] = [
'$id' => $token->getId(),
'OS' => $dd->getOs(),
'client' => $dd->getClient(),
'device' => $dd->getDevice(),
'brand' => $dd->getBrand(),
'model' => $dd->getModel(),
'ip' => $token->getAttribute('ip', ''),
'geo' => [],
];
try {
2020-10-25 00:48:07 +13:00
$record = $geodb->get($token->getAttribute('ip', ''));
if(isset($record)){
$sessions[$index]['geo']['isoCode'] = \strtolower($record['country']['iso_code']);
$sessions[$index]['geo']['country'] = (isset($countries[$record['country']['iso_code']])) ? $countries[$record['country']['iso_code']] : $locale->getText('locale.country.unknown');
}
else{
$sessions[$index]['geo']['isoCode'] = '--';
$sessions[$index]['geo']['country'] = $locale->getText('locale.country.unknown');
}
2020-06-30 23:09:28 +12:00
} catch (\Exception $e) {
$sessions[$index]['geo']['isoCode'] = '--';
$sessions[$index]['geo']['country'] = $locale->getText('locale.country.unknown');
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
++$index;
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
$response->json($sessions);
2020-07-03 10:13:42 +12:00
}, ['response', 'projectDB', 'locale', 'geodb']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/users/:userId/logs')
2019-05-09 18:54:39 +12:00
->desc('Get User Logs')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.read')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'getLogs')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/get-user-logs.md')
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
2020-07-03 08:24:14 +12:00
->action(function ($userId, $response, $register, $project, $projectDB, $locale, $geodb) {
2020-06-30 23:09:28 +12:00
/** @var Utopia\Response $response */
/** @var Utopia\Registry\Registry $register */
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Locale\Locale $locale */
2020-07-03 08:24:14 +12:00
/** @var GeoIp2\Database\Reader $geodb */
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$adapter = new AuditAdapter($register->get('db'));
$adapter->setNamespace('app_'.$project->getId());
$audit = new Audit($adapter);
$countries = $locale->getText('countries');
$logs = $audit->getLogsByUserAndActions($user->getId(), [
'account.create',
'account.delete',
'account.update.name',
'account.update.email',
'account.update.password',
'account.update.prefs',
'account.sessions.create',
'account.sessions.delete',
'account.recovery.create',
'account.recovery.update',
'account.verification.create',
'account.verification.update',
'teams.membership.create',
'teams.membership.update',
'teams.membership.delete',
]);
$output = [];
foreach ($logs as $i => &$log) {
$log['userAgent'] = (!empty($log['userAgent'])) ? $log['userAgent'] : 'UNKNOWN';
$dd = new DeviceDetector($log['userAgent']);
$dd->skipBotDetection(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
$dd->parse();
$output[$i] = [
'event' => $log['event'],
'ip' => $log['ip'],
'time' => \strtotime($log['time']),
'OS' => $dd->getOs(),
'client' => $dd->getClient(),
'device' => $dd->getDevice(),
'brand' => $dd->getBrand(),
'model' => $dd->getModel(),
'geo' => [],
];
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
try {
2020-10-25 00:48:07 +13:00
$record = $geodb->get($log['ip']);
if(isset($record)){
$output[$i]['geo']['isoCode'] = \strtolower($record['country']['iso_code']);
$output[$i]['geo']['country'] = (isset($countries[$record['country']['iso_code']])) ? $countries[$record['country']['iso_code']] : $locale->getText('locale.country.unknown');
}
else{
$output[$i]['geo']['isoCode'] = '--';
$output[$i]['geo']['country'] = $locale->getText('locale.country.unknown');
}
2020-06-30 23:09:28 +12:00
} catch (\Exception $e) {
$output[$i]['geo']['isoCode'] = '--';
$output[$i]['geo']['country'] = $locale->getText('locale.country.unknown');
2019-05-09 18:54:39 +12:00
}
}
2020-06-30 23:09:28 +12:00
$response->json($output);
2020-07-03 08:24:14 +12:00
}, ['response', 'register', 'project', 'projectDB', 'locale', 'geodb']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::patch('/v1/users/:userId/status')
2019-10-10 16:52:59 +13:00
->desc('Update User Status')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.write')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'updateStatus')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/update-user-status.md')
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
->param('status', '', new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED], true), '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)
2020-06-30 23:09:28 +12:00
->action(function ($userId, $status, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
'status' => (int)$status,
]));
2019-10-21 19:01:07 +13:00
2020-06-30 23:09:28 +12:00
if (false === $user) {
throw new Exception('Failed saving user to DB', 500);
}
$oauth2Keys = [];
2019-10-21 19:01:07 +13:00
2020-06-30 23:09:28 +12:00
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
2019-10-21 19:01:07 +13:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
$response
->json(\array_merge($user->getArrayCopy(\array_merge([
'$id',
'status',
'email',
'registration',
'emailVerification',
'name',
], $oauth2Keys)), ['roles' => []]));
}, ['response', 'projectDB']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::patch('/v1/users/:userId/prefs')
2020-01-23 19:27:19 +13:00
->desc('Update User Preferences')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
->label('scope', 'users.write')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'updatePrefs')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/update-user-prefs.md')
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
->param('prefs', '', new Assoc(), 'Prefs key-value JSON object.')
2020-06-30 23:09:28 +12:00
->action(function ($userId, $prefs, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
2020-06-30 23:09:28 +12:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2020-01-20 09:38:00 +13:00
2020-06-30 23:09:28 +12:00
$old = \json_decode($user->getAttribute('prefs', '{}'), true);
$old = ($old) ? $old : [];
2019-10-21 19:01:07 +13:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
'prefs' => \json_encode(\array_merge($old, $prefs)),
]));
2020-06-30 23:09:28 +12:00
if (false === $user) {
throw new Exception('Failed saving user to DB', 500);
}
2019-10-21 19:01:07 +13:00
2020-06-30 23:09:28 +12:00
$prefs = $user->getAttribute('prefs', '');
2019-10-21 19:01:07 +13:00
2020-06-30 23:09:28 +12:00
try {
$prefs = \json_decode($prefs, true);
$prefs = ($prefs) ? $prefs : [];
} catch (\Exception $error) {
throw new Exception('Failed to parse prefs', 500);
}
2020-06-30 23:09:28 +12:00
$response->json($prefs);
}, ['response', 'projectDB']);
2020-06-29 05:31:21 +12:00
App::delete('/v1/users/:userId/sessions/:sessionId')
2019-05-09 18:54:39 +12:00
->desc('Delete User Session')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.write')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'deleteSession')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/delete-user-session.md')
2019-05-09 18:54:39 +12:00
->label('abuse-limit', 100)
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
->param('sessionId', null, new UID(), 'User unique session ID.')
2020-06-30 23:09:28 +12:00
->action(function ($userId, $sessionId, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$tokens = $user->getAttribute('tokens', []);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
foreach ($tokens as $token) { /* @var $token Document */
if ($sessionId == $token->getId()) {
if (!$projectDB->deleteDocument($token->getId())) {
throw new Exception('Failed to remove token from DB', 500);
2019-05-09 18:54:39 +12:00
}
}
}
2020-06-30 23:09:28 +12:00
$response->json(array('result' => 'success'));
}, ['response', 'projectDB']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::delete('/v1/users/:userId/sessions')
2019-05-09 18:54:39 +12:00
->desc('Delete User Sessions')
2020-06-26 06:32:12 +12:00
->groups(['api', 'users'])
2019-05-09 18:54:39 +12:00
->label('scope', 'users.write')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'users')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'deleteSessions')
2019-10-09 21:31:51 +13:00
->label('sdk.description', '/docs/references/users/delete-user-sessions.md')
2019-05-09 18:54:39 +12:00
->label('abuse-limit', 100)
2020-09-11 02:40:14 +12:00
->param('userId', '', new UID(), 'User unique ID.')
2020-06-30 23:09:28 +12:00
->action(function ($userId, $response, $projectDB) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB->getDocument($userId);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$tokens = $user->getAttribute('tokens', []);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
foreach ($tokens as $token) { /* @var $token Document */
if (!$projectDB->deleteDocument($token->getId())) {
throw new Exception('Failed to remove token from DB', 500);
2019-05-09 18:54:39 +12:00
}
}
2020-06-30 23:09:28 +12:00
$response->json(array('result' => 'success'));
}, ['response', 'projectDB']);
App::delete('/v1/users/:userId')
->desc('Delete User')
->groups(['api', 'users'])
->label('scope', 'users.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'users')
->label('sdk.method', 'deleteUser')
->label('sdk.description', '/docs/references/users/delete-user.md')
->label('abuse-limit', 100)
->param('userId', '', function () {return new UID();}, 'User unique ID.')
->action(function ($userId, $response, $projectDB, $deletes) {
/** @var Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Event\Event $deletes */
$user = $projectDB->getDocument($userId);
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
if (!$projectDB->deleteDocument($userId)) {
throw new Exception('Failed to remove user from DB', 500);
}
if (!$projectDB->deleteUniqueKey(md5('users:email='.$user->getAttribute('email', null)))) {
throw new Exception('Failed to remove unique key from DB', 500);
}
$reservedId = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_RESERVED,
'$id' => $userId,
'$permissions' => [
'read' => ['*'],
],
]);
if (false === $reservedId) {
throw new Exception('Failed saving reserved id to DB', 500);
2019-05-09 18:54:39 +12:00
}
$deletes->setParam('document', $user);
$response->noContent();
}, ['response', 'projectDB', 'deletes']);