diff --git a/CHANGES.md b/CHANGES.md index 08bcd6962f..21fcc31b60 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,9 @@ ## Features -- Grouped auth related attributes in project collection. Introduced new attribute `auths` and removed all attributes related to auth methods and `usersAuthLimit` as well, all these are grouped under `auths` attribute +- Grouped auth related attributes in project collection. Introduced new attribute `auths` and removed all attributes related to auth methods and `usersAuthLimit` as well, all these are grouped under `auths` attribute +- Grouped oAuth related attributes in project collection. Introduced new attribute `providers` and removed all attributes related to OAuth2 providers. All OAuth2 attributes are grouped under `providers` +- Project model changed, `userAuth` => `auth` example `userAuthEmailPassword` => `authEmailPassword`, also `userOauth2...` => `provider...` example `userOauth2GithubAppid` => `providerGithubAppid` # Version 0.9.3 diff --git a/app/config/collections2.php b/app/config/collections2.php index 413defe558..6625210696 100644 --- a/app/config/collections2.php +++ b/app/config/collections2.php @@ -166,6 +166,17 @@ $collections = [ 'array' => false, 'filters' => ['json'], ], + [ + '$id' => 'providers', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['json'], + ], [ '$id' => 'platforms', 'type' => Database::VAR_STRING, @@ -1394,37 +1405,4 @@ $collections = [ ], ]; -/* - * Add enabled OAuth2 providers to default data rules - */ -foreach ($providers as $index => $provider) { - if (!$provider['enabled']) { - continue; - } - - $collections['projects']['attributes'][] = [ - '$id' => 'usersOauth2' . \ucfirst($index) . 'Appid', - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 16384, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ]; - - $collections['projects']['attributes'][] = [ - '$id' => 'usersOauth2' . \ucfirst($index) . 'Secret', - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 16384, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ]; -} - return $collections; diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 1eda0f7b57..540f76096e 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -256,8 +256,8 @@ App::get('/v1/account/sessions/oauth2/:provider') $protocol = $request->getProtocol(); $callback = $protocol.'://'.$request->getHostname().'/v1/account/sessions/oauth2/callback/'.$provider.'/'.$project->getId(); - $appId = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Appid', ''); - $appSecret = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Secret', '{}'); + $appId = $project->getAttribute('providers', [])[$provider.'Appid'] ?? ''; + $appSecret = $project->getAttribute('providers', [])[$provider.'Secret'] ?? '{}'; if (!empty($appSecret) && isset($appSecret['version'])) { $key = App::getEnv('_APP_OPENSSL_KEY_V'.$appSecret['version']); @@ -369,8 +369,8 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect') $defaultState = ['success' => $project->getAttribute('url', ''), 'failure' => '']; $validateURL = new URL(); - $appId = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Appid', ''); - $appSecret = $project->getAttribute('usersOauth2'.\ucfirst($provider).'Secret', '{}'); + $appId = $project->getAttribute('providers', [])[$provider.'Appid'] ?? ''; + $appSecret = $project->getAttribute('providers', [])[$provider.'Secret'] ?? '{}'; if (!empty($appSecret) && isset($appSecret['version'])) { $key = App::getEnv('_APP_OPENSSL_KEY_V'.$appSecret['version']); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 9185caee24..d9af95ae83 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -505,10 +505,11 @@ App::patch('/v1/projects/:projectId/oauth2') throw new Exception('Project not found', 404); } - $project = $dbForConsole->updateDocument('projects', $project->getId(), $project - ->setAttribute('usersOauth2' . \ucfirst($provider) . 'Appid', $appId) - ->setAttribute('usersOauth2' . \ucfirst($provider) . 'Secret', $secret) - ); + $providers = $project->getAttribute('providers', []); + $providers[$provider . 'Appid'] = $appId; + $providers[$provider . 'Secret'] = $secret; + + $project = $dbForConsole->updateDocument('projects', $project->getId(), $project->setAttribute('providers', $providers)); $response->dynamic($project, Response::MODEL_PROJECT); }); diff --git a/app/views/console/users/index.phtml b/app/views/console/users/index.phtml index 8c7d8fc20f..5a765bebf2 100644 --- a/app/views/console/users/index.phtml +++ b/app/views/console/users/index.phtml @@ -439,15 +439,15 @@ $auth = $this->getParam('auth', []); - + - + - + - +
@@ -469,14 +469,14 @@ $auth = $this->getParam('auth', []);
+ {{console-project.providerescape(ucfirst($provider)); ?>Appid}} && + {{console-project.providerescape(ucfirst($provider)); ?>Secret}}"> + !{{console-project.providerescape(ucfirst($provider)); ?>Appid}} || + !{{console-project.providerescape(ucfirst($provider)); ?>Secret}}"> diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index cfda2e5542..514a06a3c5 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -138,13 +138,13 @@ class Project extends Model $name = (isset($provider['name'])) ? $provider['name'] : 'Unknown'; $this - ->addRule('usersOauth2'.\ucfirst($index).'Appid', [ + ->addRule('provider'.\ucfirst($index).'Appid', [ 'type' => self::TYPE_STRING, 'description' => $name.' OAuth app ID.', 'example' => '123247283472834787438', 'default' => '', ]) - ->addRule('usersOauth2'.\ucfirst($index).'Secret', [ + ->addRule('provider'.\ucfirst($index).'Secret', [ 'type' => self::TYPE_STRING, 'description' => $name.' OAuth secret ID.', 'example' => 'djsgudsdsewe43434343dd34...', @@ -238,6 +238,18 @@ class Project extends Model $document->setAttribute('auth' . ucfirst($key), $value); } + $providers = Config::getParam('providers', []); + $providerValues = $document->getAttribute('providers', []); + + foreach ($providers as $key => $provider) { + if (!$provider['enabled']) { + continue; + } + $appId = $providerValues[$key . 'Appid'] ?? ''; + $secret = $providerValues[$key . 'Secret'] ?? ''; + $document->setAttribute($key . 'Appid', $appId)->setAttribute($key . 'Secret', $secret); + } + return $document; } } \ No newline at end of file diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 709966558e..ae5360dd9f 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -275,8 +275,8 @@ class ProjectsConsoleClientTest extends Scope $this->assertEquals($id, $response['body']['$id']); foreach ($providers as $key => $provider) { - $this->assertEquals('AppId-'.ucfirst($key), $response['body']['usersOauth2'.ucfirst($key).'Appid']); - $this->assertEquals('Secret-'.ucfirst($key), $response['body']['usersOauth2'.ucfirst($key).'Secret']); + $this->assertEquals('AppId-'.ucfirst($key), $response['body']['provider'.ucfirst($key).'Appid']); + $this->assertEquals('Secret-'.ucfirst($key), $response['body']['provider'.ucfirst($key).'Secret']); } /**