Skip to content

Commit 0a11c01

Browse files
Modified checkusers.sh to move expired users' data to trash
1 parent 91a8622 commit 0a11c01

1 file changed

Lines changed: 43 additions & 7 deletions

File tree

bin/checkusers.sh

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# It is vital to have RUN env > /etc/environment in Dockerfile so that
44
# cronjob can have access to env variables from docker
55
. /home/hackmd/cron.env
6+
UPLOADS_DIR="/home/hackmd/app/public/uploads"
7+
TRASH_DIR="/home/hackmd/trash"
68
isActive() {
79
if ! ldapres=$(ldapsearch -x -H $CMD_LDAP_URL -D "$CMD_LDAP_BINDDN" -w $CMD_LDAP_BINDCREDENTIALS -b $CMD_LDAP_SEARCHBASE $query 2>/dev/null); then
810
echo "Ldap is DOWN. Exiting..."
@@ -22,20 +24,48 @@ DELETE FROM "Users" WHERE profileid = '${profileid}';
2224
SQL
2325
}
2426
deleteNotes(){
25-
# Deleting all user's notes from postgres database
26-
local id="$1"
27+
# Deleting all user's notes from postgres database and moving them to
28+
# trash can
29+
shortids=$(psql $CMD_DB_URL -At<<-SQL
30+
SELECT shortid FROM "Notes" where "ownerId" = '${id}';
31+
SQL
32+
)
33+
while IFS=' ' read shortid; do
34+
title=$(psql $CMD_DB_URL -At<<-SQL
35+
SELECT content FROM "Notes" where shortid = '${shortid}';
36+
SQL
37+
)
38+
safe_title=$(echo "$title" | tr -cd '[:alnum:]._-')
39+
filename="${USER_TRASH_DIR}/${safe_title}.md"
40+
41+
content=$(psql $CMD_DB_URL -At<<-SQL
42+
SELECT content FROM "Notes" WHERE shortid = '${shortid}';
43+
SQL
44+
)
45+
46+
echo "$content" > "$filename"
47+
done<<<"$shortids"
48+
2749
psql $CMD_DB_URL <<-SQL
2850
DELETE FROM "Notes" where "ownerId" = '${id}';
2951
SQL
3052
}
3153
deleteFiles(){
3254
# Deleting all user's uploads from location /home/hackmd/app/public/uploads
33-
local id="$1"
3455
# The files are saved as upload_{user.id}_{random letters}.{jpg,png,...}
35-
find /home/hackmd/app/public/uploads -type f -name "upload_${id}_*" -delete; 2>&1
56+
find $UPLOADS_DIR -type f -name "upload_${id}_*" -exec mv {} "$USER_TRASH_DIR" \; 2>&1
57+
}
58+
actuallyDeleteFiles(){
59+
# Deleting all user's uploads from location /home/hackmd/app/public/uploads
60+
# The files are saved as upload_{user.id}_{random letters}.{jpg,png,...}
61+
62+
echo "Removing old trash files"
63+
find $TRASH_DIR -mindepth 1 -maxdepth 1 -type d -mtime +90 -exec rm -rf {} \; 2>&1
3664
}
3765
# Write down the date-time
3866
date
67+
# Removing old trash files
68+
actuallyDeleteFiles
3969
allusers=$(psql $CMD_DB_URL -t -c 'SELECT profileid FROM "Users";')
4070
if [ -z "$allusers" ]; then
4171
echo "Ha Ha nothing to delete"
@@ -55,10 +85,16 @@ SQL
5585
echo "The account $uid is active"
5686
else
5787
echo "The account $uid must be destroyed"
58-
echo "Beggining destruction..."
88+
# This will be the user specific trash can
89+
USER_TRASH_DIR=${TRASH_DIR}/${uid}
90+
echo "Moving to Trash ${USER_TRASH_DIR}..."
91+
mkdir -p $USER_TRASH_DIR
92+
# These functions move uploads and notes to trash can
93+
# and delete them permanently after 90 days
5994
deleteUser $username
60-
deleteNotes $id
61-
deleteFiles $id
95+
deleteNotes
96+
deleteFiles
97+
actuallyDeleteFiles
6298

6399
fi
64100
done <<< "$allusers"

0 commit comments

Comments
 (0)