Пример #1
0
        public ActionResult Edit(WebUserFormViewModel model)
        {
            ViewBag.AutorList = Mapper.Map <IEnumerable <WebUserDto>, IList <WebUserListViewModel> >(ServiceUser.GetList());

            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrEmpty(model.AccountId))
                    {
                        var user = UserManager.FindByEmail(model.Email);
                        if (user == null)
                        {
                            user = new ApplicationUser {
                                UserName = model.Email, Email = model.Email
                            };
                            var result = UserManager.Create(user);
                            result = UserManager.SetLockoutEnabled(user.Id, false);

                            if (result.Succeeded)
                            {
                                model.AccountId = user.Id;
                                // User cannot to have admin role
                            }
                        }
                        else
                        {
                            model.AccountId = user.Id;
                        }
                    }

                    var User = ServiceUser.GetItem(model.UserId);

                    User.Name      = model.Name;
                    User.LastName  = model.LastName;
                    User.Email     = model.Email;
                    User.AccountId = model.AccountId; //Update from Identity
                    User.Active    = model.Active;

                    ServiceUser.Edit(User);
                }
                else
                {
                    ModelState.AddModelError("Error", "Algunos datos ingresados no son válidos");
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "Se ha producido un error: " + ex.Message);
                return(View(model));
            }

            return(RedirectToAction("Index"));
        }
Пример #2
0
        public ActionResult Create(WebUserFormViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var user = UserManager.FindByEmail(model.Email);
                    if (user == null)
                    {
                        user = new ApplicationUser {
                            UserName = model.Email, Email = model.Email
                        };
                        var result = UserManager.Create(user, model.Password);
                        result = UserManager.SetLockoutEnabled(user.Id, false);

                        if (result.Succeeded)
                        {
                            model.AccountId = user.Id;
                            // User cannot to have admin role
                        }
                    }
                    else
                    {
                        model.AccountId = user.Id;
                    }

                    ServiceUser.Add(new WebUserDto()
                    {
                        Name         = model.Name,
                        LastName     = model.LastName,
                        Email        = model.Email,
                        AccountId    = model.AccountId, //Update from Identity
                        RegisterDate = DateTime.Now,
                        Active       = model.Active
                    });
                }
                else
                {
                    ModelState.AddModelError("Error", "Algunos datos ingresados no son válidos");
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", "Se ha producido un error: " + ex.Message);
                return(View(model));
            }

            return(RedirectToAction("Index"));
        }
Пример #3
0
        public ActionResult Create()
        {
            var model = new WebUserFormViewModel();

            return(View(model));
        }