1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

feat: adding update membership role

This commit is contained in:
Christy Jacob 2021-05-12 19:14:41 +05:30
parent 934b9aec27
commit a38678f30d
3 changed files with 270 additions and 121 deletions

View file

@ -197,6 +197,11 @@ return [
'model' => Response::MODEL_MEMBERSHIP,
'note' => 'version >= 0.7',
],
'teams.memberships.update' => [
'description' => 'This event triggers when a team membership is updated.',
'model' => Response::MODEL_MEMBERSHIP,
'note' => 'version >= 0.8',
],
'teams.memberships.update.status' => [
'description' => 'This event triggers when a team memberships status is updated.',
'model' => Response::MODEL_MEMBERSHIP,

View file

@ -444,7 +444,7 @@ App::post('/v1/teams/:teamId/memberships')
->setParam('{{text-cta}}', '#ffffff')
;
if (!$isPrivilegedUser && !$isAppUser) { // No need in comfirmation when in admin or app mode
if (!$isPrivilegedUser && !$isAppUser) { // No need of confirmation when in admin or app mode
$mails
->setParam('event', 'teams.membership.create')
->setParam('from', ($project->getId() === 'console') ? '' : \sprintf($locale->getText('account.emails.team'), $project->getAttribute('name')))
@ -471,6 +471,110 @@ App::post('/v1/teams/:teamId/memberships')
;
});
App::patch('/v1/teams/:teamId/memberships/:membershipId')
->desc('Update Membership Roles')
->groups(['api', 'teams'])
->label('event', 'teams.memberships.update')
->label('scope', 'public')
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'teams')
->label('sdk.method', 'updateMembershipRoles')
->label('sdk.description', '/docs/references/teams/update-team-membership-roles.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_MEMBERSHIP)
->param('teamId', '', new UID(), 'Team unique ID.')
->param('membershipId', '', new UID(), 'Membership ID.')
->param('roles', [], new ArrayList(new Key()), 'Array of strings. Use this param to set the user roles in the team. A role can be any string. Learn more about [roles and permissions](/docs/permissions). Max length for each role is 32 chars.')
->inject('request')
->inject('response')
->inject('user')
->inject('projectDB')
->inject('audits')
->action(function ($teamId, $membershipId, $roles, $request, $response, $user, $projectDB,$audits) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Document $user */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Event\Event $audits */
$membership = $projectDB->getDocument($membershipId);
if (empty($membership->getId()) || Database::SYSTEM_COLLECTION_MEMBERSHIPS != $membership->getCollection()) {
throw new Exception('Membership not found', 404);
}
if ($membership->getAttribute('teamId') !== $teamId) {
throw new Exception('Team IDs don\'t match', 404);
}
$team = $projectDB->getDocument($teamId);
if (empty($team->getId()) || Database::SYSTEM_COLLECTION_TEAMS != $team->getCollection()) {
throw new Exception('Team not found', 404);
}
$userId = $membership->getAttribute('userId', '');
$user = $projectDB->getCollectionFirst([ // Get user
'limit' => 1,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_USERS,
'$id='.$userId,
],
]);
if (empty($user) || $user->getId() === null) {
throw new Exception("User associated with Membership Id not found", 404);
}
$membership // Update the roles
->setAttribute('roles', $roles)
;
$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);
}
Authorization::disable();
$team = $projectDB->updateDocument(\array_merge($team->getArrayCopy(), [
'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')
->setParam('resource', 'teams/'.$teamId)
;
if (!Config::getParam('domainVerification')) {
$response
->addHeader('X-Fallback-Cookies', \json_encode([Auth::$cookieName => Auth::encodeSession($user->getId(), $secret)]))
;
}
$response
->addCookie(Auth::$cookieName.'_legacy', Auth::encodeSession($user->getId(), $secret), $expiry, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, null)
->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);
});
App::get('/v1/teams/:teamId/memberships')
->desc('Get Team Memberships')
->groups(['api', 'teams'])

View file

