MongoDB Tutorial 3: Delete User
MongoDB Tutorial 3 : Introduction | Port and Password | Create database | Drop database | Delete User | Main Application | Mongo-Start
|
|
MongoDB Production Standalone UniServer 6-Carbo. |
Delete a user
Introduction
For security reasons you generally assign a single user to a database. However there may be applications that require several users. You can add more users using the create database window. This window displays only the first user assigned to a database.
This page covers displaying all users assigned to a database allowing you to select and delete an individual user.
Select a database and user
Select a database
|
|
Add select a user'
|
Complete script
<?php //-- Constants ---------------------------------------------------------------- define('IDC_USER_DATABASE3', 4010); // Text box Database name define('IDC_USER_3', 4020); // Text box Database name define('IDC_DELETE_USER', 4030); // Button Delete user define('IDC_DB_LISTBOX3', 4040); // Database list box define('IDC_USER_LISTBOX3', 4050); // Users list box // Common variables $mongo_user_win3; // Window handle $user_new_db3; // List of dbs $user_new_3; // List of users //=== Create function ========================================================= function delete_user_window($parent){ global $mongo_user_win3; global $user_new_db3; // Check server running if(!mongodb_running()){ $str = ""; $str .= "This item requires access to a running server.\n"; $str .= "Please start MonGoDB server and run\n"; $str .= "this item again."; wb_message_box(NULL, $str,"Server not running", WBC_INFO); return; // Give up } //=== 1) Create main window --------------------------------------------------- $mongo_user_win3 = wb_create_window($parent, ModalDialog, "MongoDB Delete User", WBC_CENTER, WBC_CENTER, 474, 230, WBC_INVISIBLE | WBC_NOTIFY | WBC_TOP, WBC_HEADERSEL); //=== 2) Create controls for the main window ---------------------------------- wb_create_control($mongo_user_win3, Frame, 'Database', 65, 5, 165, 55, 0, 0, 0, 0); wb_create_control($mongo_user_win3, PushButton, 'Delete', 5, 25, 50, 25, IDC_DELETE_USER, 0, 0, 0); wb_create_control($mongo_user_win3, EditBox, '', 75, 25, 145, 20, IDC_USER_DATABASE3, 0, 0, 0); wb_create_control($mongo_user_win3, Frame, 'Select a database', 5, 65, 225, 125, 0, 0, 0, 0); $list3 = wb_create_control($mongo_user_win3, ListView, 'Head1', 20, 85, 200, 95, IDC_DB_LISTBOX3, 0x00100080, 0, 0); wb_create_control($mongo_user_win3, Frame, 'User', 235, 5, 225, 55, 0, 0, 0, 0); wb_create_control($mongo_user_win3, EditBox, '', 250, 25, 200, 20, IDC_USER_3, 0, 0, 0); wb_create_control($mongo_user_win3, Frame, 'Select a user', 235, 65, 225, 125, 0, 0, 0, 0); $list4 = wb_create_control($mongo_user_win3, ListView, 'Head1', 250, 85, 200, 95, IDC_USER_LISTBOX3, 0x00100080, 0, 0); //=== INIT ==================== //--- Set up listview 1 --- // Set column titles and widths wb_set_text($list3, array( array("List of Databases", 180), )); $user_new_db3 = list_dbs_auth(); // Get list of dbs // Create rows and columns wb_create_items($list3,$user_new_db3); // Fill list //--- Set up listview 2 --- // Set column titles and widths wb_set_text($list4, array( array("List of Users", 180), )); //--- End Set up listview --- //=== END INIT ================ //=== 3) Assign handler function to the main window -------------------------- wb_set_handler($mongo_user_win3, "process_delete_user"); wb_set_image($mongo_user_win3, getcwd()."/images/utray.ico"); // Add logo wb_set_visible($mongo_user_win3, true); // Show window //=== 5) Enter application loop ----------------------------------------------- //wb_main_loop(); }//==================================================== End Create function === //=== 4) Handler Function ----------------------------------------------------- function process_delete_user($window, $id, $ctrl=0, $lparam1=0, $lparam2=0){ global $user_new_db3; global $user_new_3; switch($id) { //=== DELETE USER =========================================================== case IDC_DELETE_USER: $db = wb_get_text(wb_get_control($window, IDC_USER_DATABASE3)); // Get db $user = wb_get_text(wb_get_control($window, IDC_USER_3)); // Get user // Validate - User name cannot be root if($user == 'root'){ $str = ""; $str .= "The root User\n"; $str .= "Can not be deleted "; wb_message_box($window, $str,"User Name", WBC_INFO); break; } // Validate - db name user may have typed in name if (!(preg_match("/^[A-Za-z0-9_]+$/", $db))) { $str = ""; $str .= "If database name is empty please select a name\n"; $str .= "Or type in a database name\n"; $str .= "Only alphanumeric characters allowed"; wb_message_box($window, $str,"DB Name", WBC_INFO); break; } // Validate - User name user may have typed in name if (!(preg_match("/^[A-Za-z0-9_]+$/", $user))) { $str = ""; $str .= "If User name is empty please select a name\n"; $str .= "Or type in a name\n"; $str .= "Only alphanumeric characters allowed"; wb_message_box($window, $str,"User Name", WBC_INFO); break; } // Delete user delete_user_auth($db,$user); // Delete user from selected database // Update user list $user_new_3 = get_user_auth($db); //Select a db and obtain user/s asigned array $temp = $user_new_3; // Save array wb_delete_items (wb_get_control($window, IDC_USER_LISTBOX3),NULL); // Delete all list view entries wb_create_items (wb_get_control($window, IDC_USER_LISTBOX3),$temp); // Add array wb_set_text(wb_get_control($window, IDC_USER_3),""); // Reset Set User break; //===================================================== END DELETE USER ==== //=== Item selected from list of dbs ======================================== case IDC_DB_LISTBOX3: // A row was selected from db list $sel = wb_get_selected($ctrl); // Get selection $sel = $sel ? implode(", ", $sel) : "none"; // Set selected value if($sel =="none"){ wb_set_text(wb_get_control($window, IDC_USER_DATABASE3),""); // Reset DB wb_set_text(wb_get_control($window, IDC_USER_3),""); // Reset User wb_delete_items (wb_get_control($window, IDC_USER_LISTBOX3),NULL); // Delete all list view entries } else{ $selected_index = $sel; // Save Array index $contents = wb_get_text($ctrl); // Get row selected Returns an array of arrays $first_array = $contents[0]; // Select first array (which is selected row) $db_selected_name = $first_array[0]; // Select first column (which is db) $db_selected_name = trim($db_selected_name); // Clean wb_set_text(wb_get_control($window, IDC_USER_DATABASE3),$db_selected_name); // Set DB $user_new_3 = get_user_auth($db_selected_name); //Select a db and obtain user/s asigned array $temp = $user_new_3; // Save array wb_delete_items (wb_get_control($window, IDC_USER_LISTBOX3),NULL); // Delete all list view entries wb_create_items (wb_get_control($window, IDC_USER_LISTBOX3),$temp); // Add array } break; //======================================== Item selected from list of dbs === //=== Item selected from list of users ====================================== case IDC_USER_LISTBOX3: // A row was selected from db list $sel = wb_get_selected($ctrl); // Get selection $sel = $sel ? implode(", ", $sel) : "none"; // Set selected value if($sel =="none"){ wb_set_text(wb_get_control($window, IDC_USER_3),""); // Reset User } else{ $selected_index = $sel; // Save Array index $contents = wb_get_text($ctrl); // Get row selected Returns an array of arrays $first_array = $contents[0]; // Select first array (which is selected row) $user_selected_name = $first_array[0]; // Select first column (which is user) $user_selected_name = trim($user_selected_name); // Clean wb_set_text(wb_get_control($window, IDC_USER_3),$user_selected_name); // Set User } break; //================================== Item selected from dbs list of users === case IDCLOSE: // Constant IDCLOSE (8) predefined wb_destroy_window($window); // Destroy the window break; } } //==================================================== End Handler Function === ?>
Test
Edit file z_mongo\mongodb_1\control\mongo_db.php Add the following to the include section:
include_once "window_delete_user.inc.php"; // Window delete user
Change the following section as shown:
//=== Display help info ==================================================== case ID_HELP_BUTTON: // Button delete_user_window($window); // drop_database_window($window); // create_database_window($window); // create_port_change_window($window); // wb_exec('Notepad',INFO_TXT); break; //================================================ END Display help info ===
Run
|
Summary
Above is last in a series of pop-up utilities required for our application.
To test these utilities we have been using button "Help and Information"
Next page looks at integrating these into the main application.