Skip to content

Commit

Permalink
Allow a user to delete their own account
Browse files Browse the repository at this point in the history
  • Loading branch information
peregrineshahin committed Jul 20, 2024
1 parent 12981ff commit ac7a704
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
76 changes: 75 additions & 1 deletion server/fishtest/templates/user.mak
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,42 @@
e.target.submit();
});
}

async function handleDeleteAccount() {
await DOMContentLoaded();
document.getElementById("password").addEventListener("input", (e) => {
const value = e.target.value;
const deleteAccBtn = document.getElementById("delete_account_btn");
const saveBtn = document.getElementById("save_btn");

if (value) {
deleteAccBtn.removeAttribute("disabled");
saveBtn.removeAttribute("disabled");
} else {
deleteAccBtn.setAttribute("disabled", "");
saveBtn.setAttribute("disabled", "");
}
});

document.getElementById("delete_own_account").addEventListener("input", (e) => {
const value = e.target.value;
const confirmDeleteBtn = document.getElementById("confirm_delete_btn");
if (value == "${user['username']}") {
confirmDeleteBtn.removeAttribute("disabled");
} else {
confirmDeleteBtn.setAttribute("disabled", "");
}
})

document.getElementById("delete_own_account").addEventListener("keypress", (e) => {
if (e.key === 'Enter') {
e.preventDefault();
}
})
}

handleGitHubToken();
handleDeleteAccount();
</script>
% else:
<script>
Expand Down Expand Up @@ -211,7 +246,46 @@
</div>
</div>
</div>
<button type="submit" class="btn btn-primary w-100">Save</button>
<button
id="save_btn"
type="submit"
class="btn btn-primary w-100 mb-2"
disabled
>Save</button>
<button
id="delete_account_btn"
type="button"
class="btn btn-danger w-100"
data-bs-toggle="modal"
data-bs-target="#delete_account_modal"
disabled
>Delete Your Account</button>

<div id="delete_account_modal" class="modal fade" tabindex="-1" aria-labelledby="delete_account_modal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirmation Required</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete your own account? Please type your username to confirm.</p>
<input
type="text"
id="delete_own_account"
name="delete_own_account"
class="form-control"
placeholder="Enter your username"
>
<div class="invalid-feedback">Username does not match. Please try again.</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button id="confirm_delete_btn" type="submit" class="btn btn-primary" disabled>Confirm</button>
</div>
</div>
</div>
</div>
% elif 'pending' in user and user['pending']:
<div class="alert alert-dark mb-3">
<label class="mb-2 h5">User Approval:</label>
Expand Down
18 changes: 12 additions & 6 deletions server/fishtest/userdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,24 @@ def save_user(self, user):
self.last_blocked_time = 0
self.clear_cache()

def remove_user(self, user, rejector):

def remove_user(self, user, rejector=""):
self.user_cache.delete_one({"_id": user["_id"]})
result = self.users.delete_one({"_id": user["_id"]})
if result.deleted_count > 0:
# User successfully deleted
self.last_pending_time = 0
self.clear_cache()
# logs rejected users to the server
print(
f"user: {user['username']} with email: {user['email']} was rejected by: {rejector}",
flush=True,
)
if rejector:
print(
f"user: {user['username']} with email: {user['email']} was rejected by: {rejector}",
flush=True,
)
else:
print(
f"user: {user['username']} with email: {user['email']} deleted their own account",
flush=True,
)
return True
else:
# User not found
Expand Down
10 changes: 10 additions & 0 deletions server/fishtest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ def user(request):
user_data = request.userdb.get_user(user_name)
if user_data is None:
raise HTTPNotFound()

if (
profile
and "delete_own_account" in request.POST
and request.POST["delete_own_account"] == user_name
):
removed = request.userdb.remove_user(user_data, rejector=None)
if removed:
logout(request)

if "user" in request.POST:
if profile:
old_password = request.params.get("old_password").strip()
Expand Down

0 comments on commit ac7a704

Please sign in to comment.