Example #1
0
        public string?GetRouteById(bool preview, int contentId, string?culture = null)
        {
            IAppCache?cache = preview == false || PublishedSnapshotService.FullCacheWhenPreviewing
                ? _elementsCache
                : _snapshotCache;
            var key = CacheKeys.ContentCacheRouteByContent(contentId, preview, culture);

            return(cache?.GetCacheItem(key, () => GetRouteByIdInternal(preview, contentId, null, culture)));
        }
        public IPublishedContent GetByProviderKey(object key)
        {
            return(_requestCache.GetCacheItem <IPublishedContent>(
                       GetCacheKey("GetByProviderKey", key), () =>
            {
                var provider = Core.Security.MembershipProviderExtensions.GetMembersMembershipProvider();
                if (provider.IsUmbracoMembershipProvider() == false)
                {
                    throw new NotSupportedException("Cannot access this method unless the Umbraco membership provider is active");
                }

                var result = _memberService.GetByProviderKey(key);
                if (result == null)
                {
                    return null;
                }
                var type = _contentTypeCache.Get(PublishedItemType.Member, result.ContentTypeId);
                return new PublishedMember(result, type, _umbracoContextAccessor).CreateModel();
            }));
        }
Example #3
0
    public IPublishedContent?GetByRoute(bool preview, string route, bool?hideTopLevelNode = null, string?culture = null)
    {
        if (route == null)
        {
            throw new ArgumentNullException(nameof(route));
        }

        IAppCache?cache = preview == false || PublishedSnapshotService.FullCacheWhenPreviewing
            ? _elementsCache
            : _snapshotCache;
        var key = CacheKeys.ContentCacheContentByRoute(route, preview, culture);

        return(cache?.GetCacheItem(key, () => GetByRouteInternal(preview, route, hideTopLevelNode, culture)));
    }
Example #4
0
        /// <summary>
        /// Tries to lookup the user's Gravatar to see if the endpoint can be reached, if so it returns the valid URL
        /// </summary>
        /// <param name="user"></param>
        /// <param name="cache"></param>
        /// <returns>
        /// A list of 5 different sized avatar URLs
        /// </returns>
        internal static string[] GetUserAvatarUrls(this IUser user, IAppCache cache)
        {
            // If FIPS is required, never check the Gravatar service as it only supports MD5 hashing.
            // Unfortunately, if the FIPS setting is enabled on Windows, using MD5 will throw an exception
            // and the website will not run.
            // Also, check if the user has explicitly removed all avatars including a Gravatar, this will be possible and the value will be "none"
            if (user.Avatar == "none" || CryptoConfig.AllowOnlyFipsAlgorithms)
            {
                return(new string[0]);
            }

            if (user.Avatar.IsNullOrWhiteSpace())
            {
                var gravatarHash = user.Email.GenerateHash <MD5>();
                var gravatarUrl  = "https://www.gravatar.com/avatar/" + gravatarHash + "?d=404";

                //try Gravatar
                var gravatarAccess = cache.GetCacheItem <bool>("UserAvatar" + user.Id, () =>
                {
                    // Test if we can reach this URL, will fail when there's network or firewall errors
                    var request = (HttpWebRequest)WebRequest.Create(gravatarUrl);
                    // Require response within 10 seconds
                    request.Timeout = 10000;
                    try
                    {
                        using ((HttpWebResponse)request.GetResponse()) { }
                    }
                    catch (Exception)
                    {
                        // There was an HTTP or other error, return an null instead
                        return(false);
                    }
                    return(true);
                });

                if (gravatarAccess)
                {
                    return(new[]
                    {
                        gravatarUrl + "&s=30",
                        gravatarUrl + "&s=60",
                        gravatarUrl + "&s=90",
                        gravatarUrl + "&s=150",
                        gravatarUrl + "&s=300"
                    });
                }

                return(new string[0]);
            }

            //use the custom avatar
            var avatarUrl    = Current.MediaFileSystem.GetUrl(user.Avatar);
            var urlGenerator = Current.ImageUrlGenerator;

            return(new[]
            {
                urlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl)
                {
                    ImageCropMode = "crop", Width = 30, Height = 30
                }),
                urlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl)
                {
                    ImageCropMode = "crop", Width = 60, Height = 60
                }),
                urlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl)
                {
                    ImageCropMode = "crop", Width = 90, Height = 90
                }),
                urlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl)
                {
                    ImageCropMode = "crop", Width = 150, Height = 150
                }),
                urlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl)
                {
                    ImageCropMode = "crop", Width = 300, Height = 300
                })
            });
        }
Example #5
0
        /// <summary>
        /// Tries to lookup the user's Gravatar to see if the endpoint can be reached, if so it returns the valid URL
        /// </summary>

        /// <returns>
        /// A list of 5 different sized avatar URLs
        /// </returns>
        public static string[] GetUserAvatarUrls(int userId, string userEmail, string userAvatar, IAppCache cache)
        {
            // If FIPS is required, never check the Gravatar service as it only supports MD5 hashing.
            // Unfortunately, if the FIPS setting is enabled on Windows, using MD5 will throw an exception
            // and the website will not run.
            // Also, check if the user has explicitly removed all avatars including a Gravatar, this will be possible and the value will be "none"
            if (userAvatar == "none" || CryptoConfig.AllowOnlyFipsAlgorithms)
            {
                return(new string[0]);
            }

            if (userAvatar.IsNullOrWhiteSpace())
            {
                var gravatarHash = HashEmailForGravatar(userEmail);
                var gravatarUrl  = "https://www.gravatar.com/avatar/" + gravatarHash + "?d=404";

                //try Gravatar
                var gravatarAccess = cache.GetCacheItem <bool>("UserAvatar" + userId, () =>
                {
                    // Test if we can reach this URL, will fail when there's network or firewall errors
                    var request = (HttpWebRequest)WebRequest.Create(gravatarUrl);
                    // Require response within 10 seconds
                    request.Timeout = 10000;
                    try
                    {
                        using ((HttpWebResponse)request.GetResponse()) { }
                    }
                    catch (Exception)
                    {
                        // There was an HTTP or other error, return an null instead
                        return(false);
                    }
                    return(true);
                });

                if (gravatarAccess)
                {
                    return(new[]
                    {
                        gravatarUrl + "&s=30",
                        gravatarUrl + "&s=60",
                        gravatarUrl + "&s=90",
                        gravatarUrl + "&s=150",
                        gravatarUrl + "&s=300"
                    });
                }

                return(new string[0]);
            }

            var customAvatarUrl = "/media/" + userAvatar;

            return(new []
            {
                GetAvatarCrop(customAvatarUrl, 30),
                GetAvatarCrop(customAvatarUrl, 60),
                GetAvatarCrop(customAvatarUrl, 90),
                GetAvatarCrop(customAvatarUrl, 150),
                GetAvatarCrop(customAvatarUrl, 300),
            });
        }
        public AllowedIpDto GetByIpAddress(string ipAddress)
        {
            var cacheKey = $"{typeof(AllowedIpServiceCachedProxy)}_{ipAddress}";

            return(_runtimeCache.GetCacheItem(cacheKey, () => _allowedIpService.GetByIpAddress(ipAddress)));
        }