Cleanup and fixes

This commit is contained in:
2013-01-12 14:58:33 -05:00
parent bc019923d2
commit e50052d41e
27 changed files with 240 additions and 156 deletions
+30 -14
View File
@@ -1,10 +1,9 @@
using System;
using System.Web.Mvc;
using System.Web.Security;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Email;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels.Account;
namespace MileageTraker.Web.Controllers
@@ -74,23 +73,31 @@ namespace MileageTraker.Web.Controllers
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
: null;
ViewBag.ReturnUrl = Url.Action("Manage");
return View();
var user = DataService.FindUserByUsername(User.Identity.Name);
var viewModel = new ChangePasswordViewModel(user);
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
[ActionLog]
public ActionResult Manage(ChangePasswordViewModel model)
{
ViewBag.ReturnUrl = Url.Action("Manage");
var membershipUser = Membership.GetUser(User.Identity.Name, true);
//var user = DataService.FindUserByUsername(User.Identity.Name);
//model.SetProperties(user);
if (ModelState.IsValid)
{
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
bool changePasswordSucceeded;
try
{
var currentUser = Membership.GetUser(User.Identity.Name, true);
changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
changePasswordSucceeded = membershipUser.ChangePassword(model.OldPassword, model.NewPassword);
}
catch (Exception)
{
@@ -130,7 +137,7 @@ namespace MileageTraker.Web.Controllers
/// View the Reset Password form
/// </summary>
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Get)]
[HttpGet]
public ViewResult ResetPassword(string username)
{
return View(new ResetPasswordViewModel{Username = username});
@@ -140,12 +147,13 @@ namespace MileageTraker.Web.Controllers
/// Begins the Reset Password process
/// </summary>
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Post)]
[HttpPost]
[ValidateAntiForgeryToken]
[ActionLog]
public ActionResult ResetPassword(ResetPasswordViewModel viewModel)
{
var user = DataService.FindUserByUsername(viewModel.Username);
if (user != null && Request.Url != null)
if (user != null && user.IsApproved && Request.Url != null)
{
var email = new EmailNotificationService();
var resetPasswordUrl = ResetPassword(user);
@@ -161,11 +169,12 @@ namespace MileageTraker.Web.Controllers
/// Action users are sent to when they reset their password.
/// </summary>
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Get)]
[HttpGet]
public ActionResult NewPassword(Guid userId, string passwordResetToken)
{
var user = DataService.GetUser(userId);
if (user != null && user.PasswordResetToken == passwordResetToken)
if (user != null && user.IsApproved &&
user.PasswordResetToken == passwordResetToken)
{
var newPasswordViewModel
= new NewPasswordViewModel
@@ -185,18 +194,25 @@ namespace MileageTraker.Web.Controllers
/// </summary>
/// <param name="viewModel">The view model.</param>
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Post)]
[HttpPost]
[ValidateAntiForgeryToken]
[ActionLog]
public ActionResult NewPassword(NewPasswordViewModel viewModel)
{
if (ModelState.IsValid)
{
var user = DataService.GetUser(viewModel.UserId);
if (user != null && user.PasswordResetToken == viewModel.PasswordResetToken)
if (user != null && user.IsApproved &&
user.PasswordResetToken == viewModel.PasswordResetToken)
{
DataService.UpdateUserPassword(viewModel.UserId, viewModel.NewPassword);
TempData["StatusMessage"] = "Password set for " + viewModel.Username;
return RedirectToAction("Login", new {username = viewModel.Username});
var success = Membership.ValidateUser(user.Username, viewModel.NewPassword);
if (success)
{
FormsAuthentication.SetAuthCookie(viewModel.Username, false);
TempData["StatusMessage"] = "Password set for " + viewModel.Username + ", logged in";
return RedirectToAction("Index", "CreateLog");
}
}
}
+4 -8
View File
@@ -22,17 +22,13 @@ namespace MileageTraker.Web.Controllers
[HttpPost]
public ActionResult Index(CreateLogViewModel model)
{
if (!ModelState.IsValid)
if (ModelState.IsValid)
{
ViewBag.updateError = "Create Failure";
if (model.LogType == null) // WTF why doesn't the model binder get this
model.LogType = new MileageLogTypeWrapper();
return View(model);
var confirmCreateLogViewModel = new ConfirmCreateLogViewModel(model);
return View("Confirm", confirmCreateLogViewModel);
}
var confirmCreateLogViewModel = new ConfirmCreateLogViewModel(model);
return View("Confirm", confirmCreateLogViewModel);
return View(model);
}
[HttpParamAction]
-13
View File
@@ -1,13 +0,0 @@
using System.Web.Mvc;
namespace MileageTraker.Web.Controllers
{
public class EmployeeController : ControllerBase
{
public JsonResult Autocomplete(string term)
{
var employees = DataService.GetEmployeeNamesAutocomplete(term);
return Json(employees, JsonRequestBehavior.AllowGet);
}
}
}
+16 -11
View File
@@ -52,13 +52,18 @@ namespace MileageTraker.Web.Controllers
public ActionResult Export(LogQueryViewModel query)
{
var logs = DataService.GetLogs();
var filteredLogs =
DataService.FilterLogs(logs, query)
.ToList()
.Select(log => new LogViewModel(log));
var name = string.Format(
"MileageLogs{0}-{1}{2}",
query.Year,
query.Month,
query.LogType.HasValue ? "-" + query.LogType.Value.GetDisplayShortName() : "");
var export = ExcelWriter.WriteXls(DataService.FilterLogs(logs, query), name, name);
var export = ExcelWriter.WriteXls(filteredLogs, name, name);
return File(export, "application/ms-excel", name + ".xls");
}
@@ -71,11 +76,11 @@ namespace MileageTraker.Web.Controllers
return View(report);
}
public ActionResult MonthlyEmployeeMileage(LogQueryViewModel query)
public ActionResult MonthlyDriverMileage(LogQueryViewModel query)
{
var items = DataService.GetMonthlyEmployeeMileageItems(query);
var report = new EmployeeMileageViewModel(items, query);
var report = new DriverMileageViewModel(items, query);
return View(report);
}
@@ -119,15 +124,15 @@ namespace MileageTraker.Web.Controllers
return RedirectToAction("Details", new { id = logId });
}
public ActionResult Create()
[HttpGet]
public ActionResult Create(string vehicleId)
{
var viewModel = new LogViewModel { Date = DateTime.Today };
var vehicleId = Request["vehicleId"];
var viewModel = new LogViewModel
{
Date = DateTime.Today,
VehicleId = vehicleId
};
if (vehicleId != null)
{
viewModel.VehicleId = vehicleId;
}
return View(viewModel);
}
@@ -166,7 +171,7 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
var log = DataService.GetLog(viewModel.LogId);
viewModel.UpdateLog(log);
viewModel.SetProperties(log);
log.User = DataService.FindUserByFullName(viewModel.UserFullName);
DataService.UpdateLog(log);
+38 -10
View File
@@ -2,9 +2,10 @@
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Email;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels;
using MileageTraker.Web.ViewModels.Account;
using MileageTraker.Web.ViewModels.User;
namespace MileageTraker.Web.Controllers
@@ -18,13 +19,28 @@ namespace MileageTraker.Web.Controllers
DataService
.GetUsers()
.ToList()
//.Where(u => u.Filter(q))
.OrderBy(u => u.Username);
return View(users);
}
//todo: add export
[ActionLog]
public ActionResult Export()
{
var users = DataService.GetUsers();
var userViewModels =
users
.ToList()
.OrderBy(u => u.Username)
.Select(log => new ExportUserViewModel(log));
var name = string.Format(
"MileageTrakerUsers_{0:MM-dd-yyyy}", DateTime.Today);
var export = ExcelWriter.WriteXls(userViewModels, name, name);
return File(export, "application/ms-excel", name + ".xls");
}
public ActionResult Details(Guid id)
{
@@ -78,6 +94,7 @@ namespace MileageTraker.Web.Controllers
return View(vm);
}
[ActionLog]
[HttpPost]
public ActionResult Create(CreateUserViewModel viewModel)
{
@@ -101,7 +118,7 @@ namespace MileageTraker.Web.Controllers
return View(viewModel);
}
if (viewModel.Roles.Selected != null && viewModel.Roles.Selected.Any())
if (viewModel.Roles != null)
{
Roles.AddUserToRoles(
membershipUser.UserName,
@@ -150,14 +167,15 @@ namespace MileageTraker.Web.Controllers
return View(vm);
}
[ActionLog]
[HttpPost]
public ActionResult Edit(EditUserViewModel viewModel)
{
if (ModelState.IsValid)
{
{
if (ModelState.IsValid)
{
var user = DataService.GetUser(viewModel.UserId);
viewModel.UpdateUser(user);
DataService.UpdateUserPersonalInfo(user);
DataService.UpdateUserPersonalInfo(user);
Roles.RemoveUserFromRoles(user.Username, Roles.GetAllRoles());
if (viewModel.Roles != null)
@@ -169,9 +187,9 @@ namespace MileageTraker.Web.Controllers
TempData["StatusMessage"] = "Changes saved for " + user.Username;
return RedirectToAction("Details", new { id = viewModel.UserId});
}
}
return View(viewModel);
}
}
public ActionResult SetPassword(Guid id)
{
@@ -197,6 +215,7 @@ namespace MileageTraker.Web.Controllers
return View(viewModel);
}
[ActionLog]
public ActionResult DisableUser(Guid id)
{
var user = DataService.GetUser(id);
@@ -214,6 +233,7 @@ namespace MileageTraker.Web.Controllers
return RedirectToAction("Index");
}
[ActionLog]
public ActionResult EnableUser(Guid id)
{
var user = DataService.GetUser(id);
@@ -231,6 +251,7 @@ namespace MileageTraker.Web.Controllers
return RedirectToAction("Index");
}
[ActionLog]
public ActionResult UnlockUser(Guid id)
{
var user = DataService.GetUser(id);
@@ -249,6 +270,13 @@ namespace MileageTraker.Web.Controllers
return RedirectToAction("Index");
}
[Authorize(Roles = "Driver, Administrator, Developer")]
public JsonResult Autocomplete(string term)
{
var employees = DataService.GetUserFullNamesAutocomplete(term);
return Json(employees, JsonRequestBehavior.AllowGet);
}
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
+9 -3
View File
@@ -1,5 +1,6 @@
using System;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels.Vehicle;
@@ -33,12 +34,14 @@ namespace MileageTraker.Web.Controllers
}
[HttpPost]
public ActionResult Create(Vehicle vehicle)
[ActionLog]
public ActionResult Create(Vehicle vehicle)
{
if (ModelState.IsValid)
{
DataService.AddVehicle(vehicle);
TempData["StatusMessage"] = "Vehicle " + vehicle.VehicleId + "created";
TempData["StatusMessage"] =
string.Format("Vehicle {0} created", vehicle.VehicleId);
return RedirectToAction("Index");
}
@@ -52,7 +55,8 @@ namespace MileageTraker.Web.Controllers
}
[HttpPost]
public ActionResult Edit(Vehicle vehicle)
[ActionLog]
public ActionResult Edit(Vehicle vehicle)
{
if (ModelState.IsValid)
{
@@ -63,12 +67,14 @@ namespace MileageTraker.Web.Controllers
return View(vehicle);
}
[AllowAnonymous]
public JsonResult Exists(string vehicleId)
{
var vehicle = DataService.GetVehicle(vehicleId);
return Json(vehicle != null, JsonRequestBehavior.AllowGet);
}
[ActionLog]
public FileResult Export()
{
var vehicles = DataService.GetVehicles();