mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Merge branch 'swoole-and-functions' of github.com:appwrite/appwrite into feat-add-webhook-events-to-the-users-service
This commit is contained in:
+69
-171
@@ -9,10 +9,10 @@ use Utopia\Validator\Text;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Audit\Audit;
|
||||
use Utopia\Audit\Adapters\MySQL as AuditAdapter;
|
||||
use Utopia\Config\Config;
|
||||
use Appwrite\Auth\Auth;
|
||||
use Appwrite\Auth\Validator\Password;
|
||||
use Appwrite\Database\Database;
|
||||
use Appwrite\Database\Document;
|
||||
use Appwrite\Database\Exception\Duplicate;
|
||||
use Appwrite\Database\Validator\UID;
|
||||
use Appwrite\Utopia\Response;
|
||||
@@ -30,7 +30,7 @@ App::post('/v1/users')
|
||||
->param('password', '', function () { return new Password(); }, 'User password. Must be between 6 to 32 chars.')
|
||||
->param('name', '', function () { return new Text(128); }, 'User name. Max length: 128 chars.', true)
|
||||
->action(function ($email, $password, $name, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$profile = $projectDB->getCollectionFirst([ // Get user by email address
|
||||
@@ -65,27 +65,8 @@ App::post('/v1/users')
|
||||
throw new Exception('Account already exists', 409);
|
||||
}
|
||||
|
||||
$oauth2Keys = [];
|
||||
|
||||
foreach (Config::getParam('providers') as $key => $provider) {
|
||||
if (!$provider['enabled']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
|
||||
}
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->json(\array_merge($user->getArrayCopy(\array_merge([
|
||||
'$id',
|
||||
'status',
|
||||
'email',
|
||||
'registration',
|
||||
'emailVerification',
|
||||
'name',
|
||||
], $oauth2Keys)), ['roles' => []]));
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
App::get('/v1/users')
|
||||
@@ -101,7 +82,7 @@ App::get('/v1/users')
|
||||
->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'], true); }, 'Order result by ASC or DESC order.', true)
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$results = $projectDB->getCollection([
|
||||
@@ -116,32 +97,10 @@ App::get('/v1/users')
|
||||
],
|
||||
]);
|
||||
|
||||
$oauth2Keys = [];
|
||||
|
||||
foreach (Config::getParam('providers') as $key => $provider) {
|
||||
if (!$provider['enabled']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
|
||||
}
|
||||
|
||||
$results = \array_map(function ($value) use ($oauth2Keys) { /* @var $value \Database\Document */
|
||||
return $value->getArrayCopy(\array_merge(
|
||||
[
|
||||
'$id',
|
||||
'status',
|
||||
'email',
|
||||
'registration',
|
||||
'emailVerification',
|
||||
'name',
|
||||
],
|
||||
$oauth2Keys
|
||||
));
|
||||
}, $results);
|
||||
|
||||
$response->json(['sum' => $projectDB->getSum(), 'users' => $results]);
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $projectDB->getSum(),
|
||||
'users' => $results
|
||||
]), Response::MODEL_USER_LIST);
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
App::get('/v1/users/:userId')
|
||||
@@ -154,7 +113,7 @@ App::get('/v1/users/:userId')
|
||||
->label('sdk.description', '/docs/references/users/get-user.md')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->action(function ($userId, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
@@ -163,28 +122,7 @@ App::get('/v1/users/:userId')
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$oauth2Keys = [];
|
||||
|
||||
foreach (Config::getParam('providers') as $key => $provider) {
|
||||
if (!$provider['enabled']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
|
||||
}
|
||||
|
||||
$response->json(\array_merge($user->getArrayCopy(\array_merge(
|
||||
[
|
||||
'$id',
|
||||
'status',
|
||||
'email',
|
||||
'registration',
|
||||
'emailVerification',
|
||||
'name',
|
||||
],
|
||||
$oauth2Keys
|
||||
)), ['roles' => []]));
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
App::get('/v1/users/:userId/prefs')
|
||||
@@ -197,7 +135,7 @@ App::get('/v1/users/:userId/prefs')
|
||||
->label('sdk.description', '/docs/references/users/get-user-prefs.md')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->action(function ($userId, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
@@ -208,13 +146,6 @@ App::get('/v1/users/:userId/prefs')
|
||||
|
||||
$prefs = $user->getAttribute('prefs', '');
|
||||
|
||||
try {
|
||||
$prefs = \json_decode($prefs, true);
|
||||
$prefs = ($prefs) ? $prefs : [];
|
||||
} catch (\Exception $error) {
|
||||
throw new Exception('Failed to parse prefs', 500);
|
||||
}
|
||||
|
||||
$response->json($prefs);
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
@@ -227,11 +158,10 @@ App::get('/v1/users/:userId/sessions')
|
||||
->label('sdk.method', 'getSessions')
|
||||
->label('sdk.description', '/docs/references/users/get-user-sessions.md')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->action(function ($userId, $response, $projectDB, $locale, $geodb) {
|
||||
/** @var Utopia\Response $response */
|
||||
->action(function ($userId, $response, $projectDB, $locale) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Locale\Locale $locale */
|
||||
/** @var GeoIp2\Database\Reader $geodb */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
|
||||
@@ -241,7 +171,6 @@ App::get('/v1/users/:userId/sessions')
|
||||
|
||||
$tokens = $user->getAttribute('tokens', []);
|
||||
$sessions = [];
|
||||
$index = 0;
|
||||
$countries = $locale->getText('countries');
|
||||
|
||||
foreach ($tokens as $token) { /* @var $token Document */
|
||||
@@ -249,40 +178,19 @@ App::get('/v1/users/:userId/sessions')
|
||||
continue;
|
||||
}
|
||||
|
||||
$userAgent = (!empty($token->getAttribute('userAgent'))) ? $token->getAttribute('userAgent') : 'UNKNOWN';
|
||||
$token->setAttribute('countryName', (isset($countries[$token->getAttribute('contryCode')]))
|
||||
? $countries[$token->getAttribute('contryCode')]
|
||||
: $locale->getText('locale.country.unknown'));
|
||||
$token->setAttribute('current', false);
|
||||
|
||||
$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] = [
|
||||
'$id' => $token->getId(),
|
||||
'OS' => $dd->getOs(),
|
||||
'client' => $dd->getClient(),
|
||||
'device' => $dd->getDevice(),
|
||||
'brand' => $dd->getBrand(),
|
||||
'model' => $dd->getModel(),
|
||||
'ip' => $token->getAttribute('ip', ''),
|
||||
'geo' => [],
|
||||
];
|
||||
|
||||
try {
|
||||
$record = $geodb->country($token->getAttribute('ip', ''));
|
||||
$sessions[$index]['geo']['isoCode'] = \strtolower($record->country->isoCode);
|
||||
$sessions[$index]['geo']['country'] = (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : $locale->getText('locale.country.unknown');
|
||||
} catch (\Exception $e) {
|
||||
$sessions[$index]['geo']['isoCode'] = '--';
|
||||
$sessions[$index]['geo']['country'] = $locale->getText('locale.country.unknown');
|
||||
}
|
||||
|
||||
++$index;
|
||||
$sessions[] = $token;
|
||||
}
|
||||
|
||||
$response->json($sessions);
|
||||
}, ['response', 'projectDB', 'locale', 'geodb']);
|
||||
$response->dynamic(new Document([
|
||||
'sum' => count($sessions),
|
||||
'sessions' => $sessions
|
||||
]), Response::MODEL_SESSION_LIST);
|
||||
}, ['response', 'projectDB', 'locale']);
|
||||
|
||||
App::get('/v1/users/:userId/logs')
|
||||
->desc('Get User Logs')
|
||||
@@ -294,7 +202,7 @@ App::get('/v1/users/:userId/logs')
|
||||
->label('sdk.description', '/docs/references/users/get-user-logs.md')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->action(function ($userId, $response, $register, $project, $projectDB, $locale, $geodb) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Utopia\Registry\Registry $register */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
@@ -343,30 +251,49 @@ App::get('/v1/users/:userId/logs')
|
||||
|
||||
$dd->parse();
|
||||
|
||||
$output[$i] = [
|
||||
$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'] : '';
|
||||
|
||||
$output[$i] = new Document([
|
||||
'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' => [],
|
||||
];
|
||||
|
||||
'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(),
|
||||
]);
|
||||
|
||||
try {
|
||||
$record = $geodb->country($log['ip']);
|
||||
$output[$i]['geo']['isoCode'] = \strtolower($record->country->isoCode);
|
||||
$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');
|
||||
$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'));
|
||||
} catch (\Exception $e) {
|
||||
$output[$i]['geo']['isoCode'] = '--';
|
||||
$output[$i]['geo']['country'] = $locale->getText('locale.country.unknown');
|
||||
$output[$i]->setAttribute('countryCode', '--');
|
||||
$output[$i]->setAttribute('countryName', $locale->getText('locale.country.unknown'));
|
||||
}
|
||||
}
|
||||
|
||||
$response->json($output);
|
||||
$response->dynamic(new Document(['logs' => $output]), Response::MODEL_LOG_LIST);
|
||||
}, ['response', 'register', 'project', 'projectDB', 'locale', 'geodb']);
|
||||
|
||||
App::patch('/v1/users/:userId/status')
|
||||
@@ -380,7 +307,7 @@ App::patch('/v1/users/:userId/status')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->param('status', '', function () { return 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)
|
||||
->action(function ($userId, $status, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
@@ -396,27 +323,8 @@ App::patch('/v1/users/:userId/status')
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
|
||||
$oauth2Keys = [];
|
||||
|
||||
foreach (Config::getParam('providers') as $key => $provider) {
|
||||
if (!$provider['enabled']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key);
|
||||
$oauth2Keys[] = 'oauth2'.\ucfirst($key).'AccessToken';
|
||||
}
|
||||
|
||||
$response
|
||||
->json(\array_merge($user->getArrayCopy(\array_merge([
|
||||
'$id',
|
||||
'status',
|
||||
'email',
|
||||
'registration',
|
||||
'emailVerification',
|
||||
'name',
|
||||
], $oauth2Keys)), ['roles' => []]));
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
App::patch('/v1/users/:userId/prefs')
|
||||
@@ -430,7 +338,7 @@ App::patch('/v1/users/:userId/prefs')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->param('prefs', '', function () { return new Assoc();}, 'Prefs key-value JSON object.')
|
||||
->action(function ($userId, $prefs, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
@@ -439,26 +347,14 @@ App::patch('/v1/users/:userId/prefs')
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$old = \json_decode($user->getAttribute('prefs', '{}'), true);
|
||||
$old = ($old) ? $old : [];
|
||||
|
||||
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
|
||||
'prefs' => \json_encode(\array_merge($old, $prefs)),
|
||||
'prefs' => $prefs,
|
||||
]));
|
||||
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
|
||||
$prefs = $user->getAttribute('prefs', '');
|
||||
|
||||
try {
|
||||
$prefs = \json_decode($prefs, true);
|
||||
$prefs = ($prefs) ? $prefs : [];
|
||||
} catch (\Exception $error) {
|
||||
throw new Exception('Failed to parse prefs', 500);
|
||||
}
|
||||
|
||||
$response->json($prefs);
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
@@ -474,7 +370,7 @@ App::delete('/v1/users/:userId/sessions/:sessionId')
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->param('sessionId', null, function () { return new UID(); }, 'User unique session ID.')
|
||||
->action(function ($userId, $sessionId, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
@@ -493,7 +389,7 @@ App::delete('/v1/users/:userId/sessions/:sessionId')
|
||||
}
|
||||
}
|
||||
|
||||
$response->json(array('result' => 'success'));
|
||||
$response->noContent();
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
App::delete('/v1/users/:userId/sessions')
|
||||
@@ -507,7 +403,7 @@ App::delete('/v1/users/:userId/sessions')
|
||||
->label('abuse-limit', 100)
|
||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
||||
->action(function ($userId, $response, $projectDB) {
|
||||
/** @var Utopia\Response $response */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
@@ -524,7 +420,7 @@ App::delete('/v1/users/:userId/sessions')
|
||||
}
|
||||
}
|
||||
|
||||
$response->json(array('result' => 'success'));
|
||||
$response->noContent();
|
||||
}, ['response', 'projectDB']);
|
||||
|
||||
App::delete('/v1/users/:userId')
|
||||
@@ -567,7 +463,9 @@ App::delete('/v1/users/:userId')
|
||||
throw new Exception('Failed saving reserved id to DB', 500);
|
||||
}
|
||||
|
||||
$deletes->setParam('document', $user);
|
||||
$deletes
|
||||
->setParam('document', $user)
|
||||
;
|
||||
|
||||
$response->noContent();
|
||||
}, ['response', 'projectDB', 'deletes']);
|
||||
|
||||
Reference in New Issue
Block a user