Skip to content

Commit e04a012

Browse files
committed
feat: delete database users when dropping local databases;
do not create database when creating a local user
1 parent 3ecddcf commit e04a012

File tree

2 files changed

+119
-34
lines changed

2 files changed

+119
-34
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ Windows batch script to automate switching of MongoDB database credentials for a
108108
| 3 | Drop Database | It deletes a database defined in the MongoDB connection credentials. Currently available only for localhost MongoDB. |
109109
| 4 | List Databases | Lists the available databases from the defined MongoDB connection credentials. |
110110
| 5 | List Local Databases | Lists the available localhost databases. |
111-
| 6 | Create Local Database and User | Creates a local database and a local user and password associated with the local database.<br>It also creates an empty collection using the database name with a `"_"` prefix i.e., `"_mydatabase"` |
112-
| 7 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
113-
| 8 | Reset | Resets the database conection details |
111+
| 6 | Local DB User: Create | Creates a local user for a local database that may or may not yet exist. |
112+
| 7 | Local DB User: Delete | Deletes a local user from a local database that may or may not yet exist. |
113+
| 6 | Local DB User: List | Lists all local user/s of a local database that may or may not yet exist. |
114+
| 9 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
115+
| 10 | Reset | Resets the database conection details |
114116
| x | Exit | Exit the script. |
115117

116118

main.bat

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ GoTo Main
3535
set /A PreviousScreen=0
3636
set /A NextScreen=0
3737

38+
set "TEMP_USERS_FILE=_users.txt"
39+
set "LOCAL_USERS_FILE=users.txt"
3840
set ENV_FILE=%cd%\.env
3941
echo %ENV_FILE%
4042

4143
if exist .dbs (
4244
del /f .dbs
4345
)
4446

47+
CALL :DeleteLocalUsersFiles
4548
GoTo FetchFile
4649
EXIT /B 0
4750

@@ -66,9 +69,11 @@ EXIT /B 0
6669
echo [3] Drop Database
6770
echo [4] List Databases
6871
echo [5] List Local Databases
69-
echo [6] Create Local Database and User
70-
echo [7] Update Connection Credentials
71-
echo [8] Reset
72+
echo [6] Local DB User: Create
73+
echo [7] Local DB User: Delete
74+
echo [8] Local DB User: List
75+
echo [9] Update Connection Credentials
76+
echo [10] Reset
7277
echo [x] Exit
7378
set "choice=-1"
7479
echo.
@@ -85,9 +90,11 @@ EXIT /B 0
8590
set /A NextScreen=_ViewDatabaseCredentials
8691
Goto ShowDatabases
8792
)
88-
if %choice% EQU 6 Goto CreateDatabaseAndUser
89-
if %choice% EQU 7 Goto SetDatabaseCredentials
90-
if %choice% EQU 8 Goto ResetData
93+
if %choice% EQU 6 Goto CreateLocalDatabaseUser
94+
if %choice% EQU 7 Goto DeleteLocalDatabaseUser
95+
if %choice% EQU 8 Goto ShowLocalDatabaseUsers
96+
if %choice% EQU 9 Goto SetDatabaseCredentials
97+
if %choice% EQU 10 Goto ResetData
9198
if %choice% == x EXIT /B 0
9299

93100
Goto ViewDatabaseCredentials
@@ -248,6 +255,8 @@ EXIT /B 0
248255

249256
echo. | findstr /C:%del% .dbs && (
250257
%MONGO_SHELL% %del% --eval "db.dropDatabase()"
258+
CALL :DeleteLocalDatabaseUsers %del%
259+
251260
set /A NextScreen=_ViewDatabaseCredentials
252261
GoTo ShowDatabases
253262
) || (
@@ -346,13 +355,15 @@ EXIT /B 0
346355
GoTo SetDatabaseCredentials
347356
EXIT /B 0
348357

349-
:: Creates a local database and local database user
350-
:CreateDatabaseAndUser
358+
359+
:: Creates a local database user for a local database
360+
:: The local database may or may not yet exist
361+
:CreateLocalDatabaseUser
351362
setlocal enabledelayedexpansion
352363

353364
cls
354365
echo ----------------------------------------------------------
355-
echo CREATE DATABASE AND USER
366+
echo CREATE LOCAL DATABASE USER
356367
echo ----------------------------------------------------------
357368

358369
set "databaseName="
@@ -364,8 +375,8 @@ EXIT /B 0
364375
set /p userPassword="Enter the database user password:"
365376
echo.
366377

367-
echo Do you want to create the database and user
368-
echo on host [%MONGO_HOST%]?
378+
echo Do you want to create a local database user
379+
echo on host [%MONGO_HOST%], database [%databaseName%]?
369380
echo - Database: %databaseName%
370381
echo - User: %databaseUser%
371382
echo - Passsword: %userPassword%
@@ -382,41 +393,101 @@ EXIT /B 0
382393
if /i "!retry!"=="n" (
383394
GoTo ViewDatabaseCredentials
384395
) else (
385-
GoTo CreateDatabaseAndUser
396+
GoTo CreateLocalDatabaseUser
386397
)
387398
) else (
388399
(if %MONGO_HOST% EQU localhost (
389-
echo Creating local database and user...
390-
391-
%MONGO_SHELL% !databaseName! --eval "db.createCollection('_!databaseName!')"
400+
echo Creating local database user [!databaseUser!]...
392401
%MONGO_SHELL% !databaseName! --eval "db.createUser({user: '!databaseUser!' , pwd: '!userPassword!', roles: [{ role: 'readWrite', db: '!databaseName!' }]})"
393402

394403
echo Success!
395-
set /A NextScreen=_ViewDatabaseCredentials
396-
GoTo ShowDatabases
397-
) else (
398-
echo Creating remote database...
404+
CALL :ListLocalDatabaseUsers !databaseName!
405+
406+
set /p go=Press enter to continue...
407+
GoTo ViewDatabaseCredentials
408+
))
409+
)
410+
EXIT /B 0
399411

