-
Notifications
You must be signed in to change notification settings - Fork 335
feat: Add User Manager #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChrisTitusTech
merged 9 commits into
ChrisTitusTech:main
from
jeevithakannan2:user-manipulation
Sep 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a199a4
Add User Control Panel
jeevithakannan2 427c4bc
Fix non posix
jeevithakannan2 fe115f5
Split script
jeevithakannan2 c252f6d
Merge remote-tracking branch 'upstream/main' into user-manipulation
jeevithakannan2 4b1fc28
Update userguide.md
jeevithakannan2 9cb6a6c
Update utility_functions.sh
jeevithakannan2 e412569
Remove clear
jeevithakannan2 5fcb0f2
Add checkEnv
jeevithakannan2 50f62a9
Merge branch 'main' into user-manipulation
jeevithakannan2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
#!/bin/sh -e | ||
|
||
. ../common-script.sh | ||
|
||
mainMenu() { | ||
while true; do | ||
clear | ||
printf "%b\n" "${YELLOW}User Control Panel${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
echo "1. Create a new user" | ||
echo "2. Delete a user" | ||
echo "3. Modify a user" | ||
echo "0. Exit" | ||
read -e -p "Choose an option: " choice | ||
|
||
case "$choice" in | ||
1) createUser ;; | ||
2) deleteUser ;; | ||
3) modifyUser ;; | ||
0) exit 0 ;; | ||
*) printf "%b\n" "${RED}Invalid option. Please [Enter] to try again.${RC}"; read -r ;; | ||
esac | ||
done | ||
} | ||
|
||
createUser() { | ||
clear | ||
printf "%b\n" "${YELLOW}Create a new user${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
read -e -p "Enter the username: " username | ||
|
||
# Check if username is empty | ||
if [ -z "$username" ]; then | ||
printf "%b\n" "${RED}Invalid username${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if user already exists | ||
if id "$username" > /dev/null 2>&1; then | ||
printf "%b\n" "${RED}User already exists${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if username is valid | ||
if ! echo "$username" | grep -Eq '^[a-z][-a-z0-9_]+$'; then | ||
printf "%b\n" "${RED}Username must only contain letters, numbers, hyphens, and underscores. It cannot start with a number or contain spaces.${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
password="" | ||
password2="" | ||
|
||
promptPassword() { | ||
read -s -p "Enter the password: " password | ||
echo # Moves to a new line after the password input | ||
read -s -p "Re-enter the password: " password2 | ||
echo # Moves to a new line after the password input | ||
|
||
if [ "$password" != "$password2" ]; then | ||
printf "%b\n" "${RED}Passwords do not match${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
promptPassword | ||
fi | ||
} | ||
|
||
promptPassword | ||
|
||
$ESCALATION_TOOL useradd -m "$username" -g users -G wheel,audio,video -s /bin/bash | ||
jeevithakannan2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
echo "$username:$password" | $ESCALATION_TOOL chpasswd | ||
|
||
printf "%b\n" "${GREEN}User created successfully${RC}" | ||
printf "%b\n" "${GREEN}Press [Enter] to continue...${RC}" | ||
read -r | ||
} | ||
|
||
deleteUser() { | ||
clear | ||
printf "%b\n" "${YELLOW}Delete a user${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
read -e -p "Enter the username: " username | ||
|
||
# Check if username is empty | ||
if [ -z "$username" ]; then | ||
printf "%b\n" "${RED}Invalid username${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if system reserved user | ||
if [ "$(id -u "$username")" -le 999 ]; then | ||
printf "%b\n" "${RED}Cannot delete system users${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if current user | ||
if [ "$username" = "$USER" ]; then | ||
printf "%b\n" "${RED}Cannot delete the current user${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if valid user | ||
if ! id "$username" > /dev/null 2>&1; then | ||
printf "%b\n" "${RED}User does not exist${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
$ESCALATION_TOOL userdel --remove "$username" 2>/dev/null | ||
jeevithakannan2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
printf "%b\n" "${GREEN}User deleted successfully${RC}" | ||
printf "%b\n" "${GREEN}Press [Enter] to continue...${RC}" | ||
read -r | ||
} | ||
|
||
modifyUser() { | ||
clear | ||
printf "%b\n" "${YELLOW}Modify a user${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
read -e -p "Enter the username: " username | ||
|
||
# Check if username is empty | ||
if [ -z "$username" ]; then | ||
printf "%b\n" "${RED}Invalid username${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if user exists | ||
if ! id "$username" > /dev/null 2>&1; then | ||
printf "%b\n" "${RED}User does not exist${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if system reserved user | ||
if [ "$(id -u "$username")" -le 999 ] && [ "$(id -u "$username")" -ne 0 ]; then | ||
printf "%b\n" "${RED}Cannot modify system users${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
while true; do | ||
clear | ||
printf "%b\n" "${YELLOW}Modifying user $username${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
echo "1. Change password" | ||
echo "2. Add to group" | ||
echo "3. Remove from group" | ||
echo "0. Exit" | ||
read -e -p "Choose an option: " choice | ||
|
||
case "$choice" in | ||
1) changePassword "$username" ;; | ||
2) addToGroup "$username" ;; | ||
3) removeFromGroup "$username" ;; | ||
0) break ;; | ||
*) printf "%b\n" "${RED}Invalid option. Please [Enter] to try again.${RC}"; read -r ;; | ||
esac | ||
done | ||
} | ||
|
||
changePassword() { | ||
clear | ||
printf "%b\n" "${YELLOW}Change password${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
password="" | ||
password2="" | ||
|
||
promptPassword() { | ||
read -s -p "Enter the password: " password | ||
echo # Moves to a new line after the password input | ||
read -s -p "Re-enter the password: " password2 | ||
echo # Moves to a new line after the password input | ||
|
||
if [ "$password" != "$password2" ]; then | ||
printf "%b\n" "${RED}Passwords do not match${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
promptPassword | ||
fi | ||
} | ||
|
||
promptPassword | ||
|
||
echo "$1:$password" | $ESCALATION_TOOL chpasswd | ||
printf "%b\n" "${GREEN}Password changed successfully${RC}" | ||
printf "%b\n" "${GREEN}Press [Enter] to continue...${RC}" | ||
read -r | ||
} | ||
|
||
addToGroup() { | ||
clear | ||
printf "%b\n" "${YELLOW}Add to group${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
read -e -p "Enter the group name: " group | ||
|
||
# Check if group is empty | ||
if [ -z "$group" ]; then | ||
printf "%b\n" "${RED}Invalid group name${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if group exists | ||
if ! getent group "$group" > /dev/null 2>&1; then | ||
printf "%b\n" "${RED}Group does not exist${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
$ESCALATION_TOOL usermod -aG "$group" "$1" | ||
printf "%b\n" "${GREEN}User added to group successfully${RC}" | ||
printf "%b\n" "${GREEN}Press [Enter] to continue...${RC}" | ||
read -r | ||
} | ||
|
||
removeFromGroup() { | ||
clear | ||
printf "%b\n" "${YELLOW}Remove from group${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
read -e -p "Enter the group name: " group | ||
|
||
# Check if group is empty | ||
if [ -z "$group" ]; then | ||
printf "%b\n" "${RED}Invalid group name${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
# Check if group exists | ||
if ! getent group "$group" > /dev/null 2>&1; then | ||
printf "%b\n" "${RED}Group does not exist${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read -r | ||
return | ||
fi | ||
|
||
$ESCALATION_TOOL gpasswd -d "$1" "$group" | ||
printf "%b\n" "${GREEN}User removed from group successfully${RC}" | ||
printf "%b\n" "${GREEN}Press [Enter] to continue...${RC}" | ||
read -r | ||
} | ||
|
||
checkEscalationTool | ||
mainMenu |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.