Create User works
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
using MileageTraker.Web.Membership;
|
||||
using MileageTraker.Web.ViewModels.Account;
|
||||
|
||||
namespace MileageTraker.Web.Controllers
|
||||
@@ -21,10 +20,15 @@ namespace MileageTraker.Web.Controllers
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Login(LoginViewModel model, string returnUrl)
|
||||
{
|
||||
if (ModelState.IsValid && WebSecurity.Login(model.EmailAddress, model.Password, model.RememberMe))
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
// TODO: send notification to user
|
||||
return RedirectToLocal(returnUrl);
|
||||
var success = Membership.ValidateUser(model.Username, model.Password);
|
||||
if (success)
|
||||
{
|
||||
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
|
||||
// TODO: send notification to user
|
||||
return RedirectToLocal(returnUrl);
|
||||
}
|
||||
}
|
||||
|
||||
// If we got this far, something failed, redisplay form
|
||||
@@ -36,48 +40,47 @@ namespace MileageTraker.Web.Controllers
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult LogOff()
|
||||
{
|
||||
WebSecurity.Logout();
|
||||
FormsAuthentication.SignOut();
|
||||
|
||||
// TODO: send notification to user
|
||||
return RedirectToAction("Index", "CreateLog");
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
public ActionResult Register()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
//[AllowAnonymous]
|
||||
//public ActionResult Register()
|
||||
//{
|
||||
// return View();
|
||||
//}
|
||||
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Register(RegisterModel model)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
// Attempt to register the user
|
||||
try
|
||||
{
|
||||
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
|
||||
WebSecurity.Login(model.UserName, model.Password);
|
||||
return RedirectToAction("Login");
|
||||
}
|
||||
catch (MembershipCreateUserException e)
|
||||
{
|
||||
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
|
||||
}
|
||||
}
|
||||
//[HttpPost]
|
||||
//[AllowAnonymous]
|
||||
//[ValidateAntiForgeryToken]
|
||||
//public ActionResult Register(RegisterModel model)
|
||||
//{
|
||||
// if (ModelState.IsValid)
|
||||
// {
|
||||
// // Attempt to register the user
|
||||
// try
|
||||
// {
|
||||
// WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
|
||||
// WebSecurity.Login(model.UserName, model.Password);
|
||||
// return RedirectToAction("Login");
|
||||
// }
|
||||
// catch (MembershipCreateUserException e)
|
||||
// {
|
||||
// ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
|
||||
// }
|
||||
// }
|
||||
|
||||
// If we got this far, something failed, redisplay form
|
||||
return View(model);
|
||||
}
|
||||
// // If we got this far, something failed, redisplay form
|
||||
// return View(model);
|
||||
//}
|
||||
|
||||
public ActionResult Manage(ManageMessageId? message)
|
||||
{
|
||||
ViewBag.StatusMessage =
|
||||
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
|
||||
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
|
||||
: message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
|
||||
: "";
|
||||
ViewBag.ReturnUrl = Url.Action("Manage");
|
||||
return View();
|
||||
@@ -95,9 +98,10 @@ namespace MileageTraker.Web.Controllers
|
||||
bool changePasswordSucceeded;
|
||||
try
|
||||
{
|
||||
changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
|
||||
var currentUser = Membership.GetUser(User.Identity.Name, true);
|
||||
changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception)
|
||||
{
|
||||
changePasswordSucceeded = false;
|
||||
}
|
||||
@@ -106,10 +110,7 @@ namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
|
||||
}
|
||||
else
|
||||
{
|
||||
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
|
||||
}
|
||||
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
|
||||
}
|
||||
|
||||
// If we got this far, something failed, redisplay form
|
||||
@@ -129,7 +130,6 @@ namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
ChangePasswordSuccess,
|
||||
SetPasswordSuccess,
|
||||
RemoveLoginSuccess,
|
||||
}
|
||||
|
||||
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Security;
|
||||
using MileageTraker.Web.Attributes;
|
||||
using MileageTraker.Web.Models;
|
||||
using MileageTraker.Web.Context;
|
||||
using MileageTraker.Web.ViewModels.User;
|
||||
|
||||
namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Administrator, Developer")]
|
||||
[UserActivity]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
public ActionResult Index()
|
||||
@@ -18,32 +19,66 @@ namespace MileageTraker.Web.Controllers
|
||||
|
||||
public ActionResult Details(Guid id)
|
||||
{
|
||||
var user = DataService.GetUser(id);
|
||||
var user = Membership.GetUser(id);
|
||||
if (user == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
return View(user);
|
||||
return View(DataService.GetUser(id));
|
||||
}
|
||||
|
||||
public ActionResult Create()
|
||||
{
|
||||
return View();
|
||||
var vm = new CreateUserViewModel
|
||||
{
|
||||
AvailableRoles = Roles.GetAllRoles()
|
||||
};
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Create(CreateUserViewModel createUserViewModel)
|
||||
public ActionResult Create(CreateUserViewModel viewModel)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var user = createUserViewModel.CloneToUser();
|
||||
user.UserId = Guid.NewGuid();
|
||||
//db.Users.Add(user);
|
||||
//db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
var hasRoles = viewModel.Roles != null && viewModel.Roles.Any();
|
||||
|
||||
MembershipCreateStatus membershipCreateStatus;
|
||||
var membershipUser =
|
||||
Membership.CreateUser(
|
||||
viewModel.Username,
|
||||
viewModel.Password,
|
||||
viewModel.Email,
|
||||
null,
|
||||
null,
|
||||
hasRoles,
|
||||
out membershipCreateStatus);
|
||||
|
||||
if (membershipUser == null)
|
||||
{
|
||||
ModelState.AddModelError("", ErrorCodeToString(membershipCreateStatus));
|
||||
viewModel.AvailableRoles = Roles.GetAllRoles();
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
if (hasRoles)
|
||||
{
|
||||
Roles.AddUserToRoles(
|
||||
membershipUser.UserName,
|
||||
viewModel.Roles);
|
||||
}
|
||||
|
||||
var user = DataService.GetUser(
|
||||
(Guid) membershipUser.ProviderUserKey);
|
||||
user.FullName = viewModel.FullName;
|
||||
DataService.UpdateUserPersonalInfo(user);
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
return View(createUserViewModel);
|
||||
viewModel.AvailableRoles = Roles.GetAllRoles();
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
public ActionResult Edit(Guid id)
|
||||
@@ -61,11 +96,48 @@ namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
//db.Entry(user).State = EntityState.Modified;
|
||||
//db.SaveChanges();
|
||||
DataService.UpdateUser(user);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
return View(user);
|
||||
}
|
||||
|
||||
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.";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user