User profile editing and password
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System.Web.Mvc;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Mvc;
|
||||
using AutoMapper;
|
||||
using InventoryTraker.Web.Core;
|
||||
using InventoryTraker.Web.Attributes;
|
||||
using InventoryTraker.Web.Identity;
|
||||
using InventoryTraker.Web.Models;
|
||||
using Microsoft.AspNet.Identity;
|
||||
@@ -20,19 +22,44 @@ namespace InventoryTraker.Web.Controllers
|
||||
|
||||
public ActionResult Index()
|
||||
{
|
||||
var user = _userManager.FindById(User.Identity.GetUserId());
|
||||
var model = _mapper.Map<ProfileForm>(user);
|
||||
return View(model);
|
||||
return View();
|
||||
}
|
||||
|
||||
public JsonResult Update(ProfileForm form)
|
||||
public JsonResult Get()
|
||||
{
|
||||
var user = _userManager.FindById(User.Identity.GetUserId());
|
||||
user.Email = form.EmailAddress;
|
||||
user.UserName = form.FullName;
|
||||
_userManager.Update(user);
|
||||
var model = _mapper.Map<ProfileForm>(user);
|
||||
return BetterJson(model);
|
||||
}
|
||||
|
||||
return Json(true);
|
||||
[ActionLog]
|
||||
public async Task<JsonResult> Update(ProfileForm form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
var user = _userManager.FindById(User.Identity.GetUserId());
|
||||
user.Email = form.Email;
|
||||
user.UserName = form.UserName;
|
||||
|
||||
if (!string.IsNullOrEmpty(form.NewPassword))
|
||||
{
|
||||
if (string.IsNullOrEmpty(form.CurrentPassword))
|
||||
return GetErrorListJson("Current password required");
|
||||
|
||||
var result =
|
||||
await _userManager.ChangePasswordAsync(
|
||||
user.Id, form.CurrentPassword, form.NewPassword);
|
||||
|
||||
if (!result.Succeeded)
|
||||
return GetErrorListJson(result.Errors.ToArray());
|
||||
}
|
||||
|
||||
var identityResult = _userManager.Update(user);
|
||||
if (!identityResult.Succeeded)
|
||||
return GetErrorListJson(identityResult.Errors.ToArray());
|
||||
|
||||
return BetterJson(_mapper.Map<ProfileForm>(user));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user