From 1ba9b434b4bb997faebcf117f57c87bf38e72a3e Mon Sep 17 00:00:00 2001 From: Yavor Georgiev Date: Mon, 10 Jul 2017 15:04:55 +0200 Subject: [PATCH] Add a new optional server parameter to User.adminUser (#1111) * add a new optional server parameter to User.adminUser This enables `User.openManagementRealm()` for admin token users. * changelog * API docs --- CHANGELOG.md | 2 ++ docs/sync.js | 3 ++- lib/index.d.ts | 2 +- lib/user-methods.js | 7 +++---- src/js_sync.hpp | 7 ++++++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f12fe342..bb3e5d27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ vNext Release notes (TBD) ### Enhancements * Add support for Linking Objects (AKA Backlinks). * Add support for retrieving user account information. +* Add optional `server` parameter to `Realm.Sync.User.adminUser` + Specifying the server address the same way as in `Realm.Sync.User.login` allows the admin token user to use the permission realm APIs. ### Bug fixes * Fix regression where setting a Results or List object to a `list` property would throw. diff --git a/docs/sync.js b/docs/sync.js index 22dd21cd..648dc3bd 100644 --- a/docs/sync.js +++ b/docs/sync.js @@ -169,9 +169,10 @@ class User { /** * Create an admin user for the given authentication server with an existing token * @param {string} adminToken - existing admin token + * @param {string} [server] - authentication server * @return {User} - admin user populated with the given token and server */ - static adminUser(adminToken) {} + static adminUser(adminToken, server) {} /** * A dictionary containing users that are currently logged in. diff --git a/lib/index.d.ts b/lib/index.d.ts index 099c27bb..2fe62125 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -262,7 +262,7 @@ declare namespace Realm.Sync { readonly isAdmin: boolean; readonly server: string; readonly token: string; - static adminUser(adminToken: string): User; + static adminUser(adminToken: string, server?: string): User; static login(server: string, username: string, password: string, callback: (error: any, user: User) => void): void; static loginWithProvider(server: string, provider: string, providerToken: string, callback: (error: any, user: User) => void): void; static register(server: string, username: string, password: string, callback: (error: any, user: User) => void): void; diff --git a/lib/user-methods.js b/lib/user-methods.js index 4ac77723..1a7e343d 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -152,14 +152,13 @@ module.exports = { return allUsers[keys[0]]; }, - adminUser(token) { + adminUser(token, server) { checkTypes(arguments, ['string']); - var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { + const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); - var user = this.createUser('', uuid, token, true); - return user; + return this.createUser(server || '', uuid, token, true); }, register(server, username, password, callback) { diff --git a/src/js_sync.hpp b/src/js_sync.hpp index fd24b96b..1422e2ad 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "event_loop_dispatcher.hpp" #include "platform.hpp" @@ -404,7 +405,11 @@ void SyncClass::populate_sync_config(ContextType ctx, ObjectType realm_constr } std::string raw_realm_url = Object::validated_get_string(ctx, sync_config_object, "url"); - + if (shared_user->token_type() == SyncUser::TokenType::Admin) { + static std::regex tilde("/~/"); + raw_realm_url = std::regex_replace(raw_realm_url, tilde, "/__auth/"); + } + bool client_validate_ssl = true; ValueType validate_ssl_temp = Object::get_property(ctx, sync_config_object, "validate_ssl"); if (!Value::is_undefined(ctx, validate_ssl_temp)) {