Merge remote-tracking branch 'origin/1.3.x' into feat-password-history
This commit is contained in:
commit
1b9ee249be
3 changed files with 148 additions and 47 deletions
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Appwrite\Auth\Auth;
|
use Appwrite\Auth\Auth;
|
||||||
|
use Appwrite\Auth\Validator\Phone;
|
||||||
use Appwrite\Detector\Detector;
|
use Appwrite\Detector\Detector;
|
||||||
use Appwrite\Event\Delete;
|
use Appwrite\Event\Delete;
|
||||||
use Appwrite\Event\Event;
|
use Appwrite\Event\Event;
|
||||||
|
@ -39,6 +40,7 @@ use Utopia\Validator\Text;
|
||||||
use Utopia\Validator\Range;
|
use Utopia\Validator\Range;
|
||||||
use Utopia\Validator\ArrayList;
|
use Utopia\Validator\ArrayList;
|
||||||
use Utopia\Validator\WhiteList;
|
use Utopia\Validator\WhiteList;
|
||||||
|
use Appwrite\Event\Phone as EventPhone;
|
||||||
|
|
||||||
App::post('/v1/teams')
|
App::post('/v1/teams')
|
||||||
->desc('Create Team')
|
->desc('Create Team')
|
||||||
|
@ -304,7 +306,9 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
->label('sdk.response.model', Response::MODEL_MEMBERSHIP)
|
->label('sdk.response.model', Response::MODEL_MEMBERSHIP)
|
||||||
->label('abuse-limit', 10)
|
->label('abuse-limit', 10)
|
||||||
->param('teamId', '', new UID(), 'Team ID.')
|
->param('teamId', '', new UID(), 'Team ID.')
|
||||||
->param('email', '', new Email(), 'Email of the new team member.')
|
->param('email', '', new Email(), 'Email of the new team member.', true)
|
||||||
|
->param('userId', '', new UID(), 'ID of the user to be added to a team.', true)
|
||||||
|
->param('phone', '', new Phone(), 'Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.', true)
|
||||||
->param('roles', [], new ArrayList(new Key(), APP_LIMIT_ARRAY_PARAMS_SIZE), '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). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' roles are allowed, each 32 characters long.')
|
->param('roles', [], new ArrayList(new Key(), APP_LIMIT_ARRAY_PARAMS_SIZE), '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). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' roles are allowed, each 32 characters long.')
|
||||||
->param('url', '', fn($clients) => 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
|
->param('url', '', fn($clients) => 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
|
||||||
->param('name', '', new Text(128), 'Name of the new team member. Max length: 128 chars.', true)
|
->param('name', '', new Text(128), 'Name of the new team member. Max length: 128 chars.', true)
|
||||||
|
@ -314,9 +318,13 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
->inject('dbForProject')
|
->inject('dbForProject')
|
||||||
->inject('locale')
|
->inject('locale')
|
||||||
->inject('mails')
|
->inject('mails')
|
||||||
|
->inject('messaging')
|
||||||
->inject('events')
|
->inject('events')
|
||||||
->action(function (string $teamId, string $email, array $roles, string $url, string $name, Response $response, Document $project, Document $user, Database $dbForProject, Locale $locale, Mail $mails, Event $events) {
|
->action(function (string $teamId, string $email, string $userId, string $phone, array $roles, string $url, string $name, Response $response, Document $project, Document $user, Database $dbForProject, Locale $locale, Mail $mails, EventPhone $messaging, Event $events) {
|
||||||
|
|
||||||
|
if (empty($userId) && empty($email) && empty($phone)) {
|
||||||
|
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'At least one of userId, email, or phone is required');
|
||||||
|
}
|
||||||
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
|
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
|
||||||
$isAppUser = Auth::isAppUser(Authorization::getRoles());
|
$isAppUser = Auth::isAppUser(Authorization::getRoles());
|
||||||
|
|
||||||
|
@ -331,8 +339,31 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
if ($team->isEmpty()) {
|
if ($team->isEmpty()) {
|
||||||
throw new Exception(Exception::TEAM_NOT_FOUND);
|
throw new Exception(Exception::TEAM_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
if (!empty($userId)) {
|
||||||
|
$invitee = $dbForProject->getDocument('users', $userId);
|
||||||
|
if ($invitee->isEmpty()) {
|
||||||
|
throw new Exception(Exception::USER_NOT_FOUND, 'User with given userId doesn\'t exist.', 404);
|
||||||
|
}
|
||||||
|
if (!empty($email) && $invitee->getAttribute('email', '') != $email) {
|
||||||
|
throw new Exception(Exception::USER_ALREADY_EXISTS, 'Given userId and email doesn\'t match', 409);
|
||||||
|
}
|
||||||
|
if (!empty($phone) && $invitee->getAttribute('phone', '') != $phone) {
|
||||||
|
throw new Exception(Exception::USER_ALREADY_EXISTS, 'Given userId and phone doesn\'t match', 409);
|
||||||
|
}
|
||||||
|
$email = $invitee->getAttribute('email', '');
|
||||||
|
$phone = $invitee->getAttribute('phone', '');
|
||||||
|
$name = empty($name) ? $invitee->getAttribute('name', '') : $name;
|
||||||
|
} elseif (!empty($email)) {
|
||||||
$invitee = $dbForProject->findOne('users', [Query::equal('email', [$email])]); // Get user by email address
|
$invitee = $dbForProject->findOne('users', [Query::equal('email', [$email])]); // Get user by email address
|
||||||
|
if (!empty($invitee) && !empty($phone) && $invitee->getAttribute('phone', '') != $phone) {
|
||||||
|
throw new Exception(Exception::USER_ALREADY_EXISTS, 'Given email and phone doesn\'t match', 409);
|
||||||
|
}
|
||||||
|
} elseif (!empty($phone)) {
|
||||||
|
$invitee = $dbForProject->findOne('users', [Query::equal('phone', [$phone])]);
|
||||||
|
if (!empty($invitee) && !empty($email) && $invitee->getAttribute('email', '') != $email) {
|
||||||
|
throw new Exception(Exception::USER_ALREADY_EXISTS, 'Given phone and email doesn\'t match', 409);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($invitee)) { // Create new user if no user with same email found
|
if (empty($invitee)) { // Create new user if no user with same email found
|
||||||
$limit = $project->getAttribute('auths', [])['limit'] ?? 0;
|
$limit = $project->getAttribute('auths', [])['limit'] ?? 0;
|
||||||
|
@ -355,7 +386,8 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
Permission::update(Role::user($userId)),
|
Permission::update(Role::user($userId)),
|
||||||
Permission::delete(Role::user($userId)),
|
Permission::delete(Role::user($userId)),
|
||||||
],
|
],
|
||||||
'email' => $email,
|
'email' => empty($email) ? null : $email,
|
||||||
|
'phone' => empty($phone) ? null : $phone,
|
||||||
'emailVerification' => false,
|
'emailVerification' => false,
|
||||||
'status' => true,
|
'status' => true,
|
||||||
'password' => Auth::passwordHash(Auth::passwordGenerator(), Auth::DEFAULT_ALGO, Auth::DEFAULT_ALGO_OPTIONS),
|
'password' => Auth::passwordHash(Auth::passwordGenerator(), Auth::DEFAULT_ALGO, Auth::DEFAULT_ALGO_OPTIONS),
|
||||||
|
@ -427,13 +459,11 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
} catch (Duplicate $th) {
|
} catch (Duplicate $th) {
|
||||||
throw new Exception(Exception::TEAM_INVITE_ALREADY_EXISTS);
|
throw new Exception(Exception::TEAM_INVITE_ALREADY_EXISTS);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$url = Template::parseURL($url);
|
$url = Template::parseURL($url);
|
||||||
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['membershipId' => $membership->getId(), 'userId' => $invitee->getId(), 'secret' => $secret, 'teamId' => $teamId]);
|
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['membershipId' => $membership->getId(), 'userId' => $invitee->getId(), 'secret' => $secret, 'teamId' => $teamId]);
|
||||||
$url = Template::unParseURL($url);
|
$url = Template::unParseURL($url);
|
||||||
|
if (!empty($email)) {
|
||||||
if (!$isPrivilegedUser && !$isAppUser) { // No need of confirmation when in admin or app mode
|
|
||||||
$projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]');
|
$projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]');
|
||||||
|
|
||||||
$from = $project->isEmpty() || $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName);
|
$from = $project->isEmpty() || $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName);
|
||||||
|
@ -467,6 +497,12 @@ App::post('/v1/teams/:teamId/memberships')
|
||||||
->setName($invitee->getAttribute('name'))
|
->setName($invitee->getAttribute('name'))
|
||||||
->trigger()
|
->trigger()
|
||||||
;
|
;
|
||||||
|
} elseif (!empty($phone)) {
|
||||||
|
$messaging
|
||||||
|
->setRecipient($phone)
|
||||||
|
->setMessage($url)
|
||||||
|
->trigger();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$events
|
$events
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
Invite a new member to join your team. If initiated from the client SDK, an email with a link to join the team will be sent to the member's email address and an account will be created for them should they not be signed up already. If initiated from server-side SDKs, the new member will automatically be added to the team.
|
Invite a new member to join your team. Provide an ID for existing users, or invite unregistered users using an email or phone number. If initiated from a Client SDK, Appwrite will send an email or sms with a link to join the team to the invited user, and an account will be created for them if one doesn't exist. If initiated from a Server SDK, the new member will be added automatically to the team.
|
||||||
|
|
||||||
Use the 'url' parameter to redirect the user from the invitation email back to your app. When the user is redirected, use the [Update Team Membership Status](/docs/client/teams#teamsUpdateMembershipStatus) endpoint to allow the user to accept the invitation to the team.
|
You only need to provide one of a user ID, email, or phone number. Appwrite will prioritize accepting the user ID > email > phone number if you provide more than one of these parameters.
|
||||||
|
|
||||||
Please note that to avoid a [Redirect Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md) the only valid redirect URL's are the once from domains you have set when adding your platforms in the console interface.
|
Use the `url` parameter to redirect the user from the invitation email to your app. After the user is redirected, use the [Update Team Membership Status](/docs/client/teams#teamsUpdateMembershipStatus) endpoint to allow the user to accept the invitation to the team.
|
||||||
|
|
||||||
|
Please note that to avoid a [Redirect Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md) Appwrite will accept the only redirect URLs under the domains you have added as a platform on the Appwrite Console.
|
||||||
|
|
|
@ -216,6 +216,69 @@ trait TeamsBaseClient
|
||||||
$membershipUid = substr($lastEmail['text'], strpos($lastEmail['text'], '?membershipId=', 0) + 14, 20);
|
$membershipUid = substr($lastEmail['text'], strpos($lastEmail['text'], '?membershipId=', 0) + 14, 20);
|
||||||
$userUid = substr($lastEmail['text'], strpos($lastEmail['text'], '&userId=', 0) + 8, 20);
|
$userUid = substr($lastEmail['text'], strpos($lastEmail['text'], '&userId=', 0) + 8, 20);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test with UserId
|
||||||
|
* Create user
|
||||||
|
*/
|
||||||
|
$secondEmail = uniqid() . 'foe@localhost.test';
|
||||||
|
$secondName = 'Another Foe';
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/account', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'userId' => 'unique()',
|
||||||
|
'email' => $secondEmail,
|
||||||
|
'password' => 'password',
|
||||||
|
'name' => $secondName
|
||||||
|
]);
|
||||||
|
$this->assertEquals(201, $response['headers']['status-code']);
|
||||||
|
$userId = $response['body']['$id'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for UserID
|
||||||
|
* Failure
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/teams/' . $teamUid . '/memberships', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'userId' => 'abcdefdg',
|
||||||
|
'roles' => ['admin', 'editor'],
|
||||||
|
'url' => 'http://localhost:5000/join-us#title'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(404, $response['headers']['status-code']);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for UserID
|
||||||
|
* SUCCESS
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/teams/' . $teamUid . '/memberships', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'userId' => $userId,
|
||||||
|
'roles' => ['admin', 'editor'],
|
||||||
|
'url' => 'http://localhost:5000/join-us#title'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(201, $response['headers']['status-code']);
|
||||||
|
$this->assertNotEmpty($response['body']['$id']);
|
||||||
|
$this->assertNotEmpty($response['body']['userId']);
|
||||||
|
$this->assertEquals($secondName, $response['body']['userName']);
|
||||||
|
$this->assertEquals($secondEmail, $response['body']['userEmail']);
|
||||||
|
$this->assertNotEmpty($response['body']['teamId']);
|
||||||
|
$this->assertNotEmpty($response['body']['teamName']);
|
||||||
|
$this->assertCount(2, $response['body']['roles']);
|
||||||
|
$this->assertEquals(false, DateTime::isValid($response['body']['joined'])); // is null in DB
|
||||||
|
$this->assertEquals(false, $response['body']['confirm']);
|
||||||
|
|
||||||
|
$lastEmail = $this->getLastEmail();
|
||||||
|
|
||||||
|
$this->assertEquals($secondEmail, $lastEmail['to'][0]['address']);
|
||||||
|
$this->assertEquals($secondName, $lastEmail['to'][0]['name']);
|
||||||
|
$this->assertEquals('Invitation to ' . $teamName . ' Team at ' . $this->getProject()['name'], $lastEmail['subject']);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for FAILURE
|
* Test for FAILURE
|
||||||
*/
|
*/
|
||||||
|
@ -292,7 +355,7 @@ trait TeamsBaseClient
|
||||||
$this->assertEquals(200, $memberships['headers']['status-code']);
|
$this->assertEquals(200, $memberships['headers']['status-code']);
|
||||||
$this->assertIsInt($memberships['body']['total']);
|
$this->assertIsInt($memberships['body']['total']);
|
||||||
$this->assertNotEmpty($memberships['body']['memberships']);
|
$this->assertNotEmpty($memberships['body']['memberships']);
|
||||||
$this->assertCount(2, $memberships['body']['memberships']);
|
$this->assertCount(3, $memberships['body']['memberships']);
|
||||||
|
|
||||||
$response = $this->client->call(Client::METHOD_GET, '/teams/' . $data['teamUid'] . '/memberships', array_merge([
|
$response = $this->client->call(Client::METHOD_GET, '/teams/' . $data['teamUid'] . '/memberships', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
|
@ -304,7 +367,7 @@ trait TeamsBaseClient
|
||||||
$this->assertEquals(200, $response['headers']['status-code']);
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
$this->assertIsInt($response['body']['total']);
|
$this->assertIsInt($response['body']['total']);
|
||||||
$this->assertNotEmpty($response['body']['memberships']);
|
$this->assertNotEmpty($response['body']['memberships']);
|
||||||
$this->assertCount(1, $response['body']['memberships']);
|
$this->assertCount(2, $response['body']['memberships']);
|
||||||
$this->assertEquals($memberships['body']['memberships'][1]['$id'], $response['body']['memberships'][0]['$id']);
|
$this->assertEquals($memberships['body']['memberships'][1]['$id'], $response['body']['memberships'][0]['$id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -560,7 +623,7 @@ trait TeamsBaseClient
|
||||||
], $this->getHeaders()));
|
], $this->getHeaders()));
|
||||||
|
|
||||||
$this->assertEquals(200, $response['headers']['status-code']);
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
$this->assertEquals(2, $response['body']['total']);
|
$this->assertEquals(3, $response['body']['total']);
|
||||||
|
|
||||||
$ownerMembershipUid = $response['body']['memberships'][0]['$id'];
|
$ownerMembershipUid = $response['body']['memberships'][0]['$id'];
|
||||||
|
|
||||||
|
@ -615,7 +678,7 @@ trait TeamsBaseClient
|
||||||
], $this->getHeaders()));
|
], $this->getHeaders()));
|
||||||
|
|
||||||
$this->assertEquals(200, $response['headers']['status-code']);
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
$this->assertEquals(1, $response['body']['total']);
|
$this->assertEquals(2, $response['body']['total']);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for when the owner tries to delete their membership
|
* Test for when the owner tries to delete their membership
|
||||||
|
|
Loading…
Reference in a new issue