1

My app is a collaborative app where users should be able to write/read into a global Realm file to share data between them. I would also like to include some initial data (realmObjects) in the Realm file which will be common to all users.

User will sign up to, then, be able to create data and share the data with all the users. The initial data, created only once, should be available to all users.

I am not able to assign user permissions (write/read) to each new user once they sign-up/login in the app. The common data is created multiple time since the query (realm.where(realmObject.class).findall() returns no results, even though it has been created.

public static final String AUTH_URL     = "http://" + BuildConfig.OBJECT_SERVER_IP + ":9080/auth";
public static final String REALM_URL    = "realm://" + BuildConfig.OBJECT_SERVER_IP + ":9080/default";

UserManager class

public class UserManager {

    // Supported authentication mode
    public enum AUTH_MODE {
        PASSWORD,
        FACEBOOK,
        GOOGLE
    }
    private static AUTH_MODE mode = AUTH_MODE.PASSWORD; // default

    public static void setAuthMode(AUTH_MODE m) {
        mode = m;
    }

    public static void logoutActiveUser() {
        switch (mode) {
            case PASSWORD: {
                break;
            }
            case FACEBOOK: {
                LoginManager.getInstance().logOut();
                break;
            }
            case GOOGLE: {
                break;
            }
        }
        SyncUser.currentUser().logout();
    }

    // Configure Realm for the current active user
    public static void setActiveUser(SyncUser user) {
        Realm.removeDefaultConfiguration();
        SyncConfiguration defaultConfig = new SyncConfiguration.Builder(user, REALM_URL).name("Realm").schemaVersion(0).build();
        Realm.setDefaultConfiguration(defaultConfig);
        setDefaultPermissionsRealm(user);
    }

    private static void setDefaultPermissionsRealm(SyncUser user){
        if (user.getIdentity().toString().equals(PRIVILAGE)){

            Realm realm = user.getManagementRealm();
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    Boolean mayRead = true; // Grant read access
                    Boolean mayWrite = true; // Keep current permission
                    Boolean mayManage = false; // Revoke management access
                    PermissionChange change = new PermissionChange(REALM_URL,
                            "*",
                            mayRead,
                            mayWrite,
                            mayManage);
                    realm.insert(change);
                }
            });
        }
    }
}

SplashActivity

SyncUser.loginAsync(SyncCredentials.usernamePassword(eMail, password, true), AUTH_URL, new SyncUser.Callback() {
                @Override
                public void onSuccess(SyncUser syncUser) {
                    loginRegistrationSuccess = true;
                    registrationComplete(syncUser);
                }

                @Override
                public void onError(ObjectServerError error) {
                    loginRegistrationSuccess = false;
                }
            });

facebookAuthRegistration    = new FacebookAuth((LoginButton) findViewById(R.id.sign_up_facebook), this) {
           @Override
           public void onRegistrationComplete(final LoginResult loginResult, User userInfo) {
               UserManager.setAuthMode(UserManager.AUTH_MODE.FACEBOOK);
               SyncCredentials credentials = SyncCredentials.facebook(loginResult.getAccessToken().getToken());
               SyncUser.loginAsync(credentials, AUTH_URL, SplashScreenActivity.this);
           }
        };

        googleAuthRegistration      = new GoogleAuth((Button) findViewById(R.id.sign_up_google), this, googleAuthLogin.getmGoogleApiClient()) {
            @Override
            public void onRegistrationComplete(GoogleSignInResult result) {
                UserManager.setAuthMode(UserManager.AUTH_MODE.GOOGLE);
                GoogleSignInAccount acct    = result.getSignInAccount();
                SyncCredentials credentials = SyncCredentials.google(acct.getIdToken());
                SyncUser.loginAsync(credentials, AUTH_URL, SplashScreenActivity.this);
            }
        };

@Override
    public void onSuccess(SyncUser syncUser) {
        loginRegistrationSuccess = true;
        showProgress(false);
        registrationComplete(syncUser);
    }

private void registrationComplete(SyncUser syncUser) {
        UserManager.setActiveUser(syncUser);
        Intent mainIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        SplashScreenActivity.this.startActivity(mainIntent);
        SplashScreenActivity.this.finish();
    }

It looks like if I am creating a different realm for each user even though I am not including in the URL the "~".

Could anyone tell me what I am doing wrong?

Thanks

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
user274051
  • 325
  • 1
  • 12

1 Answers1

0

Initially, you will need to create the Realm using the admin user, and set the permissions for all other users can access it. If you are using the Pro edition, you could create the Realm using a small node.js script. Alternatively, you could create a small internal app with the single purpose of creating the Realm using the admin user.

Users are only allowed to create Realm within their home directory (~ so to speak) but they can share it with other users. Doing so, you will have to distribute the user id of that first user.

geisshirt
  • 2,457
  • 1
  • 17
  • 20
  • Thanks. I am using the developer edition. I am logging into the app with the Realm Object Server Admin user. I have even created a new user in ROS with admin privileges, and logging into the app. So, both users should have the privilege to create a global file, but no global file is created. Moreover, the ROS is creating a trace in the log tap. I will included in the next comment – user274051 May 26 '17 at 10:32
  • [proxy] internal error. Code undefined, status: undefined, message: SyntaxError: Unexpected token j in JSON at position 0 at Object.parse (native) at ProxyService.webSocketUpgradeHandler (/usr/lib/nodejs/realm-object-server-developer/.build/src/node/services/proxy.js:104:30) at ProxyService.safeUpgrade (/usr/lib/nodejs/realm-object-server-developer/.build/src/node/services/proxy.js:78:12) at emitThree (events.js:116:13) at Server.emit (events.js:194:7) at onParserExecuteCommon (_http_server.js:411:14) at HTTPParser.onParserExecute (_http_server.js:379:5) – user274051 May 26 '17 at 10:32
  • 1
    Looks very much like https://stackoverflow.com/questions/44170810/realm-response-status-400 – geisshirt May 31 '17 at 15:01