Esempio n. 1
0
        public ActionResult Details(int id =0)
        {
            var um = (GuUser)_mp.GetUser(id, false);
            if (um == null) return new HttpNotFoundResult();

            //Mapeo Negocio -> Vista
            UserVM uv = new UserVM();
            uv.ID = (int)um.ProviderUserKey;
            uv.UserName = um.UserName;
            uv.Nombre = um.NombrePropio;
            uv.Apellido1 = um.Apellido1;
            uv.Apellido2 = um.Apellido2;
            var usr = _rp.GetRolesForUser(um.UserName);
            foreach (string rolName in _rp.GetAllRoles())
            {
                uv.InRoles.Add(new RoleUser() { RoleName = rolName, IsInRole = usr.Any(s => s == rolName) });
            }
            return View(uv);
        }
Esempio n. 2
0
        public ActionResult Edit(UserVM u)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //Mapeo VM -> D
                    GuUser du = new GuUser(u.UserName, u.ID, u.Email, null, true)
                    {
                        NombrePropio = u.Nombre,
                        Apellido1 = u.Apellido1,
                        Apellido2 = u.Apellido2
                    };
                    _mp.UpdateUser(du);

                    string[] inroles = u.InRoles.Where(i => i.IsInRole).Select(r => r.RoleName).ToArray();
                    string[] outroles = u.InRoles.Where(i => !i.IsInRole).Select(r => r.RoleName).ToArray();

                    _rp.AddUsersToRoles(new string[] { u.UserName }, inroles);
                    _rp.RemoveUsersFromRoles(new string[] { u.UserName }, outroles);

                    return RedirectToAction("Details", new { id = u.ID });
                    //return View("Details", u);
                }
                else
                {
                    return View(u);
                }
            }

            catch (Exception e)
            {
                if (e is DbEntityValidationException)
                {
                    foreach (var error in ((DbEntityValidationException)e).EntityValidationErrors)
                    {
                        foreach (var item in error.ValidationErrors)
                        {
                            ModelState.AddModelError(string.Empty, item.ErrorMessage);
                        }
                    }
                }
                ModelState.AddModelError(string.Empty, e.Message);
                return View(u);
            }
        }