public IEnumerable<ModuleDto> GetPermissions(string userName)
        {
            List<ModuleDto> moduleDtos = new List<ModuleDto>();
            var permissions = ObjectContext.UIPermissions.Where(up => up.UserName == userName).OrderBy(up => up.Parrent).ToList();
            List<UIPermissions> parrentPermission=new List<UIPermissions>();
            for (int i = 0; i < permissions.Count; i++)
            {
                UIPermissions uiPermissionse = permissions[i];
                if (uiPermissionse.Parrent != null && permissions.FirstOrDefault(x => x.Id == uiPermissionse.Parrent) == null)
                {
                    Element element = ObjectContext.Element.First(x => x.Id == uiPermissionse.Parrent);
                    permissions.Add(new UIPermissions { Id = element.Id, Name = element.Name, Parrent = element.Parrent, Type = element.Type, Property = "Enabled", Value = "True", UserName = uiPermissionse.UserName });
                }
            }
            //foreach (UIPermissions uiPermissionse in permissions)
            //{
                
            //}
            var modules = permissions.Where(x => x.Parrent == null).ToList();
            var views = permissions.Where(x => x.Type == 2).ToList();
            var controls = permissions.Where(x => x.Type == 3 && x.Value != null).ToList();

            foreach (UIPermissions modulePermissionse in modules)
            {
                var tempModule = moduleDtos.FirstOrDefault(x => x.Name == modulePermissionse.Name);
                //Ak už existuje v kolekcií modul overíme či nenastáva konflikt ak áno zobereme ten restriktívnejší
                if (tempModule != null)
                {
                    tempModule.Enabled = tempModule.Enabled && modulePermissionse.Value.Equals("true", StringComparison.CurrentCultureIgnoreCase);
                    continue;
                }
                ModuleDto module = new ModuleDto
                {
                    Name = modulePermissionse.Name,
                    Enabled = modulePermissionse.Value.Equals("true", StringComparison.CurrentCultureIgnoreCase),
                    Id=modulePermissionse.Id
                };
                foreach (UIPermissions viewPermission in views)
                {
                    var tempView = module.Views.FirstOrDefault(x => x.Name == viewPermission.Name);
                    if(viewPermission.Parrent!=modulePermissionse.Id)
                        continue;
                    //Ak uz existuje View v kolekcí aplikuje sa nan restriktívnejšie pravidlo
                    if (tempView != null)
                    {
                        tempView.Enabled = tempView.Enabled && viewPermission.Value.Equals("true", StringComparison.CurrentCultureIgnoreCase);// Math.Max(viewPermission.Right ?? 1, tempView.Visibility);
                        continue;
                    }
                    ViewDto view = new ViewDto
                    {
                        Name = viewPermission.Name,
                        Enabled = viewPermission.Value.Equals("true", StringComparison.CurrentCultureIgnoreCase),
                        Id=viewPermission.Id,
                        ModuleId=modulePermissionse.Id
                    };

                    foreach (UIPermissions controlPermission in controls)
                    {
                        var tempControl = view.Controls.FirstOrDefault(x => x.Name == controlPermission.Name);
                        //Ak nastane konflikt tzn že sa jedná o Visibility v oboch záznamoch
                        if (viewPermission.Id != controlPermission.Parrent)
                            continue;
                        if (tempControl != null && (tempControl.Property=="Visibility") && tempControl.Property==controlPermission.Property)
                        {
                            tempControl.Value = GetMoreRestrictedVisibility(tempControl.Value, controlPermission.Value);  //Math.Max(controlPermission.Right ?? 1, tempControl.Visibility);
                            continue;
                        }
                        ControlDto control = new ControlDto
                        {
                            //FullName = controlPermission.FullName,
                            ViewId=viewPermission.Id,
                            Name = controlPermission.Name,
                            Value = controlPermission.Value,
                            Property = controlPermission.Property
                            
                        };
                        view.Controls.Add(control);
                    }
                    module.Views.Add(view);
                }
                moduleDtos.Add(module);
            }
            return moduleDtos;
        }
Esempio n. 2
0
 private bool FilterViews(ViewDto entity)
 {
     return (entity.ModuleId == this.Id);
 }