Skip to content

Commit b42e5b4

Browse files
committed
add another method for finding current user
1 parent 7e38789 commit b42e5b4

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

resources-linux/postinst

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ set -e
44
GROUP_NAME="defguard"
55
SERVICE_NAME="defguard-service"
66

7+
# Get the name of user who is installing the client
8+
get_installing_user() {
9+
# Method 1: Check standard environment variables when installing with CLI
10+
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
11+
INSTALLING_USER="$SUDO_USER"
12+
elif [ -n "$USER" ] && [ "$USER" != "root" ]; then
13+
INSTALLING_USER="$USER"
14+
fi
15+
if [ -n "$INSTALLING_USER" ]; then
16+
echo "$INSTALLING_USER"
17+
return 0
18+
fi
19+
20+
# Method 2: Check loginctl for latest session
21+
username=$(loginctl list-users --no-legend | awk '$1 >= 1000 {print $2; exit}')
22+
if [ -n "$username" ]; then
23+
echo "$username"
24+
return 0
25+
fi
26+
27+
return 1
28+
}
29+
730
case "$1" in
831
1|configure)
932
# Create the group if it doesn't exist
@@ -15,12 +38,7 @@ case "$1" in
1538
fi
1639

1740
# Determine target user
18-
TARGET_USER=""
19-
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
20-
TARGET_USER="$SUDO_USER"
21-
elif [ -n "$USER" ] && [ "$USER" != "root" ]; then
22-
TARGET_USER="$USER"
23-
fi
41+
TARGET_USER=$(get_installing_user)
2442
echo "Installation triggered by user $TARGET_USER"
2543

2644
# Add user to group if we found a valid target

0 commit comments

Comments
 (0)