diff options
Diffstat (limited to 'src/mailman/rest/users.py')
| -rw-r--r-- | src/mailman/rest/users.py | 54 |
1 files changed, 18 insertions, 36 deletions
diff --git a/src/mailman/rest/users.py b/src/mailman/rest/users.py index 6e86c395b..017a09525 100644 --- a/src/mailman/rest/users.py +++ b/src/mailman/rest/users.py @@ -40,7 +40,6 @@ from mailman.rest.preferences import Preferences from mailman.rest.validator import ( PatchValidator, Validator, list_of_strings_validator) from passlib.utils import generate_password as generate -from uuid import UUID from zope.component import getUtility @@ -117,9 +116,9 @@ def create_user(arguments, request, response): password = generate(int(config.passwords.password_length)) user.password = config.password_context.encrypt(password) user.is_server_owner = is_server_owner - api_version = request.context['api_version'] - user_id = getattr(user.user_id, 'int' if api_version == '3.0' else 'hex') - location = path_to('users/{}'.format(user_id), api_version) + api = request.context['api'] + user_id = api.from_uuid(user.user_id) + location = path_to('users/{}'.format(user_id), api.version) created(response, location) return user @@ -128,17 +127,13 @@ def create_user(arguments, request, response): class _UserBase(CollectionMixin): """Shared base class for user representations.""" - def _get_uuid(self, user): - return getattr(user.user_id, - 'int' if self.api_version == '3.0' else 'hex') - def _resource_as_dict(self, user): """See `CollectionMixin`.""" # The canonical URL for a user is their unique user id, although we # can always look up a user based on any registered and validated # email address associated with their account. The user id is a UUID, # but we serialize its integer equivalent. - user_id = self._get_uuid(user) + user_id = self.api.from_uuid(user.user_id) resource = dict( created_on=user.created_on, is_server_owner=user.is_server_owner, @@ -182,9 +177,11 @@ class AllUsers(_UserBase): class AUser(_UserBase): """A user.""" - def __init__(self, api_version, user_identifier): + def __init__(self, api, user_identifier): """Get a user by various type of identifiers. + :param api: The REST API object. + :type api: IAPI :param user_identifier: The identifier used to retrieve the user. The identifier may either be an email address controlled by the user or the UUID of the user. The type of identifier is auto-detected @@ -193,7 +190,7 @@ class AUser(_UserBase): API 3.0 are integers, while in 3.1 are hex. :type user_identifier: string """ - self.api_version = api_version + self.api = api user_manager = getUtility(IUserManager) if '@' in user_identifier: self._user = user_manager.get_user(user_identifier) @@ -201,10 +198,7 @@ class AUser(_UserBase): # The identifier is the string representation of a UUID, either an # int in API 3.0 or a hex in API 3.1. try: - if api_version == '3.0': - user_id = UUID(int=int(user_identifier)) - else: - user_id = UUID(hex=user_identifier) + user_id = api.to_uuid(user_identifier) except ValueError: self._user = None else: @@ -244,7 +238,7 @@ class AUser(_UserBase): return NotFound(), [] child = Preferences( self._user.preferences, - 'users/{}'.format(self._get_uuid(self._user))) + 'users/{}'.format(self.api.from_uuid(self._user.user_id))) return child, [] def on_patch(self, request, response): @@ -319,14 +313,14 @@ class AddressUser(_UserBase): if self._user: conflict(response) return - api_version = request.context['api_version'] + api = request.context['api'] # When creating a linked user by POSTing, the user either must already # exist, or it can be automatically created, if the auto_create flag # is given and true (if missing, it defaults to true). However, in # this case we do not accept 'email' as a POST field. fields = CREATION_FIELDS.copy() del fields['email'] - fields['user_id'] = (int if api_version == '3.0' else str) + fields['user_id'] = api.to_uuid fields['auto_create'] = as_boolean fields['_optional'] = fields['_optional'] + ( 'user_id', 'auto_create', 'is_server_owner') @@ -338,16 +332,10 @@ class AddressUser(_UserBase): return user_manager = getUtility(IUserManager) if 'user_id' in arguments: - raw_uid = arguments['user_id'] - kws = {('int' if api_version == '3.0' else 'hex'): raw_uid} - try: - user_id = UUID(**kws) - except ValueError as error: - bad_request(response, str(error)) - return + user_id = arguments['user_id'] user = user_manager.get_user_by_id(user_id) if user is None: - not_found(response, b'No user with ID {}'.format(raw_uid)) + not_found(response, b'No user with ID {}'.format(user_id)) return okay(response) else: @@ -364,12 +352,12 @@ class AddressUser(_UserBase): def on_put(self, request, response): """Set or replace the addresses's user.""" - api_version = request.context['api_version'] + api = request.context['api'] if self._user: self._user.unlink(self._address) # Process post data and check for an existing user. fields = CREATION_FIELDS.copy() - fields['user_id'] = (int if api_version == '3.0' else str) + fields['user_id'] = api.to_uuid fields['_optional'] = fields['_optional'] + ( 'user_id', 'email', 'is_server_owner') try: @@ -380,16 +368,10 @@ class AddressUser(_UserBase): return user_manager = getUtility(IUserManager) if 'user_id' in arguments: - raw_uid = arguments['user_id'] - kws = {('int' if api_version == '3.0' else 'hex'): raw_uid} - try: - user_id = UUID(**kws) - except ValueError as error: - bad_request(response, str(error)) - return + user_id = arguments['user_id'] user = user_manager.get_user_by_id(user_id) if user is None: - not_found(response, b'No user with ID {}'.format(raw_uid)) + not_found(response, b'No user with ID {}'.format(user_id)) return okay(response) else: |
