Skip to content

Commit

Permalink
feat: add new endpoint to unlink the logged in user
Browse files Browse the repository at this point in the history
  • Loading branch information
jajjibhai008 committed Nov 1, 2024
1 parent 760694e commit fa6156f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Unreleased
----------
* nothing unreleased

[4.31.0]
--------
* feat: add new endpoint to unlink the logged in user.

[4.30.1]
--------
* fix: serialize best_mode_for_course_run field in DefaultEnterpriseEnrollmentIntentionSerializer.
Expand Down
9 changes: 9 additions & 0 deletions consent/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ def has_permission(self, request, view):
return True

return super().has_permission(request, view)


class IsUseremailInRequest(permissions.BasePermission):
"""
Permission that checks to see if the request user email matches the user email indicated in the request body.
"""

def has_permission(self, request, view):
return request.user.email == get_request_value(request, 'user_email', '')

Check warning on line 38 in consent/api/permissions.py

View check run for this annotation

Codecov / codecov/patch

consent/api/permissions.py#L38

Added line #L38 was not covered by tests
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.30.1"
__version__ = "4.31.0"
15 changes: 15 additions & 0 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,21 @@ class EnterpriseCustomerUnlinkUsersSerializer(serializers.Serializer):
)


class EnterpriseCustomerUnlinkSelfUserSerializer(serializers.Serializer):
"""
Serializer for the ``EnterpriseCustomerViewSet`` unlink_self action.
"""

user_email = serializers.EmailField(
allow_blank=False,
)

is_relinkable = serializers.BooleanField(
required=False,
default=False,
)


class EnterpriseCatalogQuerySerializer(serializers.ModelSerializer):
"""
Serializer for the ``EnterpriseCatalogQuery`` model.
Expand Down
31 changes: 30 additions & 1 deletion enterprise/api/v1/views/enterprise_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
track_enrollment,
validate_email_to_link,
)
from consent.api.permissions import IsUseremailInRequest

User = auth.get_user_model()

Expand Down Expand Up @@ -460,7 +461,6 @@ def unlink_users(self, request, pk=None): # pylint: disable=unused-argument
"""
Unlinks users with the given emails from the enterprise.
"""

serializer = serializers.EnterpriseCustomerUnlinkUsersSerializer(
data=request.data
)
Expand All @@ -487,3 +487,32 @@ def unlink_users(self, request, pk=None): # pylint: disable=unused-argument
raise UnlinkUserFromEnterpriseError(msg) from exc

return Response(status=HTTP_200_OK)

@action(methods=['post'], detail=True, permission_classes=[permissions.IsAuthenticated, IsUseremailInRequest])
def unlink_self(self, request, pk=None): # pylint: disable=unused-argument
"""
Unlink request user from the enterprise.
"""
serializer = serializers.EnterpriseCustomerUnlinkSelfUserSerializer(

Check warning on line 496 in enterprise/api/v1/views/enterprise_customer.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer.py#L496

Added line #L496 was not covered by tests
data=request.data
)

serializer.is_valid(raise_exception=True)
enterprise_customer = self.get_object()
user_email = serializer.data.get('user_email')
is_relinkable = serializer.data.get('is_relinkable', True)

Check warning on line 503 in enterprise/api/v1/views/enterprise_customer.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer.py#L500-L503

Added lines #L500 - L503 were not covered by tests

try:
models.EnterpriseCustomerUser.objects.unlink_user(

Check warning on line 506 in enterprise/api/v1/views/enterprise_customer.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer.py#L505-L506

Added lines #L505 - L506 were not covered by tests
enterprise_customer=enterprise_customer, user_email=user_email, is_relinkable=is_relinkable
)
except (models.EnterpriseCustomerUser.DoesNotExist, models.PendingEnterpriseCustomerUser.DoesNotExist):
msg = "[UNLINK_SELF] User with email {} does not exist in enterprise {}.".format(

Check warning on line 510 in enterprise/api/v1/views/enterprise_customer.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer.py#L510

Added line #L510 was not covered by tests
user_email, enterprise_customer
)
LOGGER.warning(msg)
except Exception as exc:
msg = "[UNLINK_SELF] Could not unlink {} from {}".format(user_email, enterprise_customer)
raise UnlinkUserFromEnterpriseError(msg) from exc

Check warning on line 516 in enterprise/api/v1/views/enterprise_customer.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer.py#L513-L516

Added lines #L513 - L516 were not covered by tests

return Response(status=HTTP_200_OK)

Check warning on line 518 in enterprise/api/v1/views/enterprise_customer.py

View check run for this annotation

Codecov / codecov/patch

enterprise/api/v1/views/enterprise_customer.py#L518

Added line #L518 was not covered by tests

0 comments on commit fa6156f

Please sign in to comment.