chore: poc for new roles

This commit is contained in:
Christy Jacob committed 2024-07-18 01:18:15 +04:00
1 parent d98e04cf63
commit 1bbce77c92
6 files changed
+207 -43

No files matched your search

+66 -36
View File
@@ -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',
+25
View File
@@ -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 (
+1 -1
View File
@@ -48,7 +48,7 @@ services:
build:
context: .
args:
DEBUG: false
DEBUG: true
TESTING: true
VERSION: dev
ports:
+3
View File
@@ -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';
+3
View File
@@ -10,6 +10,9 @@ use Utopia\Database\Validator\Datetime as DatetimeValidator;
trait TeamsBase
{
/**
* @group testing
*/
public function testCreateTeam(): array
{
/**
@@ -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']);
}
}