Exemplo n.º 1
0
        private static ILookup <string, NavigationItemAttribute> GetNavigationItemAttributes(
            ITypeSource typeSource, IServiceProvider serviceProvider,
            Func <NavigationItemAttribute, bool> filter)
        {
            var list = new List <NavigationItemAttribute>();

            foreach (NavigationItemAttribute attr in typeSource
                     .GetAssemblyAttributes <NavigationItemAttribute>())
            {
                if (filter == null || filter(attr))
                {
                    list.Add(attr);
                }
            }

            foreach (var navItemType in typeSource.GetTypesWithInterface(typeof(INavigationItemSource))
                     .Where(x => !x.IsAbstract && !x.IsInterface))
            {
                var navItem = (INavigationItemSource)ActivatorUtilities.CreateInstance(
                    serviceProvider, navItemType);
                foreach (var item in navItem.GetItems())
                {
                    if (filter == null || filter(item))
                    {
                        list.Add(item);
                    }
                }
            }

            return(ByCategory(list));
        }
Exemplo n.º 2
0
        public static IEnumerable <string> ListPermissionKeys(IMemoryCache memoryCache, ITypeSource typeSource)
        {
            if (typeSource is null)
            {
                throw new ArgumentNullException(nameof(typeSource));
            }

            return(memoryCache.Get("Administration:PermissionKeys", TimeSpan.Zero, () =>
            {
                var result = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                result.AddRange(NestedPermissionKeyRegistration.AddNestedPermissions(registry: null, typeSource));

                foreach (var attr in typeSource.GetAssemblyAttributes <PermissionAttributeBase>())
                {
                    if (!attr.Permission.IsEmptyOrNull())
                    {
                        result.AddRange(SplitPermissions(attr.Permission));
                    }
                }

                foreach (var type in typeSource.GetTypes())
                {
                    ProcessAttributes <PageAuthorizeAttribute>(result, type, x => x.Permission);
                    ProcessAttributes <PermissionAttributeBase>(result, type, x => x.Permission);
                    ProcessAttributes <ServiceAuthorizeAttribute>(result, type, x => x.Permission);

                    foreach (var member in type.GetMethods(BindingFlags.Instance | BindingFlags.Public))
                    {
                        ProcessAttributes <PageAuthorizeAttribute>(result, member, x => x.Permission);
                        ProcessAttributes <PermissionAttributeBase>(result, member, x => x.Permission);
                        ProcessAttributes <ServiceAuthorizeAttribute>(result, member, x => x.Permission);
                    }

                    foreach (var member in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        if (member.GetIndexParameters().Length == 0)
                        {
                            ProcessAttributes <PermissionAttributeBase>(result, member, x => x.Permission);
                        }
                    }
                }

                result.Remove("*");
                result.Remove("?");

                return result;
            }));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Gets all attributes for assemblies
 /// </summary>
 /// <returns>List of attributes for assemblies</returns>
 public static IEnumerable <TAttribute> GetAssemblyAttributes <TAttribute>(this ITypeSource typeSource)
     where TAttribute : Attribute
 {
     return(typeSource.GetAssemblyAttributes(typeof(TAttribute)).Cast <TAttribute>());
 }