@ -6,34 +6,34 @@ use Tests\E2E\Client;
trait TeamsBaseClient
{
/**
* @depends testCreateTeam
*/
public function testGetTeamMemberships($data):array
{
$teamUid = $data['teamUid'] ?? '';
// /**
// * @depends testCreateTeam
// */
// public function testGetTeamMemberships($data):array
// {
// $teamUid = $data['teamUid'] ?? '';
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
// /**
// * Test for SUCCESS
// */
// $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertIsInt($response['body']['sum']);
$this->assertNotEmpty($response['body']['memberships'][0]['$id']);
$this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['name']);
$this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['email']);
$this->assertEquals('owner', $response['body']['memberships'][0]['roles'][0]);
// $this->assertEquals(200, $response['headers']['status-code']);
// $this->assertIsInt($response['body']['sum']);
// $this->assertNotEmpty($response['body']['memberships'][0]['$id']);
// $this->assertEquals($this->getUser()['name'], $response['body']['memberships'][0]['name']);
// $this->assertEquals($this->getUser()['email'], $response['body']['memberships'][0]['email']);
// $this->assertEquals('owner', $response['body']['memberships'][0]['roles'][0]);
/**
* Test for FAILURE
*/
// /**
// * Test for FAILURE
// */
return $data;
}
// return $data;
// }
/**
* @depends testCreateTeam
@ -122,118 +122,158 @@ trait TeamsBaseClient
];
}
// /**
// * @depends testCreateTeamMembership
// */
// public function testUpdateTeamMembership($data):array
// {
// $teamUid = $data['teamUid'] ?? '';
// $secret = $data['secret'] ?? '';
// $membershipUid = $data['membershipUid'] ?? '';
// $userUid = $data['userUid'] ?? '';
// /**
// * Test for SUCCESS
// */
// $response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ]), [
// 'secret' => $secret,
// 'userId' => $userUid,
// ]);
// $this->assertEquals(200, $response['headers']['status-code']);
// $this->assertNotEmpty($response['body']['$id']);
// $this->assertNotEmpty($response['body']['userId']);
// $this->assertNotEmpty($response['body']['teamId']);
// $this->assertCount(2, $response['body']['roles']);
// $this->assertIsInt($response['body']['joined']);
// $this->assertEquals(true, $response['body']['confirm']);
// /**
// * Test for FAILURE
// */
// $response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ]), [
// 'secret' => 'sdasdasd',
// 'userId' => $userUid,
// ]);
// $this->assertEquals(401, $response['headers']['status-code']);
// $response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ]), [
// 'secret' => '',
// 'userId' => $userUid,
// ]);
// $this->assertEquals(400, $response['headers']['status-code']);
// $response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ]), [
// 'secret' => $secret,
// 'userId' => 'sdasd',
// ]);
// $this->assertEquals(401, $response['headers']['status-code']);
// $response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ]), [
// 'secret' => $secret,
// 'userId' => '',
// ]);
// $this->assertEquals(400, $response['headers']['status-code']);
// return $data;
// }
// /**
// * @depends testUpdateTeamMembership
// */
// public function testDeleteTeamMembership($data):array
// {
// $teamUid = $data['teamUid'] ?? '';
// $membershipUid = $data['membershipUid'] ?? '';
// /**
// * Test for SUCCESS
// */
// $response = $this->client->call(Client::METHOD_DELETE, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
// $this->assertEquals(204, $response['headers']['status-code']);
// $this->assertEmpty($response['body']);
// /**
// * Test for FAILURE
// */
// $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
// $this->assertEquals(200, $response['headers']['status-code']);
// $this->assertCount(1, $response['body']['memberships']);
// return [];
// }
/**
* @depends testCreateTeamMembership
*/
public function testUpdateTeamMembership($data):array
{
$teamUid = $data['teamUid'] ?? '';
$secret = $data['secret'] ?? '';
$membershipUid = $data['membershipUid'] ?? '';
$userUid = $data['userUid'] ?? '';
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => $secret,
'userId' => $userUid,
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertNotEmpty($response['body']['userId']);
$this->assertNotEmpty($response['body']['teamId']);
$this->assertCount(2, $response['body']['roles']);
$this->assertIsInt($response['body']['joined']);
$this->assertEquals(true, $response['body']['confirm']);
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => 'sdasdasd',
'userId' => $userUid,
]);
$this->assertEquals(401, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => '',
'userId' => $userUid,
]);
$this->assertEquals(400, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => $secret,
'userId' => 'sdasd',
]);
$this->assertEquals(401, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid.'/status', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'secret' => $secret,
'userId' => '',
]);
$this->assertEquals(400, $response['headers']['status-code']);
return $data;
}
/**
* @depends testUpdateTeamMembership
*/
public function testDeleteTeamMembership($data):array
public function testUpdateTeamMembershipRoles($data):array
{
$teamUid = $data['teamUid'] ?? '';
$membershipUid = $data['membershipUid'] ?? '';
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_DELETE, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
], $this->getHeaders(), [
]));
$this->assertEquals(204, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
var_dump($response);
var_dump($teamUid);
var_dump($membershipUid);
exit();
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
// /**
// * Test for FAILURE
// */
// $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamUid.'/memberships/'.$membershipUid, array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertCount(1, $response['body']['memberships']);
// $this->assertEquals(200, $response['headers']['status-code']);
// $this->assertCount(1, $response['body']['memberships']);
return [];
}
}