Last active 1736855915

sqlite3crudwithdialog.sh Raw
1#!/bin/bash
2
3DB_FILE="/path/to/your/database.sqlite3"
4
5# Function to initialize the database
6function initialize_db() {
7 sqlite3 "$DB_FILE" "CREATE TABLE IF NOT EXISTS users (
8 id INTEGER PRIMARY KEY AUTOINCREMENT,
9 username TEXT NOT NULL UNIQUE,
10 password TEXT NOT NULL,
11 domain TEXT NOT NULL,
12 email TEXT NOT NULL
13 );"
14}
15
16# Function to validate the email format
17function validate_email_format() {
18 local email="$1"
19 if [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
20 return 0
21 else
22 dialog --msgbox "Invalid email format." 6 40
23 return 1
24 fi
25}
26
27# Function to check if the email domain exists
28function check_domain_exists() {
29 local domain="$1"
30 if dig +short "$domain" | grep -q '^[0-9]'; then
31 return 0
32 else
33 dialog --msgbox "Domain does not exist." 6 40
34 return 1
35 fi
36}
37
38# Function to validate the domain name
39function validate_domain() {
40 local domain="$1"
41 if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
42 return 0
43 else
44 dialog --msgbox "Invalid domain name format." 6 40
45 return 1
46 fi
47}
48
49# Function to validate the username
50function validate_username() {
51 local username="$1"
52 if [[ -z "$username" ]] || [[ ${#username} -lt 5 ]] || [[ ${#username} -gt 16 ]] || ! [[ "$username" =~ ^[a-zA-Z0-9]+$ ]] || id "$username" &>/dev/null; then
53 dialog --msgbox "Invalid or existing username. Username must be 5-16 characters long, contain only letters and numbers, and cannot exist already." 6 40
54 return 1
55 fi
56 return 0
57}
58
59# Function to validate the password
60function validate_password() {
61 local password="$1"
62 local username="$2"
63 if [[ -z "$password" ]] || [[ ${#password} -lt 5 ]] || [[ ${#password} -gt 16 ]] || [[ "$password" == "$username" ]] || [[ "$password" =~ \ ]] || ! [[ "$password" =~ [A-Z] ]] || ! [[ "$password" =~ [0-9] ]] || ! [[ "$password" =~ [^a-zA-Z0-9] ]]; then
64 dialog --msgbox "Invalid password. Password must be 5-16 characters, not contain spaces, not be the same as the username, and must include at least one uppercase letter, one number, and one special character." 6 40
65 return 1
66 fi
67 return 0
68}
69
70# Function to add user to SQLite database
71function add_user_to_db() {
72 local username="$1"
73 local password="$2"
74 local domain="$3"
75 local email="$4"
76
77 if sqlite3 "$DB_FILE" "SELECT 1 FROM users WHERE username='$username';" | grep -q 1; then
78 dialog --msgbox "Username already exists!" 6 40
79 return 1
80 fi
81
82 sqlite3 "$DB_FILE" "INSERT INTO users (username, password, domain, email) VALUES ('$username', '$password', '$domain', '$email');"
83 dialog --msgbox "User added successfully!" 6 40
84}
85
86# Function to list users from SQLite database
87function list_users() {
88 sqlite3 "$DB_FILE" "SELECT * FROM users;" > /tmp/userlist
89 dialog --textbox /tmp/userlist 22 76
90}
91
92# Function to delete user from SQLite database
93function delete_user() {
94 local username="$1"
95
96 if sqlite3 "$DB_FILE" "DELETE FROM users WHERE username='$username';"; then
97 dialog --msgbox "User deleted successfully!" 6 40
98 else
99 dialog --msgbox "Failed to delete user." 6 40
100 fi
101}
102
103# Function to update user in SQLite database
104function update_user() {
105 local id="$1"
106 local username="$2"
107 local password="$3"
108 local domain="$4"
109 local email="$5"
110
111 if sqlite3 "$DB_FILE" "UPDATE users SET username='$username', password='$password', domain='$domain', email='$email' WHERE id='$id';"; then
112 dialog --msgbox "User updated successfully!" 6 40
113 else
114 dialog --msgbox "Failed to update user." 6 40
115 fi
116}
117
118# Function to get user details for updating
119function get_user_details() {
120 local username="$1"
121
122 sqlite3 "$DB_FILE" "SELECT * FROM users WHERE username='$username';" > /tmp/userdetails
123 dialog --textbox /tmp/userdetails 22 76
124}
125
126initialize_db
127
128while true; do
129 dialog --title "User Management" --menu "Choose an action" 15 50 4 \
130 1 "Add User" \
131 2 "List Users" \
132 3 "Update User" \
133 4 "Delete User" \
134 5 "Exit" 2> /tmp/menuitem
135
136 choice=$(cat /tmp/menuitem)
137
138 case $choice in
139 1)
140 dialog --title "Add User" --form "Enter details" 15 50 4 \
141 "Username:" 1 1 "" 1 20 25 0 \
142 "Password:" 2 1 "" 2 20 25 0 \
143 "Domain:" 3 1 "" 3 20 25 0 \
144 "Email:" 4 1 "" 4 20 25 0 2> /tmp/userform
145
146 username=$(sed -n '1p' /tmp/userform)
147 password=$(sed -n '2p' /tmp/userform)
148 domain=$(sed -n '3p' /tmp/userform)
149 email=$(sed -n '4p' /tmp/userform)
150
151 if validate_username "$username" && validate_password "$password" "$username" && validate_domain "$domain" && validate_email_format "$email" && check_domain_exists "$(echo "$email" | awk -F'@' '{print $2}')"; then
152 add_user_to_db "$username" "$password" "$domain" "$email"
153 else
154 dialog --msgbox "One or more fields are invalid. Please try again." 6 40
155 fi
156 ;;
157 2)
158 list_users
159 ;;
160 3)
161 dialog --inputbox "Enter username of user to update:" 8 40 2> /tmp/username
162 update_username=$(cat /tmp/username)
163 get_user_details "$update_username"
164
165 dialog --title "Update User" --form "Update details" 15 50 4 \
166 "ID:" 1 1 "" 1 20 25 0 \
167 "Username:" 2 1 "" 2 20 25 0 \
168 "Password:" 3 1 "" 3 20 25 0 \
169 "Domain:" 4 1 "" 4 20 25 0 \
170 "Email:" 5 1 "" 5 20 25 0 2> /tmp/userform
171
172 id=$(sed -n '1p' /tmp/userform)
173 username=$(sed -n '2p' /tmp/userform)
174 password=$(sed -n '3p' /tmp/userform)
175 domain=$(sed -n '4p' /tmp/userform)
176 email=$(sed -n '5p' /tmp/userform)
177
178 if validate_username "$username" && validate_password "$password" "$username" && validate_domain "$domain" && validate_email_format "$email" && check_domain_exists "$(echo "$email" | awk -F'@' '{print $2}')"; then
179 update_user "$id" "$username" "$password" "$domain" "$email"
180 else
181 dialog --msgbox "One or more fields are invalid. Please try again." 6 40
182 fi
183 ;;
184 4)
185 dialog --inputbox "Enter username of user to delete:" 8 40 2> /tmp/username
186 delete_username=$(cat /tmp/username)
187 delete_user "$delete_username"
188 ;;
189 5)
190 break
191 ;;
192 esac
193done
194
195rm -f /tmp/menuitem /tmp/userform /tmp/username /tmp/userlist /tmp/userdetails
196