diff --git a/app/config/roles.php b/app/config/roles.php index 65b9643b89..cf2de4cb5b 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -2,34 +2,42 @@ use Appwrite\Auth\Auth; -$member = [ +$guest = [ 'global', 'public', 'home', 'console', 'graphql', 'sessions.write', - 'account', - 'teams.read', - 'teams.write', 'documents.read', 'documents.write', 'files.read', 'files.write', - 'projects.read', - 'projects.write', 'locale.read', 'avatars.read', - 'execution.read', - 'execution.write', - 'targets.read', - 'targets.write', - 'subscribers.write', - 'subscribers.read', - 'assistant.read', + 'execution.write' ]; -$admins = [ +$member = array_merge($guest, [ + 'account', + 'teams.read', + 'teams.write', + 'projects.read', + 'projects.write', + 'execution.read', + 'targets.read', + 'targets.write', + 'subscribers.read', + 'subscribers.write', + 'assistant.read' +]); + +$billing = array_merge($guest, [ + 'teams.read', + 'teams.write' +]); + +$admin = [ 'global', 'graphql', 'sessions.write', @@ -78,40 +86,62 @@ $admins = [ 'subscribers.read' ]; +# Same as owner but without the ability to modify teams and projects +$developer = array_diff($admin, ['teams.write', 'projects.write']); + +# Same as developer but without the ability to modify collections, buckets, topics, providers, migrations, rules, subscribers, and messages +$editor = array_diff($developer, [ + 'collections.write', + 'buckets.write', + 'topics.write', + 'providers.write', + 'migrations.write', + 'rules.write', + 'subscribers.write', + 'messages.write' +]); + +# Same as editor but without the ability to modify functions, execution, vcs, and webhooks +$analyst = array_diff($editor, [ + 'databases.write', + 'functions.write', + 'execution.write', + 'vcs.write', + 'webhooks.write' +]); + return [ Auth::USER_ROLE_GUESTS => [ 'label' => 'Guests', - 'scopes' => [ - 'global', - 'public', - 'home', - 'console', - 'graphql', - 'sessions.write', - 'documents.read', - 'documents.write', - 'files.read', - 'files.write', - 'locale.read', - 'avatars.read', - 'execution.write', - ], + 'scopes' => $guest, ], Auth::USER_ROLE_USERS => [ 'label' => 'Users', - 'scopes' => \array_merge($member), + 'scopes' => $member, ], Auth::USER_ROLE_ADMIN => [ 'label' => 'Admin', - 'scopes' => \array_merge($admins), - ], - Auth::USER_ROLE_DEVELOPER => [ - 'label' => 'Developer', - 'scopes' => \array_merge($admins), + 'scopes' => $admin, ], Auth::USER_ROLE_OWNER => [ 'label' => 'Owner', - 'scopes' => \array_merge($member, $admins), + 'scopes' => \array_merge($member, $admin), + ], + Auth::USER_ROLE_DEVELOPER => [ + 'label' => 'Developer', + 'scopes' => $developer, + ], + Auth::USER_ROLE_EDITOR => [ + 'label' => 'Editor', + 'scopes' => $editor, + ], + Auth::USER_ROLE_ANALYST => [ + 'label' => 'Analyst', + 'scopes' => $analyst, + ], + Auth::USER_ROLE_BILLING => [ + 'label' => 'Billing', + 'scopes' => $billing, ], Auth::USER_ROLE_APPS => [ 'label' => 'Applications', diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 2b0013db29..57f58f0168 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -172,6 +172,17 @@ App::init() ? Role::guests()->toString() : Role::users()->toString(); + + var_dump('###########'); + var_dump("Project ID : " . $project->getId()); + var_dump($project->getAttribute('teamId')); + $memberships = $user->getAttribute('memberships', []); + foreach($memberships as $membership) { + var_dump($membership['teamId']); + var_dump($membership['roles']); + } + var_dump('###########'); + // Add user roles $memberships = $user->find('teamId', $project->getAttribute('teamId'), 'memberships'); @@ -187,6 +198,15 @@ App::init() case 'developer': $role = Auth::USER_ROLE_DEVELOPER; break; + case 'editor': + $role = Auth::USER_ROLE_EDITOR; + break; + case 'analyst': + $role = Auth::USER_ROLE_ANALYST; + break; + case 'billing': + $role = Auth::USER_ROLE_BILLING; + break; } } } @@ -255,6 +275,11 @@ App::init() Authorization::setRole($authRole); } + var_dump('###########'); + var_dump(Authorization::getRoles()); + var_dump($scopes); + var_dump('###########'); + $service = $route->getLabel('sdk.namespace', ''); if (!empty($service)) { if ( diff --git a/docker-compose.yml b/docker-compose.yml index be46825992..1130916b4b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,7 +48,7 @@ services: build: context: . args: - DEBUG: false + DEBUG: true TESTING: true VERSION: dev ports: diff --git a/src/Appwrite/Auth/Auth.php b/src/Appwrite/Auth/Auth.php index 1e8109622e..e0bbed3c5d 100644 --- a/src/Appwrite/Auth/Auth.php +++ b/src/Appwrite/Auth/Auth.php @@ -39,6 +39,9 @@ class Auth public const USER_ROLE_USERS = 'users'; public const USER_ROLE_ADMIN = 'admin'; public const USER_ROLE_DEVELOPER = 'developer'; + public const USER_ROLE_EDITOR = 'editor'; + public const USER_ROLE_ANALYST = 'analyst'; + public const USER_ROLE_BILLING = 'billing'; public const USER_ROLE_OWNER = 'owner'; public const USER_ROLE_APPS = 'apps'; public const USER_ROLE_SYSTEM = 'system'; diff --git a/tests/e2e/Services/Teams/TeamsBase.php b/tests/e2e/Services/Teams/TeamsBase.php index 2328e4cdbf..a2884f7b08 100644 --- a/tests/e2e/Services/Teams/TeamsBase.php +++ b/tests/e2e/Services/Teams/TeamsBase.php @@ -10,6 +10,9 @@ use Utopia\Database\Validator\Datetime as DatetimeValidator; trait TeamsBase { + /** + * @group testing + */ public function testCreateTeam(): array { /** diff --git a/tests/e2e/Services/Teams/TeamsConsoleClientTest.php b/tests/e2e/Services/Teams/TeamsConsoleClientTest.php index 61d0f6a027..3cb859a571 100644 --- a/tests/e2e/Services/Teams/TeamsConsoleClientTest.php +++ b/tests/e2e/Services/Teams/TeamsConsoleClientTest.php @@ -28,12 +28,13 @@ class TeamsConsoleClientTest extends Scope // Create a user account before we create a invite so we can check if the user has permissions when it shouldn't $user = $this->client->call(Client::METHOD_POST, '/account', [ 'content-type' => 'application/json', - 'x-appwrite-project' => 'console'], [ - 'userId' => 'unique()', - 'email' => $email, - 'password' => $password, - 'name' => $name, - ], false); + 'x-appwrite-project' => 'console' + ], [ + 'userId' => 'unique()', + 'email' => $email, + 'password' => $password, + 'name' => $name, + ], false); $this->assertEquals(201, $user['headers']['status-code']); @@ -76,4 +77,106 @@ class TeamsConsoleClientTest extends Scope return $data; } + + + /** + * @depends testCreateTeam + * @group testing + */ + public function testTeamMemberships($data) + { + $teamUid = $data['teamUid'] ?? ''; + $teamName = $data['teamName'] ?? ''; + $email = uniqid() . 'friend@localhost.test'; + $name = 'Friend User'; + $password = 'password'; + + /** + * Invite team member with developer role + */ + $response = $this->client->call(Client::METHOD_POST, '/teams/' . $teamUid . '/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'email' => $email, + 'name' => $name, + 'roles' => ['developer'], + 'url' => 'http://localhost:5000/join-us#title' + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + /** + * Accept the invite + */ + $lastEmail = $this->getLastEmail(); + $this->assertEquals($email, $lastEmail['to'][0]['address']); + $this->assertEquals($name, $lastEmail['to'][0]['name']); + $this->assertEquals('Invitation to ' . $teamName . ' Team at ' . $this->getProject()['name'], $lastEmail['subject']); + $secret = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256); + $membershipUid = substr($lastEmail['text'], strpos($lastEmail['text'], '?membershipId=', 0) + 14, 20); + $userUid = substr($lastEmail['text'], strpos($lastEmail['text'], '&userId=', 0) + 8, 20); + + $response = $this->client->call(Client::METHOD_PATCH, '/teams/' . $teamUid . '/memberships/' . $response['body']['$id'] . '/status', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'userId' => $userUid, + 'secret' => $secret, + ]); + + $key = 'a_session_' . $this->getProject()['$id']; + $cookie = $key . '=' . $response['cookies'][$key]; + + $this->assertEquals(200, $response['headers']['status-code']); + + /** + * Test teams.read scope + */ + $response = $this->client->call(Client::METHOD_GET, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => $cookie, + ]), []); + $this->assertEquals(200, $response['headers']['status-code']); + + /** + * Test teams.write scope + */ + $response = $this->client->call(Client::METHOD_PUT, '/teams/' . $teamUid, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => $cookie, + ]), [ + 'name' => 'Arsenal Updated', + ]); + + $this->assertEquals(401, $response['headers']['status-code']); + + /** + * Test projects.read scope + */ + $response = $this->client->call(Client::METHOD_GET, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => $cookie, + ]), []); + + $this->assertEquals(200, $response['headers']['status-code']); + + /** + * Test projects.write scope + */ + $response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => $cookie, + ]), [ + 'projectId' => 'unique()', + 'name' => 'Project Name', + 'teamId' => $teamUid, + ]); + + $this->assertEquals(401, $response['headers']['status-code']); + } }