1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-30 09:40:09 +00:00

feat(adduser): add adduser plugin (#10441)

Automatic "Oh My ZSH" installation when adding new users
This commit is contained in:
Nikolas Garofil 2021-12-21 19:40:46 +01:00
parent 904f8685f7
commit ecc5aab0e7
2 changed files with 81 additions and 0 deletions

26
plugins/adduser/README.md Normal file
View file

@ -0,0 +1,26 @@
# adduser
This plugin adds support for installing "Oh My ZSH" when adding new users.
To use it, add `adduser` to the plugins array of your `~/.zshrc` file:
```zsh
plugins=(... adduser)
```
## Usage
Just run `adduser` as you normally would do and now:
1. The regular `adduser` command will run.
2. The shell of the new user will switch to zsh
3. "Oh My zsh will be installed (as if he would have ran `install.sh` himself).
## NOTES
- It is assumed that the last argument will be the username.<br>*(In rare cases people provide the group as last argument)*
- `useradd` behaviour is not changed.
## Author
[Nikolas Garofil](https://github.com/ngaro)

View file

@ -0,0 +1,55 @@
# Copyright (c) 2021 Nikolas Garofil
adduser() {
local returncode
local result_string="Installation of 'Oh My Zsh' "
local temp_installscript
local path_installscript="$ZSH/tools/install.sh"
#Create user, errors will be reported by the 'real' adduser
#Use which/tail combination to call the binary instead of this function
$(which -a adduser | tail -1) $@ || return 1
echo "\nUser '${@[$#]}' has been created. I will now try to install 'Oh My Zsh'"
if [[ -f $path_installscript ]] ; then
#copy install.sh to a new file in temp that we can give the right owner to execute
#and also make sure that after the install script we are no longer the new user
temp_installscript=$(mktemp)
cat $path_installscript | \
sed 's/exec zsh -l//' |
sed 's/read -r opt/opt=y; echo "\n--- This time I am answering \\"yes\\" for you, but you will still have to type in the password of that user ---"/' \
> $temp_installscript
chown ${@[$#]} $temp_installscript && chmod +x $temp_installscript
#try installing with sudo or su when not available
if [[ -x $(which sudo) ]] ; then
sudo -u ${@[$#]} $temp_installscript
returncode=$?
else
if [[ -x $(which su) ]] ; then
su -l ${@[$#]} -c $temp_installscript
returncode=$?
else
echo "You can't become ${@[$#]} (no 'sudo' or 'su' available)" > /dev/stderr;
returncode=1
fi
fi
#cleanup
rm $temp_installscript
else
echo "Installationscript '$path_installscript' not available" 2> /dev/stderr;
returncode=1
fi
#mention the result
if [[ $returncode -eq 0 ]]; then
echo "$result_string succeeded."
else
echo "$result_string failed." 2> /dev/stderr
fi
return $returncode
}