RLMSyncUser.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #import "RLMSyncCredentials.h"
  20. @class RLMSyncUser, RLMSyncUserInfo, RLMSyncCredentials, RLMSyncPermissionValue, RLMSyncPermissionResults, RLMSyncSession, RLMRealm;
  21. /**
  22. The state of the user object.
  23. */
  24. typedef NS_ENUM(NSUInteger, RLMSyncUserState) {
  25. /// The user is logged out. Call `logInWithCredentials:...` with valid credentials to log the user back in.
  26. RLMSyncUserStateLoggedOut,
  27. /// The user is logged in, and any Realms associated with it are syncing with the Realm Object Server.
  28. RLMSyncUserStateActive,
  29. /// The user has encountered a fatal error state, and cannot be used.
  30. RLMSyncUserStateError,
  31. };
  32. /// A block type used for APIs which asynchronously vend an `RLMSyncUser`.
  33. typedef void(^RLMUserCompletionBlock)(RLMSyncUser * _Nullable, NSError * _Nullable);
  34. /// A block type used to report the status of a password change operation.
  35. /// If the `NSError` argument is nil, the operation succeeded.
  36. typedef void(^RLMPasswordChangeStatusBlock)(NSError * _Nullable);
  37. /// A block type used to report the status of a permission apply or revoke operation.
  38. /// If the `NSError` argument is nil, the operation succeeded.
  39. typedef void(^RLMPermissionStatusBlock)(NSError * _Nullable);
  40. /// A block type used to asynchronously report results of a permissions get operation.
  41. /// Exactly one of the two arguments will be populated.
  42. typedef void(^RLMPermissionResultsBlock)(RLMSyncPermissionResults * _Nullable, NSError * _Nullable);
  43. /// A block type used to asynchronously report results of a user info retrieval.
  44. /// Exactly one of the two arguments will be populated.
  45. typedef void(^RLMRetrieveUserBlock)(RLMSyncUserInfo * _Nullable, NSError * _Nullable);
  46. /// A block type used to report an error related to a specific user.
  47. typedef void(^RLMUserErrorReportingBlock)(RLMSyncUser * _Nonnull, NSError * _Nonnull);
  48. NS_ASSUME_NONNULL_BEGIN
  49. /**
  50. A `RLMSyncUser` instance represents a single Realm Object Server user account.
  51. A user may have one or more credentials associated with it. These credentials
  52. uniquely identify the user to the authentication provider, and are used to sign
  53. into a Realm Object Server user account.
  54. Note that user objects are only vended out via SDK APIs, and cannot be directly
  55. initialized. User objects can be accessed from any thread.
  56. */
  57. @interface RLMSyncUser : NSObject
  58. /**
  59. A dictionary of all valid, logged-in user identities corresponding to their user objects.
  60. */
  61. + (NSDictionary<NSString *, RLMSyncUser *> *)allUsers NS_REFINED_FOR_SWIFT;
  62. /**
  63. The logged-in user. `nil` if none exists.
  64. @warning Throws an exception if more than one logged-in user exists.
  65. */
  66. + (nullable RLMSyncUser *)currentUser NS_REFINED_FOR_SWIFT;
  67. /**
  68. The unique Realm Object Server user ID string identifying this user.
  69. */
  70. @property (nullable, nonatomic, readonly) NSString *identity;
  71. /**
  72. The URL of the authentication server this user will communicate with.
  73. */
  74. @property (nullable, nonatomic, readonly) NSURL *authenticationServer;
  75. /**
  76. Whether the user is a Realm Object Server administrator. Value reflects the
  77. state at the time of the last successful login of this user.
  78. */
  79. @property (nonatomic, readonly) BOOL isAdmin;
  80. /**
  81. The current state of the user.
  82. */
  83. @property (nonatomic, readonly) RLMSyncUserState state;
  84. #pragma mark - Lifecycle
  85. /**
  86. Create, log in, and asynchronously return a new user object, specifying a custom timeout for the network request.
  87. Credentials identifying the user must be passed in. The user becomes available in the completion block, at which point
  88. it is ready for use.
  89. */
  90. + (void)logInWithCredentials:(RLMSyncCredentials *)credentials
  91. authServerURL:(NSURL *)authServerURL
  92. timeout:(NSTimeInterval)timeout
  93. onCompletion:(RLMUserCompletionBlock)completion NS_REFINED_FOR_SWIFT;
  94. /**
  95. Create, log in, and asynchronously return a new user object. Credentials identifying the user must be passed in. The
  96. user becomes available in the completion block, at which point it is ready for use.
  97. */
  98. + (void)logInWithCredentials:(RLMSyncCredentials *)credentials
  99. authServerURL:(NSURL *)authServerURL
  100. onCompletion:(RLMUserCompletionBlock)completion
  101. NS_SWIFT_UNAVAILABLE("Use the full version of this API.");
  102. /**
  103. Log a user out, destroying their server state, unregistering them from the SDK,
  104. and removing any synced Realms associated with them from on-disk storage on
  105. next app launch. If the user is already logged out or in an error state, this
  106. method does nothing.
  107. This method should be called whenever the application is committed to not using
  108. a user again unless they are recreated.
  109. Failing to call this method may result in unused files and metadata needlessly
  110. taking up space.
  111. */
  112. - (void)logOut;
  113. /**
  114. An optional error handler which can be set to notify the host application when
  115. the user encounters an error. Errors reported by this error handler are always
  116. `RLMSyncAuthError`s.
  117. @note Check for `RLMSyncAuthErrorInvalidAccessToken` to see if the user has
  118. been remotely logged out because its refresh token expired, or because the
  119. third party authentication service providing the user's identity has
  120. logged the user out.
  121. @warning Regardless of whether an error handler is defined, certain user errors
  122. will automatically cause the user to enter the logged out state.
  123. */
  124. @property (nullable, nonatomic) RLMUserErrorReportingBlock errorHandler NS_REFINED_FOR_SWIFT;
  125. #pragma mark - Sessions
  126. /**
  127. Retrieve a valid session object belonging to this user for a given URL, or `nil` if no such object exists.
  128. */
  129. - (nullable RLMSyncSession *)sessionForURL:(NSURL *)url;
  130. /**
  131. Retrieve all the valid sessions belonging to this user.
  132. */
  133. - (NSArray<RLMSyncSession *> *)allSessions;
  134. #pragma mark - Passwords
  135. /**
  136. Change this user's password asynchronously.
  137. @warning Changing a user's password using an authentication server that doesn't
  138. use HTTPS is a major security flaw, and should only be done while
  139. testing.
  140. @param newPassword The user's new password.
  141. @param completion Completion block invoked when login has completed or failed.
  142. The callback will be invoked on a background queue provided
  143. by `NSURLSession`.
  144. */
  145. - (void)changePassword:(NSString *)newPassword completion:(RLMPasswordChangeStatusBlock)completion;
  146. /**
  147. Change an arbitrary user's password asynchronously.
  148. @note The current user must be an admin user for this operation to succeed.
  149. @warning Changing a user's password using an authentication server that doesn't
  150. use HTTPS is a major security flaw, and should only be done while
  151. testing.
  152. @param newPassword The user's new password.
  153. @param userID The identity of the user whose password should be changed.
  154. @param completion Completion block invoked when login has completed or failed.
  155. The callback will be invoked on a background queue provided
  156. by `NSURLSession`.
  157. */
  158. - (void)changePassword:(NSString *)newPassword forUserID:(NSString *)userID completion:(RLMPasswordChangeStatusBlock)completion;
  159. #pragma mark - Administrator API
  160. /**
  161. Given a Realm Object Server authentication provider and a provider identifier for a user
  162. (for example, a username), look up and return user information for that user.
  163. @param providerUserIdentity The username or identity of the user as issued by the authentication provider.
  164. In most cases this is different from the Realm Object Server-issued identity.
  165. @param provider The authentication provider that manages the user whose information is desired.
  166. @param completion Completion block invoked when request has completed or failed.
  167. The callback will be invoked on a background queue provided
  168. by `NSURLSession`.
  169. */
  170. - (void)retrieveInfoForUser:(NSString *)providerUserIdentity
  171. identityProvider:(RLMIdentityProvider)provider
  172. completion:(RLMRetrieveUserBlock)completion;
  173. // This set of permissions APIs uses immutable `RLMSyncPermissionValue` objects to
  174. // retrieve and apply permissions. It is intended to replace the set of APIs which
  175. // directly access Realms and Realm model objects to work with permissions.
  176. #pragma mark - Value-based Permissions API
  177. /**
  178. Asynchronously retrieve all permissions associated with the user calling this method.
  179. The results will be returned through the callback block, or an error if the operation failed.
  180. The callback block will be run on the same thread the method was called on.
  181. @warning This method must be called from a thread with a currently active run loop. Unless
  182. you have manually configured a run loop on a side thread, this will usually be the
  183. main thread.
  184. @see `RLMSyncPermissionResults`
  185. */
  186. - (void)retrievePermissionsWithCallback:(RLMPermissionResultsBlock)callback;
  187. /**
  188. Apply a given permission.
  189. The operation will take place asynchronously, and the callback will be used to report whether
  190. the permission change succeeded or failed. The user calling this method must have the right
  191. to grant the given permission, or else the operation will fail.
  192. @see `RLMSyncPermissionValue`
  193. */
  194. - (void)applyPermission:(RLMSyncPermissionValue *)permission callback:(RLMPermissionStatusBlock)callback;
  195. /**
  196. Revoke a given permission.
  197. The operation will take place asynchronously, and the callback will be used to report whether
  198. the permission change succeeded or failed. The user calling this method must have the right
  199. to grant the given permission, or else the operation will fail.
  200. @see `RLMSyncPermissionValue`
  201. */
  202. - (void)revokePermission:(RLMSyncPermissionValue *)permission callback:(RLMPermissionStatusBlock)callback;
  203. // These permission APIs access Realms and Realm model objects representing
  204. // various permission states and actions, as well as standard Realm
  205. // affordances, to work with permissions. It is being deprecated in favor of
  206. // the `retrievePermissionsWithCallback:`, `applyPermission:callback:`, and
  207. // `revokePermission:callback:` APIs.
  208. #pragma mark - Realm Object-based Permissions API
  209. /**
  210. Returns an instance of the Management Realm owned by the user.
  211. This Realm can be used to control access permissions for Realms managed by the user.
  212. This includes granting other users access to Realms.
  213. */
  214. - (RLMRealm *)managementRealmWithError:(NSError **)error NS_REFINED_FOR_SWIFT;
  215. /**
  216. Returns an instance of the Permission Realm owned by the user.
  217. This read-only Realm contains `RLMSyncPermission` objects reflecting the
  218. synchronized Realms and permission details this user has access to.
  219. */
  220. - (RLMRealm *)permissionRealmWithError:(NSError **)error __deprecated_msg("Use `-retrievePermissionsWithCallback:`") NS_REFINED_FOR_SWIFT;
  221. #pragma mark - Miscellaneous
  222. /// :nodoc:
  223. - (instancetype)init __attribute__((unavailable("RLMSyncUser cannot be created directly")));
  224. /// :nodoc:
  225. + (instancetype)new __attribute__((unavailable("RLMSyncUser cannot be created directly")));
  226. @end
  227. /**
  228. A data object representing information about a user that was retrieved from a user lookup call.
  229. */
  230. @interface RLMSyncUserInfo : NSObject
  231. /**
  232. The authentication provider which manages the user represented by this user info instance.
  233. */
  234. @property (nonatomic, readonly) RLMIdentityProvider provider;
  235. /**
  236. The username or identity issued to this user by the authentication provider.
  237. */
  238. @property (nonatomic, readonly) NSString *providerUserIdentity;
  239. /**
  240. The identity issued to this user by the Realm Object Server.
  241. */
  242. @property (nonatomic, readonly) NSString *identity;
  243. /**
  244. Whether the user is flagged on the Realm Object Server as an administrator.
  245. */
  246. @property (nonatomic, readonly) BOOL isAdmin;
  247. #pragma mark - Miscellaneous
  248. /// :nodoc:
  249. - (instancetype)init __attribute__((unavailable("RLMSyncUserInfo cannot be created directly")));
  250. /// :nodoc:
  251. + (instancetype)new __attribute__((unavailable("RLMSyncUserInfo cannot be created directly")));
  252. @end
  253. NS_ASSUME_NONNULL_END