"""Logic for applying permissions to user objects.""" from sound_recordings.constants.access import ACCESS_ANALYTICS from sound_recordings.permissions.analytics import has_analytics from sound_recordings.services.ows_features import get_mobile_features_parallel def apply_permissions(users): """Apply 'access' field to all users.""" return list(map(lambda user: _apply_user_permissions(user), users)) def apply_features(users): """Apply 'features' field to all users.""" user_ids = list(map(lambda u: u["user_id"], users)) features = get_mobile_features_parallel(user_ids) return list(map(lambda u: {**u, "features": features[u["user_id"]]}, users)) def _apply_user_permissions(user): """Apply 'access' field to user.""" account_type = user.get("account_type") account_id = user.get("account_id") user_id = user.get("user_id") # Map access values to booleans indicating whether they're enabled. access_map = {ACCESS_ANALYTICS: has_analytics(account_type, account_id, user_id)} return {**user, "access": [key for key, allowed in access_map.items() if allowed]}