RLMSyncUser.h 12 KB

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