Ejemplo n.º 1
0
        /// <summary>
        /// iterates through all different permissoins of each permission object and creates a command for each of them
        /// it does make  alot of commands but it okay
        /// </summary>
        /// <returns></returns>
        public IResult Update()
        {
            String message   = "";
            bool   isSuccess = false;

            var commands = new List <SqlCommand>();

            foreach (Permission per in _permissions)
            {
                foreach (Tuple <String, bool> tup in per.Permissions)
                {
                    //var action = "";
                    SqlCommand cmd;
                    if (tup.Item2 == true)
                    {
                        cmd = new SqlCommand($"INSERT INTO {Environment.GetEnvironmentVariable("tableNamePermissions", EnvironmentVariableTarget.User)} (SysID, Permission) VALUES (@userID, @singlePermission)");
                    }
                    else
                    {
                        cmd = new SqlCommand($"DELETE FROM {Environment.GetEnvironmentVariable("tableNamePermissions", EnvironmentVariableTarget.User)} WHERE SysID = @userID AND Permission = @singlePermission");
                    }

                    //cmd.Parameters.AddWithValue("@action", action);
                    //cmd.Parameters.AddWithValue("@tableName", Environment.GetEnvironmentVariable("tableNamePermissions", EnvironmentVariableTarget.User));
                    cmd.Parameters.AddWithValue("@singlePermission", tup.Item1);
                    cmd.Parameters.AddWithValue("@userID", per.UserID);
                    commands.Add(cmd);
                }
            }

            var rowsChanged = _dao.RunCommand(commands);

            if (rowsChanged != commands.Count)
            {
                isSuccess = false;
                message  += ConfigurationManager.AppSettings["failureMessage"];
            }
            else
            {
                isSuccess = true;
                message  += ConfigurationManager.AppSettings["successMessage"];
            }



            return(new CheckResult(message, isSuccess));
        }
        /// <summary>
        /// this is the publicly accessed interface method that returns an IResult based on success of updating all accounts
        /// </summary>
        /// <returns>CheckResult</returns>
        public IResult Update()
        {
            String            message   = "";
            bool              isSuccess = true;
            List <SqlCommand> commands  = new List <SqlCommand>();

            //creates a set of queries within a list to pass towards a dao for updating
            foreach (User newUser in _newUsers)
            {
                var cmd = new SqlCommand(ConfigurationManager.AppSettings["updateCmd"]);
                cmd.Parameters.AddWithValue("@email", newUser.UserEmail);
                cmd.Parameters.AddWithValue("@fName", newUser.FirstName);
                cmd.Parameters.AddWithValue("@lName", newUser.LastName);
                cmd.Parameters.AddWithValue("@dob", newUser.DateOfBirth);
                cmd.Parameters.AddWithValue("@gender", newUser.Gender);
                cmd.Parameters.AddWithValue("@status", newUser.AccountStatus);

                commands.Add(cmd);
            }

            //changing result based upon if all accounts were updated successfully
            int rowsChanged = _update.RunCommand(commands);

            if (rowsChanged == commands.Count)
            {
                message += ConfigurationManager.AppSettings["successMessage"];
            }
            else
            {
                message  += ConfigurationManager.AppSettings["failureMessage"];
                isSuccess = false;
            }



            return(new CheckResult(message, isSuccess));
        }
        /// <summary>
        /// Delete a user, starting with their system information,
        /// </summary>
        /// <returns></returns>
        public IResult Delete()
        {
            string message      = "";
            bool   isSuccess    = true;
            int    totalSuccess = 0;

            List <SqlCommand> commands  = new List <SqlCommand>();
            IMapperDAO        mapperDAO = new SqlMapperDAO(Environment.GetEnvironmentVariable("sqlConnectionMapping", EnvironmentVariableTarget.User));

            foreach (User targetUser in _targetUsers)
            {
                List <SqlCommand> deleteUserCommands = new List <SqlCommand>();
                var cmd = new SqlCommand(ConfigurationManager.AppSettings["queryDeleteSystem"]);
                cmd.Parameters.AddWithValue("@sysID", targetUser.SystemID);
                deleteUserCommands.Add(cmd);

                int rowsDeleted = _deleteSystemdb.RunCommand(deleteUserCommands);
                if (rowsDeleted > 0)
                {
                    List <SqlCommand> deleteMapperCommands = new List <SqlCommand>();
                    deleteMapperCommands = new List <SqlCommand>();
                    cmd = new SqlCommand(ConfigurationManager.AppSettings["queryDeleteMapping"]);
                    cmd.Parameters.AddWithValue("@sysID", mapperDAO.GetSysID(targetUser.UserEmail));
                    deleteMapperCommands.Add(cmd);
                    rowsDeleted = _deleteMappingdb.RunCommand(deleteMapperCommands);
                    if (rowsDeleted > 0)
                    {
                        List <SqlCommand> deleteAccountCommands = new List <SqlCommand>();
                        deleteAccountCommands = new List <SqlCommand>();
                        cmd = new SqlCommand(ConfigurationManager.AppSettings["queryDeleteAccount"]);
                        cmd.Parameters.AddWithValue("@email", targetUser.UserEmail);
                        deleteAccountCommands.Add(cmd);
                        rowsDeleted = _deleteAccountdb.RunCommand(deleteAccountCommands);
                        if (rowsDeleted > 0)
                        {
                            totalSuccess += 1;
                        }
                        else
                        {
                            message  += "Failed to delete from Accounts!";
                            isSuccess = false;
                        }
                    }
                    else
                    {
                        message  += "Failed to delete from Mapping!";
                        isSuccess = false;
                    }
                }
                else
                {
                    message  += "Failed to delete from System!";
                    isSuccess = false;
                }
            }
            if (totalSuccess == _targetUsers.Count)
            {
                message += ConfigurationManager.AppSettings["DeleteAccountSuccess"];
            }
            else
            {
                message  += ConfigurationManager.AppSettings["DeleteAccountFailure"];
                isSuccess = false;
            }
            return(new CheckResult(message, isSuccess));
        }