Ejemplo n.º 1
0
        public ByARpt GuardarRoles(List<ModuloRoles> lst, string UserName)
        {
            string msg = "";

            var userStore = new UserStore<IdentityUser>();
            var userMgr = new UserManager<IdentityUser>(userStore);
            var user = userMgr.FindByName(UserName);
            IdentityResult IdUserResult;

            foreach (ModuloRoles item in lst)
            {
                bool hasRoolAnt = userMgr.IsInRole(user.Id, item.Roles);
                if (item.hasRol != hasRoolAnt)//Si cambió
                {
                    try
                    {
                        if (item.hasRol)
                        {
                            IdUserResult = userMgr.AddToRole(user.Id, item.Roles);
                            msg += String.Format("Se Asignó el Rol {0} - [ {1} ]</br>", item.Titulo, item.Roles);
                        }
                        else
                        {
                            IdUserResult = userMgr.RemoveFromRole(user.Id, item.Roles);
                            msg += String.Format("Se Retiró el Rol {0} - [ {1} ]</br>", item.Titulo, item.Roles);
                        }
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;

                    }
                }
            }
            if (String.IsNullOrEmpty(msg))
            {
                byaRpt.Mensaje = "No realizó ningun cambio de Roles al usuario";
            }
            else
            {
                byaRpt.Mensaje = msg;
            }

            byaRpt.Error = false;
            //GuardarRolesUsuarios
            return byaRpt;
        }
        public ActionResult DeleteRoleForUser(string userName, string roleName)
        {
            List<string> userRoles;
            List<string> roles;
            List<string> users;
            using (var context = new ApplicationDbContext())
            {
                var roleStore = new RoleStore<IdentityRole>(context);
                var roleManager = new RoleManager<IdentityRole>(roleStore);

                roles = (from r in roleManager.Roles select r.Name).ToList();

                var userStore = new UserStore<ApplicationUser>(context);
                var userManager = new UserManager<ApplicationUser>(userStore);

                users = (from u in userManager.Users select u.UserName).ToList();

                var user = userManager.FindByName(userName);
                if (user == null)
                    throw new Exception("User not found!");

                if (userManager.IsInRole(user.Id, roleName))
                {
                    userManager.RemoveFromRole(user.Id, roleName);
                    context.SaveChanges();

                    ViewBag.ResultMessage = "Role removed from this user successfully !";
                }
                else
                {
                    ViewBag.ResultMessage = "This user doesn't belong to selected role.";
                }

                var userRoleIds = (from r in user.Roles select r.RoleId);
                userRoles = (from id in userRoleIds
                             let r = roleManager.FindById(id)
                             select r.Name).ToList();
            }

            ViewBag.RolesForThisUser = userRoles;
            ViewBag.Roles = new SelectList(roles);
            ViewBag.Users = new SelectList(users);
            return View("RoleAddToUser");
        }