示例#1
0
        /// <summary>
        /// Associates a per-type authorization rule with
        /// the business type.
        /// </summary>
        /// <param name="objectType">Type of business object.</param>
        /// <param name="rule">Rule object.</param>
        /// <param name="ruleSet">Rule set name.</param>
        public static void AddRule(Type objectType, IAuthorizationRule rule, string ruleSet)
        {
            var typeRules = AuthorizationRuleManager.GetRulesForType(objectType, ruleSet);

            EnsureUniqueRule(typeRules, rule);
            typeRules.Rules.Add(rule);
        }
示例#2
0
 /// <summary>
 /// Creates a AuthorizationContext instance for unit testing.
 /// </summary>
 /// <param name="rule">The rule.</param>
 /// <param name="user">The current user.</param>
 /// <param name="target">The target.</param>
 /// <param name="targetType">Type of the target.</param>
 public AuthorizationContext(IAuthorizationRule rule, IPrincipal user, object target, Type targetType)
 {
     Rule       = rule;
     User       = user;
     Target     = target;
     TargetType = targetType;
 }
示例#3
0
 /// <summary>
 /// Creates a AuthorizationContext instance for unit testing.
 /// </summary>
 /// <param name="applicationContext"></param>
 /// <param name="rule">The rule.</param>
 /// <param name="target">The target.</param>
 /// <param name="targetType">Type of the target.</param>
 public AuthorizationContext(ApplicationContext applicationContext, IAuthorizationRule rule, object target, Type targetType)
 {
     ApplicationContext = applicationContext;
     Rule       = rule;
     Target     = target;
     TargetType = targetType;
 }
示例#4
0
 public AuthorizationService(
     IRepository<UserGroup> userGroupRepository,
     IAuthorizationRule[] rules,
     IPermissionProvider[] providers
     )
 {
     this.userGroupRepository = userGroupRepository;
     this.rules = rules;
     this.providers = providers;
 }
示例#5
0
        /// <summary>
        /// Initializes the test.
        /// </summary>
        /// <param name="rule">The rule.</param>
        /// <param name="target">The target.</param>
        /// <param name="type">The type.</param>
        public void InitializeTest(IAuthorizationRule rule, object target, Type type)
        {
            var services = new ServiceCollection();

            services.AddCsla();
            var provider           = services.BuildServiceProvider();
            var applicationContext = provider.GetRequiredService <ApplicationContext>();

            Accessor = new ObjectAccessor(applicationContext);

            AuthorizationContext = new AuthorizationContext(applicationContext, rule, target, type);
        }
示例#6
0
        private BooleanExpression GetParsedExpression(string ruleName)
        {
            IAuthorizationRule rule = null;

            authorizationRules.TryGetValue(ruleName, out rule);
            if (rule == null)
            {
                return(null);
            }

            string expression = rule.Expression;
            Parser parser     = new Parser();

            return(parser.Parse(expression));
        }
示例#7
0
        private static void EnsureUniqueRule(AuthorizationRuleManager mgr, IAuthorizationRule rule)
        {
            IAuthorizationRule oldRule = null;

            if (rule.Element != null)
            {
                oldRule = mgr.Rules.FirstOrDefault(c => c.Element != null && c.Element.Name == rule.Element.Name && c.Action == rule.Action);
            }
            else
            {
                oldRule = mgr.Rules.FirstOrDefault(c => c.Element == null && c.Action == rule.Action);
            }
            if (oldRule != null)
            {
                throw new ArgumentException("rule");
            }
        }
        BooleanExpression GetParsedExpression(string ruleName, bool expEmpty)
        {
            expEmpty = false;
            IAuthorizationRule rule = null;

            authorizationRules.TryGetValue(ruleName, out rule);
            if (rule == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(rule.Expression))
            {
                expEmpty = true;
            }

            Parser parser = new Parser();

            return(parser.Parse(rule.Expression));
        }
示例#9
0
 /// <summary>
 /// Initializes the test.
 /// </summary>
 /// <param name="rule">The rule.</param>
 /// <param name="target">The target.</param>
 /// <param name="type">The type.</param>
 public void InitializeTest(IAuthorizationRule rule, object target, Type type)
 {
     AuthorizationContext = new AuthorizationContext(rule, target, type);
 }