400-
%MONGO_SHELL% mongodb+srv://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST%/!databaseName! --eval "db.createCollection('_!databaseName!'); db.adminCommand({ listDatabases: 1, nameOnly:true })" > .dbs
401412

402-
echo.
403-
echo Success!
404-
echo [NOTE]: Creating database users on remote hosts are currently not supported.
405-
echo [NOTE]: Skipping creating remote database user...
413+
:: Deletes all database users of a given local database
414+
:DeleteLocalDatabaseUsers
415+
setlocal enabledelayedexpansion
416+
set "dbName=%~1"
406417

407-
echo.
408-
echo ----------------------------------------------------------
409-
echo Host: %MONGO_HOST%
410-
echo Available databases:
411-
findstr /C:name .dbs
418+
CALL :ListLocalDatabaseUsers %dbName%
412419

413-
set /p go=Press enter to continue...
414-
GoTo ViewDatabaseCredentials
420+
if exist %LOCAL_USERS_FILE% (
421+
(for /f "tokens=*" %%a in (%LOCAL_USERS_FILE%) do (
422+
for /f "tokens=2 delims='" %%u in ("%%a") do (
423+
echo Deleting user: %%u
424+
%MONGO_SHELL% %dbName% --eval "db.dropUser('%%u')"
425+
)
426+
))
427+
)
428+
429+
CALL :DeleteLocalUsersFiles
430+
set /p go=Press enter to continue...
431+
EXIT /B 0
432+
433+
434+
:: Lists the local database users of a given local database
435+
:ListLocalDatabaseUsers
436+
setlocal enabledelayedexpansion
437+
set "dbName=%~1"
438+
439+
CALL :DeleteLocalUsersFiles
440+
441+
%MONGO_SHELL% %dbName% --eval "db.getUsers()" > %TEMP_USERS_FILE%
442+
findstr /C:user: %TEMP_USERS_FILE% > %LOCAL_USERS_FILE%
443+
444+
if exist %LOCAL_USERS_FILE% (
445+
echo.
446+
echo ----------------------------------------------------------
447+
echo Available LOCAL users on localhost database [%dbName%]:
448+
449+
(for /f "tokens=*" %%a in (%LOCAL_USERS_FILE%) do (
450+
for /f "tokens=2 delims='" %%u in ("%%a") do (
451+
echo - User: %%u
452+
)
415453
))
416454
)
417455
EXIT /B 0
418456

419457

458+
:: Prompt local database name for listing its local users
459+
:ShowLocalDatabaseUsers
460+
cls
461+
echo ----------------------------------------------------------
462+
echo LIST LOCAL DATABASE USERS
463+
echo ----------------------------------------------------------
464+
465+
set "databaseName="
466+
set /p databaseName="Enter the database name:"
467+
echo.
468+
469+
set "continue=Y"
470+
echo Press enter to continue
471+
set /p continue=Type "n" and press enter to cancel:
472+
473+
if %continue% EQU n (
474+
set "retry=Y"
475+
set /p retry="Retry? [Y/n]:"
476+
477+
if /i "!retry!"=="n" (
478+
GoTo ViewDatabaseCredentials
479+
) else (
480+
GoTo ShowLocalDatabaseUsers
481+
)
482+
) else (
483+
CALL :ListLocalDatabaseUsers !databaseName!
484+
485+
set /p go=Press enter to continue...
486+
GoTo ViewDatabaseCredentials
487+
)
488+
EXIT /B 0
489+
490+
420491
::----------------------------------------------------------
421492
:: Utility helper scripts
422493
::----------------------------------------------------------
@@ -615,4 +686,16 @@ EXIT /B 0
615686
echo.
616687
set "AUTHSOURCE="
617688
set /p AUTHSOURCE="Enter the user authentication database:"
689+
EXIT /B 0
690+
691+
692+
:: Deletes the local database users list file
693+
:DeleteLocalUsersFiles
694+
if exist %LOCAL_USERS_FILE% (
695+
del /f %LOCAL_USERS_FILE%
696+
)
697+
698+
if exist %TEMP_USERS_FILE% (
699+
del /f %TEMP_USERS_FILE%
700+
)
618701
EXIT /B 0

0 commit comments

Comments
 (0)