public static List <Menu> GetAllByUserId(string userId) { try { using (var ctx = new SuchDBContext()) { MenuComparer comparer = new MenuComparer(); HashSet <Menu> menuList = null; var user = DBUserConfigurations.GetById(userId); if (user == null) { throw new Exception(); } if (user.Administrador && true) { menuList = ctx.Menu.Where(p => p.Active == true).ToHashSet(comparer); } else { HashSet <int> featuresIds = GetFeaturesByUserId(userId); HashSet <int> menusIds = null; // list menu id from features if (featuresIds != null && featuresIds.Count() > 0) { menusIds = ctx.FeaturesMenus.Where(fm => featuresIds.Contains(fm.IdFeature)).Select(fm => fm.IdMenu).ToHashSet(); } // get menu if (menusIds != null && menusIds.Count() > 0) { var userMenuList = ctx.Menu.Where(m => menusIds.Contains(m.Id) && m.Active).ToHashSet(comparer); var parentMenuList = GetAllParentsByMenuList(userMenuList); menuList = userMenuList.Union(parentMenuList).ToHashSet(comparer); } } return(menuList.OrderBy(m => m.Weight).ToList()); } } catch (Exception ex) { return(null); } }
private static UserAccessesViewModel GetByUserAreaFunctionality(string UserId, List <int> features) { UserAccessesViewModel userAccess = new UserAccessesViewModel() { IdUser = UserId, Feature = 0, Create = false, Read = false, Update = false, Delete = false }; try { //TODO: Remover area using (var ctx = new SuchDBContext()) { ConfigUtilizadores CUser = DBUserConfigurations.GetById(UserId); if (CUser.Administrador) { userAccess.Feature = features.FirstOrDefault(); userAccess.Create = true; userAccess.Read = true; userAccess.Update = true; userAccess.Delete = true; } else { var userAccessess = ctx.AcessosUtilizador.Where(x => x.IdUtilizador.ToLower() == UserId.ToLower()).Where(x => features.Contains(x.Funcionalidade)).ToList(); if (userAccessess.Count > 0) { userAccess = ParseToViewModel(userAccessess.FirstOrDefault()); userAccess.Create = userAccessess.Any(x => x.Inserção.Value); userAccess.Read = userAccessess.Any(x => x.Leitura.Value); userAccess.Update = userAccessess.Any(x => x.Modificação.Value); userAccess.Delete = userAccessess.Any(x => x.Eliminação.Value); } } } } catch (Exception ex) { return(null); } return(userAccess); }