示例#10
0
 /// <summary>
 /// Associates an authorization rule with the business object.
 /// </summary>
 /// <param name="rule">Rule object.</param>
 public void AddRule(IAuthorizationRule rule)
 {
     EnsureUniqueRule(TypeAuthRules, rule);
     TypeAuthRules.Rules.Add(rule);
 }
示例#11
0
 /// <summary>
 /// Associates a per-type authorization rule with
 /// the business type in the default rule set.
 /// </summary>
 /// <param name="objectType">Type of business object.</param>
 /// <param name="rule">Rule object.</param>
 public static void AddRule(Type objectType, IAuthorizationRule rule)
 {
     AddRule(objectType, rule, ApplicationContext.RuleSet);
 }
示例#12
0
 /// <summary>
 /// Creates a AuthorizationContext instance for unit testing.
 /// </summary>
 /// <param name="rule">The rule.</param>
 /// <param name="target">The target.</param>
 /// <param name="targetType">Type of the target.</param>
 public AuthorizationContext(IAuthorizationRule rule, object target, Type targetType)
 {
   Rule = rule;
   Target = target;
   TargetType = targetType;
 }
示例#13
0
 /// <summary>
 /// Creates a AuthorizationContext instance for unit testing.
 /// </summary>
 /// <param name="rule">The rule.</param>
 /// <param name="target">The target.</param>
 /// <param name="targetType">Type of the target.</param>
 public AuthorizationContext(IAuthorizationRule rule, object target, Type targetType)
 {
     Rule       = rule;
     Target     = target;
     TargetType = targetType;
 }
示例#14
0
 /// <summary>
 /// Associates a per-type authorization rule with 
 /// the business type.
 /// </summary>
 /// <param name="objectType">Type of business object.</param>
 /// <param name="rule">Rule object.</param>
 /// <param name="ruleSet">Rule set name.</param>
 public static void AddRule(Type objectType, IAuthorizationRule rule, string ruleSet)
 {
   var typeRules = AuthorizationRuleManager.GetRulesForType(objectType, ruleSet);
   EnsureUniqueRule(typeRules, rule);
   typeRules.Rules.Add(rule);
 }
示例#15
0
 private static void EnsureUniqueRule(AuthorizationRuleManager mgr, IAuthorizationRule rule)
 {
   IAuthorizationRule oldRule = null;
   if (rule.Element != null)
     oldRule = mgr.Rules.FirstOrDefault(c => c.Element != null && c.Element.Name == rule.Element.Name && c.Action == rule.Action);
   else
     oldRule = mgr.Rules.FirstOrDefault(c => c.Element == null && c.Action == rule.Action);
   if (oldRule != null)
     throw new ArgumentException("rule");
 }
示例#16
0
 /// <summary>
 /// Associates a per-type authorization rule with 
 /// the business type in the default rule set.
 /// </summary>
 /// <param name="objectType">Type of business object.</param>
 /// <param name="rule">Rule object.</param>
 public static void AddRule(Type objectType, IAuthorizationRule rule)
 {
   AddRule(objectType, rule, ApplicationContext.RuleSet);
 }
示例#17
0
 /// <summary>
 /// Initializes the test.
 /// </summary>
 /// <param name="rule">The rule.</param>
 /// <param name="target">The target.</param>
 /// <param name="type">The type.</param>
 public void InitializeTest(IAuthorizationRule rule, object target, Type type)
 {
   AuthorizationContext = new AuthorizationContext(rule, target, type);
 }
示例#18
0
 private static string GetReason(IAuthorizationRule <T> rule)
 {
     return($"Allowed by requirement - {rule.GetType().Name}");
 }
示例#19
0
 public Allow(IAuthorizationRule <T> rule)
     : base(GetReason(rule))
 {
 }
示例#20
0
 public RuleDefinition(IAuthorizationRule rule, RuleConcatenationModeEnum mode)
     : this()
 {
     this.Rule = rule;
     this.Mode = mode;
 }
 public AuthorizationPolicy(IAuthorizationRule <TModel> innerRule)
 {
     _innerRule = innerRule;
 }
示例#22
0
 /// <summary>
 /// Associates an authorization rule with the business object.
 /// </summary>
 /// <param name="rule">Rule object.</param>
 public void AddRule(IAuthorizationRule rule)
 {
   EnsureUniqueRule(TypeAuthRules, rule);
   TypeAuthRules.Rules.Add(rule);
 }