Skip to content

Commit

Permalink
Merge pull request #2799 from appwrite/fix-0.12-teams-hotfix
Browse files Browse the repository at this point in the history
[0.12.x] fix: teams and search migration
  • Loading branch information
TorstenDittmann committed Feb 16, 2022
2 parents 4de45b7 + 2bfaf56 commit 137d8eb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
27 changes: 21 additions & 6 deletions app/controllers/api/teams.php
Expand Up @@ -564,25 +564,40 @@

$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
$isAppUser = Auth::isAppUser(Authorization::getRoles());
$isOwner = Authorization::isRole('team:'.$team->getId().'/owner');;
$isOwner = Authorization::isRole('team:' . $team->getId() . '/owner');;

if (!$isOwner && !$isPrivilegedUser && !$isAppUser) { // Not owner, not admin, not app (server)
throw new Exception('User is not allowed to modify roles', 401);
}

// Update the roles
/**
* Update the roles
*/
$membership->setAttribute('roles', $roles);
$membership = $dbForProject->updateDocument('memberships', $membership->getId(), $membership);

// TODO sync updated membership in the user $profile object using TYPE_REPLACE
/**
* Replace membership on profile
*/
$memberships = array_filter($profile->getAttribute('memberships'), fn (Document $m) => $m->getId() !== $membership->getId());

$profile
->setAttribute('memberships', $memberships)
->setAttribute('memberships', $membership, Document::SET_TYPE_APPEND);

Authorization::skip(fn () => $dbForProject->updateDocument('users', $profile->getId(), $profile));

$audits
->setParam('userId', $user->getId())
->setParam('event', 'teams.memberships.update')
->setParam('resource', 'team/'.$teamId)
;
->setParam('resource', 'team/' . $teamId);

$response->dynamic($membership, Response::MODEL_MEMBERSHIP);
$response->dynamic(
$membership
->setAttribute('email', $profile->getAttribute('email'))
->setAttribute('name', $profile->getAttribute('name')),
Response::MODEL_MEMBERSHIP
);
});

App::patch('/v1/teams/:teamId/memberships/:membershipId/status')
Expand Down
71 changes: 64 additions & 7 deletions src/Appwrite/Migration/Version/V11.php
Expand Up @@ -449,9 +449,9 @@ protected function fixDocument(OldDocument $oldDocument): Document|null
*/
$document->removeAttribute('tasks');

/*
* Add enabled OAuth2 providers to default data rules
*/
/**
* Add enabled OAuth2 providers to default data rules
*/
foreach ($providers as $index => $provider) {
$appId = $document->getAttribute('usersOauth2' . \ucfirst($index) . 'Appid');
$appSecret = $document->getAttribute('usersOauth2' . \ucfirst($index) . 'Secret');
Expand All @@ -467,9 +467,14 @@ protected function fixDocument(OldDocument $oldDocument): Document|null

$document->setAttribute('providers', $newProviders);

/*
* Migrate User providers settings
*/
/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name'], $document));

/**
* Migrate User providers settings
*/
$oldAuths = [
'email-password' => 'usersAuthEmailPassword',
'magic-url' => 'usersAuthMagicURL',
Expand Down Expand Up @@ -594,7 +599,29 @@ protected function fixDocument(OldDocument $oldDocument): Document|null
case OldDatabase::SYSTEM_COLLECTION_FUNCTIONS:
$document->setAttribute('events', $document->getAttribute('events', []));

/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name', 'runtime'], $document));

break;

case OldDatabase::SYSTEM_COLLECTION_TAGS:
/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'command'], $document));

break;

case OldDatabase::SYSTEM_COLLECTION_EXECUTIONS:
/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'functionId'], $document));

break;

case OldDatabase::SYSTEM_COLLECTION_WEBHOOKS:
$projectId = $this->getProjectIdFromReadPermissions($document);

Expand Down Expand Up @@ -656,18 +683,28 @@ protected function fixDocument(OldDocument $oldDocument): Document|null
$write = $document->getWrite();
$document->setAttribute('$write', str_replace('user:{self}', "user:{$document->getId()}", $write));

/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'email', 'name'], $document));

break;
case OldDatabase::SYSTEM_COLLECTION_TEAMS:

/**
* Replace team:{self} with team:TEAM_ID
*/
$read = $document->getWrite();
$read = $document->getRead();
$write = $document->getWrite();

$document->setAttribute('$read', str_replace('team:{self}', "team:{$document->getId()}", $read));
$document->setAttribute('$write', str_replace('team:{self}', "team:{$document->getId()}", $write));

/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name'], $document));

break;
case OldDatabase::SYSTEM_COLLECTION_FILES:
/**
Expand Down Expand Up @@ -699,6 +736,12 @@ protected function fixDocument(OldDocument $oldDocument): Document|null
*/
$document->removeAttribute('folderId');
$document->removeAttribute('token');

/**
* Populate search string
*/
$document->setAttribute('search', $this->buildSearchAttribute(['$id', 'name'], $document));

break;
}

Expand Down Expand Up @@ -818,4 +861,18 @@ protected function getProjectIdFromReadPermissions(Document $document): string|n

return $project->getId();
}

/**
* Builds a search string for a fulltext index.
*
* @param array $values
* @param Document $document
* @return string
*/
private function buildSearchAttribute(array $values, Document $document): string
{
$values = array_filter(array_map(fn (string $value) => $document->getAttribute($value) ?? '', $values));

return implode(' ', $values);
}
}

0 comments on commit 137d8eb

Please sign in to comment.