public List<ModuloRoles> GetRoles(string Modulo, string UserName)
        {
            var userStore = new UserStore<IdentityUser>();
            var userMgr = new UserManager<IdentityUser>(userStore);
            var user = userMgr.FindByName(UserName);

            ctx = new ieEntities();

            List<ModuloRoles> lm = ctx.fc_menu.Where(t => t.fc_modulo == Modulo && t.fc_menuid != t.fc_padreid && t.fc_roles != null).OrderBy(t => t.fc_posicion)
                .Select(t => new ModuloRoles
                {
                    Modulo = t.fc_modulo,
                    Roles = t.fc_roles,
                    Titulo = t.fc_titulo
                }).Distinct().ToList();
            foreach (ModuloRoles item in lm)
            {
                if (item.Roles != null)
                {
                    item.hasRol = (userMgr.IsInRole(user.Id, item.Roles));
                }

            }
            return lm;
        }
Exemple #2
0
        public string GenerarRoles()
        {
            var roleStore = new RoleStore<IdentityRole>();
            var roleMgr = new RoleManager<IdentityRole>(roleStore);

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

            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            using (ieEntities ctx = new ieEntities())
            {
                string strrole = "";
                List<fc_menu> lm = ctx.fc_menu.Where(t => !String.IsNullOrEmpty(t.fc_roles)).ToList();
                foreach (fc_menu im in lm)
                {
                    string rol = im.fc_roles;
                    //!String.IsNullOrEmpty(rol) &&
                    bool RolExiste = roleMgr.RoleExists(rol);

                    if ((!RolExiste))
                    {
                        IdRoleResult = roleMgr.Create(new IdentityRole(rol));
                        IdUserResult = userMgr.AddToRole(user.Id, rol);
                        strrole = strrole + rol + "<br>";
                    }
                    else
                    {
                        bool adminTieneElRol = userMgr.IsInRole(user.Id, rol);
                        if (!adminTieneElRol)
                        {
                            IdUserResult = userMgr.AddToRole(user.Id, rol);
                            strrole = strrole + rol + "<br>";
                        }
                    }
                }
                    return strrole;
            }
        }
        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");
        }
        public ActionResult RoleAddToUser(string roleName, string userName)
        {
            List<string> roles;
            List<string> users;
            using (var context = new ApplicationDbContext())
            {
                var roleStore = new RoleStore<IdentityRole>(context);
                var roleManager = new RoleManager<IdentityRole>(roleStore);

                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!");

                var role = roleManager.FindByName(roleName);
                if (role == null)
                    throw new Exception("Role not found!");

                if (userManager.IsInRole(user.Id, role.Name))
                {
                    ViewBag.ResultMessage = "This user already has the role specified !";
                }
                else
                {
                    userManager.AddToRole(user.Id, role.Name);
                    context.SaveChanges();

                    ViewBag.ResultMessage = "Username added to the role succesfully !";
                }

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

            ViewBag.Roles = new SelectList(roles);
            ViewBag.Users = new SelectList(users);
            return View();
        }
        public List<dataTree> getOpciones(string modulo, string usuario)
        {
            var userStore = new UserStore<IdentityUser>();
            var userMgr = new UserManager<IdentityUser>(userStore);
            var user = userMgr.FindByName(usuario);

            List<dataTree> lt;
            using (ctx = new ieEntities())
            {

                lt = ctx.fc_menu.Where(t => t.fc_modulo == modulo && t.fc_menuid != t.fc_padreid && t.fc_habilitado == 1).OrderBy(t=> t.fc_posicion).Select(t => new dataTree
                {
                    id = t.fc_menuid,
                    text = t.fc_titulo,
                    value = new valueTree { icono = t.fc_icono, descripcion = t.fc_descripcion, target = t.fc_target, url = t.fc_url, roles = t.fc_roles },
                    parentid = t.fc_menuid == t.fc_padreid ? "-1" : t.fc_padreid,roles = t.fc_roles
                }
                ).ToList();

                lt = lt.Where(t => (t.parentid == "-1") || (userMgr.IsInRole(user.Id, t.roles))).ToList();

            return lt;
            }
        }