Skip to content

Commit 762d342

Browse files
safwanrahmanagjohnson
authored andcommitted
[Fix #3182] Better user deletion (#3214)
* [Fix #3182] Better user deletion * fixup according to comments * Delete user after user ask to get deleted * fixing lint
1 parent 0fb9b7e commit 762d342

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

readthedocs/core/signals.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import logging
66

77
from corsheaders import signals
8+
from django.conf import settings
9+
from django.db.models.signals import pre_delete
810
from django.dispatch import Signal
9-
from django.db.models import Q
11+
from django.db.models import Q, Count
12+
from django.dispatch import receiver
1013
from future.backports.urllib.parse import urlparse
1114

1215
from readthedocs.projects.models import Project, Domain
1316

14-
1517
log = logging.getLogger(__name__)
1618

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

6365
return False
6466

67+
68+
@receiver(pre_delete, sender=settings.AUTH_USER_MODEL)
69+
def delete_projects_and_organizations(sender, instance, *args, **kwargs):
70+
# Here we count the owner list from the projects that the user own
71+
# Then exclude the projects where there are more than one owner
72+
projects = instance.projects.all().annotate(num_users=Count('users')).exclude(num_users__gt=1)
73+
74+
# Here we count the users list from the organization that the user belong
75+
# Then exclude the organizations where there are more than one user
76+
oauth_organizations = (instance.oauth_organizations.annotate(num_users=Count('users'))
77+
.exclude(num_users__gt=1))
78+
79+
projects.delete()
80+
oauth_organizations.delete()
81+
82+
6583
signals.check_request_enabled.connect(decide_if_cors)

readthedocs/profiles/views.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,9 @@ def delete_account(request):
196196
if request.method == 'POST':
197197
form = UserDeleteForm(instance=request.user, data=request.POST)
198198
if form.is_valid():
199-
200-
# Do not delete the account permanently because it may create disaster
201-
# Inactive the user instead.
202-
request.user.is_active = False
203-
request.user.save()
199+
# Delete the user permanently
200+
# It will also delete some projects where he is the only owner
201+
request.user.delete()
204202
logout(request)
205203
messages.info(request, 'You have successfully deleted your account')
206204

readthedocs/templates/profiles/private/delete_account.html

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<form method="POST" action=".">
1313
{% csrf_token %}
1414
{{ form }}
15+
<div>
16+
<strong>{% trans "Be careful! This can not be undone!" %}</strong>
17+
</div>
1518
<input type="submit" name="submit" value="{% trans "Delete Account" %}" id="submit"/>
1619
</form>
1720
{% endblock %}

0 commit comments

Comments
 (0)