1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/app/controllers/api/users.php

552 lines
21 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2020-03-29 01:42:16 +13:00
global $utopia, $response, $projectDB;
2019-05-09 18:54:39 +12:00
use Utopia\Exception;
use Utopia\Response;
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;
2019-05-09 18:54:39 +12:00
use Utopia\Locale\Locale;
use Appwrite\Auth\Auth;
use Appwrite\Auth\Validator\Password;
use Appwrite\Database\Database;
use Appwrite\Database\Exception\Duplicate;
use Appwrite\Database\Validator\UID;
2019-05-09 18:54:39 +12:00
use DeviceDetector\DeviceDetector;
use GeoIp2\Database\Reader;
2020-02-05 19:31:34 +13:00
$utopia->post('/v1/users')
->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-02-10 18:58:29 +13:00
->param('email', '', function () { return new Email(); }, 'User email.')
2020-05-31 10:24:05 +12:00
->param('password', '', function () { return new Password(); }, 'User password. Must be between 6 to 32 chars.')
2020-02-10 18:58:29 +13:00
->param('name', '', function () { return new Text(100); }, 'User name.', true)
2020-02-05 19:31:34 +13:00
->action(
2020-03-29 01:42:16 +13:00
function ($email, $password, $name) use ($response, $projectDB) {
2020-02-05 19:31:34 +13:00
$profile = $projectDB->getCollection([ // Get user by email address
'limit' => 1,
'first' => true,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
'email='.$email,
],
]);
if (!empty($profile)) {
2020-03-05 02:06:52 +13:00
throw new Exception('User already registered', 409);
2020-02-05 19:31:34 +13:00
}
2020-03-22 10:10:06 +13:00
try {
$user = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_USERS,
'$permissions' => [
'read' => ['*'],
'write' => ['user:{self}'],
],
'email' => $email,
'emailVerification' => false,
'status' => Auth::USER_STATUS_UNACTIVATED,
'password' => Auth::passwordHash($password),
2020-06-20 23:20:49 +12:00
'password-update' => \time(),
'registration' => \time(),
2020-03-22 10:10:06 +13:00
'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-02-17 00:41:03 +13:00
$oauth2Keys = [];
2020-02-05 19:31:34 +13:00
2020-03-29 01:42:16 +13:00
foreach (Config::getParam('providers') as $key => $provider) {
2020-02-05 19:31:34 +13:00
if (!$provider['enabled']) {
continue;
}
2020-06-20 23:20:49 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
2020-02-05 19:31:34 +13:00
}
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
2020-06-20 23:20:49 +12:00
->json(\array_merge($user->getArrayCopy(\array_merge([
2020-02-17 20:16:11 +13:00
'$id',
2020-02-05 19:31:34 +13:00
'status',
'email',
'registration',
2020-02-10 10:37:28 +13:00
'emailVerification',
2020-02-05 19:31:34 +13:00
'name',
2020-02-17 00:41:03 +13:00
], $oauth2Keys)), ['roles' => []]));
2020-02-05 19:31:34 +13:00
}
);
2019-05-09 18:54:39 +12:00
$utopia->get('/v1/users')
->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')
->param('search', '', function () { return new Text(256); }, 'Search term to filter your list results.', true)
->param('limit', 25, function () { return 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, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true)
2019-05-09 18:54:39 +12:00
->action(
2020-03-29 01:42:16 +13:00
function ($search, $limit, $offset, $orderType) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'orderField' => 'registration',
'orderType' => $orderType,
'orderCast' => 'int',
'search' => $search,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
2019-05-09 18:54:39 +12:00
],
]);
2020-02-17 00:41:03 +13:00
$oauth2Keys = [];
2020-03-29 01:42:16 +13:00
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
2020-06-20 23:20:49 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
}
2020-06-20 23:20:49 +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
));
2019-05-09 18:54:39 +12:00
}, $results);
$response->json(['sum' => $projectDB->getSum(), 'users' => $results]);
}
);
$utopia->get('/v1/users/:userId')
->desc('Get User')
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', 'get')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/users/get-user.md')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2019-05-09 18:54:39 +12:00
->action(
2020-03-29 01:42:16 +13:00
function ($userId) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
2020-02-17 00:41:03 +13:00
$oauth2Keys = [];
2020-03-29 01:42:16 +13:00
foreach (Config::getParam('providers') as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
2020-06-20 23:20:49 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
}
2020-06-20 23:20:49 +12:00
$response->json(\array_merge($user->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
2019-10-21 19:01:07 +13:00
)), ['roles' => []]));
2019-05-09 18:54:39 +12:00
}
);
$utopia->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) use ($response, $request, $projectDB) {
$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 file from DB', 500);
}
$tokens = $user->getAttribute('tokens', []);
foreach ($tokens as $token) {
if (!$projectDB->deleteDocument($token->getId())) {
throw new Exception('Failed to remove token from DB', 500);
}
}
$response->noContent();
}
);
2019-05-09 18:54:39 +12:00
$utopia->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')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($userId) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
$prefs = $user->getAttribute('prefs', '');
2019-05-09 18:54:39 +12:00
try {
2020-06-20 23:20:49 +12:00
$prefs = \json_decode($prefs, true);
2020-01-20 09:38:00 +13:00
$prefs = ($prefs) ? $prefs : [];
} catch (\Exception $error) {
2019-05-09 18:54:39 +12:00
throw new Exception('Failed to parse prefs', 500);
}
$response->json($prefs);
}
);
$utopia->get('/v1/users/:userId/sessions')
->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')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($userId) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
$tokens = $user->getAttribute('tokens', []);
2020-01-24 08:46:03 +13:00
$reader = new Reader(__DIR__.'/../../db/DBIP/dbip-country-lite-2020-01.mmdb');
$sessions = [];
$index = 0;
$countries = Locale::getText('countries');
2019-05-09 18:54:39 +12:00
foreach ($tokens as $token) { /* @var $token Document */
if (Auth::TOKEN_TYPE_LOGIN != $token->getAttribute('type')) {
2019-05-09 18:54:39 +12:00
continue;
}
$userAgent = (!empty($token->getAttribute('userAgent'))) ? $token->getAttribute('userAgent') : 'UNKNOWN';
2019-05-09 18:54:39 +12:00
$dd = new DeviceDetector($userAgent);
// OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
// $dd->skipBotDetection();
$dd->parse();
$sessions[$index] = [
2020-02-17 20:16:11 +13:00
'$id' => $token->getId(),
'OS' => $dd->getOs(),
'client' => $dd->getClient(),
'device' => $dd->getDevice(),
'brand' => $dd->getBrand(),
'model' => $dd->getModel(),
'ip' => $token->getAttribute('ip', ''),
'geo' => [],
2019-05-09 18:54:39 +12:00
];
try {
$record = $reader->country($token->getAttribute('ip', ''));
2020-06-20 23:20:49 +12:00
$sessions[$index]['geo']['isoCode'] = \strtolower($record->country->isoCode);
2019-05-09 18:54:39 +12:00
$sessions[$index]['geo']['country'] = (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : Locale::getText('locale.country.unknown');
} catch (\Exception $e) {
2019-05-09 18:54:39 +12:00
$sessions[$index]['geo']['isoCode'] = '--';
$sessions[$index]['geo']['country'] = Locale::getText('locale.country.unknown');
}
++$index;
2019-05-09 18:54:39 +12:00
}
$response->json($sessions);
}
);
$utopia->get('/v1/users/:userId/logs')
->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')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($userId) use ($response, $register, $projectDB, $project) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
$adapter = new AuditAdapter($register->get('db'));
2020-02-17 20:16:11 +13:00
$adapter->setNamespace('app_'.$project->getId());
$audit = new Audit($adapter);
$countries = Locale::getText('countries');
2019-05-09 18:54:39 +12:00
2020-05-14 09:10:29 +12:00
$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',
]);
2019-05-09 18:54:39 +12:00
2020-01-24 08:46:03 +13:00
$reader = new Reader(__DIR__.'/../../db/DBIP/dbip-country-lite-2020-01.mmdb');
$output = [];
2019-05-09 18:54:39 +12:00
foreach ($logs as $i => &$log) {
$log['userAgent'] = (!empty($log['userAgent'])) ? $log['userAgent'] : 'UNKNOWN';
2019-05-09 18:54:39 +12:00
$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'],
2020-06-20 23:20:49 +12:00
'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
];
try {
$record = $reader->country($log['ip']);
2020-06-20 23:20:49 +12:00
$output[$i]['geo']['isoCode'] = \strtolower($record->country->isoCode);
2019-05-09 18:54:39 +12:00
$output[$i]['geo']['country'] = $record->country->name;
$output[$i]['geo']['country'] = (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : Locale::getText('locale.country.unknown');
} catch (\Exception $e) {
2019-05-09 18:54:39 +12:00
$output[$i]['geo']['isoCode'] = '--';
$output[$i]['geo']['country'] = Locale::getText('locale.country.unknown');
2019-05-09 18:54:39 +12:00
}
}
$response->json($output);
}
);
$utopia->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')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2019-10-21 19:01:07 +13:00
->param('status', '', function () { return new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED]); }, '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)
2019-05-09 18:54:39 +12:00
->action(
2020-03-29 01:42:16 +13:00
function ($userId, $status) use ($response, $projectDB) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
2020-06-20 23:20:49 +12:00
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
2019-12-13 05:40:01 +13:00
'status' => (int)$status,
2019-05-09 18:54:39 +12:00
]));
if (false === $user) {
2019-05-09 18:54:39 +12:00
throw new Exception('Failed saving user to DB', 500);
}
2019-10-21 19:01:07 +13:00
2020-02-17 00:41:03 +13:00
$oauth2Keys = [];
2019-10-21 19:01:07 +13:00
2020-03-29 01:42:16 +13:00
foreach (Config::getParam('providers') as $key => $provider) {
2019-10-21 19:01:07 +13:00
if (!$provider['enabled']) {
continue;
}
2020-06-20 23:20:49 +12:00
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
2019-10-21 19:01:07 +13:00
}
2019-05-09 18:54:39 +12:00
$response
2020-06-20 23:20:49 +12:00
->json(\array_merge($user->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',
2019-10-21 19:01:07 +13:00
'name',
2020-02-17 00:41:03 +13:00
], $oauth2Keys)), ['roles' => []]));
2019-05-09 18:54:39 +12:00
}
);
$utopia->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')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2020-01-20 09:38:00 +13:00
->param('prefs', '', function () { return new Assoc();}, 'Prefs key-value JSON object.')
->action(
2020-03-29 01:42:16 +13:00
function ($userId, $prefs) use ($response, $projectDB) {
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
2020-06-20 23:20:49 +12:00
$old = \json_decode($user->getAttribute('prefs', '{}'), true);
2020-01-20 09:38:00 +13:00
$old = ($old) ? $old : [];
2020-06-20 23:20:49 +12:00
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
'prefs' => \json_encode(\array_merge($old, $prefs)),
]));
2019-10-21 19:01:07 +13:00
if (false === $user) {
throw new Exception('Failed saving user to DB', 500);
}
2019-10-21 19:43:12 +13:00
$prefs = $user->getAttribute('prefs', '');
2019-10-21 19:01:07 +13:00
2019-10-21 19:43:12 +13:00
try {
2020-06-20 23:20:49 +12:00
$prefs = \json_decode($prefs, true);
2020-01-20 09:38:00 +13:00
$prefs = ($prefs) ? $prefs : [];
2019-10-21 19:43:12 +13:00
} catch (\Exception $error) {
throw new Exception('Failed to parse prefs', 500);
2019-10-21 19:01:07 +13:00
}
2019-10-21 19:43:12 +13:00
$response->json($prefs);
}
);
2020-05-01 18:27:02 +12:00
$utopia->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)
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('sessionId', null, function () { return new UID(); }, 'User unique session ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($userId, $sessionId) use ($response, $request, $projectDB) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
$tokens = $user->getAttribute('tokens', []);
foreach ($tokens as $token) { /* @var $token Document */
2020-02-17 20:16:11 +13:00
if ($sessionId == $token->getId()) {
if (!$projectDB->deleteDocument($token->getId())) {
2019-05-09 18:54:39 +12:00
throw new Exception('Failed to remove token from DB', 500);
}
}
}
$response->json(array('result' => 'success'));
}
);
$utopia->delete('/v1/users/:userId/sessions')
->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)
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
2019-05-09 18:54:39 +12:00
->action(
function ($userId) use ($response, $request, $projectDB) {
2019-05-09 18:54:39 +12:00
$user = $projectDB->getDocument($userId);
2020-02-17 20:16:11 +13:00
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('User not found', 404);
}
$tokens = $user->getAttribute('tokens', []);
foreach ($tokens as $token) { /* @var $token Document */
2020-02-17 20:16:11 +13:00
if (!$projectDB->deleteDocument($token->getId())) {
2019-05-09 18:54:39 +12:00
throw new Exception('Failed to remove token from DB', 500);
}
}
$response->json(array('result' => 'success'));
}
);