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 ;
2019-12-29 22:47:55 +13:00
use Utopia\Audit\Audit ;
use Utopia\Audit\Adapters\MySQL as AuditAdapter ;
2020-03-25 06:56:32 +13:00
use Appwrite\Auth\Auth ;
use Appwrite\Auth\Validator\Password ;
use Appwrite\Database\Database ;
2020-08-10 03:00:09 +12:00
use Appwrite\Database\Document ;
2020-03-25 06:56:32 +13:00
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-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-06-30 23:09:28 +12:00
-> action ( function ( $email , $password , $name , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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 );
}
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-08-07 00:45:58 +12:00
$response -> setStatusCode ( Response :: STATUS_CODE_CREATED );
2020-08-07 00:00:16 +12:00
$response -> dynamic ( $user , Response :: MODEL_USER );
2020-06-30 23:09:28 +12:00
}, [ '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' )
2019-10-07 09:10:52 +13:00
-> 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 )
2020-06-30 23:09:28 +12:00
-> action ( function ( $search , $limit , $offset , $orderType , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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 ,
],
]);
2020-08-10 03:00:09 +12:00
$response -> dynamic ( new Document ([
'sum' => $projectDB -> getSum (),
'users' => $results
2020-08-10 03:34:33 +12:00
]), Response :: MODEL_USER_LIST );
2020-06-30 23:09:28 +12:00
}, [ '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' )
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' )
2019-10-07 09:10:52 +13:00
-> param ( 'userId' , '' , function () { return new UID (); }, 'User unique ID.' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $userId , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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-09-04 08:37:34 +12:00
2020-08-07 00:00:16 +12:00
$response -> dynamic ( $user , Response :: MODEL_USER );
2020-06-30 23:09:28 +12:00
}, [ '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' )
2019-10-07 09:10:52 +13:00
-> param ( 'userId' , '' , function () { return new UID (); }, 'User unique ID.' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $userId , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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
$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' )
2019-10-07 09:10:52 +13:00
-> param ( 'userId' , '' , function () { return new UID (); }, 'User unique ID.' )
2020-08-10 15:52:19 +12:00
-> action ( function ( $userId , $response , $projectDB , $locale ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Locale\Locale $locale */
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 = [];
$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-08-10 15:52:19 +12:00
$token -> setAttribute ( 'countryName' , ( isset ( $countries [ $token -> getAttribute ( 'contryCode' )]))
? $countries [ $token -> getAttribute ( 'contryCode' )]
: $locale -> getText ( 'locale.country.unknown' ));
$token -> setAttribute ( 'current' , false );
2019-05-09 18:54:39 +12:00
2020-08-10 15:52:19 +12:00
$sessions [] = $token ;
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
2020-08-10 15:52:19 +12:00
$response -> dynamic ( new Document ([
'sum' => count ( $sessions ),
'sessions' => $sessions
]), Response :: MODEL_SESSION_LIST );
}, [ 'response' , 'projectDB' , 'locale' ]);
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' )
2019-10-07 09:10:52 +13:00
-> param ( 'userId' , '' , function () { return new UID (); }, 'User unique ID.' )
2020-07-03 08:24:14 +12:00
-> action ( function ( $userId , $response , $register , $project , $projectDB , $locale , $geodb ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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 ();
2020-09-06 07:04:35 +12:00
$os = $dd -> getOs ();
$osCode = ( isset ( $os [ 'short_name' ])) ? $os [ 'short_name' ] : '' ;
$osName = ( isset ( $os [ 'name' ])) ? $os [ 'name' ] : '' ;
$osVersion = ( isset ( $os [ 'version' ])) ? $os [ 'version' ] : '' ;
$client = $dd -> getClient ();
$clientType = ( isset ( $client [ 'type' ])) ? $client [ 'type' ] : '' ;
$clientCode = ( isset ( $client [ 'short_name' ])) ? $client [ 'short_name' ] : '' ;
$clientName = ( isset ( $client [ 'name' ])) ? $client [ 'name' ] : '' ;
$clientVersion = ( isset ( $client [ 'version' ])) ? $client [ 'version' ] : '' ;
$clientEngine = ( isset ( $client [ 'engine' ])) ? $client [ 'engine' ] : '' ;
$clientEngineVersion = ( isset ( $client [ 'engine_version' ])) ? $client [ 'engine_version' ] : '' ;
2020-09-06 16:21:36 +12:00
$output [ $i ] = new Document ([
2020-06-30 23:09:28 +12:00
'event' => $log [ 'event' ],
'ip' => $log [ 'ip' ],
'time' => \strtotime ( $log [ 'time' ]),
2020-09-06 07:04:35 +12:00
'osCode' => $osCode ,
'osName' => $osName ,
'osVersion' => $osVersion ,
'clientType' => $clientType ,
'clientCode' => $clientCode ,
'clientName' => $clientName ,
'clientVersion' => $clientVersion ,
'clientEngine' => $clientEngine ,
'clientEngineVersion' => $clientEngineVersion ,
'deviceName' => $dd -> getDeviceName (),
'deviceBrand' => $dd -> getBrandName (),
'deviceModel' => $dd -> getModel (),
2020-09-06 16:21:36 +12:00
]);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
try {
2020-07-03 08:24:14 +12:00
$record = $geodb -> country ( $log [ 'ip' ]);
2020-09-06 16:21:36 +12:00
$output [ $i ] -> setAttribute ( 'countryCode' , \strtolower ( $record -> country -> isoCode ));
$output [ $i ] -> setAttribute ( 'countryName' , ( isset ( $countries [ $record -> country -> isoCode ])) ? $countries [ $record -> country -> isoCode ] : $locale -> getText ( 'locale.country.unknown' ));
2020-06-30 23:09:28 +12:00
} catch ( \Exception $e ) {
2020-09-06 16:21:36 +12:00
$output [ $i ] -> setAttribute ( 'countryCode' , '--' );
$output [ $i ] -> setAttribute ( 'countryName' , $locale -> getText ( 'locale.country.unknown' ));
2019-05-09 18:54:39 +12:00
}
}
2020-06-30 23:09:28 +12:00
2020-09-06 07:04:35 +12:00
$response -> dynamic ( new Document ([ 'logs' => $output ]), Response :: MODEL_LOG_LIST );
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' )
2019-10-07 09:10:52 +13:00
-> 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 )
2020-06-30 23:09:28 +12:00
-> action ( function ( $userId , $status , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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 );
}
2020-08-07 00:00:16 +12:00
$response -> dynamic ( $user , Response :: MODEL_USER );
2020-06-30 23:09:28 +12:00
}, [ 'response' , 'projectDB' ]);
2019-05-09 18:54:39 +12:00
2020-08-10 03:00:09 +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' ])
2019-10-05 10:04:49 +13:00
-> label ( 'scope' , 'users.write' )
2020-01-27 19:14:14 +13:00
-> label ( 'sdk.platform' , [ APP_PLATFORM_SERVER ])
2019-10-05 10:04:49 +13:00
-> 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' )
2019-10-07 09:10:52 +13:00
-> 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.' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $userId , $prefs , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @var Appwrite\Database\Database $projectDB */
2019-10-05 10:04:49 +13:00
2020-06-30 23:09:28 +12:00
$user = $projectDB -> getDocument ( $userId );
2019-10-05 10:04:49 +13: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 );
}
2020-01-20 09:38:00 +13:00
2020-06-30 23:09:28 +12:00
$user = $projectDB -> updateDocument ( \array_merge ( $user -> getArrayCopy (), [
2020-08-07 00:45:58 +12:00
'prefs' => $prefs ,
2020-06-30 23:09:28 +12:00
]));
2019-10-05 10:04:49 +13:00
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
$response -> json ( $prefs );
}, [ 'response' , 'projectDB' ]);
2019-10-05 10:04:49 +13:00
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 )
2019-10-07 09:10:52 +13:00
-> param ( 'userId' , '' , function () { return new UID (); }, 'User unique ID.' )
-> param ( 'sessionId' , null , function () { return new UID (); }, 'User unique session ID.' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $userId , $sessionId , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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
2020-08-07 00:00:16 +12:00
$response -> noContent ();
2020-06-30 23:09:28 +12:00
}, [ '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 )
2019-10-07 09:10:52 +13:00
-> param ( 'userId' , '' , function () { return new UID (); }, 'User unique ID.' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $userId , $response , $projectDB ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @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
2020-08-07 00:00:16 +12:00
$response -> noContent ();
2020-06-30 23:09:28 +12:00
}, [ 'response' , 'projectDB' ]);
2020-08-30 08:40:40 +12:00
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
}
2020-08-30 08:40:40 +12:00
2020-08-30 17:09:34 +12:00
$deletes
-> setParam ( 'document' , $user )
;
2020-08-30 08:40:40 +12:00
$response -> noContent ();
}, [ 'response' , 'projectDB' , 'deletes' ]);