Nearing feature complete for driver auth

This commit is contained in:
2013-01-09 21:33:57 -05:00
parent 0b4c7914b3
commit bc019923d2
49 changed files with 609 additions and 335 deletions
+3 -13
View File
@@ -3,6 +3,7 @@ using System.Web.Mvc;
using System.Web.Security;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Email;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels.Account;
@@ -146,20 +147,9 @@ namespace MileageTraker.Web.Controllers
var user = DataService.FindUserByUsername(viewModel.Username);
if (user != null && Request.Url != null)
{
var passwordResetToken = Algorithms.GenerateToken();
var url = Request.Url.Scheme +
@"://" +
Request.Url.Authority +
Url.Action("NewPassword", "Account",
new NewPasswordViewModel
{
UserId = user.UserId,
PasswordResetToken = passwordResetToken
});
user.PasswordResetToken = passwordResetToken;
DataService.UpdateUser(user);
var email = new EmailNotificationService();
email.NotifyResetPassword(user,url);
var resetPasswordUrl = ResetPassword(user);
email.SendResetPassword(user, resetPasswordUrl);
}
TempData["StatusMessage"] = "Please check your email - we have sent a request for you to reset the password.";
+44
View File
@@ -1,6 +1,10 @@
using System;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels.Account;
namespace MileageTraker.Web.Controllers
{
@@ -28,5 +32,45 @@ namespace MileageTraker.Web.Controllers
base.OnException(filterContext);
}
protected string GetCookieValue(string key)
{
return
HttpContext.Request.Cookies[key] != null
? HttpContext.Request.Cookies[key].Value
: null;
}
protected void SetCookieValue(string key, string value)
{
var cookies = HttpContext.Response.Cookies;
var httpCookie = cookies[key];
if (httpCookie == null) return;
httpCookie.Value = value;
httpCookie.Expires = DateTime.MaxValue;
}
protected string LinkForUrlAction(string urlAction)
{
return
string.Format("{0}://{1}{2}",
Request.Url.Scheme, Request.Url.Authority, urlAction);
}
protected string ResetPassword(User user)
{
var passwordResetToken = Algorithms.GenerateToken();
var url = LinkForUrlAction(
Url.Action("NewPassword", "Account",
new NewPasswordViewModel
{
UserId = user.UserId,
PasswordResetToken = passwordResetToken
}));
user.PasswordResetToken = passwordResetToken;
DataService.UpdateUser(user);
return url;
}
}
}
+6 -18
View File
@@ -6,6 +6,7 @@ using MileageTraker.Web.ViewModels.CreateLog;
namespace MileageTraker.Web.Controllers
{
[Authorize(Roles = "Driver, Administrator, Developer")]
public class CreateLogController : ControllerBase
{
private const string CookeNameVehicleid = "mr_vehicleId";
@@ -72,10 +73,12 @@ namespace MileageTraker.Web.Controllers
return View("Index", model);
}
public PartialViewResult RecentLogs(string employeeName)
public PartialViewResult RecentLogs()
{
var logs = DataService.GetRecentLogsByEmployee(employeeName);
ViewData["employeeName"] = employeeName;
var username = User.Identity.Name;
var user = DataService.FindUserByUsername(username);
var logs = DataService.GetRecentLogsByUsername(user.Username);
ViewData["name"] = user.FullName;
return PartialView(logs);
}
@@ -92,20 +95,5 @@ namespace MileageTraker.Web.Controllers
Date = DateTime.Today
};
}
private string GetCookieValue(string key)
{
return
HttpContext.Request.Cookies[key] != null
? HttpContext.Request.Cookies[key].Value
: null;
}
private void SetCookieValue(string key, string value)
{
var cookies = HttpContext.Response.Cookies;
cookies[key].Value = value;
cookies[key].Expires = DateTime.MaxValue;
}
}
}
+21 -22
View File
@@ -3,7 +3,6 @@ using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels;
using MileageTraker.Web.ViewModels.Log;
@@ -84,7 +83,8 @@ namespace MileageTraker.Web.Controllers
public ViewResult Details(int id)
{
var log = DataService.GetLog(id);
return View(log);
var viewModel = new LogViewModel(log);
return View(viewModel);
}
public ActionResult PreviousDetails(int id)
@@ -121,22 +121,24 @@ namespace MileageTraker.Web.Controllers
public ActionResult Create()
{
var log = new Log { Date = DateTime.Today };
var viewModel = new LogViewModel { Date = DateTime.Today };
var vehicleId = Request["vehicleId"];
if (vehicleId != null)
{
log.VehicleId = vehicleId;
viewModel.VehicleId = vehicleId;
}
return View(log);
return View(viewModel);
}
[HttpPost]
[ActionLog]
public ActionResult Create(Log log)
public ActionResult Create(LogViewModel viewModel)
{
if (ModelState.IsValid)
{
var log = viewModel.GetLog();
log.User = DataService.FindUserByFullName(viewModel.UserFullName);
log.Source = HttpContext.Request.Url.AbsolutePath;
log.UserHostAddress = HttpContext.Request.UserHostAddress;
log.UserAgent = HttpContext.Request.UserAgent;
@@ -147,42 +149,39 @@ namespace MileageTraker.Web.Controllers
return RedirectToAction("Index");
}
return View(log);
return View(viewModel);
}
public ActionResult Edit(int id)
{
var log = DataService.GetLog(id);
return View(log);
var logViewModel = new LogViewModel(log);
return View(logViewModel);
}
[HttpPost]
[ActionLog]
public ActionResult Edit(Log log)
public ActionResult Edit(LogViewModel viewModel)
{
RemoveModelStateErrors();
if (ModelState.IsValid)
{
DataService.UpdateLog(log);
var log = DataService.GetLog(viewModel.LogId);
viewModel.UpdateLog(log);
log.User = DataService.FindUserByFullName(viewModel.UserFullName);
DataService.UpdateLog(log);
TempData["StatusMessage"] = "Log updated";
return RedirectToAction("Details", new{id = log.LogId});
}
return View(log);
return View(viewModel);
}
private void RemoveModelStateErrors()
{
ModelState.Remove("Source");
ModelState.Remove("UserAgent");
ModelState.Remove("UserHostAddress");
ModelState.Remove("VehiclePreviousLogId");
ModelState.Remove("VehiclePreviousLog");
}
public ActionResult Delete(int id)
{
var log = DataService.GetLog(id);
return View(log);
var viewModel = new LogViewModel(log);
return View(viewModel);
}
[HttpPost, ActionName("Delete")]
+45 -18
View File
@@ -2,6 +2,9 @@
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using MileageTraker.Web.Email;
using MileageTraker.Web.ViewModels;
using MileageTraker.Web.ViewModels.Account;
using MileageTraker.Web.ViewModels.User;
namespace MileageTraker.Web.Controllers
@@ -21,6 +24,8 @@ namespace MileageTraker.Web.Controllers
return View(users);
}
//todo: add export
public ActionResult Details(Guid id)
{
var user = Membership.GetUser(id);
@@ -48,23 +53,32 @@ namespace MileageTraker.Web.Controllers
return Json(user == null, JsonRequestBehavior.AllowGet);
}
public JsonResult EmailAvailable(string email)
public JsonResult FullNameAvailable(string fullname)
{
var user = DataService.FindUserByEmail(email);
var user = DataService.FindUserByFullName(fullname);
return Json(user == null, JsonRequestBehavior.AllowGet);
}
public ActionResult Create()
public JsonResult ExistsByFullName(string userFullName)
{
var user = DataService.FindUserByFullName(userFullName);
return Json(user != null, JsonRequestBehavior.AllowGet);
}
public ActionResult Create()
{
var vm = new CreateUserViewModel
{
AvailableRoles = Roles.GetAllRoles()
Roles = new CheckBoxViewModel{
Available = Roles.GetAllRoles(),
Selected = new []{"Driver"} // default driver
}
};
return View(vm);
}
[HttpPost]
[HttpPost]
public ActionResult Create(CreateUserViewModel viewModel)
{
if (ModelState.IsValid)
@@ -72,8 +86,8 @@ namespace MileageTraker.Web.Controllers
MembershipCreateStatus membershipCreateStatus;
var membershipUser =
Membership.CreateUser(
viewModel.Username,
viewModel.Password,
viewModel.Username,
"uninitialized_state",
viewModel.Email,
null,
null,
@@ -83,30 +97,40 @@ namespace MileageTraker.Web.Controllers
if (membershipUser == null)
{
ModelState.AddModelError("", ErrorCodeToString(membershipCreateStatus));
viewModel.AvailableRoles = Roles.GetAllRoles();
viewModel.Roles.Available = Roles.GetAllRoles();
return View(viewModel);
}
if (viewModel.Roles != null && viewModel.Roles.Any())
if (viewModel.Roles.Selected != null && viewModel.Roles.Selected.Any())
{
Roles.AddUserToRoles(
membershipUser.UserName,
viewModel.Roles);
viewModel.Roles.Selected);
}
var user = DataService.GetUser((Guid) membershipUser.ProviderUserKey);
user.FullName = viewModel.FullName;
DataService.UpdateUserPersonalInfo(user);
TempData["StatusMessage"] = "User " + user.Username + " created";
if (viewModel.SetPassword)
{
TempData["StatusMessage"] = "User " + user.Username + " created";
return RedirectToAction("SetPassword", new { id = user.UserId });
}
var email = new EmailNotificationService();
var resetPasswordUrl = ResetPassword(user);
email.SendInitializePassword(user, resetPasswordUrl);
TempData["StatusMessage"] = "User " + user.Username + " created, invitation sent to " + user.Email;
return RedirectToAction("Index");
}
viewModel.AvailableRoles = Roles.GetAllRoles();
viewModel.Roles.Available = Roles.GetAllRoles();
return View(viewModel);
}
public ActionResult Edit(Guid id)
public ActionResult Edit(Guid id)
{
var user = DataService.GetUser(id);
if (user == null)
@@ -116,14 +140,17 @@ namespace MileageTraker.Web.Controllers
var vm = new EditUserViewModel(user)
{
Roles = Roles.GetRolesForUser(user.Username),
AvailableRoles = Roles.GetAllRoles()
Roles = new CheckBoxViewModel
{
Selected = Roles.GetRolesForUser(user.Username),
Available = Roles.GetAllRoles()
}
};
return View(vm);
}
[HttpPost]
[HttpPost]
public ActionResult Edit(EditUserViewModel viewModel)
{
if (ModelState.IsValid)
@@ -133,11 +160,11 @@ namespace MileageTraker.Web.Controllers
DataService.UpdateUserPersonalInfo(user);
Roles.RemoveUserFromRoles(user.Username, Roles.GetAllRoles());
if (viewModel.Roles != null && viewModel.Roles.Any())
if (viewModel.Roles != null)
{
Roles.AddUserToRoles(
user.Username,
viewModel.Roles);
viewModel.Roles.Selected);
}
TempData["StatusMessage"] = "Changes saved for " + user.Username;
-1
View File
@@ -63,7 +63,6 @@ namespace MileageTraker.Web.Controllers
return View(vehicle);
}
[AllowAnonymous]
public JsonResult Exists(string vehicleId)
{
var vehicle = DataService.GetVehicle(vehicleId);