Esempio n. 1
0
        private IReadOnlyDictionary <string, RoleCached> GetUserRoles(IRolesCache rolesCache)
        {
            if (!Identity.IsAuthenticated)
            {
                return(new Dictionary <string, RoleCached>
                {
                    [RoleNames.Unregistered] = rolesCache.GetRole(RoleNames.Unregistered)
                }.ToImmutableDictionary());
            }

            var roles     = GetRolesNames();
            var allGroups = rolesCache.AllRoles;


            var dictionaryBuilder = ImmutableDictionary.CreateBuilder <string, RoleCached>();

            var registeredGroup = rolesCache.GetRole(RoleNames.Registered);

            dictionaryBuilder.Add(registeredGroup.Name, registeredGroup);
            foreach (var role in roles)
            {
                if (!allGroups.ContainsKey(role))
                {
                    continue;
                }

                var userGroup = allGroups[role];
                dictionaryBuilder.Add(userGroup.Name, userGroup);
            }

            return(dictionaryBuilder.ToImmutable());
        }
Esempio n. 2
0
        public SectionClientCached(Section section, Dictionary <string, Type> clientComponentTypes,
                                   IRolesCache rolesCache)
        {
            Id   = section.Id;
            Name = section.Name;
            Type = section.Type;
            if (section.Roles != null)
            {
                Roles = section.Roles.Split(',')
                        .Select(x => rolesCache.GetRole(x))
                        .ToDictionary(x => x.Id, x => x)
                        .ToImmutableDictionary();
            }
            else
            {
                Roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
            }

            if (clientComponentTypes.TryGetValue(Type, out Type type))
            {
                object clientData = JsonSerializer.Deserialize(section.Options, type);
                string clientJson = JsonSerializer.Serialize(clientData);
                Options = SunJson.MakeJElement(clientJson);
            }
        }
Esempio n. 3
0
        public void Initialize()
        {
            using var db = dataBaseFactory.CreateDb();

            var menuItems    = db.MenuItems.Where(x => !x.IsHidden).ToDictionary(x => x.Id, x => x);
            var allMenuItems = new List <MenuItemCached>(menuItems.Count);

            foreach (var menuItem in menuItems.Values)
            {
                ImmutableDictionary <int, RoleCached> roles;
                if (menuItem.Roles != null)
                {
                    roles = menuItem.Roles.Split(',')
                            .Select(x => rolesCache.GetRole(x))
                            .ToDictionary(x => x.Id, x => x)
                            .ToImmutableDictionary();
                }
                else
                {
                    roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
                }


                if (CheckIsVisible(menuItem))
                {
                    allMenuItems.Add(new MenuItemCached(menuItem, roles));
                }
            }

            AllMenuItems = allMenuItems.OrderBy(x => x.SortNumber).ToImmutableList();

            RootMenuItem = AllMenuItems.First(x => x.Id == 1);


            bool CheckIsVisible(MenuItem menuItem)
            {
                while (true)
                {
                    if (menuItem.IsHidden)
                    {
                        return(false);
                    }

                    if (!menuItem.ParentId.HasValue)
                    {
                        return(true);
                    }

                    if (!menuItems.ContainsKey(menuItem.ParentId.Value))
                    {
                        return(false);
                    }

                    menuItem = menuItems[menuItem.ParentId.Value];
                }
            }
        }
Esempio n. 4
0
        public void Initialize()
        {
            lock (lockObject)
            {
                using (var db = dataBaseFactory.CreateDb())
                {
                    var components = db.Components.ToList();

                    Dictionary <string, ComponentServerCached> serverComponentsTmp =
                        new Dictionary <string, ComponentServerCached>(components.Count);

                    List <ComponentClientCached> clientComponentsTmp = new List <ComponentClientCached>();

                    foreach (var component in components)
                    {
                        try
                        {
                            ImmutableDictionary <int, RoleCached> roles;
                            if (component.Roles != null)
                            {
                                roles = component.Roles.Split(',')
                                        .Select(x => rolesCache.GetRole(x))
                                        .ToDictionary(x => x.Id, x => x)
                                        .ToImmutableDictionary();
                            }
                            else
                            {
                                roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
                            }

                            serverComponentsTmp[component.Name] =
                                new ComponentServerCached(component, ComponentsDataTypes[component.Type], roles);

                            clientComponentsTmp.Add(new ComponentClientCached(component, roles));
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    serverComponents = serverComponentsTmp.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase);

                    clientComponents = clientComponentsTmp.ToImmutableList();
                }
            }
        }
Esempio n. 5
0
        public SectionServerCached(Section section, Type serverSectionType, IRolesCache rolesCache)
        {
            Id          = section.Id;
            Name        = section.Name;
            IsCacheData = section.IsCacheData;
            if (section.Roles != null)
            {
                Roles = section.Roles.Split(',')
                        .Select(x => rolesCache.GetRole(x))
                        .ToDictionary(x => x.Id, x => x)
                        .ToImmutableDictionary();
            }
            else
            {
                Roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
            }

            Data = JsonSerializer.Deserialize(section.Options, serverSectionType);
        }
Esempio n. 6
0
        public SectionServerCached(Section section, Dictionary <string, Type> serverTypes, IRolesCache rolesCache)
        {
            Id          = section.Id;
            Name        = section.Name;
            IsCacheData = section.IsCacheData;
            if (section.Roles != null)
            {
                Roles = section.Roles.Split(',')
                        .Select(x => rolesCache.GetRole(x))
                        .ToDictionary(x => x.Id, x => x)
                        .ToImmutableDictionary();
            }
            else
            {
                Roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
            }

            if (!serverTypes.TryGetValue(section.Type, out Type type))
            {
                throw new SunException("No component type found: " + section.Type);
            }

            Data = JsonSerializer.Deserialize(section.Options, type);
        }