コード例 #1
0
        public async Task <string> GetContentAncestorsPath(int?contentId, bool includeCurrentPage = false)
        {
            var cacheResult = await EZmsInMemoryCache.GetOrCreateAsync(
                $"NavigationService:GetContentAncestorsPath:{contentId}:{includeCurrentPage}",
                async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(60)).SetPriority(CacheItemPriority.Normal);
                if (!contentId.HasValue)
                {
                    return(string.Empty);
                }

                var content = await _contentRepository.GetContent(contentId);
                if (content?.ParentId == null)
                {
                    return(content == null ? "" : content.UrlSlug);
                }

                var ancestors = await GetContentAncestors(contentId.Value, includeCurrentPage);

                var ancestorPath = string.Join("/",
                                               ancestors.Where(w => !string.IsNullOrEmpty(w.UrlSlug)).Select(w => w.UrlSlug))
                                   .Trim()
                                   .Trim('/')
                                   .ToLower();

                return(ancestorPath);
            });

            return(cacheResult);
        }
コード例 #2
0
ファイル: PageConstraint.cs プロジェクト: Floydan/EZms.Core
        private IDictionary <string, int> GetPageList()
        {
            lock (_lock)
            {
                if (!EZmsInMemoryCache.TryGetValue(CacheKey, out IDictionary <string, int> pages))
                {
                    var dataProvider = ServiceLocator.Current.GetInstance <ICachedRouteDataProvider>();
                    // Only allow one thread to poplate the data
                    lock (_lock)
                    {
                        if (EZmsInMemoryCache.TryGetValue(CacheKey, out pages))
                        {
                            return(pages);
                        }

                        pages = dataProvider.GetContentToIdMap();

                        EZmsInMemoryCache.Set(CacheKey, pages,
                                              new MemoryCacheEntryOptions()
                        {
                            Priority = CacheItemPriority.NeverRemove,
                            AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(900)
                        });
                    }
                }

                return(pages);
            }
        }
コード例 #3
0
 public void Update(IDictionary <string, int> pages)
 {
     EZmsInMemoryCache.Set(_cacheKey, pages,
                           new MemoryCacheEntryOptions {
         Priority = CacheItemPriority.NeverRemove,
         AbsoluteExpirationRelativeToNow = _cacheTimeout
     });
 }
コード例 #4
0
        public override async Task <IEnumerable <T> > GetChildren <T>(int id)
        {
            return(await EZmsInMemoryCache.GetOrCreateAsync($"DefaultContentLoader:GetChildren<IEnumerable<{typeof(T).FullName}>>({id})",
                                                            async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));

                var childrenOfType = (await _repository.GetChildren <T>(id)).ToArray();
                return childrenOfType.IsNullOrEmpty() ? Enumerable.Empty <T>() : childrenOfType;
            }));
        }
コード例 #5
0
        public override async Task <IEnumerable <T> > GetAll <T>()
        {
            return(await EZmsInMemoryCache.GetOrCreateAsync(
                       $"DefaultContentLoader:GetAll<IEnumerable<{typeof(T).FullName}>",
                       async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));

                var contents = await _repository.GetAll <T>();
                return contents;
            }));
        }
コード例 #6
0
        public override async Task <IEnumerable <IContent> > GetAll()
        {
            return(await EZmsInMemoryCache.GetOrCreateAsync(
                       $"DefaultContentLoader:GetAll<IEnumerable<IContent>",
                       async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));

                var ancestors = await _repository.GetAll();
                return ancestors;
            }));
        }
コード例 #7
0
        public override async Task <IEnumerable <int> > GetDescendents(int id)
        {
            return(await EZmsInMemoryCache.GetOrCreateAsync(
                       $"DefaultContentLoader:GetDescendents<IEnumerable<int>({id})",
                       async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));

                var contentChildren = await _repository.GetChildren(id);

                return contentChildren.Select(w => w.Id);
            }));
        }
コード例 #8
0
        public IDictionary <string, int> Get()
        {
            if (!EZmsInMemoryCache.TryGetValue(_cacheKey, out IDictionary <string, int> pages))
            {
                // Only allow one thread to populate the data
                lock (_lock)
                {
                    if (EZmsInMemoryCache.TryGetValue(_cacheKey, out pages))
                    {
                        return(pages);
                    }
                }
            }

            return(pages);
        }
コード例 #9
0
        public async Task <IEnumerable <IContent> > GetContentAncestors(int?contentId, bool includeCurrentPage = false)
        {
            var cacheResult = await EZmsInMemoryCache.GetOrCreateAsync(
                $"NavigationService:GetContentAncestors:{contentId}:{includeCurrentPage}",
                async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(60)).SetPriority(CacheItemPriority.Normal);
                if (!contentId.HasValue)
                {
                    return(Enumerable.Empty <Content>());
                }

                var ancestors = await _contentRepository.GetAncestors(contentId.Value, includeCurrentPage);
                return(ancestors);
            });

            return(cacheResult);
        }
