public async Task<ActionResult> ChangeData()
        {
            var userData = await AspUserManager.FindByNameAsync(User.Identity.Name);
            var viewModel = new AdminDataViewModel
            {
                FirstName = userData.FirstName,
                LastName = userData.LastName,
                Email = userData.Email
            };

            return View(viewModel);
        }
Exemple #2
0
        // GET: Admin/Home
        public async Task<ActionResult> Index(Message? message)
        {
            ViewBag.Message = message == Message.ChangeDataSuccess ? SetAlert.Set("Dane zostały poprawnie zmienione!", "Sukces", AlertType.Success)
                : message == Message.ChagnePasswordSuccess ? SetAlert.Set("Hasło zostało poprawnie zmienione!", "Sukces", AlertType.Success)
                : message == Message.Error ? SetAlert.Set("Wystąpił błąd, spróbuj ponownie poźniej", "Uwaga", AlertType.Danger)
                : null;

            var adminData = await AspUserManager.FindByNameAsync(User.Identity.Name);

            AdminDataViewModel viewModel = new AdminDataViewModel
            {
                FirstName = adminData.FirstName,
                LastName = adminData.LastName,
                Email = adminData.Email,
                Role = HttpContext.User.IsInRole("Root") ? "Root (super admin)" : "Administrator"
            };

            return View(viewModel);
        }
        public async Task<ActionResult> ChangeData(AdminDataViewModel model)
        {
            if (ModelState.IsValid)
            {
                var adminData = await AspUserManager.FindByIdAsync(User.Identity.GetUserId());

                adminData.FirstName = model.FirstName;
                adminData.LastName = model.LastName;
                adminData.Email = model.Email;
                adminData.UserName = model.Email;

                var result = await AspUserManager.UpdateAsync(adminData);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(adminData, isPersistent: false, rememberBrowser: false);
                    return RedirectToAction("Index", "Home", new { Message = Message.ChangeDataSuccess });
                }
                else
                    return RedirectToAction("Index", "Home", new { Message = Message.Error });
            }
            else
                return RedirectToAction("Index", "Home", new { Message = Message.Error });
        }