Fixed add-user command
This commit is contained in:
parent
097396d750
commit
9a70a9be99
2 changed files with 32 additions and 41 deletions
|
@ -161,4 +161,9 @@ if [ -d /Applications ] && [ -d System ]; then
|
||||||
brew autoupdate start --cleanup --greedy --upgrade
|
brew autoupdate start --cleanup --greedy --upgrade
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
### Scrub ~/.viminfo
|
||||||
|
if [ -f "$HOME/.viminfo" ]; then
|
||||||
|
logg info 'Removing ~/.viminfo' && sudo rm -f "$HOME/.vimino"
|
||||||
|
fi
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
# there is already a user / group with the name present on the system before running
|
# 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
|
# any code. On macOS, it assigns the user an ID that equal to the maximum user ID present
|
||||||
# on the system plus one.
|
# on the system plus one.
|
||||||
#
|
|
||||||
# This script was generated with ChatGPT.
|
|
||||||
|
|
||||||
# Check if the script is being run as root
|
# Check if the script is being run as root
|
||||||
if [[ $EUID -ne 0 ]]; then
|
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"
|
echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if the group already exists
|
# Ensure group exists
|
||||||
if ! sudo dscl . read /Groups/"${GROUP}" >/dev/null 2>&1; then
|
if ! dscl . read "/Groups/$GROUP" gid &> /dev/null; then
|
||||||
# Create the group
|
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
|
||||||
sudo dscl . create /Groups/"${GROUP}"
|
PRIMARY_GROUP_ID="$((MAX_ID_GROUP+1))"
|
||||||
# Ensure the group has a GID
|
dscl . create "/Groups/$GROUP"
|
||||||
if [[ "$(sudo dscl . read /Groups/"$group" gid 2>&1)" == *"No such key"* ]]; then
|
# This also sets the PrimaryGroupID
|
||||||
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
|
sudo dscl . create "/Groups/$GROUP" gid "$PRIMARY_GROUP_ID"
|
||||||
GROUP_ID="$((MAX_ID_GROUP+1))"
|
sudo dscl . append "/Groups/$GROUP" GroupMembership "$USER"
|
||||||
sudo dscl . create /Groups/"${GROUP}" gid "$GROUP_ID"
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
if command -v logg > /dev/null; then
|
PRIMARY_GROUP_ID="$(dscl . read "/Groups/$GROUP" gid | awk '{print $2}')"
|
||||||
logg info "Group ${GROUP} already exists."
|
|
||||||
else
|
|
||||||
echo -e "\e[93mGroup ${GROUP} already exists\e[0m"
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if the user already exists
|
# Ensure user exists
|
||||||
if ! sudo dscl . read /Users/"${GROUP}" >/dev/null 2>&1; then
|
if ! dscl . read "/Users/$GROUP" UniqueID &> /dev/null; then
|
||||||
# Create the user
|
MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")"
|
||||||
sudo dscl . create /Users/"${GROUP}"
|
UNIQUE_ID="$((MAX_ID_USER+1))"
|
||||||
# Ensure the user has a PrimaryGroupID
|
sudo dscl . create "/Users/$GROUP"
|
||||||
if [[ "$(sudo dscl . read /Users/"${GROUP}" PrimaryGroupID 2>&1)" == *"No such key"* ]]; then
|
sudo dscl . create "/Users/$GROUP" UniqueID "$UNIQUE_ID"
|
||||||
sudo dscl . create /Users/"${GROUP}" PrimaryGroupID 20
|
sudo dscl . create "/Users/$GROUP" PrimaryGroupID "$PRIMARY_GROUP_ID"
|
||||||
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}"
|
|
||||||
else
|
else
|
||||||
if command -v logg > /dev/null; then
|
UNIQUE_ID="$(dscl . read "/Users/$GROUP" UniqueID | awk '{print $2}')"
|
||||||
logg info "User ${GROUP} already exists"
|
|
||||||
else
|
|
||||||
echo -e "\e[93mUser ${GROUP} already exists\e[0m"
|
|
||||||
fi
|
|
||||||
fi
|
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
|
if command -v logg > /dev/null; then
|
||||||
logg info "Group and user ${GROUP} created successfully on macOS"
|
logg info "Group and user ${GROUP} created successfully on macOS"
|
||||||
else
|
else
|
||||||
|
@ -127,6 +111,7 @@ elif [[ "$(uname)" == "Linux" ]]; then
|
||||||
else
|
else
|
||||||
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
|
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -136,4 +121,5 @@ if command -v logg > /dev/null; then
|
||||||
else
|
else
|
||||||
echo -e "\e[91mUnsupported operating system\e[0m"
|
echo -e "\e[91mUnsupported operating system\e[0m"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
|
|
Loading…
Reference in a new issue