public ActionResult AddUser(Models.Account.User user)
        {
            bool isEmailError = false;

            try
            {
                user.Password = CreateTempPassword(8);
                user.NewPassword = user.Password;
                user.Guid = Guid.NewGuid();

                // all users are Admins
                user.IsAdmin = true;

                if (_db.AddUser(user))
                {
                    isEmailError = true;
                    Logger.Log(LogLevel.Info, "User has been created [email = " + user.Email + "], requested by " + User.Identity.Name);

                    string urlAuthority = Request.Url.GetLeftPart(UriPartial.Authority);
                    var smtp = new SmtpMail();
                    smtp.SendPasswordResetEmail(user, urlAuthority);
                    isEmailError = false;

                    return RedirectToAction("ManageUsers", "Admin");
                }
                else
                {
                    ModelState.AddModelError("Error", ResourceCache.Localize("duplicate_user_msg"));
                    Logger.Log(LogLevel.Error, "Error in AddUser: duplicate user [email = " + user.Email + "], requested by " + User.Identity.Name);
                }
            }
            catch (Exception e)
            {
                if (isEmailError)
                {
                    ModelState.AddModelError("Error", ResourceCache.Localize("email_error_msg"));
                }
                else
                {
                    ModelState.AddModelError("Error", ResourceCache.Localize("error_msg"));
                }

                Logger.Log(LogLevel.Error, "Error in AddUser [email = " + user.Email + "], requested by " + User.Identity.Name + ": " + e.Message);
            }

            return View(user);
        }
        private bool EmailDocument(long responseId)
        {
            var model = _db.GetAnswers(QuestionCache.AssessmentName, _culture, responseId);

            var scoringModel = new ScoringModel();
            var report = scoringModel.GetReport(model);

            var smtpMail = new SmtpMail();
            return smtpMail.SendReport(report);
        }
        public ActionResult _requestResetPassword(string userId)
        {
            string outcome = "success";
            string message = string.Empty;

            long id = 0;
            long.TryParse(userId, out id);
            var model = _db.GetUser(id);

            model.NewPassword = CreateTempPassword(8);

            try
            {
                if (_db.ResetPassword(model))
                {
                    string urlAuthority = Request.Url.GetLeftPart(UriPartial.Authority);
                    var smtp = new SmtpMail();
                    smtp.SendPasswordResetEmail(model, urlAuthority);
                    TempData["Email"] = model.Email;
                    Logger.Log(LogLevel.Info, "Password has been reset [user name = " + model.UserName + "], requested by " + User.Identity.Name);
                }
                else
                {
                    outcome = "error";
                    Logger.Log(LogLevel.Error, "Reset Password failed [user name = " + model.UserName + "], requested by " + User.Identity.Name);
                }
            }
            catch (Exception e)
            {
                outcome = "error";
                Logger.Log(LogLevel.Error, "Error in _requestResetPassword [user name = " + model.UserName + "], requested by " +  User.Identity.Name + ": " + e.Message);
            }

            return Json(new { result = outcome, message = message }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ResetPassword(Models.Account.User model)
        {
            model.NewPassword = CreateTempPassword(8);

            try
            {
                if (_db.ResetPassword(model))
                {
                    Logger.Log(LogLevel.Info, "Password has been reset [user name = " + model.UserName + "]");
                    string urlAuthority = Request.Url.GetLeftPart(UriPartial.Authority);
                    var smtp = new SmtpMail();
                    smtp.SendPasswordResetEmail(model, urlAuthority);
                    TempData["EmailSent"] = bool.TrueString;
                }
                else
                {
                    TempData["EmailSent"] = bool.FalseString;
                    Logger.Log(LogLevel.Error, "Password was not reset [user name = " + model.UserName + "]");
                }

                return RedirectToAction("ResetPasswordConfirmation");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", ResourceCache.Localize("ui_request_not_processed"));
                Logger.Log(LogLevel.Error, "Error in ResetPassword [user name = " + model.UserName + "]: " + e.Message);
            }

            return View();
        }
        public ActionResult EmailCsv(string id, string numberofdays, string dayofweek)
        {
            if (id != _csvReportId) return null;

            string msg = null;
            bool success = false;
            var today = DateTime.Today;

            try
            {
                int dof = 0;
                if (!string.IsNullOrEmpty(dayofweek) && !int.TryParse(dayofweek, out dof))
                {
                    msg = "Invalid dayofweek parameter: " + dayofweek;
                    Logger.Log(LogLevel.Error, msg);
                    Response.Write(msg);
                    return null;
                }

                // date to execute: 0 today, 1 Sunday, 2 Monday, 3 Tuesday, 4 Wednesday, 5 Thursday, 6 Friday, 7 Saturday
                if (dof != 0 && dof != (int)today.DayOfWeek + 1) return null;

                // number of days csv report will include in the data, default to 7 days
                int days = 7;
                if (!string.IsNullOrEmpty(numberofdays) && !int.TryParse(numberofdays, out days))
                {
                    msg = "Invalid numberofdays parameter: " + numberofdays;
                    Logger.Log(LogLevel.Error, msg);
                    Response.Write(msg);
                    return null;
                }

                var startDate = today.AddDays(-days);

                var report = new CsvReport(QuestionCache.AssessmentName, _csvReportViewName);
                string csv = report.ToCSV(_cultureName, startDate, today);
                string fileName = FormatCsvReportFileName(startDate, today.AddDays(-1));

                var smtpMail = new SmtpMail();
                success = smtpMail.SendCsvReport(csv, fileName);
            }
            catch (Exception e)
            {
                msg = e.Message;
                Logger.Log(LogLevel.Error, msg);
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
            }

            if (success)
            {
                Response.Write("Success");
            }
            else
            {
                if (msg == null) msg = "Failed to send email";
                Response.Write(msg);
            }

            return null;
        }