Set password operational

Sitewide plan for status messages
This commit is contained in:
2012-12-29 20:41:50 -05:00
parent ce6fd9c215
commit 8739251066
32 changed files with 332 additions and 144 deletions
+4
View File
@@ -78,6 +78,10 @@ footer {
padding: 4px;
}
.alert .close {
position: static;
}
.qtip-content dt {
line-height: 10px;
width: 110px;
+4 -41
View File
@@ -26,7 +26,7 @@ namespace MileageTraker.Web.Controllers
if (success)
{
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
// TODO: send notification to user
TempData["StatusMessage"] = "Logged in as " + model.Username;
return RedirectToLocal(returnUrl);
}
}
@@ -42,7 +42,7 @@ namespace MileageTraker.Web.Controllers
{
FormsAuthentication.SignOut();
// TODO: send notification to user
TempData["StatusMessage"] = "Logged off";
return RedirectToAction("Index", "CreateLog");
}
@@ -108,7 +108,8 @@ namespace MileageTraker.Web.Controllers
if (changePasswordSucceeded)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
TempData["StatusMessage"] = ManageMessageId.ChangePasswordSuccess;
return RedirectToAction("Manage");
}
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
}
@@ -131,43 +132,5 @@ namespace MileageTraker.Web.Controllers
ChangePasswordSuccess,
SetPasswordSuccess,
}
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
// a full list of status codes.
switch (createStatus)
{
case MembershipCreateStatus.DuplicateUserName:
return "User name already exists. Please enter a different user name.";
case MembershipCreateStatus.DuplicateEmail:
return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
case MembershipCreateStatus.InvalidPassword:
return "The password provided is invalid. Please enter a valid password value.";
case MembershipCreateStatus.InvalidEmail:
return "The e-mail address provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidAnswer:
return "The password retrieval answer provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidQuestion:
return "The password retrieval question provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidUserName:
return "The user name provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.ProviderError:
return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
case MembershipCreateStatus.UserRejected:
return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
default:
return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
}
}
}
}
-1
View File
@@ -2,7 +2,6 @@
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels.CreateLog;
namespace MileageTraker.Web.Controllers
+6 -2
View File
@@ -98,7 +98,7 @@ namespace MileageTraker.Web.Controllers
else
{
logId = id;
TempData["Message"] = "This is the first log for this vehicle";
TempData["StatusMessage"] = "This is the first log for this vehicle";
}
return RedirectToAction("Details", new {id = logId});
}
@@ -114,7 +114,7 @@ namespace MileageTraker.Web.Controllers
else
{
logId = id;
TempData["Message"] = "This is the most recent log for this vehicle";
TempData["StatusMessage"] = "This is the most recent log for this vehicle";
}
return RedirectToAction("Details", new { id = logId });
}
@@ -142,6 +142,8 @@ namespace MileageTraker.Web.Controllers
log.UserAgent = HttpContext.Request.UserAgent;
DataService.AddLog(log);
TempData["StatusMessage"] = "Log created";
return RedirectToAction("Index");
}
@@ -162,6 +164,7 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
DataService.UpdateLog(log);
TempData["StatusMessage"] = "Log updated";
return RedirectToAction("Details", new{id = log.LogId});
}
return View(log);
@@ -188,6 +191,7 @@ namespace MileageTraker.Web.Controllers
{
DataService.DeleteLog(id);
TempData["StatusMessage"] = "Log deleted";
if (Session["LogPage"] != null)
return Redirect((string) Session["LogPage"]);
return RedirectToAction("Index");
+62 -14
View File
@@ -2,8 +2,6 @@
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels.User;
namespace MileageTraker.Web.Controllers
@@ -41,8 +39,6 @@ namespace MileageTraker.Web.Controllers
{
if (ModelState.IsValid)
{
var hasRoles = viewModel.Roles != null && viewModel.Roles.Any();
MembershipCreateStatus membershipCreateStatus;
var membershipUser =
Membership.CreateUser(
@@ -51,7 +47,7 @@ namespace MileageTraker.Web.Controllers
viewModel.Email,
null,
null,
hasRoles,
true,
out membershipCreateStatus);
if (membershipUser == null)
@@ -61,19 +57,19 @@ namespace MileageTraker.Web.Controllers
return View(viewModel);
}
if (hasRoles)
if (viewModel.Roles != null && viewModel.Roles.Any())
{
Roles.AddUserToRoles(
membershipUser.UserName,
viewModel.Roles);
}
var user = DataService.GetUser(
(Guid) membershipUser.ProviderUserKey);
var user = DataService.GetUser((Guid) membershipUser.ProviderUserKey);
user.FullName = viewModel.FullName;
DataService.UpdateUserPersonalInfo(user);
return RedirectToAction("Index");
TempData["StatusMessage"] = "User " + user.Username + " created";
return RedirectToAction("Index");
}
viewModel.AvailableRoles = Roles.GetAllRoles();
@@ -87,20 +83,72 @@ namespace MileageTraker.Web.Controllers
{
return HttpNotFound();
}
return View(user);
var vm = new EditUserViewModel(user)
{
Roles = Roles.GetRolesForUser(user.Username),
AvailableRoles = Roles.GetAllRoles()
};
return View(vm);
}
[HttpPost]
public ActionResult Edit(User user)
public ActionResult Edit(EditUserViewModel viewModel)
{
if (ModelState.IsValid)
{
DataService.UpdateUser(user);
return RedirectToAction("Index");
var user = DataService.GetUser(viewModel.UserId);
viewModel.UpdateUser(user);
DataService.UpdateUserPersonalInfo(user);
Roles.RemoveUserFromRoles(user.Username, Roles.GetAllRoles());
if (viewModel.Roles != null && viewModel.Roles.Any())
{
Roles.AddUserToRoles(
user.Username,
viewModel.Roles);
}
TempData["StatusMessage"] = "Changes saved for " + user.Username;
return RedirectToAction("Index");
}
return View(user);
return View(viewModel);
}
public ActionResult SetPassword(Guid id)
{
var user = Membership.GetUser(id);
if (user == null)
{
return HttpNotFound();
}
var viewModel = new SetPasswordViewModel {UserId = id, Username = user.UserName};
return View(viewModel);
}
[HttpPost]
public ActionResult SetPassword(SetPasswordViewModel viewModel)
{
if (ModelState.IsValid)
{
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
try
{
DataService.UpdateUserPassword(viewModel.UserId, viewModel.NewPassword);
TempData["StatusMessage"] = "Password set for " + viewModel.Username;
return RedirectToAction("Details", new { id = viewModel.UserId});
}
catch (Exception)
{
ModelState.AddModelError("", "The new password is invalid.");
}
}
// If we got this far, something failed, redisplay form
return View(viewModel);
}
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
+3 -1
View File
@@ -38,7 +38,8 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
DataService.AddVehicle(vehicle);
return RedirectToAction("Index");
TempData["StatusMessage"] = "Vehicle " + vehicle.VehicleId + "created";
return RedirectToAction("Index");
}
return View(vehicle);
@@ -56,6 +57,7 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
DataService.UpdateVehicle(vehicle);
TempData["StatusMessage"] = "Changes saved for vehicle " + vehicle.VehicleId;
return RedirectToAction("Details", new { id = vehicle.VehicleId });
}
return View(vehicle);
+1 -1
View File
@@ -116,7 +116,7 @@ namespace MileageTraker.Web.DAL
return newUser.ToMembershipUser(Membership.Provider.Name);
}
}
public override bool ValidateUser(string username, string password)
{
if (string.IsNullOrEmpty(username))
+3 -4
View File
@@ -196,13 +196,12 @@ namespace MileageTraker.Web.DAL
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotSupportedException();
using (var context = new MileageTrakerContext())
using (var context = new DataService())
{
foreach (var username in usernames)
{
var us = username;
var user = context.Users.FirstOrDefault(u => u.Username == us);
var user = context.GetUsers().FirstOrDefault(u => u.Username == us);
if (user != null)
{
foreach (var roleName in roleNames)
@@ -214,9 +213,9 @@ namespace MileageTraker.Web.DAL
user.Roles.Remove(role);
}
}
context.UpdateUser(user);
}
}
context.SaveChanges();
}
}
}
+8 -9
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
@@ -471,6 +470,14 @@ namespace MileageTraker.Web.DAL
UpdateUser(original);
}
public void UpdateUserPassword(Guid userId, string password)
{
var user = GetUser(userId);
user.Password = Crypto.HashPassword(password);
user.LastPasswordChangedDate = DateTime.Now;
UpdateUser(user);
}
public void UpdateUserLastActivity(string username)
{
var user = FindUserByUsername(username);
@@ -505,13 +512,5 @@ namespace MileageTraker.Web.DAL
{
return _db.Roles;
}
public Role GetRoleByName(string roleName)
{
return
(from role in _db.Roles
where roleName == role.RoleName
select role).FirstOrDefault();
}
}
}
+5
View File
@@ -42,6 +42,7 @@ namespace MileageTraker.Web.Models
[HiddenInput(DisplayValue = false)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime LastPasswordFailureDate { get; set; }
[HiddenInput(DisplayValue = false)]
@@ -50,14 +51,17 @@ namespace MileageTraker.Web.Models
[HiddenInput(DisplayValue = false)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime LastLockoutDate { get; set; }
[HiddenInput(DisplayValue = false)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime LastLoginDate { get; set; }
[HiddenInput(DisplayValue = false)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime Created { get; set; }
[HiddenInput]
@@ -65,6 +69,7 @@ namespace MileageTraker.Web.Models
[HiddenInput]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime LastPasswordChangedDate { get; set; }
[HiddenInput(DisplayValue = false)]
+2 -2
View File
@@ -107,9 +107,9 @@ namespace MileageTraker.Web.Utility
public static string ToVerboseStringHistoric(this TimeSpan ts)
{
if (ts.TotalDays == 0)
if (ts.TotalDays < 1)
return "Today";
if (ts.TotalDays == 1)
if (ts.TotalDays < 2)
return "Yesterday";
var sb = new StringBuilder();
+5 -20
View File
@@ -1,13 +1,10 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using AutoMapper;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
namespace MileageTraker.Web.ViewModels.User
{
public class CreateUserViewModel
public class CreateUserViewModel : RolesViewModel
{
[Required]
[StringLength(64)]
@@ -28,25 +25,13 @@ namespace MileageTraker.Web.ViewModels.User
[Required, DataType(DataType.Password)]
[StringLength(16, MinimumLength = 6)]
[InputSize("medium")]
public string Password { get; set; }
public string[] Roles { get; set; }
public IList<string> AvailableRoles { get; set; }
public IList<string> SelectedRoles { get; set; }
static CreateUserViewModel()
{
Mapper.CreateMap<CreateUserViewModel, Models.User>();
Mapper.CreateMap<string, Role>()
.ConvertUsing(
roleName =>
{
using (var ds = new DataService())
{
return ds.GetRoleByName(roleName);
}
});
Mapper.CreateMap<CreateUserViewModel, Models.User>()
.ForMember(vm => vm.Roles, opt => opt.Ignore());
}
public Models.User ToUser()
+54
View File
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AutoMapper;
using MileageTraker.Web.Attributes;
namespace MileageTraker.Web.ViewModels.User
{
public class EditUserViewModel : RolesViewModel
{
[HiddenInput(DisplayValue = false)]
public Guid UserId { get; set; }
[Required]
[StringLength(64)]
[InputSize("small")]
public string Username { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}", ErrorMessage = "Must be an email address")]
[InputSize("large")]
public string Email { get; set; }
[Required]
[StringLength(128)]
[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Need complete name")]
[InputSize("medium")]
public string FullName { get; set; }
static EditUserViewModel()
{
Mapper.CreateMap<Models.User, EditUserViewModel>()
.ForMember(u => u.Roles, opt => opt.Ignore());
}
public EditUserViewModel()
{
}
public EditUserViewModel(Models.User user)
{
Mapper.Map(user, this);
}
public void UpdateUser(Models.User user)
{
user.Username = this.Username;
user.FullName = this.FullName;
user.Email = this.Email;
}
}
}
+10
View File
@@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace MileageTraker.Web.ViewModels.User
{
public class RolesViewModel
{
public string[] Roles { get; set; }
public IList<string> AvailableRoles { get; set; }
}
}
@@ -0,0 +1,26 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MileageTraker.Web.ViewModels.User
{
public class SetPasswordViewModel
{
[HiddenInput(DisplayValue = false)]
public Guid UserId { get; set; }
[HiddenInput(DisplayValue = false)]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}
@@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MileageTraker.Web.ViewModels.User
{
public class UserDetailsViewModel
{
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string[] Roles { get; set; }
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime LastActivityDate { get; set; }
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime CreateDate { get; set; }
public bool IsLockedOut { get; set; }
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
public DateTime LastLockoutDate { get; set; }
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
<h2 class="center-content">@ViewBag.Title</h2>
<p class="center-content label label-success">@ViewBag.StatusMessage</p>
@Html.Partial("_StatusMessage")
<div class="center-content well">
+2
View File
@@ -6,6 +6,8 @@
@{ Html.RenderPartial("BackToLogs"); }
@Html.Partial("_StatusMessage")
<h2 class="center-content">@ViewBag.Title</h2>
<div>
+2
View File
@@ -9,6 +9,8 @@
<link href="@Url.Content("~/Content/VehicleColors.css")" rel="stylesheet" type="text/css" />
}
@Html.Partial("_StatusMessage")
<h2 id="log-title">@ViewBag.Title</h2>
<div class="btn-toolbar">
@@ -10,11 +10,26 @@
<dt>
@Html.ViewData.ModelMetadata.DisplayName
</dt>
<dd @(!string.IsNullOrEmpty(color) ? "class='" + color + "'" : "") >
@RenderBody()
@if (!string.IsNullOrEmpty(units))
{
<small><em>@units</em></small>
}
</dd>
@if (string.IsNullOrEmpty(color))
{
<dd>
@RenderBody()
@if (!string.IsNullOrEmpty(units))
{
<small><em>@units</em></small>
}
</dd>
}
else
{
<dd>
<span class="label @Html.Encode(color)">
@RenderBody()
</span>
@if (!string.IsNullOrEmpty(units))
{
<small><em>@units</em></small>
}
</dd>
}
</dl>
+13
View File
@@ -0,0 +1,13 @@
@if (TempData.ContainsKey("StatusMessage"))
{
<p class="center-content alert alert-info">
<button type="button" class="close" data-dismiss="alert">&times;</button>
@TempData["StatusMessage"]
</p>
} else if (ViewBag.StatusMessage != null)
{
<p class="center-content alert alert-info">
<button type="button" class="close" data-dismiss="alert">&times;</button>
@ViewBag.StatusMessage
</p>
}
+9 -6
View File
@@ -7,6 +7,8 @@
@{ Html.RenderPartial("BackToUsers"); }
@Html.Partial("_StatusMessage")
<h2 class="center-content">@ViewBag.Title</h2>
<div class="center-content well">
@@ -31,11 +33,6 @@
</dd>
</dl>
@Html.DisplayFor(m => m.IsLockedOut)
@if (Model.IsLockedOut) {
@Html.DisplayFor(m => m.LastLockoutDate)
}
<dl class="dl-horizontal lastActivity">
<dt>
@Html.DisplayNameFor(m => m.LastActivityDate)
@@ -52,9 +49,15 @@
}
</dd>
</dl>
@if (Model.IsLockedOut) {
<p class="alert alert-info">
Locked out on @Html.DisplayTextFor(m => m.LastLockoutDate)
</p>
}
</div>
<div class="btn-toolbar center-content">
@Html.ActionLink("Edit", "Edit", new { id = Model.UserId }, new { @class = "btn" })
@Html.ActionLink("Set Password", "SetPassword", new { id = Model.UserId }, new { @class = "btn" })
</div>
+6 -1
View File
@@ -1,4 +1,4 @@
@model MileageTraker.Web.Models.User
@model MileageTraker.Web.ViewModels.User.EditUserViewModel
@{
ViewBag.Title = "Edit User";
@@ -16,9 +16,14 @@
<legend></legend>
@Html.EditorForModel()
@Html.Partial("_Roles", Model)
<div class="form-actions">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</fieldset>
}
<div class="btn-toolbar center-content">
@Html.ActionLink("Set Password", "SetPassword", new { id = Model.UserId }, new { @class = "btn" })
</div>
+2
View File
@@ -5,6 +5,8 @@
ViewBag.Title = "Users";
var grid = new WebGrid(Model, rowsPerPage: 45);
}
@Html.Partial("_StatusMessage")
<h2 id="user-title">@ViewBag.Title</h2>
+19
View File
@@ -0,0 +1,19 @@
@model MileageTraker.Web.ViewModels.User.SetPasswordViewModel
@{
ViewBag.Title = "Change Password";
}
<h2 class="center-content">@ViewBag.Title</h2>
@using (Html.BeginForm("SetPassword", "User", FormMethod.Post, new { @class = "form-horizontal well center-content" }))
{
@Html.Partial("_ValidationSummary")
@Html.AntiForgeryToken()
<fieldset>
<legend>Set password for @Model.Username</legend>
@Html.EditorForModel()
<div class="form-actions">
<input type="submit" value="Change password" class="btn btn-primary" />
</div>
</fieldset>
}
@@ -1,4 +1,4 @@
@model MileageTraker.Web.ViewModels.User.CreateUserViewModel
@model MileageTraker.Web.ViewModels.User.RolesViewModel
@{
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
}
+2
View File
@@ -8,6 +8,8 @@
<link href="@Url.Content("~/Content/VehicleColors.css")" rel="stylesheet" type="text/css" />
}
@Html.Partial("_StatusMessage")
@{ Html.RenderPartial("BackToVehicles"); }
<h2 class="center-content">@ViewBag.Title</h2>
+2
View File
@@ -8,6 +8,8 @@
<link href="@Url.Content("~/Content/VehicleColors.css")" rel="stylesheet" type="text/css" />
}
@Html.Partial("_StatusMessage")
<h2 id="vehicle-title">@ViewBag.Title</h2>
<div class="btn-toolbar">
+6 -2
View File
@@ -147,8 +147,10 @@
<Compile Include="ViewModels\EmployeeMileageViewModel.cs" />
<Compile Include="ViewModels\Log\LogIndexViewModel.cs" />
<Compile Include="ViewModels\Log\LogPartialDetails.cs" />
<Compile Include="ViewModels\User\SetPasswordViewModel.cs" />
<Compile Include="ViewModels\User\CreateUserViewModel.cs" />
<Compile Include="ViewModels\User\UserDetailsViewModel.cs" />
<Compile Include="ViewModels\User\EditUserViewModel.cs" />
<Compile Include="ViewModels\User\RolesViewModel.cs" />
<Compile Include="ViewModels\Vehicle\VehicleMileageViewModel.cs" />
<Compile Include="ViewModels\Vehicle\VehiclePartialDetails.cs" />
<Compile Include="ViewModels\Vehicle\VehicleMileageItem.cs" />
@@ -273,7 +275,9 @@
<Content Include="Views\Shared\EditorTemplates\EmailAddress.cshtml" />
<Content Include="Views\Shared\DisplayTemplates\EmailAddress.cshtml" />
<Content Include="Views\Shared\DisplayTemplates\Boolean.cshtml" />
<Content Include="Views\Shared\_Roles.cshtml" />
<Content Include="Views\User\_Roles.cshtml" />
<Content Include="Views\User\SetPassword.cshtml" />
<Content Include="Views\Shared\_StatusMessage.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config">