diff --git a/lib/browser/rpc.js b/lib/browser/rpc.js index 427df21e..f71eeac5 100644 --- a/lib/browser/rpc.js +++ b/lib/browser/rpc.js @@ -72,6 +72,12 @@ export function createUser(args) { return deserialize(undefined, result); } +export function _adminUser(args) { + args = args.map((arg) => serialize(null, arg)); + const result = sendRequest('_adminUser', {arguments: args}); + return deserialize(undefined, result); +} + export function callMethod(realmId, id, name, args) { if (args) { args = args.map((arg) => serialize(realmId, arg)); diff --git a/lib/browser/user.js b/lib/browser/user.js index 3c4de66a..ca55b915 100644 --- a/lib/browser/user.js +++ b/lib/browser/user.js @@ -19,7 +19,7 @@ 'use strict'; -import { createUser as createUserRPC, getAllUsers as getAllUsersRPC } from './rpc'; +import { createUser as createUserRPC, _adminUser as _adminUserRPC, getAllUsers as getAllUsersRPC } from './rpc'; import { keys, objectTypes } from './constants'; import { createMethods } from './util'; @@ -28,6 +28,10 @@ export default class User { return createUserRPC(Array.from(arguments)); } + static _adminUser(adminToken, server) { + return _adminUserRPC(Array.from(arguments)); + } + static get all() { return getAllUsersRPC(); } diff --git a/src/rpc.cpp b/src/rpc.cpp index 896fad8a..7fc630c8 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -160,6 +160,27 @@ RPCServer::RPCServer() { JSObjectRef user_object = (JSObjectRef)jsc::Function::call(m_context, create_user_method, arg_count, arg_values); return (json){{"result", serialize_json_value(user_object)}}; }; + m_requests["/_adminUser"] = [this](const json dict) { + JSObjectRef realm_constructor = m_session_id ? JSObjectRef(m_objects[m_session_id]) : NULL; + if (!realm_constructor) { + throw std::runtime_error("Realm constructor not found!"); + } + + JSObjectRef sync_constructor = (JSObjectRef)jsc::Object::get_property(m_context, realm_constructor, "Sync"); + JSObjectRef user_constructor = (JSObjectRef)jsc::Object::get_property(m_context, sync_constructor, "User"); + JSObjectRef create_user_method = (JSObjectRef)jsc::Object::get_property(m_context, user_constructor, "_adminUser"); + + json::array_t args = dict["arguments"]; + size_t arg_count = args.size(); + JSValueRef arg_values[arg_count]; + + for (size_t i = 0; i < arg_count; i++) { + arg_values[i] = deserialize_json_value(args[i]); + } + + JSObjectRef user_object = (JSObjectRef)jsc::Function::call(m_context, create_user_method, arg_count, arg_values); + return (json){{"result", serialize_json_value(user_object)}}; + }; m_requests["/call_method"] = [this](const json dict) { JSObjectRef object = m_objects[dict["id"].get()]; std::string method_string = dict["name"].get();