Пример #1
0
        public void TestAuthRuleSetsOnStaticHasPermissionMethodsWhenAddingAuthzRuleSetUsingApplicationContextRuleSet()
        {
            var root = PermissionsRoot2.NewPermissionsRoot();

            Csla.ApplicationContext.User = GetPrincipal("Admin");

            Assert.IsTrue(System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
            Assert.IsFalse(System.Threading.Thread.CurrentPrincipal.IsInRole("User"));

            //BusinessRules.AddRule(typeof(PermissionsRoot), new IsInRole(AuthorizationActions.DeleteObject, "User"), ApplicationContext.DefaultRuleSet);
            //BusinessRules.AddRule(typeof(PermissionsRoot), new IsInRole(AuthorizationActions.DeleteObject, "Admin"), "custom1");
            //BusinessRules.AddRule(typeof(PermissionsRoot), new IsInRole(AuthorizationActions.DeleteObject, "User", "Admin"), "custom2");

            // implicit usage of ApplicationContext.RuleSet
            ApplicationContext.RuleSet = ApplicationContext.DefaultRuleSet;
            Assert.IsFalse(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2)));
            ApplicationContext.RuleSet = "custom1";
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2)));
            ApplicationContext.RuleSet = "custom2";
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2)));

            ApplicationContext.RuleSet = ApplicationContext.DefaultRuleSet;

            // directly specifying which ruleset to use
            Assert.IsFalse(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2), ApplicationContext.DefaultRuleSet));
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2), "custom1"));
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2), "custom2"));

            Csla.ApplicationContext.User = new ClaimsPrincipal();
        }
Пример #2
0
        public void TestAuthRuleSetsOnStaticHasPermissionMethodsWhenAddingAuthzRuleSetUsingApplicationContextRuleSet()
        {
            var root = PermissionsRoot2.NewPermissionsRoot();

#pragma warning disable CS0436 // Type conflicts with imported type
            Csla.Test.Security.TestPrincipal.SimulateLogin();
#pragma warning restore CS0436 // Type conflicts with imported type

            Assert.IsTrue(System.Threading.Thread.CurrentPrincipal.IsInRole("Admin"));
            Assert.IsFalse(System.Threading.Thread.CurrentPrincipal.IsInRole("User"));

            //BusinessRules.AddRule(typeof(PermissionsRoot), new IsInRole(AuthorizationActions.DeleteObject, "User"), ApplicationContext.DefaultRuleSet);
            //BusinessRules.AddRule(typeof(PermissionsRoot), new IsInRole(AuthorizationActions.DeleteObject, "Admin"), "custom1");
            //BusinessRules.AddRule(typeof(PermissionsRoot), new IsInRole(AuthorizationActions.DeleteObject, "User", "Admin"), "custom2");

            // implicit usage of ApplicationContext.RuleSet
            ApplicationContext.RuleSet = ApplicationContext.DefaultRuleSet;
            Assert.IsFalse(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2)));
            ApplicationContext.RuleSet = "custom1";
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2)));
            ApplicationContext.RuleSet = "custom2";
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2)));

            ApplicationContext.RuleSet = ApplicationContext.DefaultRuleSet;

            // directly specifying which ruleset to use
            Assert.IsFalse(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2), ApplicationContext.DefaultRuleSet));
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2), "custom1"));
            Assert.IsTrue(BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(PermissionsRoot2), "custom2"));

#pragma warning disable CS0436 // Type conflicts with imported type
            Csla.Test.Security.TestPrincipal.SimulateLogout();
#pragma warning restore CS0436 // Type conflicts with imported type
        }