Fixed add-user command

This commit is contained in:
Brian Zalewski 2023-11-28 01:03:00 +00:00
parent 097396d750
commit 9a70a9be99
2 changed files with 32 additions and 41 deletions

View file

@ -161,4 +161,9 @@ if [ -d /Applications ] && [ -d System ]; then
brew autoupdate start --cleanup --greedy --upgrade
fi
fi
### Scrub ~/.viminfo
if [ -f "$HOME/.viminfo" ]; then
logg info 'Removing ~/.viminfo' && sudo rm -f "$HOME/.vimino"
fi
{{ end -}}

View file

@ -7,8 +7,6 @@
# there is already a user / group with the name present on the system before running
# any code. On macOS, it assigns the user an ID that equal to the maximum user ID present
# on the system plus one.
#
# This script was generated with ChatGPT.
# Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then
@ -40,51 +38,37 @@ if [[ "$(uname)" == "Darwin" ]]; then
echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m"
fi
# Check if the group already exists
if ! sudo dscl . read /Groups/"${GROUP}" >/dev/null 2>&1; then
# Create the group
sudo dscl . create /Groups/"${GROUP}"
# Ensure the group has a GID
if [[ "$(sudo dscl . read /Groups/"$group" gid 2>&1)" == *"No such key"* ]]; then
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
GROUP_ID="$((MAX_ID_GROUP+1))"
sudo dscl . create /Groups/"${GROUP}" gid "$GROUP_ID"
fi
# Ensure group exists
if ! dscl . read "/Groups/$GROUP" gid &> /dev/null; then
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
PRIMARY_GROUP_ID="$((MAX_ID_GROUP+1))"
dscl . create "/Groups/$GROUP"
# This also sets the PrimaryGroupID
sudo dscl . create "/Groups/$GROUP" gid "$PRIMARY_GROUP_ID"
sudo dscl . append "/Groups/$GROUP" GroupMembership "$USER"
else
if command -v logg > /dev/null; then
logg info "Group ${GROUP} already exists."
else
echo -e "\e[93mGroup ${GROUP} already exists\e[0m"
fi
PRIMARY_GROUP_ID="$(dscl . read "/Groups/$GROUP" gid | awk '{print $2}')"
fi
# Check if the user already exists
if ! sudo dscl . read /Users/"${GROUP}" >/dev/null 2>&1; then
# Create the user
sudo dscl . create /Users/"${GROUP}"
# Ensure the user has a PrimaryGroupID
if [[ "$(sudo dscl . read /Users/"${GROUP}" PrimaryGroupID 2>&1)" == *"No such key"* ]]; then
sudo dscl . create /Users/"${GROUP}" PrimaryGroupID 20
fi
# Ensure the user has a UniqueID
if [[ "$(sudo dscl . read /Users/"${GROUP}" UniqueID 2>&1)" == *"No such key"* ]]; then
MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")"
USER_ID="$((MAX_ID_USER+1))"
sudo dscl . create /Users/"${GROUP}" UniqueID "$USER_ID"
fi
# Add the user to the group
sudo dseditgroup -o edit -t user -a "${GROUP}" "${GROUP}"
# Add the current user to the group
sudo dseditgroup -o edit -t user -a "${USER}" "${GROUP}"
# Ensure user exists
if ! dscl . read "/Users/$GROUP" UniqueID &> /dev/null; then
MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")"
UNIQUE_ID="$((MAX_ID_USER+1))"
sudo dscl . create "/Users/$GROUP"
sudo dscl . create "/Users/$GROUP" UniqueID "$UNIQUE_ID"
sudo dscl . create "/Users/$GROUP" PrimaryGroupID "$PRIMARY_GROUP_ID"
else
if command -v logg > /dev/null; then
logg info "User ${GROUP} already exists"
else
echo -e "\e[93mUser ${GROUP} already exists\e[0m"
fi
UNIQUE_ID="$(dscl . read "/Users/$GROUP" UniqueID | awk '{print $2}')"
fi
# Add the user to the group
sudo dseditgroup -o edit -t user -a "$GROUP" "$GROUP"
# Add the current user to the group
sudo dseditgroup -o edit -t user -a "$USER" "$GROUP"
# Add USER group to the group
sudo dseditgroup -o edit -t group -a "$USER" "$GROUP"
if command -v logg > /dev/null; then
logg info "Group and user ${GROUP} created successfully on macOS"
else
@ -127,6 +111,7 @@ elif [[ "$(uname)" == "Linux" ]]; then
else
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
fi
exit 0
fi
@ -136,4 +121,5 @@ if command -v logg > /dev/null; then
else
echo -e "\e[91mUnsupported operating system\e[0m"
fi
exit 1