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-03-29 01:42:16 +13:00
use Utopia\Config\Config ;
2021-03-25 06:47:43 +13:00
use Appwrite\Network\Validator\Email ;
2019-05-09 18:54:39 +12:00
use Utopia\Validator\Text ;
2021-03-25 06:47:43 +13:00
use Appwrite\Network\Validator\Host ;
2019-05-09 18:54:39 +12:00
use Utopia\Validator\Range ;
use Utopia\Validator\ArrayList ;
use Utopia\Validator\WhiteList ;
2020-03-25 06:56:32 +13:00
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 ;
2020-09-10 20:29:59 +12:00
use Appwrite\Database\Validator\Key ;
2021-02-15 06:28:54 +13:00
use Appwrite\Detector\Detector ;
2020-03-25 06:56:32 +13:00
use Appwrite\Template\Template ;
2020-06-24 17:14:26 +12:00
use Appwrite\Utopia\Response ;
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/teams' )
2019-05-09 18:54:39 +12:00
-> desc ( 'Create Team' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-12-03 11:15:20 +13:00
-> label ( 'event' , 'teams.create' )
2019-05-09 18:54:39 +12:00
-> label ( 'scope' , 'teams.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-05-09 18:54:39 +12:00
-> label ( 'sdk.namespace' , 'teams' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'create' )
2019-10-08 20:09:35 +13:00
-> label ( 'sdk.description' , '/docs/references/teams/create-team.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TEAM )
2020-09-11 02:40:14 +12:00
-> param ( 'name' , null , new Text ( 128 ), 'Team name. Max length: 128 chars.' )
2020-09-12 00:00:41 +12:00
-> 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 )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'user' )
-> inject ( 'projectDB' )
2020-11-20 19:48:15 +13:00
-> action ( function ( $name , $roles , $response , $user , $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\Document $user */
/** @var Appwrite\Database\Database $projectDB */
Authorization :: disable ();
2021-03-02 10:04:53 +13:00
$isPrivilegedUser = Auth :: isPrivilegedUser ( Authorization :: $roles );
2020-11-20 19:48:15 +13:00
$isAppUser = Auth :: isAppUser ( Authorization :: $roles );
2020-06-30 23:09:28 +12:00
$team = $projectDB -> createDocument ([
'$collection' => Database :: SYSTEM_COLLECTION_TEAMS ,
'$permissions' => [
'read' => [ 'team:{self}' ],
'write' => [ 'team:{self}/owner' ],
],
'name' => $name ,
2021-03-02 10:04:53 +13:00
'sum' => ( $isPrivilegedUser || $isAppUser ) ? 0 : 1 ,
2020-06-30 23:09:28 +12:00
'dateCreated' => \time (),
]);
Authorization :: reset ();
if ( false === $team ) {
throw new Exception ( 'Failed saving team to DB' , 500 );
}
2019-05-09 18:54:39 +12:00
2021-03-02 10:04:53 +13:00
if ( ! $isPrivilegedUser && ! $isAppUser ) { // Don't add user on server mode
2020-06-30 23:09:28 +12:00
$membership = new Document ([
'$collection' => Database :: SYSTEM_COLLECTION_MEMBERSHIPS ,
2019-09-07 05:10:41 +12:00
'$permissions' => [
2020-06-30 23:09:28 +12:00
'read' => [ 'user:' . $user -> getId (), 'team:' . $team -> getId ()],
'write' => [ 'user:' . $user -> getId (), 'team:' . $team -> getId () . '/owner' ],
2019-05-09 18:54:39 +12:00
],
2020-06-30 23:09:28 +12:00
'userId' => $user -> getId (),
'teamId' => $team -> getId (),
'roles' => $roles ,
'invited' => \time (),
'joined' => \time (),
'confirm' => true ,
'secret' => '' ,
2019-05-09 18:54:39 +12:00
]);
2020-06-30 23:09:28 +12:00
// Attach user to team
$user -> setAttribute ( 'memberships' , $membership , Document :: SET_TYPE_APPEND );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB -> updateDocument ( $user -> getArrayCopy ());
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $user ) {
throw new Exception ( 'Failed saving user to DB' , 500 );
2019-05-09 18:54:39 +12:00
}
}
2020-06-30 23:09:28 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $team , Response :: MODEL_TEAM )
;
2020-12-27 05:48:43 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/teams' )
2020-02-01 11:34:07 +13:00
-> desc ( 'List Teams' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'teams.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'teams' )
-> label ( 'sdk.method' , 'list' )
-> label ( 'sdk.description' , '/docs/references/teams/list-teams.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TEAM_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
-> param ( 'limit' , 25 , 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 , 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 )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
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 ,
'orderType' => $orderType ,
'search' => $search ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_TEAMS ,
],
]);
2020-08-07 02:49:29 +12:00
$response -> dynamic ( new Document ([
'sum' => $projectDB -> getSum (),
'teams' => $results
]), Response :: MODEL_TEAM_LIST );
2020-12-27 05:48:43 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/teams/:teamId' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Get Team' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'teams.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'teams' )
-> label ( 'sdk.method' , 'get' )
-> label ( 'sdk.description' , '/docs/references/teams/get-team.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TEAM )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $teamId , $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 */
2020-02-01 11:34:07 +13:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> getDocument ( $teamId );
2020-02-01 11:34:07 +13:00
2020-06-30 23:09:28 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
2020-02-01 11:34:07 +13:00
}
2020-06-30 23:09:28 +12:00
2020-08-07 02:49:29 +12:00
$response -> dynamic ( $team , Response :: MODEL_TEAM );
2020-12-27 05:48:43 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App :: put ( '/v1/teams/:teamId' )
2019-05-09 18:54:39 +12:00
-> desc ( 'Update Team' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-12-03 11:15:20 +13:00
-> label ( 'event' , 'teams.update' )
2019-05-09 18:54:39 +12:00
-> label ( 'scope' , 'teams.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-05-09 18:54:39 +12:00
-> label ( 'sdk.namespace' , 'teams' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'update' )
2019-10-08 20:09:35 +13:00
-> label ( 'sdk.description' , '/docs/references/teams/update-team.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TEAM )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
-> param ( 'name' , null , new Text ( 128 ), 'Team name. Max length: 128 chars.' )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $teamId , $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 */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> getDocument ( $teamId );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> updateDocument ( \array_merge ( $team -> getArrayCopy (), [
'name' => $name ,
]));
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $team ) {
throw new Exception ( 'Failed saving team to DB' , 500 );
2019-05-09 18:54:39 +12:00
}
2020-08-07 02:49:29 +12:00
$response -> dynamic ( $team , Response :: MODEL_TEAM );
2020-12-27 05:48:43 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App :: delete ( '/v1/teams/:teamId' )
2019-05-09 18:54:39 +12:00
-> desc ( 'Delete Team' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-12-03 11:15:20 +13:00
-> label ( 'event' , 'teams.delete' )
2019-05-09 18:54:39 +12:00
-> label ( 'scope' , 'teams.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-05-09 18:54:39 +12:00
-> label ( 'sdk.namespace' , 'teams' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'delete' )
2019-10-08 20:09:35 +13:00
-> label ( 'sdk.description' , '/docs/references/teams/delete-team.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
-> inject ( 'events' )
2020-12-07 11:14:57 +13:00
-> action ( function ( $teamId , $response , $projectDB , $events ) {
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 */
2020-12-07 11:14:57 +13:00
/** @var Appwrite\Event\Event $events */
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> getDocument ( $teamId );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$memberships = $projectDB -> getCollection ([
'limit' => 2000 , // TODO add members limit
'offset' => 0 ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_MEMBERSHIPS ,
'teamId=' . $teamId ,
],
]);
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
foreach ( $memberships as $member ) {
if ( ! $projectDB -> deleteDocument ( $member -> getId ())) {
throw new Exception ( 'Failed to remove membership for team from DB' , 500 );
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( ! $projectDB -> deleteDocument ( $teamId )) {
throw new Exception ( 'Failed to remove team from DB' , 500 );
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
2020-12-07 11:14:57 +13:00
$events
2021-03-30 07:00:10 +13:00
-> setParam ( 'eventData' , $response -> output ( $team , Response :: MODEL_TEAM ))
2020-12-03 11:15:20 +13:00
;
2021-03-19 09:32:16 +13:00
$response -> dynamic ( new Document (), Response :: MODEL_NONE );
2020-12-27 05:48:43 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App :: post ( '/v1/teams/:teamId/memberships' )
2019-05-09 18:54:39 +12:00
-> desc ( 'Create Team Membership' )
2021-03-01 07:36:13 +13:00
-> groups ([ 'api' , 'teams' , 'auth' ])
2020-12-03 11:15:20 +13:00
-> label ( 'event' , 'teams.memberships.create' )
2020-02-10 05:53:33 +13:00
-> label ( 'scope' , 'teams.write' )
2021-03-01 07:36:13 +13:00
-> label ( 'auth.type' , 'invites' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-05-09 18:54:39 +12:00
-> label ( 'sdk.namespace' , 'teams' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'createMembership' )
2019-10-08 20:09:35 +13:00
-> label ( 'sdk.description' , '/docs/references/teams/create-team-membership.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MEMBERSHIP )
2021-04-13 21:38:40 +12:00
-> label ( 'abuse-limit' , 10 )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
-> param ( 'email' , '' , new Email (), 'New team member email.' )
-> param ( 'name' , '' , new Text ( 128 ), 'New team member name. Max length: 128 chars.' , true )
2020-09-12 00:00:41 +12:00
-> 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.' )
2020-06-30 23:09:28 +12:00
-> param ( 'url' , '' , function ( $clients ) { return new Host ( $clients ); }, 'URL to redirect the user back to your app from the invitation email. Only URLs from hostnames in your project platform list are allowed. This requirement helps to prevent an [open redirect](https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html) attack against your project API.' , false , [ 'clients' ]) // TODO add our own built-in confirm page
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'project' )
-> inject ( 'user' )
-> inject ( 'projectDB' )
-> inject ( 'locale' )
-> inject ( 'audits' )
-> inject ( 'mails' )
2020-11-20 19:48:15 +13:00
-> action ( function ( $teamId , $email , $name , $roles , $url , $response , $project , $user , $projectDB , $locale , $audits , $mails ) {
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Document $user */
/** @var Appwrite\Database\Database $projectDB */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Event\Event $mails */
2020-11-20 19:48:15 +13:00
2021-03-02 10:04:53 +13:00
$isPrivilegedUser = Auth :: isPrivilegedUser ( Authorization :: $roles );
2020-11-20 19:48:15 +13:00
$isAppUser = Auth :: isAppUser ( Authorization :: $roles );
2021-06-04 07:05:11 +12:00
$email = \strtolower ( $email );
2020-06-30 23:09:28 +12:00
$name = ( empty ( $name )) ? $email : $name ;
$team = $projectDB -> getDocument ( $teamId );
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$memberships = $projectDB -> getCollection ([
2021-05-13 05:15:55 +12:00
'limit' => 2000 ,
2020-06-30 23:09:28 +12:00
'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 ,
],
]);
if ( empty ( $invitee )) { // Create new user if no user with same email found
2019-05-09 18:54:39 +12:00
2021-03-01 07:36:13 +13:00
$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 ();
if ( $sum >= $limit ) {
throw new Exception ( 'Project registration is restricted. Contact your administrator for more information.' , 501 );
}
}
2020-06-30 23:09:28 +12:00
Authorization :: disable ();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
try {
$invitee = $projectDB -> createDocument ([
'$collection' => Database :: SYSTEM_COLLECTION_USERS ,
'$permissions' => [
'read' => [ 'user:{self}' , '*' ],
'write' => [ 'user:{self}' ],
],
'email' => $email ,
'emailVerification' => false ,
'status' => Auth :: USER_STATUS_UNACTIVATED ,
'password' => Auth :: passwordHash ( Auth :: passwordGenerator ()),
2021-05-05 06:52:46 +12:00
/**
2021-05-07 04:30:44 +12:00
* Set the password update time to 0 for users created using
2021-05-07 04:30:57 +12:00
* team invite and OAuth to allow password updates without an
2021-05-07 04:30:44 +12:00
* old password
2021-05-05 06:52:46 +12:00
*/
'passwordUpdate' => 0 ,
2020-06-30 23:09:28 +12:00
'registration' => \time (),
'reset' => false ,
'name' => $name ,
2021-02-20 02:59:36 +13:00
'sessions' => [],
2020-06-30 23:09:28 +12:00
'tokens' => [],
], [ 'email' => $email ]);
} catch ( Duplicate $th ) {
throw new Exception ( 'Account already exists' , 409 );
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
Authorization :: reset ();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $invitee ) {
throw new Exception ( 'Failed saving user to DB' , 500 );
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$isOwner = false ;
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
foreach ( $memberships as $member ) {
if ( $member -> getAttribute ( 'userId' ) == $invitee -> getId ()) {
throw new Exception ( 'User has already been invited or is already a member of this team' , 409 );
2020-06-09 04:15:35 +12:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( $member -> getAttribute ( 'userId' ) == $user -> getId () && \in_array ( 'owner' , $member -> getAttribute ( 'roles' , []))) {
$isOwner = true ;
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
}
2019-05-09 18:54:39 +12:00
2021-03-02 10:04:53 +13:00
if ( ! $isOwner && ! $isPrivilegedUser && ! $isAppUser ) { // Not owner, not admin, not app (server)
2020-06-30 23:09:28 +12:00
throw new Exception ( 'User is not allowed to send invitations for this team' , 401 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$secret = Auth :: tokenGenerator ();
$membership = new Document ([
'$collection' => Database :: SYSTEM_COLLECTION_MEMBERSHIPS ,
'$permissions' => [
'read' => [ '*' ],
'write' => [ 'user:' . $invitee -> getId (), 'team:' . $team -> getId () . '/owner' ],
],
'userId' => $invitee -> getId (),
'teamId' => $team -> getId (),
'roles' => $roles ,
'invited' => \time (),
2021-03-02 10:04:53 +13:00
'joined' => ( $isPrivilegedUser || $isAppUser ) ? \time () : 0 ,
'confirm' => ( $isPrivilegedUser || $isAppUser ),
2020-06-30 23:09:28 +12:00
'secret' => Auth :: hash ( $secret ),
]);
2021-03-02 10:04:53 +13:00
if ( $isPrivilegedUser || $isAppUser ) { // Allow admin to create membership
2020-06-30 23:09:28 +12:00
Authorization :: disable ();
$membership = $projectDB -> createDocument ( $membership -> getArrayCopy ());
2019-05-09 18:54:39 +12:00
2020-07-09 23:15:42 +12:00
$team = $projectDB -> updateDocument ( \array_merge ( $team -> getArrayCopy (), [
'sum' => $team -> getAttribute ( 'sum' , 0 ) + 1 ,
]));
2019-05-09 18:54:39 +12:00
2020-07-10 23:54:00 +12:00
// Attach user to team
$invitee -> setAttribute ( 'memberships' , $membership , Document :: SET_TYPE_APPEND );
2020-07-10 21:06:30 +12:00
2020-07-10 23:54:00 +12:00
$invitee = $projectDB -> updateDocument ( $invitee -> getArrayCopy ());
2019-05-09 18:54:39 +12:00
2020-07-10 23:54:00 +12:00
if ( false === $invitee ) {
throw new Exception ( 'Failed saving user to DB' , 500 );
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
Authorization :: reset ();
} else {
$membership = $projectDB -> createDocument ( $membership -> getArrayCopy ());
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $membership ) {
throw new Exception ( 'Failed saving membership to DB' , 500 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$url = Template :: parseURL ( $url );
2021-05-08 03:49:23 +12:00
$url [ 'query' ] = Template :: mergeQuery ((( isset ( $url [ 'query' ])) ? $url [ 'query' ] : '' ), [ 'membershipId' => $membership -> getId (), 'teamId' => $team -> getId (), 'userId' => $invitee -> getId (), 'secret' => $secret , 'teamId' => $teamId ]);
2020-06-30 23:09:28 +12:00
$url = Template :: unParseURL ( $url );
$body = new Template ( __DIR__ . '/../../config/locale/templates/email-base.tpl' );
$content = new Template ( __DIR__ . '/../../config/locale/translations/templates/' . $locale -> getText ( 'account.emails.invitation.body' ));
$cta = new Template ( __DIR__ . '/../../config/locale/templates/email-cta.tpl' );
2021-02-09 22:16:39 +13:00
$title = \sprintf ( $locale -> getText ( 'account.emails.invitation.title' ), $team -> getAttribute ( 'name' , '[TEAM-NAME]' ), $project -> getAttribute ( 'name' , [ '[APP-NAME]' ]));
2020-06-30 23:09:28 +12:00
$body
-> setParam ( '{{content}}' , $content -> render ())
-> setParam ( '{{cta}}' , $cta -> render ())
2021-02-09 22:16:39 +13:00
-> setParam ( '{{title}}' , $title )
2020-06-30 23:09:28 +12:00
-> setParam ( '{{direction}}' , $locale -> getText ( 'settings.direction' ))
-> setParam ( '{{project}}' , $project -> getAttribute ( 'name' , [ '[APP-NAME]' ]))
-> setParam ( '{{team}}' , $team -> getAttribute ( 'name' , '[TEAM-NAME]' ))
-> setParam ( '{{owner}}' , $user -> getAttribute ( 'name' , '' ))
-> setParam ( '{{redirect}}' , $url )
-> setParam ( '{{bg-body}}' , '#f6f6f6' )
-> setParam ( '{{bg-content}}' , '#ffffff' )
-> setParam ( '{{bg-cta}}' , '#3498db' )
-> setParam ( '{{bg-cta-hover}}' , '#34495e' )
-> setParam ( '{{text-content}}' , '#000000' )
-> setParam ( '{{text-cta}}' , '#ffffff' )
;
2021-05-13 01:44:41 +12:00
if ( ! $isPrivilegedUser && ! $isAppUser ) { // No need of confirmation when in admin or app mode
2020-07-06 02:19:59 +12:00
$mails
2021-05-13 05:20:10 +12:00
-> setParam ( 'event' , 'teams.memberships.create' )
2020-07-06 09:54:41 +12:00
-> setParam ( 'from' , ( $project -> getId () === 'console' ) ? '' : \sprintf ( $locale -> getText ( 'account.emails.team' ), $project -> getAttribute ( 'name' )))
2020-06-30 23:09:28 +12:00
-> setParam ( 'recipient' , $email )
-> setParam ( 'name' , $name )
2021-02-09 22:16:39 +13:00
-> setParam ( 'subject' , $title )
2020-06-30 23:09:28 +12:00
-> setParam ( 'body' , $body -> render ())
2021-02-09 22:25:09 +13:00
-> trigger ()
2019-05-09 18:54:39 +12:00
;
}
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 23:09:28 +12:00
-> setParam ( 'userId' , $invitee -> getId ())
2021-05-13 05:20:10 +12:00
-> setParam ( 'event' , 'teams.memberships.create' )
2020-06-30 23:09:28 +12:00
-> setParam ( 'resource' , 'teams/' . $teamId )
;
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( new Document ( \array_merge ( $membership -> getArrayCopy (), [
'email' => $email ,
'name' => $name ,
])), Response :: MODEL_MEMBERSHIP )
;
2020-12-27 05:48:43 +13:00
});
2019-05-09 18:54:39 +12:00
2021-05-13 01:44:41 +12:00
App :: patch ( '/v1/teams/:teamId/memberships/:membershipId' )
-> desc ( 'Update Membership Roles' )
-> groups ([ 'api' , 'teams' ])
-> label ( 'event' , 'teams.memberships.update' )
2021-05-14 02:47:35 +12:00
-> label ( 'scope' , 'teams.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2021-05-13 01:44:41 +12:00
-> 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 */
2021-05-13 02:47:56 +12:00
$team = $projectDB -> getDocument ( $teamId );
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2021-05-13 01:44:41 +12:00
$membership = $projectDB -> getDocument ( $membershipId );
if ( empty ( $membership -> getId ()) || Database :: SYSTEM_COLLECTION_MEMBERSHIPS != $membership -> getCollection ()) {
throw new Exception ( 'Membership not found' , 404 );
}
2021-05-13 05:00:22 +12:00
$isPrivilegedUser = Auth :: isPrivilegedUser ( Authorization :: $roles );
$isAppUser = Auth :: isAppUser ( Authorization :: $roles );
2021-05-14 02:01:52 +12:00
$isOwner = Authorization :: isRole ( 'team:' . $team -> getId () . '/owner' );;
2021-05-13 05:00:22 +12:00
if ( ! $isOwner && ! $isPrivilegedUser && ! $isAppUser ) { // Not owner, not admin, not app (server)
throw new Exception ( 'User is not allowed to modify roles' , 401 );
}
2021-05-13 01:44:41 +12:00
2021-05-13 05:00:22 +12:00
// Update the roles
$membership -> setAttribute ( 'roles' , $roles );
2021-05-13 02:47:56 +12:00
$membership = $projectDB -> updateDocument ( $membership -> getArrayCopy ());
2021-05-13 01:44:41 +12:00
2021-05-13 02:47:56 +12:00
if ( false === $membership ) {
throw new Exception ( 'Failed updating membership' , 500 );
2021-05-13 01:44:41 +12:00
}
$audits
-> setParam ( 'userId' , $user -> getId ())
2021-05-13 05:00:22 +12:00
-> setParam ( 'event' , 'teams.memberships.update' )
2021-05-13 01:44:41 +12:00
-> setParam ( 'resource' , 'teams/' . $teamId )
;
2021-05-13 02:47:56 +12:00
$response -> dynamic ( new Document ( $membership -> getArrayCopy ()), Response :: MODEL_MEMBERSHIP );
2021-05-13 01:44:41 +12:00
});
2020-06-29 05:31:21 +12:00
App :: get ( '/v1/teams/:teamId/memberships' )
2020-02-01 11:34:07 +13:00
-> desc ( 'Get Team Memberships' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-02-01 11:34:07 +13:00
-> label ( 'scope' , 'teams.read' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2020-02-01 11:34:07 +13:00
-> label ( 'sdk.namespace' , 'teams' )
-> label ( 'sdk.method' , 'getMemberships' )
-> label ( 'sdk.description' , '/docs/references/teams/get-team-members.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MEMBERSHIP_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
-> param ( 'limit' , 25 , 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 , 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 )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-06-30 23:09:28 +12:00
-> action ( function ( $teamId , $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 */
2020-02-01 11:34:07 +13:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> getDocument ( $teamId );
2020-02-01 11:34:07 +13:00
2020-06-30 23:09:28 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2020-02-01 11:34:07 +13:00
2020-06-30 23:09:28 +12:00
$memberships = $projectDB -> getCollection ([
'limit' => $limit ,
'offset' => $offset ,
'orderType' => $orderType ,
'search' => $search ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_MEMBERSHIPS ,
'teamId=' . $teamId ,
],
]);
$users = [];
foreach ( $memberships as $membership ) {
if ( empty ( $membership -> getAttribute ( 'userId' , null ))) {
continue ;
}
$temp = $projectDB -> getDocument ( $membership -> getAttribute ( 'userId' , null )) -> getArrayCopy ([ 'email' , 'name' ]);
2020-08-07 02:49:29 +12:00
$users [] = new Document ( \array_merge ( $temp , $membership -> getArrayCopy ()));
2020-02-01 11:34:07 +13:00
}
2020-06-30 23:09:28 +12:00
2020-08-07 02:49:29 +12:00
$response -> dynamic ( new Document ([ 'sum' => $projectDB -> getSum (), 'memberships' => $users ]), Response :: MODEL_MEMBERSHIP_LIST );
2020-12-27 05:48:43 +13:00
});
2020-02-01 11:34:07 +13:00
2021-05-08 03:49:23 +12:00
App :: patch ( '/v1/teams/:teamId/memberships/:membershipId/status' )
2019-05-09 18:54:39 +12:00
-> desc ( 'Update Team Membership Status' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-12-03 11:15:20 +13:00
-> label ( 'event' , 'teams.memberships.update.status' )
2020-01-20 01:22:54 +13:00
-> label ( 'scope' , 'public' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_JWT ])
2019-05-09 18:54:39 +12:00
-> label ( 'sdk.namespace' , 'teams' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'updateMembershipStatus' )
2019-10-08 20:09:35 +13:00
-> label ( 'sdk.description' , '/docs/references/teams/update-team-membership-status.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MEMBERSHIP )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
2021-05-08 03:49:23 +12:00
-> param ( 'membershipId' , '' , new UID (), 'Membership ID.' )
2020-09-11 02:40:14 +12:00
-> param ( 'userId' , '' , new UID (), 'User unique ID.' )
-> param ( 'secret' , '' , new Text ( 256 ), 'Secret key.' )
2020-12-27 05:48:43 +13:00
-> inject ( 'request' )
-> inject ( 'response' )
-> inject ( 'user' )
-> inject ( 'projectDB' )
-> inject ( 'geodb' )
-> inject ( 'audits' )
2021-05-08 03:49:23 +12:00
-> action ( function ( $teamId , $membershipId , $userId , $secret , $request , $response , $user , $projectDB , $geodb , $audits ) {
2020-08-15 23:39:44 +12:00
/** @var Utopia\Swoole\Request $request */
2020-08-15 09:56:33 +12:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 23:09:28 +12:00
/** @var Appwrite\Database\Document $user */
/** @var Appwrite\Database\Database $projectDB */
2020-10-27 14:03:54 +13:00
/** @var MaxMind\Db\Reader $geodb */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2020-06-30 23:09:28 +12:00
$protocol = $request -> getProtocol ();
2021-05-08 03:49:23 +12:00
$membership = $projectDB -> getDocument ( $membershipId );
2020-06-30 23:09:28 +12:00
if ( empty ( $membership -> getId ()) || Database :: SYSTEM_COLLECTION_MEMBERSHIPS != $membership -> getCollection ()) {
throw new Exception ( 'Invite not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( $membership -> getAttribute ( 'teamId' ) !== $teamId ) {
throw new Exception ( 'Team IDs don\'t match' , 404 );
}
2020-01-20 01:22:54 +13:00
2020-06-30 23:09:28 +12:00
Authorization :: disable ();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> getDocument ( $teamId );
Authorization :: reset ();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( Auth :: hash ( $secret ) !== $membership -> getAttribute ( 'secret' )) {
throw new Exception ( 'Secret key not valid' , 401 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( $userId != $membership -> getAttribute ( 'userId' )) {
2021-05-05 06:52:46 +12:00
throw new Exception ( 'Invite does not belong to current user (' . $user -> getAttribute ( 'email' ) . ')' , 401 );
2020-06-30 23:09:28 +12:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( empty ( $user -> getId ())) {
$user = $projectDB -> getCollectionFirst ([ // Get user
'limit' => 1 ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_USERS ,
'$id=' . $userId ,
],
]);
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( $membership -> getAttribute ( 'userId' ) !== $user -> getId ()) {
2021-05-05 06:52:46 +12:00
throw new Exception ( 'Invite does not belong to current user (' . $user -> getAttribute ( 'email' ) . ')' , 401 );
2020-06-30 23:09:28 +12:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$membership // Attach user to team
-> setAttribute ( 'joined' , \time ())
-> setAttribute ( 'confirm' , true )
;
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user
-> setAttribute ( 'emailVerification' , true )
-> setAttribute ( 'memberships' , $membership , Document :: SET_TYPE_APPEND )
;
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
// Log user in
2020-08-12 02:28:51 +12:00
2021-02-15 06:28:54 +13:00
$detector = new Detector ( $request -> getUserAgent ( 'UNKNOWN' ));
$record = $geodb -> get ( $request -> getIP ());
2020-06-30 23:09:28 +12:00
$expiry = \time () + Auth :: TOKEN_EXPIRATION_LOGIN_LONG ;
$secret = Auth :: tokenGenerator ();
2021-02-15 06:28:54 +13:00
$session = new Document ( array_merge ([
2021-02-20 01:12:47 +13:00
'$collection' => Database :: SYSTEM_COLLECTION_SESSIONS ,
2020-06-30 23:09:28 +12:00
'$permissions' => [ 'read' => [ 'user:' . $user -> getId ()], 'write' => [ 'user:' . $user -> getId ()]],
2020-11-26 19:12:24 +13:00
'userId' => $user -> getId (),
2021-02-20 01:12:47 +13:00
'provider' => Auth :: SESSION_PROVIDER_EMAIL ,
2021-02-19 23:02:02 +13:00
'providerUid' => $user -> getAttribute ( 'email' ),
2020-11-13 00:54:16 +13:00
'secret' => Auth :: hash ( $secret ), // One way hash encryption to protect DB leak
2020-06-30 23:09:28 +12:00
'expire' => $expiry ,
2020-07-04 03:14:51 +12:00
'userAgent' => $request -> getUserAgent ( 'UNKNOWN' ),
2020-06-30 23:09:28 +12:00
'ip' => $request -> getIP (),
2021-02-15 06:28:54 +13:00
'countryCode' => ( $record ) ? \strtolower ( $record [ 'country' ][ 'iso_code' ]) : '--' ,
], $detector -> getOS (), $detector -> getClient (), $detector -> getDevice ()));
2020-08-12 02:28:51 +12:00
2021-02-20 01:12:47 +13:00
$user -> setAttribute ( 'sessions' , $session , Document :: SET_TYPE_APPEND );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
Authorization :: setRole ( 'user:' . $userId );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$user = $projectDB -> updateDocument ( $user -> getArrayCopy ());
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $user ) {
throw new Exception ( 'Failed saving user to DB' , 500 );
}
2020-01-20 01:22:54 +13:00
2020-06-30 23:09:28 +12:00
Authorization :: disable ();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> updateDocument ( \array_merge ( $team -> getArrayCopy (), [
'sum' => $team -> getAttribute ( 'sum' , 0 ) + 1 ,
]));
2020-01-20 09:02:50 +13:00
2020-06-30 23:09:28 +12:00
Authorization :: reset ();
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $team ) {
throw new Exception ( 'Failed saving team to DB' , 500 );
}
2019-05-09 18:54:39 +12:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 23:09:28 +12:00
-> setParam ( 'userId' , $user -> getId ())
2021-05-13 05:20:10 +12:00
-> setParam ( 'event' , 'teams.memberships.update.status' )
2020-06-30 23:09:28 +12:00
-> setParam ( 'resource' , 'teams/' . $teamId )
;
2020-03-18 00:36:13 +13:00
2020-06-30 23:09:28 +12:00
if ( ! Config :: getParam ( 'domainVerification' )) {
2020-01-15 09:50:49 +13:00
$response
2020-06-30 23:09:28 +12:00
-> addHeader ( 'X-Fallback-Cookies' , \json_encode ([ Auth :: $cookieName => Auth :: encodeSession ( $user -> getId (), $secret )]))
2020-01-15 09:50:49 +13:00
;
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
$response
2020-07-01 18:35:57 +12:00
-> 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' ))
2020-06-30 23:09:28 +12:00
;
2020-07-03 09:48:02 +12:00
$response -> dynamic ( new Document ( \array_merge ( $membership -> getArrayCopy (), [
'email' => $user -> getAttribute ( 'email' ),
'name' => $user -> getAttribute ( 'name' ),
])), Response :: MODEL_MEMBERSHIP );
2020-12-27 05:48:43 +13:00
});
2019-05-09 18:54:39 +12:00
2021-05-08 03:49:23 +12:00
App :: delete ( '/v1/teams/:teamId/memberships/:membershipId' )
2019-05-09 18:54:39 +12:00
-> desc ( 'Delete Team Membership' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'teams' ])
2020-12-03 11:15:20 +13:00
-> label ( 'event' , 'teams.memberships.delete' )
2020-02-10 05:53:33 +13:00
-> label ( 'scope' , 'teams.write' )
2021-04-16 19:22:17 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_KEY , APP_AUTH_TYPE_JWT ])
2019-05-09 18:54:39 +12:00
-> label ( 'sdk.namespace' , 'teams' )
2020-01-31 05:18:46 +13:00
-> label ( 'sdk.method' , 'deleteMembership' )
2019-10-08 20:09:35 +13:00
-> label ( 'sdk.description' , '/docs/references/teams/delete-team-membership.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'teamId' , '' , new UID (), 'Team unique ID.' )
2021-05-08 03:49:23 +12:00
-> param ( 'membershipId' , '' , new UID (), 'Membership ID.' )
2020-12-27 05:48:43 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
-> inject ( 'audits' )
-> inject ( 'events' )
2021-05-08 03:49:23 +12:00
-> action ( function ( $teamId , $membershipId , $response , $projectDB , $audits , $events ) {
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 */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2020-12-07 11:14:57 +13:00
/** @var Appwrite\Event\Event $events */
2019-05-09 18:54:39 +12:00
2021-05-08 03:49:23 +12:00
$membership = $projectDB -> getDocument ( $membershipId );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( empty ( $membership -> getId ()) || Database :: SYSTEM_COLLECTION_MEMBERSHIPS != $membership -> getCollection ()) {
throw new Exception ( 'Invite not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( $membership -> getAttribute ( 'teamId' ) !== $teamId ) {
throw new Exception ( 'Team IDs don\'t match' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
$team = $projectDB -> getDocument ( $teamId );
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( empty ( $team -> getId ()) || Database :: SYSTEM_COLLECTION_TEAMS != $team -> getCollection ()) {
throw new Exception ( 'Team not found' , 404 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( ! $projectDB -> deleteDocument ( $membership -> getId ())) {
throw new Exception ( 'Failed to remove membership from DB' , 500 );
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( $membership -> getAttribute ( 'confirm' )) { // Count only confirmed members
$team = $projectDB -> updateDocument ( \array_merge ( $team -> getArrayCopy (), [
2021-05-31 16:56:06 +12:00
'sum' => \max ( $team -> getAttribute ( 'sum' , 0 ) - 1 , 0 ), // Ensure that sum >= 0
2020-06-30 23:09:28 +12:00
]));
}
2019-05-09 18:54:39 +12:00
2020-06-30 23:09:28 +12:00
if ( false === $team ) {
throw new Exception ( 'Failed saving team to DB' , 500 );
2019-05-09 18:54:39 +12:00
}
2020-06-30 23:09:28 +12:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 23:09:28 +12:00
-> setParam ( 'userId' , $membership -> getAttribute ( 'userId' ))
2021-05-13 05:20:10 +12:00
-> setParam ( 'event' , 'teams.memberships.delete' )
2020-06-30 23:09:28 +12:00
-> setParam ( 'resource' , 'teams/' . $teamId )
;
2020-12-07 11:14:57 +13:00
$events
2021-03-30 07:00:10 +13:00
-> setParam ( 'eventData' , $response -> output ( $membership , Response :: MODEL_MEMBERSHIP ))
2020-12-03 11:15:20 +13:00
;
2021-03-19 09:32:16 +13:00
$response -> dynamic ( new Document (), Response :: MODEL_NONE );
2020-12-27 05:48:43 +13:00
});