From 87414c9d63e220e5d0c95ce18d67de8a5f6d368a Mon Sep 17 00:00:00 2001 From: Brian Zalewski Date: Wed, 19 Apr 2023 07:06:59 +0000 Subject: [PATCH] Update 2 files - /home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl - /home/dot_local/bin/executable_install-program --- ...change_before_20-ensure-user-group.sh.tmpl | 34 +++++++++++++++++-- home/dot_local/bin/executable_install-program | 16 ++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl index 532cd251..caadc0cc 100644 --- a/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl +++ b/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl @@ -9,6 +9,36 @@ {{ includeTemplate "universal/logg-before" }} ### Ensure user has group of same name (required for Macports) -logg info 'Ensuring user has a group with the same name and that it is a member. Sudo privileges may be required' -echo y | sudo dseditgroup -o create "$USER" > /dev/null +logg info "Ensuring user ($USER) has a group with the same name ($USER) and that it is a member. Sudo privileges may be required" + +GROUP="$USER" +USERNAME="$USER" + +### Add group +sudo dscl . create /Groups/$GROUP + +### Add GroupID to group +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 + +### Add user +sudo dscl . create /Users/$USERNAME + +### Add PrimaryGroupID to user +if [[ "$(sudo dscl . read /Users/$USERNAME PrimaryGroupID 2>&1)" == *'No such key'* ]]; then + sudo dscl . create /Users/$USERNAME PrimaryGroupID 20 +fi + +### Add UniqueID to user +if [[ "$(sudo dscl . read /Users/$USERNAME UniqueID 2>&1)" == *'No such key'* ]]; then + MAX_ID_USER="$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)" + USER_ID="$((MAX_ID_USER+1))" + sudo dscl . create /Users/$USERNAME UniqueID "$USERID" +fi + +### Add user to group +sudo dseditgroup -o edit -t user -a $USERNAME $GROUP {{ end -}} diff --git a/home/dot_local/bin/executable_install-program b/home/dot_local/bin/executable_install-program index d1af9140..f51cc134 100644 --- a/home/dot_local/bin/executable_install-program +++ b/home/dot_local/bin/executable_install-program @@ -1501,15 +1501,21 @@ async function addUserGroup(group) { log('info', logStage, `Ensuring the ${group} group / user is added`) if (osType === 'linux') { const useradd = which.sync('useradd', { nothrow: true }) - if (useradd) { + const usermod = which.sync('usermod', { nothrow: true }) + if (useradd && usermod) { runCommand(`Adding the ${group} user / group`, `sudo useradd ${group}`) + runCommand(`Adding the ${process.env.USER} user to the ${group} group`, `sudo usermod -a -G ${group} ${process.env.USER}`) } else { - log('error', logStage, `The useradd command is unavailable`) + log('error', logStage, `The useradd and / or usermod command is unavailable`) } } else if (osType === 'darwin') { - runCommand(`Adding the ${group} group`, `sudo dscl . -create /Users/${group}`) - runCommand(`Creating the ${group} group`, `sudo dscl . -create /Groups/${group}`) - runCommand(`Adding the ${group} user to the ${group} group`, `sudo dscl . -append /Groups/${group} GroupMembership ${group}`) + runCommand(`Creating the ${group} group`, `sudo dscl . create /Groups/${group}`) + runCommand(`Ensuring the ${group} 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`) + runCommand(`Adding the ${group} user`, `sudo dscl . create /Users/${group}`) + runCommand(`Ensuring the ${group} 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`) + runCommand(`Ensuring the ${group} user has a UniqueID`, `if [[ "$(sudo dscl . read /Users/${group} UniqueID 2>&1)" == *'No such key'* ]]; then MAX_ID_USER="$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)"; USER_ID="$((MAX_ID_USER+1))"; sudo dscl . create /Users/${group} UniqueID "$USERID"; fi`) + runCommand(`Adding the ${group} user to the ${group} group`, `sudo dseditgroup -o edit -t user -a ${group} ${group}`) + runCommand(`Adding the ${process.env.USER} user to the ${group} group`, `sudo dseditgroup -o edit -t user -a ${process.env.USER} ${group}`) } else if (osType === 'windows') { log('warn', logStage, `Windows support not yet added`) } else {