Ejemplo n.º 1
0
        // main driver for addUser logic
        public void main(string first, string last, string username, string password, string accType, string file)
        {
            Database_Manager dbm = new Database_Manager();
            string           msg = "";
            int    addedCount    = 0;
            int    failedCount   = 0;
            bool   valid;
            bool   added  = false;
            bool   exists = true;
            string line;

            // entries from a batch file
            if (file != "")
            {
                // while not at the end of the csv file, read the line and save the data in the corresponding variables
                using (StreamReader sr = File.OpenText(file))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] values = new string[5] {
                            "", "", "", "", ""
                        };                                                     // sets initial values to 0
                        string[] tempValues = line.Split(',');                 // creates a temp array, so if something is missing, it doesnt mess up assigning values below
                        for (int i = 0; i < tempValues.Length; i++)
                        {
                            values[i] = tempValues[i];
                        }
                        // set values
                        first    = values[0];
                        last     = values[1];
                        username = values[2];
                        password = values[3];
                        accType  = values[4];
                        // verify that required info is provided
                        failedItems = new string[5];
                        valid       = verifyInfo(first, last, username, password, accType);
                        // if it is
                        if (valid == true)
                        {
                            // check database to see if username exists
                            exists = dbm.checkUsername(username);
                            if (exists == false)
                            {
                                // salt and hash pasword
                                password = Salt(password);
                                int hashPass = Hash(password);
                                password = Convert.ToString(hashPass);
                                // add to database
                                added = dbm.addUser(first, last, username, password, accType);
                                // if the user was added, keep track of the addition
                                if (added == true)
                                {
                                    // user was added, update added info
                                    addedCount = updateAddedCount(addedCount);
                                    addedUserList.Add(first + " " + last + ": " + username);
                                }
                                else
                                {
                                    // user was not added, update failed info
                                    failedCount = updateFailedCount(failedCount);
                                    failedUserList.Add(first + " " + last + ": " + username);
                                }
                            }
                            else
                            {
                                // username already exists in DB
                                failedCount = updateFailedCount(failedCount);
                                failedUserList.Add(first + " " + last + ": " + username);
                            }
                        }
                        else
                        {
                            // if user was not added, keep track of failed additions
                            failedCount = updateFailedCount(failedCount);
                            failedUserList.Add(first + " " + last + ": " + username);
                        }
                    }
                    // create a message of how many added and failed users from batch file
                }
                msg = createBatchMsg(addedCount, failedCount);
                // empty the lists so the next batch file the admin uses doesnt have this files info in it
                addedUserList.Clear();
                failedUserList.Clear();
            }

            // used for text box entries
            else
            {
                // verify that required info is provided
                failedItems = new string[5];
                valid       = verifyInfo(first, last, username, password, accType);
                // if it is
                if (valid == true)
                {
                    // check database to see if username exists
                    exists = dbm.checkUsername(username);
                    if (exists == false)
                    {
                        // salt and hash pasword
                        password = Salt(password);
                        int hashPass = Hash(password);
                        password = Convert.ToString(hashPass);
                        // add to database
                        added = dbm.addUser(first, last, username, password, accType);
                        if (added == true)
                        {
                            // provide confirmation
                            msg = confirmMsg(username);
                        }
                        else
                        {
                            // this message is when the catch is called in the database
                            msg = "Error while adding user, can not use username: " + username;
                        }
                    }
                    else
                    {
                        msg = userExistsMsg(username);
                    }
                }
                else
                {
                    // provide feedback why it didn't get added
                    msg = failMsg(username);
                }
            }
            // display success/fail message
            ShowMsg(msg);
        }