Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #3182] Better user deletion #3214

Merged
merged 4 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions readthedocs/core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import logging

from corsheaders import signals
from django.conf import settings
from django.db.models.signals import pre_delete
from django.dispatch import Signal
from django.db.models import Q
from django.db.models import Q, Count
from django.dispatch import receiver
from future.backports.urllib.parse import urlparse

from readthedocs.projects.models import Project, Domain


log = logging.getLogger(__name__)

WHITELIST_URLS = ['/api/v2/footer_html', '/api/v2/search', '/api/v2/docsearch']
Expand Down Expand Up @@ -62,4 +64,20 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen

return False


@receiver(pre_delete, sender=settings.AUTH_USER_MODEL)
def delete_projects_and_organizations(sender, instance, *args, **kwargs):
# Here we count the owner list from the projects that the user own
# Then exclude the projects where there are more than one owner
projects = instance.projects.all().annotate(num_users=Count('users')).exclude(num_users__gt=1)

# Here we count the users list from the organization that the user belong
# Then exclude the organizations where there are more than one user
oauth_organizations = (instance.oauth_organizations.annotate(num_users=Count('users'))
.exclude(num_users__gt=1))

projects.delete()
oauth_organizations.delete()


signals.check_request_enabled.connect(decide_if_cors)
8 changes: 3 additions & 5 deletions readthedocs/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,9 @@ def delete_account(request):
if request.method == 'POST':
form = UserDeleteForm(instance=request.user, data=request.POST)
if form.is_valid():

# Do not delete the account permanently because it may create disaster
# Inactive the user instead.
request.user.is_active = False
request.user.save()
# Delete the user permanently
# It will also delete some projects where he is the only owner
request.user.delete()
logout(request)
messages.info(request, 'You have successfully deleted your account')

Expand Down
3 changes: 3 additions & 0 deletions readthedocs/templates/profiles/private/delete_account.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<form method="POST" action=".">
{% csrf_token %}
{{ form }}
<div>
<strong>{% trans "Be careful! This can not be undone!" %}</strong>
</div>
<input type="submit" name="submit" value="{% trans "Delete Account" %}" id="submit"/>
</form>
{% endblock %}