Work in progress account/teams/users
This commit is contained in:
parent
ca2dbb9fad
commit
e70de897d6
File diff suppressed because it is too large
Load diff
|
@ -54,9 +54,9 @@ App::post('/v1/projects')
|
|||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Database\Database $dbForExternal */
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if ($team->isEmpty()) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
<?php
|
||||
|
||||
use Utopia\App;
|
||||
use Utopia\Exception;
|
||||
use Utopia\Config\Config;
|
||||
use Appwrite\Network\Validator\Email;
|
||||
use Utopia\Validator\Text;
|
||||
use Appwrite\Network\Validator\Host;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\WhiteList;
|
||||
use Appwrite\Auth\Auth;
|
||||
use Appwrite\Database\Database;
|
||||
use Appwrite\Database\Document;
|
||||
use Appwrite\Database\Validator\UID;
|
||||
use Appwrite\Database\Validator\Authorization;
|
||||
use Appwrite\Database\Exception\Duplicate;
|
||||
use Appwrite\Database\Validator\Key;
|
||||
use Appwrite\Detector\Detector;
|
||||
use Appwrite\Template\Template;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Network\Validator\Email;
|
||||
use Appwrite\Network\Validator\Host;
|
||||
use Utopia\App;
|
||||
use Utopia\Exception;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Validator\Text;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\WhiteList;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Duplicate;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\Key;
|
||||
|
||||
App::post('/v1/teams')
|
||||
->desc('Create Team')
|
||||
|
@ -36,41 +36,33 @@ App::post('/v1/teams')
|
|||
->param('roles', ['owner'], new ArrayList(new Key()), 'Array of strings. Use this param to set the roles in the team for the user who created it. The default role is **owner**. A role can be any string. Learn more about [roles and permissions](/docs/permissions). Max length for each role is 32 chars.', true)
|
||||
->inject('response')
|
||||
->inject('user')
|
||||
->inject('projectDB')
|
||||
->action(function ($name, $roles, $response, $user, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($name, $roles, $response, $user, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $user */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Document $user */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
Authorization::disable();
|
||||
|
||||
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::$roles);
|
||||
$isAppUser = Auth::isAppUser(Authorization::$roles);
|
||||
|
||||
$team = $projectDB->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_TEAMS,
|
||||
'$permissions' => [
|
||||
'read' => ['team:{self}'],
|
||||
'write' => ['team:{self}/owner'],
|
||||
],
|
||||
$teamId = $dbForInternal->getId();
|
||||
$team = $dbForInternal->createDocument('teams', new Document([
|
||||
'$id' => $teamId ,
|
||||
'$read' => ['team:'.$teamId ],
|
||||
'$write' => ['team:'.$teamId .'/owner'],
|
||||
'name' => $name,
|
||||
'sum' => ($isPrivilegedUser || $isAppUser) ? 0 : 1,
|
||||
'dateCreated' => \time(),
|
||||
]);
|
||||
]));
|
||||
|
||||
Authorization::reset();
|
||||
|
||||
if (false === $team) {
|
||||
throw new Exception('Failed saving team to DB', 500);
|
||||
}
|
||||
|
||||
if (!$isPrivilegedUser && !$isAppUser) { // Don't add user on server mode
|
||||
$membership = new Document([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_MEMBERSHIPS,
|
||||
'$permissions' => [
|
||||
'read' => ['user:'.$user->getId(), 'team:'.$team->getId()],
|
||||
'write' => ['user:'.$user->getId(), 'team:'.$team->getId().'/owner'],
|
||||
],
|
||||
'$read' => ['user:'.$user->getId(), 'team:'.$team->getId()],
|
||||
'$write' => ['user:'.$user->getId(), 'team:'.$team->getId().'/owner'],
|
||||
'userId' => $user->getId(),
|
||||
'teamId' => $team->getId(),
|
||||
'roles' => $roles,
|
||||
|
@ -80,20 +72,16 @@ App::post('/v1/teams')
|
|||
'secret' => '',
|
||||
]);
|
||||
|
||||
$membership = $dbForInternal->createDocument('memberships', $membership);
|
||||
|
||||
// Attach user to team
|
||||
$user->setAttribute('memberships', $membership, Document::SET_TYPE_APPEND);
|
||||
|
||||
$user = $projectDB->updateDocument($user->getArrayCopy());
|
||||
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
$user = $dbForInternal->updateDocument('users', $user->getId(), $user);
|
||||
}
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($team, Response::MODEL_TEAM)
|
||||
;
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic2($team, Response::MODEL_TEAM);
|
||||
});
|
||||
|
||||
App::get('/v1/teams')
|
||||
|
@ -112,23 +100,16 @@ App::get('/v1/teams')
|
|||
->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)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$results = $projectDB->getCollection([
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'orderType' => $orderType,
|
||||
'search' => $search,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_TEAMS,
|
||||
],
|
||||
]);
|
||||
$results = $dbForInternal->find('teams', [], $limit, $offset);
|
||||
$sum = $dbForInternal->count('teams', [], 5000);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $projectDB->getSum(),
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => $sum,
|
||||
'teams' => $results
|
||||
]), Response::MODEL_TEAM_LIST);
|
||||
});
|
||||
|
@ -146,18 +127,18 @@ App::get('/v1/teams/:teamId')
|
|||
->label('sdk.response.model', Response::MODEL_TEAM)
|
||||
->param('teamId', '', new UID(), 'Team unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($teamId, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($teamId, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
$response->dynamic($team, Response::MODEL_TEAM);
|
||||
$response->dynamic2($team, Response::MODEL_TEAM);
|
||||
});
|
||||
|
||||
App::put('/v1/teams/:teamId')
|
||||
|
@ -175,26 +156,20 @@ App::put('/v1/teams/:teamId')
|
|||
->param('teamId', '', new UID(), 'Team unique ID.')
|
||||
->param('name', null, new Text(128), 'Team name. Max length: 128 chars.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($teamId, $name, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($teamId, $name, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
$team = $projectDB->updateDocument(\array_merge($team->getArrayCopy(), [
|
||||
'name' => $name,
|
||||
]));
|
||||
$team = $dbForInternal->updateDocument('teams', $team->getId(), $team->setAttribute('name', $name));
|
||||
|
||||
if (false === $team) {
|
||||
throw new Exception('Failed saving team to DB', 500);
|
||||
}
|
||||
|
||||
$response->dynamic($team, Response::MODEL_TEAM);
|
||||
$response->dynamic2($team, Response::MODEL_TEAM);
|
||||
});
|
||||
|
||||
App::delete('/v1/teams/:teamId')
|
||||
|
@ -210,40 +185,37 @@ App::delete('/v1/teams/:teamId')
|
|||
->label('sdk.response.model', Response::MODEL_NONE)
|
||||
->param('teamId', '', new UID(), 'Team unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('events')
|
||||
->action(function ($teamId, $response, $projectDB, $events) {
|
||||
->action(function ($teamId, $response, $dbForInternal, $events) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
$memberships = $projectDB->getCollection([
|
||||
'limit' => 2000, // TODO add members limit
|
||||
'offset' => 0,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
|
||||
'teamId='.$teamId,
|
||||
],
|
||||
]);
|
||||
$memberships = $dbForInternal->find('memberships', [
|
||||
new Query('teamId', Query::TYPE_EQUAL, [$teamId]),
|
||||
], 2000, 0); // TODO fix members limit
|
||||
|
||||
// TODO delete all members individually from the user object
|
||||
|
||||
foreach ($memberships as $member) {
|
||||
if (!$projectDB->deleteDocument($member->getId())) {
|
||||
if (!$dbForInternal->deleteDocument('memberships', $member->getId())) {
|
||||
throw new Exception('Failed to remove membership for team from DB', 500);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$projectDB->deleteDocument($teamId)) {
|
||||
if (!$dbForInternal->deleteDocument('teams', $teamId)) {
|
||||
throw new Exception('Failed to remove team from DB', 500);
|
||||
}
|
||||
|
||||
$events
|
||||
->setParam('eventData', $response->output($team, Response::MODEL_TEAM))
|
||||
->setParam('eventData', $response->output2($team, Response::MODEL_TEAM))
|
||||
;
|
||||
|
||||
$response->noContent();
|
||||
|
@ -271,15 +243,15 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
->inject('response')
|
||||
->inject('project')
|
||||
->inject('user')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('locale')
|
||||
->inject('audits')
|
||||
->inject('mails')
|
||||
->action(function ($teamId, $email, $name, $roles, $url, $response, $project, $user, $projectDB, $locale, $audits, $mails) {
|
||||
->action(function ($teamId, $email, $name, $roles, $url, $response, $project, $user, $dbForInternal, $locale, $audits, $mails) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
/** @var Appwrite\Database\Document $user */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Event\Event $mails */
|
||||
|
||||
|
@ -287,41 +259,21 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
$isAppUser = Auth::isAppUser(Authorization::$roles);
|
||||
|
||||
$name = (empty($name)) ? $email : $name;
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
$memberships = $projectDB->getCollection([
|
||||
'limit' => 50,
|
||||
'offset' => 0,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
|
||||
'teamId='.$team->getId(),
|
||||
],
|
||||
]);
|
||||
|
||||
$invitee = $projectDB->getCollectionFirst([ // Get user by email address
|
||||
'limit' => 1,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_USERS,
|
||||
'email='.$email,
|
||||
],
|
||||
]);
|
||||
$memberships = $dbForInternal->findFirst('memberships', [new Query('teamId', Query::TYPE_EQUAL, [$team->getId()])], 2000, 0);
|
||||
$invitee = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [$email])], 1); // Get user by email address
|
||||
|
||||
if (empty($invitee)) { // Create new user if no user with same email found
|
||||
|
||||
$limit = $project->getAttribute('usersAuthLimit', 0);
|
||||
|
||||
if ($limit !== 0 && $project->getId() !== 'console') { // check users limit, console invites are allways allowed.
|
||||
$projectDB->getCollection([ // Count users
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_USERS,
|
||||
],
|
||||
]);
|
||||
|
||||
$sum = $projectDB->getSum();
|
||||
$sum = $dbForInternal->count('users'); // Count users TODO: add a 10k limit here.
|
||||
|
||||
if($sum >= $limit) {
|
||||
throw new Exception('Project registration is restricted. Contact your administrator for more information.', 501);
|
||||
|
@ -331,12 +283,11 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
Authorization::disable();
|
||||
|
||||
try {
|
||||
$invitee = $projectDB->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_USERS,
|
||||
'$permissions' => [
|
||||
'read' => ['user:{self}', '*'],
|
||||
'write' => ['user:{self}'],
|
||||
],
|
||||
$userId = $dbForInternal->getId();
|
||||
$invitee = $dbForInternal->createDocument('users', new Document([
|
||||
'$id' => $userId,
|
||||
'$read' => ['user:'.$userId, '*'],
|
||||
'$write' => ['user:'.$userId],
|
||||
'email' => $email,
|
||||
'emailVerification' => false,
|
||||
'status' => Auth::USER_STATUS_UNACTIVATED,
|
||||
|
@ -347,16 +298,12 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
'name' => $name,
|
||||
'sessions' => [],
|
||||
'tokens' => [],
|
||||
], ['email' => $email]);
|
||||
]));
|
||||
} catch (Duplicate $th) {
|
||||
throw new Exception('Account already exists', 409);
|
||||
}
|
||||
|
||||
Authorization::reset();
|
||||
|
||||
if (false === $invitee) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
}
|
||||
|
||||
$isOwner = false;
|
||||
|
@ -378,11 +325,9 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
$secret = Auth::tokenGenerator();
|
||||
|
||||
$membership = new Document([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_MEMBERSHIPS,
|
||||
'$permissions' => [
|
||||
'read' => ['*'],
|
||||
'write' => ['user:'.$invitee->getId(), 'team:'.$team->getId().'/owner'],
|
||||
],
|
||||
'$id' => $dbForInternal->getId(),
|
||||
'$read' => ['*'],
|
||||
'$write' => ['user:'.$invitee->getId(), 'team:'.$team->getId().'/owner'],
|
||||
'userId' => $invitee->getId(),
|
||||
'teamId' => $team->getId(),
|
||||
'roles' => $roles,
|
||||
|
@ -394,28 +339,18 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
|
||||
if ($isPrivilegedUser || $isAppUser) { // Allow admin to create membership
|
||||
Authorization::disable();
|
||||
$membership = $projectDB->createDocument($membership->getArrayCopy());
|
||||
$membership = $dbForInternal->createDocument('memberships', $membership);
|
||||
|
||||
$team = $projectDB->updateDocument(\array_merge($team->getArrayCopy(), [
|
||||
'sum' => $team->getAttribute('sum', 0) + 1,
|
||||
]));
|
||||
$team = $dbForInternal->updateDocument('teams', $team->getId(), $team->setAttribute('sum', $team->getAttribute('sum', 0) + 1));
|
||||
|
||||
// Attach user to team
|
||||
$invitee->setAttribute('memberships', $membership, Document::SET_TYPE_APPEND);
|
||||
|
||||
$invitee = $projectDB->updateDocument($invitee->getArrayCopy());
|
||||
|
||||
if (false === $invitee) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
$invitee = $dbForInternal->updateDocument('users', $invitee->getId(), $invitee);
|
||||
|
||||
Authorization::reset();
|
||||
} else {
|
||||
$membership = $projectDB->createDocument($membership->getArrayCopy());
|
||||
}
|
||||
|
||||
if (false === $membership) {
|
||||
throw new Exception('Failed saving membership to DB', 500);
|
||||
$membership = $dbForInternal->createDocument('memberships', $membership);
|
||||
}
|
||||
|
||||
$url = Template::parseURL($url);
|
||||
|
@ -462,13 +397,11 @@ App::post('/v1/teams/:teamId/memberships')
|
|||
->setParam('resource', 'teams/'.$teamId)
|
||||
;
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic(new Document(\array_merge($membership->getArrayCopy(), [
|
||||
'email' => $email,
|
||||
'name' => $name,
|
||||
])), Response::MODEL_MEMBERSHIP)
|
||||
;
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic2($membership
|
||||
->setAttribute('email', $email)
|
||||
->setAttribute('name', $name)
|
||||
, Response::MODEL_MEMBERSHIP);
|
||||
});
|
||||
|
||||
App::get('/v1/teams/:teamId/memberships')
|
||||
|
@ -488,27 +421,19 @@ App::get('/v1/teams/:teamId/memberships')
|
|||
->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)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($teamId, $search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($teamId, $search, $limit, $offset, $orderType, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
$memberships = $projectDB->getCollection([
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'orderType' => $orderType,
|
||||
'search' => $search,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
|
||||
'teamId='.$teamId,
|
||||
],
|
||||
]);
|
||||
$memberships = $dbForInternal->find('memberships', [new Query('teamId', Query::TYPE_EQUAL, [$teamId])], $limit, $offset);
|
||||
$sum = $dbForInternal->count('memberships', [new Query('teamId', Query::TYPE_EQUAL, [$teamId])], 5000);
|
||||
$users = [];
|
||||
|
||||
foreach ($memberships as $membership) {
|
||||
|
@ -516,12 +441,15 @@ App::get('/v1/teams/:teamId/memberships')
|
|||
continue;
|
||||
}
|
||||
|
||||
$temp = $projectDB->getDocument($membership->getAttribute('userId', null))->getArrayCopy(['email', 'name']);
|
||||
$temp = $dbForInternal->getDocument('users', $membership->getAttribute('userId', null))->getArrayCopy(['email', 'name']);
|
||||
|
||||
$users[] = new Document(\array_merge($temp, $membership->getArrayCopy()));
|
||||
}
|
||||
|
||||
$response->dynamic(new Document(['sum' => $projectDB->getSum(), 'memberships' => $users]), Response::MODEL_MEMBERSHIP_LIST);
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => $sum,
|
||||
'memberships' => $users
|
||||
]), Response::MODEL_MEMBERSHIP_LIST);
|
||||
});
|
||||
|
||||
App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
||||
|
@ -543,21 +471,21 @@ App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
|||
->inject('request')
|
||||
->inject('response')
|
||||
->inject('user')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('geodb')
|
||||
->inject('audits')
|
||||
->action(function ($teamId, $inviteId, $userId, $secret, $request, $response, $user, $projectDB, $geodb, $audits) {
|
||||
->action(function ($teamId, $inviteId, $userId, $secret, $request, $response, $user, $dbForInternal, $geodb, $audits) {
|
||||
/** @var Utopia\Swoole\Request $request */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $user */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var MaxMind\Db\Reader $geodb */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
|
||||
$protocol = $request->getProtocol();
|
||||
$membership = $projectDB->getDocument($inviteId);
|
||||
$membership = $dbForInternal->getDocument('memberships', $inviteId);
|
||||
|
||||
if (empty($membership->getId()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $membership->getCollection()) {
|
||||
if (empty($membership->getId())) {
|
||||
throw new Exception('Invite not found', 404);
|
||||
}
|
||||
|
||||
|
@ -567,11 +495,11 @@ App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
|||
|
||||
Authorization::disable();
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
Authorization::reset();
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
|
@ -584,13 +512,7 @@ App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
|||
}
|
||||
|
||||
if (empty($user->getId())) {
|
||||
$user = $projectDB->getCollectionFirst([ // Get user
|
||||
'limit' => 1,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_USERS,
|
||||
'$id='.$userId,
|
||||
],
|
||||
]);
|
||||
$user = $dbForInternal->getDocument('users', $userId); // Get user
|
||||
}
|
||||
|
||||
if ($membership->getAttribute('userId') !== $user->getId()) {
|
||||
|
@ -614,8 +536,7 @@ App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
|||
$expiry = \time() + Auth::TOKEN_EXPIRATION_LOGIN_LONG;
|
||||
$secret = Auth::tokenGenerator();
|
||||
$session = new Document(array_merge([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_SESSIONS,
|
||||
'$permissions' => ['read' => ['user:'.$user->getId()], 'write' => ['user:'.$user->getId()]],
|
||||
'$id' => $dbForInternal->getId(),
|
||||
'userId' => $user->getId(),
|
||||
'provider' => Auth::SESSION_PROVIDER_EMAIL,
|
||||
'providerUid' => $user->getAttribute('email'),
|
||||
|
@ -630,24 +551,14 @@ App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
|||
|
||||
Authorization::setRole('user:'.$userId);
|
||||
|
||||
$user = $projectDB->updateDocument($user->getArrayCopy());
|
||||
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
$user = $dbForInternal->updateDocument('users', $user->getId(), $user);
|
||||
|
||||
Authorization::disable();
|
||||
|
||||
$team = $projectDB->updateDocument(\array_merge($team->getArrayCopy(), [
|
||||
'sum' => $team->getAttribute('sum', 0) + 1,
|
||||
]));
|
||||
$team = $dbForInternal->updateDocument('teams', $team->getId(), $team->setAttribute('sum', $team->getAttribute('sum', 0) + 1));
|
||||
|
||||
Authorization::reset();
|
||||
|
||||
if (false === $team) {
|
||||
throw new Exception('Failed saving team to DB', 500);
|
||||
}
|
||||
|
||||
$audits
|
||||
->setParam('userId', $user->getId())
|
||||
->setParam('event', 'teams.membership.update')
|
||||
|
@ -665,10 +576,10 @@ App::patch('/v1/teams/:teamId/memberships/:inviteId/status')
|
|||
->addCookie(Auth::$cookieName, Auth::encodeSession($user->getId(), $secret), $expiry, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, Config::getParam('cookieSamesite'))
|
||||
;
|
||||
|
||||
$response->dynamic(new Document(\array_merge($membership->getArrayCopy(), [
|
||||
'email' => $user->getAttribute('email'),
|
||||
'name' => $user->getAttribute('name'),
|
||||
])), Response::MODEL_MEMBERSHIP);
|
||||
$response->dynamic2($membership
|
||||
->setAttribute('email', $user->getAttribute('email'))
|
||||
->setAttribute('name', $user->getAttribute('name'))
|
||||
, Response::MODEL_MEMBERSHIP);
|
||||
});
|
||||
|
||||
App::delete('/v1/teams/:teamId/memberships/:inviteId')
|
||||
|
@ -685,18 +596,18 @@ App::delete('/v1/teams/:teamId/memberships/:inviteId')
|
|||
->param('teamId', '', new UID(), 'Team unique ID.')
|
||||
->param('inviteId', '', new UID(), 'Invite unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('audits')
|
||||
->inject('events')
|
||||
->action(function ($teamId, $inviteId, $response, $projectDB, $audits, $events) {
|
||||
->action(function ($teamId, $inviteId, $response, $dbForInternal, $audits, $events) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
|
||||
$membership = $projectDB->getDocument($inviteId);
|
||||
$membership = $dbForInternal->getDocument('memberships', $inviteId);
|
||||
|
||||
if (empty($membership->getId()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $membership->getCollection()) {
|
||||
if (empty($membership->getId())) {
|
||||
throw new Exception('Invite not found', 404);
|
||||
}
|
||||
|
||||
|
@ -704,24 +615,18 @@ App::delete('/v1/teams/:teamId/memberships/:inviteId')
|
|||
throw new Exception('Team IDs don\'t match', 404);
|
||||
}
|
||||
|
||||
$team = $projectDB->getDocument($teamId);
|
||||
$team = $dbForInternal->getDocument('teams', $teamId);
|
||||
|
||||
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
|
||||
if (empty($team->getId())) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
if (!$projectDB->deleteDocument($membership->getId())) {
|
||||
if (!$dbForInternal->deleteDocument('memberships', $membership->getId())) {
|
||||
throw new Exception('Failed to remove membership from DB', 500);
|
||||
}
|
||||
|
||||
if ($membership->getAttribute('confirm')) { // Count only confirmed members
|
||||
$team = $projectDB->updateDocument(\array_merge($team->getArrayCopy(), [
|
||||
'sum' => $team->getAttribute('sum', 0) - 1,
|
||||
]));
|
||||
}
|
||||
|
||||
if (false === $team) {
|
||||
throw new Exception('Failed saving team to DB', 500);
|
||||
$team = $dbForInternal->updateDocument('teams', $team->getId(), $team->setAttribute('sum', $team->getAttribute('sum', 0) - 1));
|
||||
}
|
||||
|
||||
$audits
|
||||
|
@ -731,7 +636,7 @@ App::delete('/v1/teams/:teamId/memberships/:inviteId')
|
|||
;
|
||||
|
||||
$events
|
||||
->setParam('eventData', $response->output($membership, Response::MODEL_MEMBERSHIP))
|
||||
->setParam('eventData', $response->output2($membership, Response::MODEL_MEMBERSHIP))
|
||||
;
|
||||
|
||||
$response->noContent();
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
<?php
|
||||
|
||||
use Appwrite\Auth\Auth;
|
||||
use Appwrite\Auth\Validator\Password;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\App;
|
||||
use Utopia\Exception;
|
||||
use Utopia\Validator\Assoc;
|
||||
|
@ -9,13 +12,9 @@ use Utopia\Validator\Text;
|
|||
use Utopia\Validator\Range;
|
||||
use Utopia\Audit\Audit;
|
||||
use Utopia\Audit\Adapters\MySQL as AuditAdapter;
|
||||
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;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Duplicate;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use DeviceDetector\DeviceDetector;
|
||||
|
||||
App::post('/v1/users')
|
||||
|
@ -34,30 +33,17 @@ App::post('/v1/users')
|
|||
->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)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($email, $password, $name, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($email, $password, $name, $response, $dbForInternal) {
|
||||
/** @var Appwrite\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);
|
||||
}
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
try {
|
||||
$user = $projectDB->createDocument([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_USERS,
|
||||
'$permissions' => [
|
||||
'read' => ['*'],
|
||||
'write' => ['user:{self}'],
|
||||
],
|
||||
$userId = $dbForInternal->getId();
|
||||
$user = $dbForInternal->createDocument('users', new Document([
|
||||
'$id' => $userId,
|
||||
'$read' => ['*'],
|
||||
'$write' => ['user:'.$userId],
|
||||
'email' => $email,
|
||||
'emailVerification' => false,
|
||||
'status' => Auth::USER_STATUS_UNACTIVATED,
|
||||
|
@ -66,15 +52,17 @@ App::post('/v1/users')
|
|||
'registration' => \time(),
|
||||
'reset' => false,
|
||||
'name' => $name,
|
||||
], ['email' => $email]);
|
||||
'prefs' => [],
|
||||
'sessions' => [],
|
||||
'tokens' => [],
|
||||
'memberships' => [],
|
||||
]));
|
||||
} catch (Duplicate $th) {
|
||||
throw new Exception('Account already exists', 409);
|
||||
}
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($user, Response::MODEL_USER)
|
||||
;
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic2($user, Response::MODEL_USER);
|
||||
});
|
||||
|
||||
App::get('/v1/users')
|
||||
|
@ -93,23 +81,16 @@ App::get('/v1/users')
|
|||
->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)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($search, $limit, $offset, $orderType, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$results = $projectDB->getCollection([
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'orderType' => $orderType,
|
||||
'search' => $search,
|
||||
'filters' => [
|
||||
'$collection='.Database::SYSTEM_COLLECTION_USERS,
|
||||
],
|
||||
]);
|
||||
$results = $dbForInternal->find('users', [], $limit, $offset);
|
||||
$sum = $dbForInternal->count('users', [], 5000);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $projectDB->getSum(),
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => $sum,
|
||||
'users' => $results
|
||||
]), Response::MODEL_USER_LIST);
|
||||
});
|
||||
|
@ -127,18 +108,18 @@ App::get('/v1/users/:userId')
|
|||
->label('sdk.response.model', Response::MODEL_USER)
|
||||
->param('userId', '', new UID(), 'User unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($userId, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($userId, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
$response->dynamic2($user, Response::MODEL_USER);
|
||||
});
|
||||
|
||||
App::get('/v1/users/:userId/prefs')
|
||||
|
@ -154,20 +135,20 @@ App::get('/v1/users/:userId/prefs')
|
|||
->label('sdk.response.model', Response::MODEL_PREFERENCES)
|
||||
->param('userId', '', new UID(), 'User unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($userId, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($userId, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$prefs = $user->getAttribute('prefs', new \stdClass());
|
||||
|
||||
$response->dynamic(new Document($prefs), Response::MODEL_PREFERENCES);
|
||||
$response->dynamic2(new Document($prefs), Response::MODEL_PREFERENCES);
|
||||
});
|
||||
|
||||
App::get('/v1/users/:userId/sessions')
|
||||
|
@ -183,16 +164,16 @@ App::get('/v1/users/:userId/sessions')
|
|||
->label('sdk.response.model', Response::MODEL_SESSION_LIST)
|
||||
->param('userId', '', new UID(), 'User unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('locale')
|
||||
->action(function ($userId, $response, $projectDB, $locale) {
|
||||
->action(function ($userId, $response, $dbForInternal, $locale) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Locale\Locale $locale */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
|
@ -210,11 +191,11 @@ App::get('/v1/users/:userId/sessions')
|
|||
$sessions[$key] = $session;
|
||||
}
|
||||
|
||||
$response->dynamic(new Document([
|
||||
$response->dynamic2(new Document([
|
||||
'sum' => count($sessions),
|
||||
'sessions' => $sessions
|
||||
]), Response::MODEL_SESSION_LIST);
|
||||
}, ['response', 'projectDB', 'locale']);
|
||||
}, ['response', 'dbForInternal', 'locale']);
|
||||
|
||||
App::get('/v1/users/:userId/logs')
|
||||
->desc('Get User Logs')
|
||||
|
@ -231,20 +212,20 @@ App::get('/v1/users/:userId/logs')
|
|||
->inject('response')
|
||||
->inject('register')
|
||||
->inject('project')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('locale')
|
||||
->inject('geodb')
|
||||
->action(function ($userId, $response, $register, $project, $projectDB, $locale, $geodb) {
|
||||
->action(function ($userId, $response, $register, $project, $dbForInternal, $locale, $geodb) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Utopia\Registry\Registry $register */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Locale\Locale $locale */
|
||||
/** @var MaxMind\Db\Reader $geodb */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
|
@ -327,7 +308,7 @@ App::get('/v1/users/:userId/logs')
|
|||
}
|
||||
}
|
||||
|
||||
$response->dynamic(new Document(['logs' => $output]), Response::MODEL_LOG_LIST);
|
||||
$response->dynamic2(new Document(['logs' => $output]), Response::MODEL_LOG_LIST);
|
||||
});
|
||||
|
||||
App::patch('/v1/users/:userId/status')
|
||||
|
@ -345,26 +326,20 @@ App::patch('/v1/users/:userId/status')
|
|||
->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)
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($userId, $status, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($userId, $status, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
|
||||
'status' => (int)$status,
|
||||
]));
|
||||
$user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('status', (int)$status));
|
||||
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
$response->dynamic2($user, Response::MODEL_USER);
|
||||
});
|
||||
|
||||
App::patch('/v1/users/:userId/prefs')
|
||||
|
@ -382,26 +357,20 @@ App::patch('/v1/users/:userId/prefs')
|
|||
->param('userId', '', new UID(), 'User unique ID.')
|
||||
->param('prefs', '', new Assoc(), 'Prefs key-value JSON object.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->action(function ($userId, $prefs, $response, $projectDB) {
|
||||
->inject('dbForInternal')
|
||||
->action(function ($userId, $prefs, $response, $dbForInternal) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if ($user->isEmpty()) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$user = $projectDB->updateDocument(\array_merge($user->getArrayCopy(), [
|
||||
'prefs' => $prefs,
|
||||
]));
|
||||
$user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('prefs', $prefs));
|
||||
|
||||
if (false === $user) {
|
||||
throw new Exception('Failed saving user to DB', 500);
|
||||
}
|
||||
|
||||
$response->dynamic(new Document($prefs), Response::MODEL_PREFERENCES);
|
||||
$response->dynamic2(new Document($prefs), Response::MODEL_PREFERENCES);
|
||||
});
|
||||
|
||||
App::delete('/v1/users/:userId/sessions/:sessionId')
|
||||
|
@ -418,32 +387,34 @@ App::delete('/v1/users/:userId/sessions/:sessionId')
|
|||
->param('userId', '', new UID(), 'User unique ID.')
|
||||
->param('sessionId', null, new UID(), 'User unique session ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('events')
|
||||
->action(function ($userId, $sessionId, $response, $projectDB, $events) {
|
||||
->action(function ($userId, $sessionId, $response, $dbForInternal, $events) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$sessions = $user->getAttribute('sessions', []);
|
||||
|
||||
foreach ($sessions as $session) {
|
||||
foreach ($sessions as $key => $session) {
|
||||
/** @var Document $session */
|
||||
|
||||
if ($sessionId == $session->getId()) {
|
||||
if (!$projectDB->deleteDocument($session->getId())) {
|
||||
throw new Exception('Failed to remove token from DB', 500);
|
||||
}
|
||||
unset($sessions[$key]);
|
||||
|
||||
$user->setAttribute('sessions', $sessions);
|
||||
|
||||
$events
|
||||
->setParam('eventData', $response->output($user, Response::MODEL_USER))
|
||||
->setParam('eventData', $response->output2($user, Response::MODEL_USER))
|
||||
;
|
||||
|
||||
$dbForInternal->updateDocument('users', $user->getId(), $user);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -464,31 +435,25 @@ App::delete('/v1/users/:userId/sessions')
|
|||
->label('sdk.response.model', Response::MODEL_NONE)
|
||||
->param('userId', '', new UID(), 'User unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('events')
|
||||
->action(function ($userId, $response, $projectDB, $events) {
|
||||
->action(function ($userId, $response, $dbForInternal, $events) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if (empty($user->getId())) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
|
||||
$sessions = $user->getAttribute('sessions', []);
|
||||
|
||||
foreach ($sessions as $session) {
|
||||
/** @var Document $session */
|
||||
|
||||
if (!$projectDB->deleteDocument($session->getId())) {
|
||||
throw new Exception('Failed to remove token from DB', 500);
|
||||
}
|
||||
}
|
||||
$dbForInternal->updateDocument('users', $user->getId(), $user);
|
||||
|
||||
$events
|
||||
->setParam('eventData', $response->output($user, Response::MODEL_USER))
|
||||
->setParam('eventData', $response->output2($user, Response::MODEL_USER))
|
||||
;
|
||||
|
||||
// TODO : Response filter implementation
|
||||
|
@ -508,39 +473,29 @@ App::delete('/v1/users/:userId')
|
|||
->label('sdk.response.model', Response::MODEL_NONE)
|
||||
->param('userId', '', function () {return new UID();}, 'User unique ID.')
|
||||
->inject('response')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('events')
|
||||
->inject('deletes')
|
||||
->action(function ($userId, $response, $projectDB, $events, $deletes) {
|
||||
->action(function ($userId, $response, $dbForInternal, $events, $deletes) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
/** @var Appwrite\Event\Event $deletes */
|
||||
|
||||
$user = $projectDB->getDocument($userId);
|
||||
$user = $dbForInternal->getDocument('users', $userId);
|
||||
|
||||
if (empty($user->getId()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
|
||||
if ($user->isEmpty()) {
|
||||
throw new Exception('User not found', 404);
|
||||
}
|
||||
if (!$projectDB->deleteDocument($userId)) {
|
||||
|
||||
if (!$dbForInternal->deleteDocument('users', $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);
|
||||
}
|
||||
// $dbForInternal->createDocument('users', new Document([
|
||||
// '$id' => $userId,
|
||||
// '$read' => ['*'],
|
||||
// ]));
|
||||
|
||||
$deletes
|
||||
->setParam('type', DELETE_TYPE_DOCUMENT)
|
||||
|
@ -548,7 +503,7 @@ App::delete('/v1/users/:userId')
|
|||
;
|
||||
|
||||
$events
|
||||
->setParam('eventData', $response->output($user, Response::MODEL_USER))
|
||||
->setParam('eventData', $response->output2($user, Response::MODEL_USER))
|
||||
;
|
||||
|
||||
// TODO : Response filter implementation
|
||||
|
|
22
app/init.php
22
app/init.php
|
@ -406,12 +406,12 @@ App::setResource('clients', function($request, $console, $project) {
|
|||
return $clients;
|
||||
}, ['request', 'console', 'project']);
|
||||
|
||||
App::setResource('user', function($mode, $project, $console, $request, $response, $projectDB, $consoleDB) {
|
||||
App::setResource('user', function($mode, $project, $console, $request, $response, $dbForInternal, $dbForConsole) {
|
||||
/** @var Utopia\Swoole\Request $request */
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Appwrite\Database\Document $project */
|
||||
/** @var Appwrite\Database\Database $consoleDB */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Document $project */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Database\Database $dbForConsole */
|
||||
/** @var bool $mode */
|
||||
|
||||
Authorization::setDefaultStatus(true);
|
||||
|
@ -437,14 +437,14 @@ App::setResource('user', function($mode, $project, $console, $request, $response
|
|||
$session = Auth::decodeSession(((isset($fallback[Auth::$cookieName])) ? $fallback[Auth::$cookieName] : ''));
|
||||
}
|
||||
|
||||
Auth::$unique = $session['id'];
|
||||
Auth::$secret = $session['secret'];
|
||||
Auth::$unique = $session['id'] ?? '';
|
||||
Auth::$secret = $session['secret'] ?? '';
|
||||
|
||||
if (APP_MODE_ADMIN !== $mode) {
|
||||
$user = $projectDB->getDocument(Auth::$unique);
|
||||
$user = $dbForInternal->getDocument('users', Auth::$unique);
|
||||
}
|
||||
else {
|
||||
$user = $consoleDB->getDocument(Auth::$unique);
|
||||
$user = $dbForConsole->getDocument('users', Auth::$unique);
|
||||
|
||||
$user
|
||||
->setAttribute('$id', 'admin-'.$user->getAttribute('$id'))
|
||||
|
@ -481,7 +481,7 @@ App::setResource('user', function($mode, $project, $console, $request, $response
|
|||
$jwtSessionId = $payload['sessionId'] ?? '';
|
||||
|
||||
if($jwtUserId && $jwtSessionId) {
|
||||
$user = $projectDB->getDocument($jwtUserId);
|
||||
$user = $dbForInternal->getDocument('users', $jwtUserId);
|
||||
}
|
||||
|
||||
if (empty($user->search('$id', $jwtSessionId, $user->getAttribute('tokens')))) { // Match JWT to active token
|
||||
|
@ -490,7 +490,7 @@ App::setResource('user', function($mode, $project, $console, $request, $response
|
|||
}
|
||||
|
||||
return $user;
|
||||
}, ['mode', 'project', 'console', 'request', 'response', 'projectDB', 'consoleDB']);
|
||||
}, ['mode', 'project', 'console', 'request', 'response', 'dbForInternal', 'dbForConsole']);
|
||||
|
||||
App::setResource('project', function($consoleDB, $request) {
|
||||
/** @var Utopia\Swoole\Request $request */
|
||||
|
@ -500,7 +500,7 @@ App::setResource('project', function($consoleDB, $request) {
|
|||
Authorization2::disable();
|
||||
|
||||
$project = $consoleDB->getDocument($request->getParam('project',
|
||||
$request->getHeader('x-appwrite-project', '')));
|
||||
$request->getHeader('x-appwrite-project', 'console')));
|
||||
|
||||
Authorization::reset();
|
||||
Authorization2::reset();
|
||||
|
|
Loading…
Reference in a new issue