Admin auth functionally complete
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
using MileageTraker.Web.DAL;
|
||||
using MileageTraker.Web.ViewModels.Account;
|
||||
|
||||
namespace MileageTraker.Web.Controllers
|
||||
@@ -22,17 +23,28 @@ namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var success = Membership.ValidateUser(model.Username, model.Password);
|
||||
if (success)
|
||||
try
|
||||
{
|
||||
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
|
||||
TempData["StatusMessage"] = "Logged in as " + model.Username;
|
||||
return RedirectToLocal(returnUrl);
|
||||
var success = Membership.ValidateUser(model.Username, model.Password);
|
||||
if (success)
|
||||
{
|
||||
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
|
||||
TempData["StatusMessage"] = "Logged in as " + model.Username;
|
||||
return RedirectToLocal(returnUrl);
|
||||
}
|
||||
ModelState.AddModelError("", "The user name or password provided is incorrect.");
|
||||
}
|
||||
catch (UserAccountDisabledException)
|
||||
{
|
||||
ModelState.AddModelError("", "Account is disabled for " + model.Username + ".");
|
||||
}
|
||||
catch (UserLockedOutException)
|
||||
{
|
||||
ModelState.AddModelError("", "Too many failed password attempts for " + model.Username + ". Account is locked.");
|
||||
}
|
||||
}
|
||||
|
||||
// If we got this far, something failed, redisplay form
|
||||
ModelState.AddModelError("", "The user name or password provided is incorrect.");
|
||||
return View(model);
|
||||
}
|
||||
|
||||
@@ -40,9 +52,10 @@ namespace MileageTraker.Web.Controllers
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult LogOff()
|
||||
{
|
||||
TempData["StatusMessage"] = User.Identity.Name + " logged off";
|
||||
|
||||
FormsAuthentication.SignOut();
|
||||
|
||||
TempData["StatusMessage"] = "Logged off";
|
||||
return RedirectToAction("Index", "CreateLog");
|
||||
}
|
||||
|
||||
@@ -81,7 +94,7 @@ namespace MileageTraker.Web.Controllers
|
||||
ViewBag.StatusMessage =
|
||||
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
|
||||
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
|
||||
: "";
|
||||
: null;
|
||||
ViewBag.ReturnUrl = Url.Action("Manage");
|
||||
return View();
|
||||
}
|
||||
@@ -108,7 +121,7 @@ namespace MileageTraker.Web.Controllers
|
||||
|
||||
if (changePasswordSucceeded)
|
||||
{
|
||||
TempData["StatusMessage"] = ManageMessageId.ChangePasswordSuccess;
|
||||
TempData["StatusMessage"] = "Your password has been changed.";
|
||||
return RedirectToAction("Manage");
|
||||
}
|
||||
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace MileageTraker.Web.Controllers
|
||||
[HttpParamAction]
|
||||
[HttpPost]
|
||||
[ActionLog]
|
||||
public ViewResult Confirm(CreateLogViewModel model)
|
||||
public ActionResult Confirm(CreateLogViewModel model)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
@@ -59,7 +59,16 @@ namespace MileageTraker.Web.Controllers
|
||||
log.UserHostAddress = HttpContext.Request.UserHostAddress;
|
||||
log.UserAgent = HttpContext.Request.UserAgent;
|
||||
DataService.AddLog(log);
|
||||
return View("Success", model);
|
||||
TempData["StatusMessage-Type"] = "alert-success";
|
||||
TempData["StatusMessage"] =
|
||||
@"You've successfully created an entry
|
||||
for <strong>" + model.EmployeeName + @"</strong>
|
||||
traveling to <strong>" + model.CityName + @"</strong>
|
||||
on <strong>" + model.Date.ToShortDateString() + @"</strong>
|
||||
in Vehicle Id <strong>" + model.VehicleId + @"</strong>
|
||||
ending in <strong>" + model.EndOdometer + @"</strong>
|
||||
miles on the odometer.";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
return View("Index", model);
|
||||
|
||||
@@ -24,6 +24,18 @@ namespace MileageTraker.Web.Controllers
|
||||
return View(DataService.GetUser(id));
|
||||
}
|
||||
|
||||
public JsonResult UsernameAvailable(string username)
|
||||
{
|
||||
var user = DataService.FindUserByUsername(username);
|
||||
return Json(user == null, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
public JsonResult EmailAvailable(string email)
|
||||
{
|
||||
var user = DataService.FindUserByEmail(email);
|
||||
return Json(user == null, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
public ActionResult Create()
|
||||
{
|
||||
var vm = new CreateUserViewModel
|
||||
@@ -111,7 +123,7 @@ namespace MileageTraker.Web.Controllers
|
||||
}
|
||||
|
||||
TempData["StatusMessage"] = "Changes saved for " + user.Username;
|
||||
return RedirectToAction("Index");
|
||||
return RedirectToAction("Details", new { id = viewModel.UserId});
|
||||
}
|
||||
return View(viewModel);
|
||||
}
|
||||
@@ -132,7 +144,6 @@ namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
|
||||
try
|
||||
{
|
||||
DataService.UpdateUserPassword(viewModel.UserId, viewModel.NewPassword);
|
||||
@@ -149,6 +160,58 @@ namespace MileageTraker.Web.Controllers
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
public ActionResult DisableUser(Guid id)
|
||||
{
|
||||
var user = DataService.GetUser(id);
|
||||
if (user == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
user.IsApproved = false;
|
||||
DataService.UpdateUser(user);
|
||||
|
||||
TempData["StatusMessage"] = user.Username + " disabled";
|
||||
|
||||
if (Request.UrlReferrer != null)
|
||||
return Redirect(Request.UrlReferrer.AbsolutePath);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
public ActionResult EnableUser(Guid id)
|
||||
{
|
||||
var user = DataService.GetUser(id);
|
||||
if (user == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
user.IsApproved = true;
|
||||
DataService.UpdateUser(user);
|
||||
|
||||
TempData["StatusMessage"] = user.Username + " enabled";
|
||||
|
||||
if (Request.UrlReferrer != null)
|
||||
return Redirect(Request.UrlReferrer.AbsolutePath);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
public ActionResult UnlockUser(Guid id)
|
||||
{
|
||||
var user = DataService.GetUser(id);
|
||||
if (user == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
user.IsLockedOut = false;
|
||||
user.PasswordFailuresSinceLastSuccess = 0;
|
||||
DataService.UpdateUser(user);
|
||||
|
||||
TempData["StatusMessage"] = user.Username + " unlocked";
|
||||
|
||||
if (Request.UrlReferrer != null)
|
||||
return Redirect(Request.UrlReferrer.AbsolutePath);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
|
||||
|
||||
@@ -62,7 +62,8 @@ namespace MileageTraker.Web.Controllers
|
||||
}
|
||||
return View(vehicle);
|
||||
}
|
||||
|
||||
|
||||
[AllowAnonymous]
|
||||
public JsonResult Exists(string vehicleId)
|
||||
{
|
||||
var vehicle = DataService.GetVehicle(vehicleId);
|
||||
|
||||
Reference in New Issue
Block a user