public ActionResult EditUser(AdminAddUpdateUserVM model)
        {
            model.AvailableRoles = IdentityPostmaster.SetAvailableRoles();

            if (model.Password != model.PasswordConfirmed)
            {
                ModelState.AddModelError("PasswordConfirmed", "Your password and password confirmation did not match.");
            }
            if (ModelState.IsValid)
            {
                var courier = IdentityPostmaster.PackageUpdateUser(model);

                if (courier.Success)
                {
                    return(RedirectToAction("Users"));
                }
                else
                {
                    ModelState.AddModelError("TargetUserName", courier.Message);

                    return(View(model));
                }
            }
            else
            {
                return(View("EditUser", model));
            }
        }
        public ActionResult AddUser()
        {
            AdminAddUpdateUserVM model = new AdminAddUpdateUserVM();

            model.AvailableRoles = IdentityPostmaster.SetAvailableRoles();

            return(View(model));
        }
        public ActionResult Users()
        {
            AdminUsersVM model = new AdminUsersVM();

            var users = IdentityPostmaster.GetAllUsers();
            var roles = IdentityPostmaster.GetAllRoles();

            model.Users = users.Package;
            model.Roles = roles.Package;

            return(View(model));
        }
        //[Authorize(Roles = "Sales, Admin")]
        public ActionResult ChangePassword(AccountChangePasswordVM model)
        {
            if (model.NewPassword != model.ConfirmPassword)
            {
                ModelState.AddModelError("ConfirmPassword", "Your password confirmation did not match the password you entered.");
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                var user = IdentityPostmaster.RetrieveUserByName(User.Identity.Name);

                if (user.Success)
                {
                    model.User = user.Package;
                }
                else
                {
                    throw new Exception(user.Message);
                }

                var result = IdentityPostmaster.PackageChangePassword(model);

                if (result.Success)
                {
                    var userManager = HttpContext.GetOwinContext().GetUserManager <UserManager <AppUser> >();
                    var authManager = HttpContext.GetOwinContext().Authentication;

                    authManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

                    var identity = userManager.CreateIdentity(result.Package, DefaultAuthenticationTypes.ApplicationCookie);
                    authManager.SignIn(identity);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("NewPassword", result.Message);

                    return(View(model));
                }
            }
        }
        public ActionResult EditUser(string userId)
        {
            AdminAddUpdateUserVM model = new AdminAddUpdateUserVM();

            var courier = IdentityPostmaster.RetrieveUserById(userId);

            model.TargetUserId   = userId;
            model.FirstName      = courier.Package.FirstName;
            model.LastName       = courier.Package.LastName;
            model.Email          = courier.Package.Email;
            model.AvailableRoles = IdentityPostmaster.SetAvailableRoles();
            model.RoleName       = IdentityPostmaster.GetUserRole(userId).Package.Name;

            foreach (var t in model.AvailableRoles)
            {
                if (t.Text == model.RoleName &&
                    t.Value == model.RoleName)
                {
                    t.Selected = true;
                }
            }

            return(View(model));
        }