-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.sh
More file actions
executable file
·88 lines (72 loc) · 2.74 KB
/
delete.sh
File metadata and controls
executable file
·88 lines (72 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Set paths to mongo commands
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/snap/wekan/current/lib/x86_64-linux-gnu
export PATH="/snap/wekan/current/bin:$PATH"
# Check if a pattern was provided
if [ -z "$1" ]; then
echo "Usage: $0 <username_pattern>"
exit 1
fi
PATTERN=$1
# Configuration
DB_NAME="wekan"
DB_HOST="127.0.0.1"
DB_PORT="27019"
# 1. Identify users to be deleted
echo "Searching for users matching pattern '$PATTERN'..."
USER_DATA=$(mongo --host $DB_HOST --port $DB_PORT $DB_NAME --quiet --eval "
var users = db.users.find(
{ 'username': { \$regex: '$PATTERN', \$options: 'i' } },
{ '_id': 1, 'username': 1 }
).toArray();
print(JSON.stringify(users));
")
if [ "$USER_DATA" == "[]" ] || [ -z "$USER_DATA" ]; then
echo "No users found matching that pattern."
exit 0
fi
echo "The following users (and their data in boards/cards/avatars) will be DELETED:"
echo "$USER_DATA" | python3 -c "import sys, json; [print(f' - {u[\"username\"]} (ID: {u[\"_id\"]})') for u in json.load(sys.stdin)]"
echo "------------------------------------------------------------------"
# 2. Safety Confirmation
read -p "Are you absolutely sure? This will scrub these users from all boards and cards. (y/n): " CONFIRM
if [[ ! $CONFIRM =~ ^[Yy]$ ]]; then
echo "Deletion cancelled."
exit 0
fi
# 3. Execute Multi-Collection Cleanup
echo "Executing global cleanup..."
mongo --host $DB_HOST --port $DB_PORT $DB_NAME --quiet --eval "
var usersToDelete = $USER_DATA;
usersToDelete.forEach(function(user) {
var uid = user._id;
print('Processing ' + user.username + ' (' + uid + ')...');
// A. Remove from Boards members
var bRes = db.boards.update(
{ 'members.userId': uid },
{ \$pull: { 'members': { 'userId': uid } } },
{ multi: true }
);
print(' - Removed from ' + bRes.nModified + ' boards.');
// B. Remove from Cards members and assignees
var cRes = db.cards.update(
{ \$or: [ { 'members': uid }, { 'assignees': uid } ] },
{ \$pull: { 'members': uid, 'assignees': uid } },
{ multi: true }
);
print(' - Removed from ' + cRes.nModified + ' cards.');
// C. Delete Avatars (Files and Chunks)
// CollectionFS stores metadata in avatars.files and data in avatars.chunks
var avatars = db['avatars.files'].find({ 'userId': uid }).toArray();
avatars.forEach(function(av) {
db['avatars.chunks'].remove({ 'files_id': av._id });
db['avatars.files'].remove({ '_id': av._id });
});
print(' - Deleted ' + avatars.length + ' avatar files.');
// D. Final deletion from Users collection
db.users.remove({ '_id': uid });
print(' - User account deleted.');
print('-----------------------------------------');
});
"
echo "Cleanup complete."