RLMSyncUser.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Foundation/Foundation.h>
  19. @class RLMSyncUser, RLMSyncCredentials, RLMSyncPermissionValue, RLMSyncPermissionResults, RLMSyncSession, RLMRealm;
  20. /**
  21. The state of the user object.
  22. */
  23. typedef NS_ENUM(NSUInteger, RLMSyncUserState) {
  24. /// The user is logged out. Call `logInWithCredentials:...` with valid credentials to log the user back in.
  25. RLMSyncUserStateLoggedOut,
  26. /// The user is logged in, and any Realms associated with it are syncing with the Realm Object Server.
  27. RLMSyncUserStateActive,
  28. /// The user has encountered a fatal error state, and cannot be used.
  29. RLMSyncUserStateError,
  30. };
  31. /// A block type used for APIs which asynchronously vend an `RLMSyncUser`.
  32. typedef void(^RLMUserCompletionBlock)(RLMSyncUser * _Nullable, NSError * _Nullable);
  33. /// A block type used to report the status of a password change operation.
  34. /// If the `NSError` argument is nil, the operation succeeded.
  35. typedef void(^RLMPasswordChangeStatusBlock)(NSError * _Nullable);
  36. /// A block type used to report the status of a permission apply or revoke operation.
  37. /// If the `NSError` argument is nil, the operation succeeded.
  38. typedef void(^RLMPermissionStatusBlock)(NSError * _Nullable);
  39. /// A block type used to asynchronously report results of a permissions get operation.
  40. /// Exactly one of the two arguments will be populated.
  41. typedef void(^RLMPermissionResultsBlock)(RLMSyncPermissionResults * _Nullable, NSError * _Nullable);
  42. NS_ASSUME_NONNULL_BEGIN
  43. /**
  44. A `RLMSyncUser` instance represents a single Realm Object Server user account.
  45. A user may have one or more credentials associated with it. These credentials
  46. uniquely identify the user to the authentication provider, and are used to sign
  47. into a Realm Object Server user account.
  48. Note that user objects are only vended out via SDK APIs, and cannot be directly
  49. initialized. User objects can be accessed from any thread.
  50. */
  51. @interface RLMSyncUser : NSObject
  52. /**
  53. A dictionary of all valid, logged-in user identities corresponding to their user objects.
  54. */
  55. + (NSDictionary<NSString *, RLMSyncUser *> *)allUsers NS_REFINED_FOR_SWIFT;
  56. /**
  57. The logged-in user. `nil` if none exists.
  58. @warning Throws an exception if more than one logged-in user exists.
  59. */
  60. + (nullable RLMSyncUser *)currentUser NS_REFINED_FOR_SWIFT;
  61. /**
  62. The unique Realm Object Server user ID string identifying this user.
  63. */
  64. @property (nullable, nonatomic, readonly) NSString *identity;
  65. /**
  66. The URL of the authentication server this user will communicate with.
  67. */
  68. @property (nullable, nonatomic, readonly) NSURL *authenticationServer;
  69. /**
  70. Whether the user is a Realm Object Server administrator. Value reflects the
  71. state at the time of the last successful login of this user.
  72. */
  73. @property (nonatomic, readonly) BOOL isAdmin;
  74. /**
  75. The current state of the user.
  76. */
  77. @property (nonatomic, readonly) RLMSyncUserState state;
  78. #pragma mark - Lifecycle
  79. /**
  80. Create, log in, and asynchronously return a new user object, specifying a custom timeout for the network request.
  81. Credentials identifying the user must be passed in. The user becomes available in the completion block, at which point
  82. it is ready for use.
  83. */
  84. + (void)logInWithCredentials:(RLMSyncCredentials *)credentials
  85. authServerURL:(NSURL *)authServerURL
  86. timeout:(NSTimeInterval)timeout
  87. onCompletion:(RLMUserCompletionBlock)completion NS_REFINED_FOR_SWIFT;
  88. /**
  89. Create, log in, and asynchronously return a new user object. Credentials identifying the user must be passed in. The
  90. user becomes available in the completion block, at which point it is ready for use.
  91. */
  92. + (void)logInWithCredentials:(RLMSyncCredentials *)credentials
  93. authServerURL:(NSURL *)authServerURL
  94. onCompletion:(RLMUserCompletionBlock)completion
  95. NS_SWIFT_UNAVAILABLE("Use the full version of this API.");
  96. /**
  97. Log a user out, destroying their server state, unregistering them from the SDK,
  98. and removing any synced Realms associated with them from on-disk storage on
  99. next app launch. If the user is already logged out or in an error state, this
  100. method does nothing.
  101. This method should be called whenever the application is committed to not using
  102. a user again unless they are recreated.
  103. Failing to call this method may result in unused files and metadata needlessly
  104. taking up space.
  105. */
  106. - (void)logOut;
  107. #pragma mark - Sessions
  108. /**
  109. Retrieve a valid session object belonging to this user for a given URL, or `nil` if no such object exists.
  110. */
  111. - (nullable RLMSyncSession *)sessionForURL:(NSURL *)url;
  112. /**
  113. Retrieve all the valid sessions belonging to this user.
  114. */
  115. - (NSArray<RLMSyncSession *> *)allSessions;
  116. #pragma mark - Passwords
  117. /**
  118. Change this user's password asynchronously.
  119. @warning Changing a user's password using an authentication server that doesn't
  120. use HTTPS is a major security flaw, and should only be done while
  121. testing.
  122. @param newPassword The user's new password.
  123. @param completion Completion block invoked when login has completed or failed.
  124. The callback will be invoked on a background queue provided
  125. by `NSURLSession`.
  126. */
  127. - (void)changePassword:(NSString *)newPassword completion:(RLMPasswordChangeStatusBlock)completion;
  128. // This set of permissions APIs uses immutable `RLMSyncPermissionValue` objects to
  129. // retrieve and apply permissions. It is intended to replace the set of APIs which
  130. // directly access Realms and Realm model objects to work with permissions.
  131. #pragma mark - Value-based Permissions API
  132. /**
  133. Asynchronously retrieve all permissions associated with the user calling this method.
  134. The results will be returned through the callback block, or an error if the operation failed.
  135. The callback block will be run on the same thread the method was called on.
  136. @warning This method must be called from a thread with a currently active run loop. Unless
  137. you have manually configured a run loop on a side thread, this will usually be the
  138. main thread.
  139. @see `RLMSyncPermissionResults`
  140. */
  141. - (void)retrievePermissionsWithCallback:(RLMPermissionResultsBlock)callback;
  142. /**
  143. Apply a given permission.
  144. The operation will take place asynchronously, and the callback will be used to report whether
  145. the permission change succeeded or failed. The user calling this method must have the right
  146. to grant the given permission, or else the operation will fail.
  147. @see `RLMSyncPermissionValue`
  148. */
  149. - (void)applyPermission:(RLMSyncPermissionValue *)permission callback:(RLMPermissionStatusBlock)callback;
  150. /**
  151. Revoke a given permission.
  152. The operation will take place asynchronously, and the callback will be used to report whether
  153. the permission change succeeded or failed. The user calling this method must have the right
  154. to grant the given permission, or else the operation will fail.
  155. @see `RLMSyncPermissionValue`
  156. */
  157. - (void)revokePermission:(RLMSyncPermissionValue *)permission callback:(RLMPermissionStatusBlock)callback;
  158. // These permission APIs access Realms and Realm model objects representing
  159. // various permission states and actions, as well as standard Realm
  160. // affordances, to work with permissions. It is being deprecated in favor of
  161. // the `retrievePermissionsWithCallback:`, `applyPermission:callback:`, and
  162. // `revokePermission:callback:` APIs.
  163. #pragma mark - Realm Object-based Permissions API
  164. /**
  165. Returns an instance of the Management Realm owned by the user.
  166. This Realm can be used to control access permissions for Realms managed by the user.
  167. This includes granting other users access to Realms.
  168. */
  169. - (RLMRealm *)managementRealmWithError:(NSError **)error NS_REFINED_FOR_SWIFT;
  170. /**
  171. Returns an instance of the Permission Realm owned by the user.
  172. This read-only Realm contains `RLMSyncPermission` objects reflecting the
  173. synchronized Realms and permission details this user has access to.
  174. */
  175. - (RLMRealm *)permissionRealmWithError:(NSError **)error __deprecated_msg("Use `-retrievePermissionsWithCallback:`") NS_REFINED_FOR_SWIFT;
  176. #pragma mark - Miscellaneous
  177. /// :nodoc:
  178. - (instancetype)init __attribute__((unavailable("RLMSyncUser cannot be created directly")));
  179. /// :nodoc:
  180. + (instancetype)new __attribute__((unavailable("RLMSyncUser cannot be created directly")));
  181. NS_ASSUME_NONNULL_END
  182. @end