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<T>
Callback for async methods available to the
SyncUser . |
Modifier and Type | Method and Description |
---|---|
String |
acceptPermissionsOffer(String offerToken)
Accepts a permission offer sent by another user.
|
RealmAsyncTask |
acceptPermissionsOfferAsync(String offerToken,
SyncUser.Callback<String> callback)
Accepts a permission offer sent by another user.
|
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 |
applyPermissions(PermissionRequest request)
Applies a given set of permissions to a Realm.
|
RealmAsyncTask |
applyPermissionsAsync(PermissionRequest request,
SyncUser.Callback<Void> callback)
Applies a given set of permissions to a Realm.
|
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<SyncUser> callback)
Changes another user's password asynchronously.
|
RealmAsyncTask |
changePasswordAsync(String newPassword,
SyncUser.Callback<SyncUser> callback)
Changes this user's password asynchronously.
|
static void |
completePasswordReset(String resetToken,
String newPassword,
String authenticationUrl)
Complete the password reset flow by using the reset token sent to the user's email as a one-time authorization
token to change the password.
|
static RealmAsyncTask |
completePasswordResetAsync(String resetToken,
String newPassword,
String authenticationUrl,
SyncUser.Callback<Void> callback)
Complete the password reset flow by using the reset token sent to the user's email as a one-time authorization
token to change the password.
|
static void |
confirmEmail(String confirmationToken,
String authenticationUrl)
Complete the email confirmation flow by using the confirmation token sent to the user's email as a one-time
authorization token to confirm their email.
|
static RealmAsyncTask |
confirmEmailAsync(String confirmationToken,
String authenticationUrl,
SyncUser.Callback<Void> callback)
Complete the email confirmation flow by using the confirmation token sent to the user's email as a one-time
authorization token to confirm their email.
|
SyncConfiguration.Builder |
createConfiguration(String uri)
Opening a synchronized Realm requires a
SyncConfiguration . |
static SyncUser |
current()
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() . |
URL |
getAuthenticationUrl()
Returns the
URL where this user was authenticated. |
SyncConfiguration |
getDefaultConfiguration()
Returns the default configuration for this user.
|
String |
getIdentity()
Returns the identity of this user on the Realm Object Server.
|
int |
hashCode() |
void |
invalidatePermissionsOffer(String offerToken)
Invalidates an existing offer.
|
RealmAsyncTask |
invalidatePermissionsOfferAsync(String offerToken,
SyncUser.Callback<Void> callback)
Invalidates an existing offer.
|
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<SyncUser> 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).
|
String |
makePermissionsOffer(PermissionOffer offer)
Makes a permissions offer to users.
|
RealmAsyncTask |
makePermissionsOfferAsync(PermissionOffer offer,
SyncUser.Callback<String> callback)
Makes a permission offer to users.
|
static void |
requestEmailConfirmation(String email,
String authenticationUrl)
Request an email confirmation email to be sent to a user's email.
|
static RealmAsyncTask |
requestEmailConfirmationAsync(String email,
String authenticationUrl,
SyncUser.Callback<Void> callback)
Request an email confirmation email to be sent to a user's email.
|
static void |
requestPasswordReset(String email,
String authenticationUrl)
Request a password reset email to be sent to a user's email.
|
static RealmAsyncTask |
requestPasswordResetAsync(String email,
String authenticationUrl,
SyncUser.Callback<Void> callback)
Request a password reset email to be sent to a user's email.
|
List<PermissionOffer> |
retrieveCreatedPermissionsOffers()
Returns the list of offers created by this user.
|
RealmAsyncTask |
retrieveCreatedPermissionsOffersAsync(SyncUser.Callback<List<PermissionOffer>> callback)
Returns the list of offers created by this user.
|
List<Permission> |
retrieveGrantedPermissions()
Retrieves the list of permissions granted to this user.
|
RealmAsyncTask |
retrieveGrantedPermissionsAsync(SyncUser.Callback<List<Permission>> callback)
Retrieves the list of permissions granted to this user.
|
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.Callback<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.
|
String |
toJson()
Returns a JSON token representing this user.
|
String |
toString() |
public static SyncUser current()
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<SyncUser> 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 SyncConfiguration.Builder createConfiguration(String uri)
SyncConfiguration
. This method creates a
SyncConfiguration.Builder
that can be used to create it by calling SyncConfiguration.Builder.build()
.
The default synchronization mode for this Realm is query-based synchronizaton,
but see the SyncConfiguration.Builder
class for more details on how to configure a Realm.
A synchronized Realm is identified by an unique URI. In the URI, /~/
can be used as a placeholder for
a user ID in case the Realm should only be available to one user e.g., "realm://objectserver.realm.io/~/default"
.
The URL cannot end with .realm
, .realm.lock
or .realm.management
.
The /~/
will automatically be replaced with the user ID when creating the SyncConfiguration
.
Moreover, the URI defines the local location on disk. The location of a synchronized Realm file is
/data/data/<packageName>/files/realm-object-server/<user-id>/<last-path-segment>
, but this behavior
can be overwritten using SyncConfiguration.Builder.name(String)
and SyncConfiguration.Builder.directory(File)
.
Many Android devices are using FAT32 file systems. FAT32 file systems have a limitation that file names cannot be longer than 255 characters. Moreover, the entire URI should not exceed 256 characters. If the file name and underlying path are too long to handle for FAT32, a shorter unique name will be generated. See also @{link https://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx}.
uri
- URI identifying the Realm. If only a path like /~/default
is given, the configuration will
assume the file is located on the same server returned by getAuthenticationUrl()
.IllegalStateException
- if the user isn't valid. See isValid()
.public SyncConfiguration getDefaultConfiguration()
IllegalStateException
- if the user isn't valid. See isValid()
.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<SyncUser> 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<SyncUser> 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.public static void requestPasswordReset(String email, String authenticationUrl) throws ObjectServerError
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
email
- email that corresponds to the user's username.authenticationUrl
- the url used to authenticate the user.IllegalStateException
- if this method is called on the UI thread.IllegalArgumentException
- if no email or authenticationUrl was provided.ObjectServerError
- if an error happened on the server.public static RealmAsyncTask requestPasswordResetAsync(String email, String authenticationUrl, SyncUser.Callback<Void> callback)
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
email
- email that corresponds to the user's username.authenticationUrl
- the url used to authenticate the user.callback
- callback when the request has completed or failed. The callback will always happen on the same thread
as this method is called on.IllegalStateException
- if this method is called on a non-looper thread.IllegalArgumentException
- if no email or authenticationUrl was provided.public static void completePasswordReset(String resetToken, String newPassword, String authenticationUrl)
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
By default, Realm Object Server will send a link to the user's email that will redirect to a webpage where they can enter their new password. If you wish to provide a native UX, you may wish to modify the password authentication provider to use a custom URL with deep linking, so you can open the app, extract the token, and navigate to a view that allows to change the password within the app.
resetToken
- the token that was sent to the user's email address.newPassword
- the user's new password.authenticationUrl
- the url used to authenticate the user.IllegalStateException
- if this method is called on the UI thread.IllegalArgumentException
- if no token
or newPassword
was provided.ObjectServerError
- if an error happened on the server.public static RealmAsyncTask completePasswordResetAsync(String resetToken, String newPassword, String authenticationUrl, SyncUser.Callback<Void> callback) throws ObjectServerError
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
By default, Realm Object Server will send a link to the user's email that will redirect to a webpage where they can enter their new password. If you wish to provide a native UX, you may wish to modify the password authentication provider to use a custom URL with deep linking, so you can open the app, extract the token, and navigate to a view that allows to change the password within the app.
resetToken
- the token that was sent to the user's email address.newPassword
- the user's new password.authenticationUrl
- the url used to authenticate the user.callback
- callback when the server has accepted the new password or failed. The callback will always happen on the same thread
as this method is called on.IllegalStateException
- if this method is called on a non-looper thread.IllegalArgumentException
- if no token
or newPassword
was provided.ObjectServerError
public static void requestEmailConfirmation(String email, String authenticationUrl) throws ObjectServerError
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
email
- the email that corresponds to the user's username.authenticationUrl
- the url used to authenticate the user.IllegalStateException
- if this method is called on the UI thread.IllegalArgumentException
- if no email
was provided.ObjectServerError
- if an error happened on the server.public static RealmAsyncTask requestEmailConfirmationAsync(String email, String authenticationUrl, SyncUser.Callback<Void> callback)
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
email
- the email that corresponds to the user's username.authenticationUrl
- the url used to authenticate the user.callback
- callback when the request has completed or failed. The callback will always happen on the same thread
as this method is called on.IllegalStateException
- if this method is called on a non-looper thread.IllegalArgumentException
- if no email
was provided.public static void confirmEmail(String confirmationToken, String authenticationUrl) throws ObjectServerError
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
By default, Realm Object Server will send a link to the user's email that will redirect to a webpage where they can enter their new password. If you wish to provide a native UX, you may wish to modify the password authentication provider to use a custom URL with deep linking, so you can open the app, extract the token, and navigate to a view that allows to confirm the email within the app.
confirmationToken
- the token that was sent to the user's email address.authenticationUrl
- the url used to authenticate the user.IllegalStateException
- if this method is called on the UI thread.IllegalArgumentException
- if no confirmationToken
was provided.ObjectServerError
- if an error happened on the server.public static RealmAsyncTask confirmEmailAsync(String confirmationToken, String authenticationUrl, SyncUser.Callback<Void> callback)
This can only be used for users who authenticated with the SyncCredentials.IdentityProvider.USERNAME_PASSWORD
provider, and passed a valid email address as a username.
By default, Realm Object Server will send a link to the user's email that will redirect to a webpage where they can enter their new password. If you wish to provide a native UX, you may wish to modify the password authentication provider to use a custom URL with deep linking, so you can open the app, extract the token, and navigate to a view that allows to confirm the email within the app.
confirmationToken
- the token that was sent to the user's email address.authenticationUrl
- the url used to authenticate the user.callback
- callback when the server has confirmed the email or failed. The callback will always happen on the same thread
as this method is called on.IllegalStateException
- if this method is called on a non-looper thread.IllegalArgumentException
- if no confirmationToken
was provided.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
.IllegalStateException
- if this method is called on the UI thread.IllegalArgumentException
- if no providerUserIdentity
or provider
string was provided.ObjectServerError
- if an error happened on the server.public RealmAsyncTask retrieveInfoForUserAsync(String providerUserIdentity, String provider, SyncUser.Callback<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.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 List<SyncSession> allSessions()
public URL getAuthenticationUrl()
URL
where this user was authenticated.URL
where the user was authenticated.public List<Permission> retrieveGrantedPermissions()
ObjectServerError
- if an error happened while trying to retrieve the list of permissions on the Realm Object Server.android.os.NetworkOnMainThreadException
- if called from the UI thread.public RealmAsyncTask retrieveGrantedPermissionsAsync(SyncUser.Callback<List<Permission>> callback)
callback
- callback notified when list the permissions are ready.RealmAsyncTask
that can be used to cancel the task if needed.IllegalStateException
- if this method is called from a thread without a looper.public void applyPermissions(PermissionRequest request)
AccessLevel.ADMIN
privileges to the Realm can use this method.
A PermissionRequest
object encapsulates a description of which users are granted what
AccessLevel
s for which Realm(s).
Once the request is successfully handled, a Permission
entry is created for each
affected user and can be found by them using retrieveGrantedPermissions()
.
request
- request object describing which permissions to grant and to what Realm(s).ObjectServerError
- if an error happened while trying to apply the permission changes on the Realm Object Server.android.os.NetworkOnMainThreadException
- if called from the UI thread.public RealmAsyncTask applyPermissionsAsync(PermissionRequest request, SyncUser.Callback<Void> callback)
AccessLevel.ADMIN
privileges to the Realm can use this method.
A PermissionRequest
object encapsulates a description of which users are granted what
AccessLevel
s for which Realm(s).
Once the request is successfully handled, a Permission
entry is created for each
affected user and can be found by them using retrieveGrantedPermissionsAsync(Callback)
.
request
- request object describing which permissions to grant and to what Realm(s).callback
- callback when the request either succeeded or failed.IllegalStateException
- if this method is called from a thread without a looper.public String makePermissionsOffer(PermissionOffer offer)
PermissionOffer
do not take effect until the offer has been accepted by a user
calling acceptPermissionsOfferAsync(String, Callback)
.
A permission offer can be used as a flexible way of sharing Realms with other users that might not be known at the time
of making the offer as well as enabling sharing across other channels like e-mail. If a specific user should be
granted access, using applyPermissionsAsync(PermissionRequest, Callback)
will be faster and quicker.
An offer can be accepted by multiple users.
offer
- the object description the kind of permissions that should be offered to other users.ObjectServerError
- if an error happened while trying to create the permissions offer.android.os.NetworkOnMainThreadException
- if called from the UI thread.public RealmAsyncTask makePermissionsOfferAsync(PermissionOffer offer, SyncUser.Callback<String> callback)
PermissionOffer
do not take effect until the offer has been accepted by a user
calling acceptPermissionsOfferAsync(String, Callback)
.
A permission offer can be used as a flexible way of sharing Realms with other users that might not be known at the time
of making the offer as well as enabling sharing across other channels like e-mail. If a specific user should be
granted access, using applyPermissionsAsync(PermissionRequest, Callback)
will be faster and quicker.
An offer can be accepted by multiple users.
offer
- the object description the kind of permissions that should be offered to other users.callback
- callback to be notified with the offer token once it is ready.android.os.NetworkOnMainThreadException
- if called from the UI thread.IllegalStateException
- if this method is called from a Thread without a looper.public String acceptPermissionsOffer(String offerToken)
offerToken
- token representing the permission offer.ObjectServerError
- if an error happened while trying to accept the offer.android.os.NetworkOnMainThreadException
- if called from the UI thread.public RealmAsyncTask acceptPermissionsOfferAsync(String offerToken, SyncUser.Callback<String> callback)
offerToken
- token representing the permission offer.callback
- with the permission details that were accepted.RealmAsyncTask
that can be used to cancel the task if needed.IllegalStateException
- if this method is called from a thread without a looper.public void invalidatePermissionsOffer(String offerToken)
offerToken
- token that should be invalidated.ObjectServerError
- if an error happened while trying to invalidate the offer on the Realm Object Server.android.os.NetworkOnMainThreadException
- if called from the UI thread.public RealmAsyncTask invalidatePermissionsOfferAsync(String offerToken, SyncUser.Callback<Void> callback)
offerToken
- token that should be invalidated.RealmAsyncTask
that can be used to cancel the task if needed.IllegalStateException
- if this method is called from a thread without a looper.public List<PermissionOffer> retrieveCreatedPermissionsOffers()
invalidatePermissionsOfferAsync(String, Callback)
or sent to other users by sending the
PermissionOffer.getToken()
.ObjectServerError
- if an error occured while trying retrieve the list of offers from the Realm Object Server.android.os.NetworkOnMainThreadException
- if called from the UI thread.public RealmAsyncTask retrieveCreatedPermissionsOffersAsync(SyncUser.Callback<List<PermissionOffer>> callback)
invalidatePermissionsOfferAsync(String, Callback)
or sent to other users by sending the
PermissionOffer.getToken()
.callback
- that will receive the list of available offers.RealmAsyncTask
that can be used to cancel the task if needed.IllegalStateException
- if this method is called from a thread without a looper.