コード例 #10
0
ファイル: Users.cshtml.cs プロジェクト: Floydan/EZms.UI
        public async Task <IActionResult> OnPostAsync(string id, UserPostModel model, IFormCollection collection)
        {
            await Init(id);

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var unselectedRoles = SelectedUserRoles.Where(r => !model.SelectedRoles?.Contains(r) ?? false).ToList();
            var newRoles        = model.SelectedRoles?.Where(r => !SelectedUserRoles.Contains(r)).ToList();
            var rolesUpdated    = false;

            if (unselectedRoles.Any())
            {
                await _userManager.RemoveFromRolesAsync(SelectedUser, unselectedRoles);

                rolesUpdated = true;
            }

            if (!newRoles.IsNullOrEmpty())
            {
                await _userManager.AddToRolesAsync(SelectedUser, newRoles);

                rolesUpdated = true;
            }

            if (rolesUpdated)
            {
                EZmsInMemoryCache.Remove($"EZms:principal:userroles:{id}");
            }

            SelectedUser.Email       = model.Email;
            SelectedUser.PhoneNumber = model.PhoneNumber;

            await _userManager.UpdateAsync(SelectedUser);

            if (unselectedRoles.Any() || newRoles.Any())
            {
                SelectedUserRoles = await GetUserRoles(SelectedUser);
            }

            return(Page());
        }
コード例 #11
0
        public override async Task <IContent> GetContent(int id, int?version = null)
        {
            return(await EZmsInMemoryCache.GetOrCreateAsync($"DefaultContentLoader:GetContent({id}, {version})",
                                                            async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));

                if (version.HasValue)
                {
                    var cv = await _contentVersionRepository.GetContent(version.Value);
                    if (cv != null)
                    {
                        return (IContent)cv;
                    }
                }

                var content = await _repository.GetContent(id);
                if (content == null || !content.Published)
                {
                    return null;
                }
                return (IContent)content;
            }));
        }
コード例 #12
0
        public override async Task <T> Get <T>(int id, int?version = null)
        {
            return(await EZmsInMemoryCache.GetOrCreateAsync($"DefaultContentLoader:Get<{typeof(T).FullName}>({id}, {version})",
                                                            async entry =>
            {
                entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));

                if (version.HasValue)
                {
                    var cv = await _contentVersionRepository.Get <T>(version.Value, true);
                    if (cv != null)
                    {
                        return cv;
                    }
                }

                var content = await _repository.GetContent(id);
                if (content == null || !content.Published || content.ModelType != typeof(T))
                {
                    return default(T);
                }
                return content.ToType <T>();
            }));
        }
コード例 #13
0
 public void Clear()
 {
     EZmsInMemoryCache.Remove(_cacheKey);
     EZmsInMemoryCache.Remove(PageConstraint.CacheKey);
     EZmsInMemoryCache.Clear();
 }
コード例 #14
0
        public static IEnumerable <T> FilterForVisitor <T>(this IEnumerable <T> contents, ClaimsPrincipal principal = null, bool includeUnPublished = false) where T : IContent
        {
            if (contents == null)
            {
                return(Enumerable.Empty <T>());
            }

            if (!includeUnPublished)
            {
                var publishedStateAccessor = ServiceLocator.Current.GetInstance <IPublishedStateAccessor>();
                contents = contents.Where(publishedStateAccessor.IsPublished);
            }

            if (principal == null)
            {
                var httpContext = ServiceLocator.Current.GetInstance <IHttpContextAccessor>()?.HttpContext;
                if (httpContext != null)
                {
                    principal = httpContext.User;
                }
            }


            var isAuthenticated = principal?.Identity?.IsAuthenticated ?? false;
            var userRoleIds     = new List <string>();

            if (isAuthenticated)
            {
                var userId   = principal.FindFirstValue(ClaimTypes.NameIdentifier);
                var cacheKey = $"EZms:principal:userroles:{userId}";

                if (!EZmsInMemoryCache.TryGetValue(cacheKey, out userRoleIds))
                {
                    var userManager = ServiceLocator.Current.GetInstance <UserManager <IdentityUser> >();
                    var user        = userManager.FindByIdAsync(userId).GetAwaiter().GetResult();
                    if (user != null)
                    {
                        var userRoles = userManager.GetRolesAsync(user).GetAwaiter().GetResult().ToList();

                        if (!userRoles.IsNullOrEmpty())
                        {
                            var roleManager = ServiceLocator.Current.GetInstance <RoleManager <IdentityRole> >();
                            userRoleIds = roleManager.Roles
                                          .Where(w => userRoles.Contains(w.Name, StringComparer.OrdinalIgnoreCase))
                                          .Select(w => w.Id)
                                          .ToListAsync().GetAwaiter().GetResult();
                        }
                    }

                    EZmsInMemoryCache.Set(
                        cacheKey,
                        userRoleIds,
                        new MemoryCacheEntryOptions {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
                    });
                }
            }

            var filtered = contents.Where(w => w.AllowedGroups.IsNullOrEmpty() || (!w.AllowedGroups.IsNullOrEmpty() && w.AllowedGroups.Intersect(userRoleIds).Any()));

            return(filtered);
        }