private static string CreateMessageForResult(PolicyResult result, PolicyResultCacheStrategy strategy, string cacheKey)
 {
     return("Enforced policy {0} - {1}! \r\n{2}: {3} at {4} with key '{5}'".FormatWith(
                result.PolicyType.FullName,
                result.ViolationOccured ? "Violation occured" : "Success",
                result.Cached ? "Cached. Strategy" : "Strategy",
                strategy.CacheLifecycle,
                strategy.CacheLevel,
                cacheKey
                ));
 }
        public void Should_be_PolicyResult_BlogController_star_TestPolicy()
        {
            // Arrange
            var policy   = new BlogAdminPolicy();
            var strategy = new PolicyResultCacheStrategy("BlogController", "Login", policy.GetType(), Cache.DoNotCache, By.Controller);
            var context  = TestDataFactory.CreateSecurityContext(true);

            // Act
            var cacheKey = PolicyResultCacheKeyBuilder.CreateFromStrategy(strategy, policy, context);

            // Assert
            Assert.That(cacheKey, Is.EqualTo("PolicyResult_BlogController_*_" + NameHelper.Policy <BlogAdminPolicy>()));
        }
        public void Should_be_PolicyResult_BlogController_Post_BlogEditorPolicy_when_custom_cache_key_is_null()
        {
            // Arrange
            var policy   = new BlogEditorPolicy(null);
            var strategy = new PolicyResultCacheStrategy("BlogController", "Post", policy.GetType(), Cache.DoNotCache);
            var context  = TestDataFactory.CreateSecurityContext(true);

            // Act
            var cacheKey = PolicyResultCacheKeyBuilder.CreateFromStrategy(strategy, policy, context);

            // Assert
            Assert.That(cacheKey, Is.EqualTo("PolicyResult_BlogController_Post_" + NameHelper.Policy <BlogEditorPolicy>()));
        }
        public void Should_be_PolicyResult_star_star_TestPolicy()
        {
            // Arrange
            var policy = new TestPolicy();
            var strategy = new PolicyResultCacheStrategy("AdminController", "Login", policy.GetType(), Cache.DoNotCache, By.Policy);
            var context = TestDataFactory.CreateSecurityContext(true);

            // Act
            var cacheKey = PolicyResultCacheKeyBuilder.CreateFromStrategy(strategy, policy, context);

            // Assert
            Assert.That(cacheKey, Is.EqualTo("PolicyResult_*_*_" + NameHelper.Policy<TestPolicy>()));
        }
 private static string BuildCacheKey(PolicyResultCacheStrategy strategy, string policyCacheKey)
 {
     string cacheKey;
     switch (strategy.CacheLevel)
     {
         case By.Controller:
             cacheKey = String.Concat(strategy.ControllerName, Separator, "*", Separator, policyCacheKey);
             break;
         case By.ControllerAction:
             cacheKey = String.Concat(strategy.ControllerName, Separator, strategy.ActionName, Separator, policyCacheKey);
             break;
         default: // Policy
             cacheKey = String.Concat("*", Separator, "*", Separator, policyCacheKey);
             break;
     }
     return String.Concat(typeof(PolicyResult).Name, Separator, cacheKey);
 }
        private static string BuildPolicyCacheKey(PolicyResultCacheStrategy strategy, ISecurityPolicy securityPolicy, ISecurityContext context)
        {
            var customPolicyCacheKey = String.Empty;

            var cacheKeyProvider = securityPolicy as ICacheKeyProvider;
            if (cacheKeyProvider != null)
            {
                customPolicyCacheKey = cacheKeyProvider.Get(context);
                if (customPolicyCacheKey != null)
                {
                    while (customPolicyCacheKey.StartsWith(" ") || customPolicyCacheKey.EndsWith(" "))
                        customPolicyCacheKey = customPolicyCacheKey.Trim();

                    if (!String.IsNullOrWhiteSpace(customPolicyCacheKey))
                        customPolicyCacheKey = String.Concat(Separator, customPolicyCacheKey);
                }
            }

            return String.Concat(strategy.PolicyType.FullName, customPolicyCacheKey);
        }
 public static string CreateFromStrategy(PolicyResultCacheStrategy strategy, ISecurityPolicy securityPolicy, ISecurityContext context)
 {
     var policyCacheKey = BuildPolicyCacheKey(strategy, securityPolicy, context);
     var cacheKey = BuildCacheKey(strategy, policyCacheKey);
     return cacheKey;
 }
        public void Should_be_PolicyResult_BlogController_Post_BlogEditorPolicy_1_when_custom_cache_key_is_1()
        {
            // Arrange
            var policy = new BlogEditorPolicy("1");
            var strategy = new PolicyResultCacheStrategy("BlogController", "Post", policy.GetType(), Cache.DoNotCache);
            var context = TestDataFactory.CreateSecurityContext(true);

            // Act
            var cacheKey = PolicyResultCacheKeyBuilder.CreateFromStrategy(strategy, policy, context);

            // Assert
            Assert.That(cacheKey, Is.EqualTo("PolicyResult_BlogController_Post_" + NameHelper.Policy<BlogEditorPolicy>() + "_1"));
        }