简体   繁体   中英

Create a global Realm file for a collaborative android app

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM