public class SyncUser extends Object
A user can log in to the Realm Object Server, and if access is granted, it is possible to synchronize the local and the remote Realm. Moreover, synchronization is halted when the user is logged out.
It is possible to persist a user. By retrieving a user, there is no need to log in to the 3rd party provider again. Persisting a user between sessions, the user's credentials are stored locally on the device, and should be treated as sensitive data.
Modifier and Type | Class and Description |
---|---|
static interface |
SyncUser.Callback |
static interface |
SyncUser.RequestCallback<T> |
Modifier and Type | Method and Description |
---|---|
static Map<String,SyncUser> |
all()
Returns all valid users known by this device.
|
List<SyncSession> |
allSessions()
Returns all the valid sessions belonging to the user.
|
void |
changePassword(String newPassword)
Changes this user's password.
|
void |
changePassword(String userId,
String newPassword)
Changes another user's password.
|
RealmAsyncTask |
changePasswordAsync(String userId,
String newPassword,
SyncUser.Callback callback)
Changes another user's password asynchronously.
|
RealmAsyncTask |
changePasswordAsync(String newPassword,
SyncUser.Callback callback)
Changes this user's password asynchronously.
|
static SyncUser |
currentUser()
Returns the current user that is logged in and still valid.
|
boolean |
equals(Object o) |
static SyncUser |
fromJson(String user)
Loads a user that has previously been serialized using
toJson() . |
Token |
getAccessToken()
Returns this user's access token.
|
URL |
getAuthenticationUrl()
Returns the
URL where this user was authenticated. |
String |
getIdentity()
Returns the identity of this user on the Realm Object Server.
|
Realm |
getManagementRealm()
Returns an instance of the Management Realm owned by the user.
|
int |
hashCode() |
boolean |
isAdmin()
Returns
true if this user is an administrator on the Realm Object Server, false otherwise. |
boolean |
isValid()
Returns
true if the user is logged into the Realm Object Server. |
static SyncUser |
login(SyncCredentials credentials,
String authenticationUrl)
Logs in the user to the Realm Object Server.
|
static RealmAsyncTask |
loginAsync(SyncCredentials credentials,
String authenticationUrl,
SyncUser.Callback callback)
Logs in the user to the Realm Object Server.
|
void |
logout()
Log a user out, destroying their server state, unregistering them from the SDK, and removing
any synced Realms associated with them, from on-disk storage on next app launch (or directly
if all instances are closed).
|
SyncUserInfo |
retrieveInfoForUser(String providerUserIdentity,
String provider)
Given a Realm Object Server authentication provider and a provider identifier for a user (for example, a username), look up and return user information for that user.
|
RealmAsyncTask |
retrieveInfoForUserAsync(String providerUserIdentity,
String provider,
SyncUser.RequestCallback<SyncUserInfo> callback)
Given a Realm Object Server authentication provider and a provider identifier for a user (for example, a username), asynchronously look up and return user information for that user.
|
SyncUser |
retrieveUser(String provider,
String providerId)
Deprecated.
as of release 3.6.0, replaced by
retrieveInfoForUser(String, String) } |
RealmAsyncTask |
retrieveUserAsync(String provider,
String providerId,
SyncUser.Callback callback)
Deprecated.
as of release 3.6.0, replaced by
retrieveInfoForUserAsync(String, String, RequestCallback) } |
String |
toJson()
Returns a JSON token representing this user.
|
String |
toString() |
public static SyncUser currentUser()
SyncUser
that has logged in and is still valid. null
if no user is logged in or the user has
expired.IllegalStateException
- if multiple users are logged in.public static Map<String,SyncUser> all()
public static SyncUser fromJson(String user)
toJson()
.user
- JSON string representing the user.IllegalArgumentException
- if the JSON couldn't be converted to a valid SyncUser
object.public static SyncUser login(SyncCredentials credentials, String authenticationUrl) throws ObjectServerError
SyncConfiguration
.credentials
- credentials to use.authenticationUrl
- server that can authenticate against.ObjectServerError
- if the login failed.IllegalArgumentException
- if the URL is malformed.public static RealmAsyncTask loginAsync(SyncCredentials credentials, String authenticationUrl, SyncUser.Callback callback)
SyncConfiguration
.credentials
- credentials to use.authenticationUrl
- server that the user is authenticated against.callback
- callback when login has completed or failed. The callback will always happen on the same thread
as this this method is called on.IllegalArgumentException
- if not on a Looper thread.public void logout()
AuthenticationListener
will be notified and user credentials will be deleted from this device.public void changePassword(String newPassword) throws ObjectServerError
WARNING: Changing a user's password through an authentication server that doesn't use HTTPS is a major security flaw, and should only be done while testing.
newPassword
- the user's new password.ObjectServerError
- if the password could not be changed.public void changePassword(String userId, String newPassword) throws ObjectServerError
This user needs admin privilege in order to change someone else's password.
WARNING: Changing a user's password through an authentication server that doesn't use HTTPS is a major security flaw, and should only be done while testing.
userId
- identity (getIdentity()
) of the user we want to change the password for.newPassword
- the user's new password.ObjectServerError
- if the password could not be changed.public RealmAsyncTask changePasswordAsync(String newPassword, SyncUser.Callback callback)
WARNING: Changing a users password using an authentication server that doesn't use HTTPS is a major security flaw, and should only be done while testing.
newPassword
- the user's new password.callback
- callback when login has completed or failed. The callback will always happen on the same thread
as this method is called on.IllegalArgumentException
- if not on a Looper thread.public RealmAsyncTask changePasswordAsync(String userId, String newPassword, SyncUser.Callback callback)
This user needs admin privilege in order to change someone else's password. WARNING: Changing a users password using an authentication server that doesn't use HTTPS is a major security flaw, and should only be done while testing.
userId
- identity (getIdentity()
) of the user we want to change the password for.newPassword
- the user's new password.callback
- callback when login has completed or failed. The callback will always happen on the same thread
as this method is called on.IllegalArgumentException
- if not on a Looper thread.@Deprecated public SyncUser retrieveUser(String provider, String providerId) throws ObjectServerError
retrieveInfoForUser(String, String)
}SyncUser
using the identity provider and the used username.provider
- identity providers SyncCredentials.IdentityProvider
used when the account was created.providerId
- username or email used to create the account for the first time,
what is needed will depend on what type of SyncCredentials
was used.SyncUser
associated with the given identity provider and providerId, or null
in case
of an invalid
provider or providerId
.ObjectServerError
- in case of an error.@Deprecated public RealmAsyncTask retrieveUserAsync(String provider, String providerId, SyncUser.Callback callback)
retrieveInfoForUserAsync(String, String, RequestCallback)
}SyncUser
using the identity provider and the used username.
This is for Admin users only.provider
- identity providers SyncCredentials.IdentityProvider
used when the account was created.providerId
- username or email used to create the account for the first time,
what is needed will depend on what type of SyncCredentials
was used.callback
- callback when the lookup has completed or failed. The callback will always happen on the same thread
as this method is called on.public SyncUserInfo retrieveInfoForUser(String providerUserIdentity, String provider) throws ObjectServerError
providerUserIdentity
- The username or identity of the user as issued by the authentication provider.
In most cases this is different from the Realm Object Server-issued identity.provider
- The authentication provider SyncCredentials.IdentityProvider
that manages the user whose information is desired.SyncUser
associated with the given identity provider and providerId, or null
in case
of an invalid
provider or providerId
.ObjectServerError
- in case of an error.public RealmAsyncTask retrieveInfoForUserAsync(String providerUserIdentity, String provider, SyncUser.RequestCallback<SyncUserInfo> callback)
providerUserIdentity
- The username or identity of the user as issued by the authentication provider.
In most cases this is different from the Realm Object Server-issued identity.provider
- The authentication provider SyncCredentials.IdentityProvider
that manages the user whose information is desired.callback
- callback when the lookup has completed or failed. The callback will always happen on the same thread
as this method is called on.SyncUser
associated with the given identity provider and providerId, or null
in case
of an invalid
provider or providerId
.public String toJson()
Possession of this JSON token can potentially grant access to data stored on the Realm Object Server, so it should be treated as sensitive data.
fromJson(String)
.fromJson(String)
public boolean isValid()
true
if the user is logged into the Realm Object Server. If this method returns true
it
implies that the user has valid credentials that have not expired.
The user might still have been logged out by the Realm Object Server which will not be detected before the
user tries to actively synchronize a Realm. If a logged out user tries to synchronize a Realm, an error will be
reported to the SyncSession.ErrorHandler
defined by
SyncConfiguration.Builder.errorHandler(SyncSession.ErrorHandler)
.
true
if the User is logged into the Realm Object Server, false
otherwise.public boolean isAdmin()
true
if this user is an administrator on the Realm Object Server, false
otherwise.
Administrators can access all Realms on the server as well as change the permissions of the Realms.
true
if the user is an administrator on the Realm Object Server, false
otherwise.public String getIdentity()
null
is returned.public Token getAccessToken()
null
is returned.public Realm getManagementRealm()
This Realm can be used to control access and permissions for Realms owned by the user. This includes giving other users access to Realms.
public List<SyncSession> allSessions()
public URL getAuthenticationUrl()
URL
where this user was authenticated.URL
where the user was authenticated.