|
21 | 21 | from firebase_admin import _auth_utils
|
22 | 22 | from firebase_admin import _http_client
|
23 | 23 | from firebase_admin import _token_gen
|
| 24 | +from firebase_admin import _user_identifier |
24 | 25 | from firebase_admin import _user_import
|
25 | 26 | from firebase_admin import _user_mgt
|
26 | 27 |
|
@@ -182,6 +183,56 @@ def get_user_by_phone_number(self, phone_number):
|
182 | 183 | response = self._user_manager.get_user(phone_number=phone_number)
|
183 | 184 | return _user_mgt.UserRecord(response)
|
184 | 185 |
|
| 186 | + def get_users(self, identifiers): |
| 187 | + """Gets the user data corresponding to the specified identifiers. |
| 188 | +
|
| 189 | + There are no ordering guarantees; in particular, the nth entry in the |
| 190 | + result list is not guaranteed to correspond to the nth entry in the input |
| 191 | + parameters list. |
| 192 | +
|
| 193 | + A maximum of 100 identifiers may be supplied. If more than 100 |
| 194 | + identifiers are supplied, this method raises a `ValueError`. |
| 195 | +
|
| 196 | + Args: |
| 197 | + identifiers (list[Identifier]): A list of ``Identifier`` instances used |
| 198 | + to indicate which user records should be returned. Must have <= 100 |
| 199 | + entries. |
| 200 | +
|
| 201 | + Returns: |
| 202 | + GetUsersResult: A ``GetUsersResult`` instance corresponding to the |
| 203 | + specified identifiers. |
| 204 | +
|
| 205 | + Raises: |
| 206 | + ValueError: If any of the identifiers are invalid or if more than 100 |
| 207 | + identifiers are specified. |
| 208 | + """ |
| 209 | + response = self._user_manager.get_users(identifiers=identifiers) |
| 210 | + |
| 211 | + def _matches(identifier, user_record): |
| 212 | + if isinstance(identifier, _user_identifier.UidIdentifier): |
| 213 | + return identifier.uid == user_record.uid |
| 214 | + if isinstance(identifier, _user_identifier.EmailIdentifier): |
| 215 | + return identifier.email == user_record.email |
| 216 | + if isinstance(identifier, _user_identifier.PhoneIdentifier): |
| 217 | + return identifier.phone_number == user_record.phone_number |
| 218 | + if isinstance(identifier, _user_identifier.ProviderIdentifier): |
| 219 | + return next(( |
| 220 | + True |
| 221 | + for user_info in user_record.provider_data |
| 222 | + if identifier.provider_id == user_info.provider_id |
| 223 | + and identifier.provider_uid == user_info.uid |
| 224 | + ), False) |
| 225 | + raise TypeError("Unexpected type: {}".format(type(identifier))) |
| 226 | + |
| 227 | + def _is_user_found(identifier, user_records): |
| 228 | + return any(_matches(identifier, user_record) for user_record in user_records) |
| 229 | + |
| 230 | + users = [_user_mgt.UserRecord(user) for user in response] |
| 231 | + not_found = [ |
| 232 | + identifier for identifier in identifiers if not _is_user_found(identifier, users)] |
| 233 | + |
| 234 | + return _user_mgt.GetUsersResult(users=users, not_found=not_found) |
| 235 | + |
185 | 236 | def list_users(self, page_token=None, max_results=_user_mgt.MAX_LIST_USERS_RESULTS):
|
186 | 237 | """Retrieves a page of user accounts from a Firebase project.
|
187 | 238 |
|
@@ -306,6 +357,33 @@ def delete_user(self, uid):
|
306 | 357 | """
|
307 | 358 | self._user_manager.delete_user(uid)
|
308 | 359 |
|
| 360 | + def delete_users(self, uids): |
| 361 | + """Deletes the users specified by the given identifiers. |
| 362 | +
|
| 363 | + Deleting a non-existing user does not generate an error (the method is |
| 364 | + idempotent.) Non-existing users are considered to be successfully |
| 365 | + deleted and are therefore included in the |
| 366 | + `DeleteUserResult.success_count` value. |
| 367 | +
|
| 368 | + A maximum of 1000 identifiers may be supplied. If more than 1000 |
| 369 | + identifiers are supplied, this method raises a `ValueError`. |
| 370 | +
|
| 371 | + Args: |
| 372 | + uids: A list of strings indicating the uids of the users to be deleted. |
| 373 | + Must have <= 1000 entries. |
| 374 | +
|
| 375 | + Returns: |
| 376 | + DeleteUsersResult: The total number of successful/failed deletions, as |
| 377 | + well as the array of errors that correspond to the failed |
| 378 | + deletions. |
| 379 | +
|
| 380 | + Raises: |
| 381 | + ValueError: If any of the identifiers are invalid or if more than 1000 |
| 382 | + identifiers are specified. |
| 383 | + """ |
| 384 | + result = self._user_manager.delete_users(uids, force_delete=True) |
| 385 | + return _user_mgt.DeleteUsersResult(result, len(uids)) |
| 386 | + |
309 | 387 | def import_users(self, users, hash_alg=None):
|
310 | 388 | """Imports the specified list of users into Firebase Auth.
|
311 | 389 |
|
|
0 commit comments