CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
RuleExtensions.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="PropertyHelper.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>internal implementation to get a registered property</summary>
7//----------------------------------------------------------------------
8using System;
9using System.Linq;
10using System.Text;
11using Csla.Core;
13using Csla.Rules;
15
16namespace Csla.Validation
17{
18
25 public delegate bool RuleHandler(object target, RuleArgs e);
26
33 public delegate bool RuleHandler<T, R>(T target, R e) where R : RuleArgs;
34
38 //[Obsolete("For migration of older apps to Csla 4.x only")]
39 public static class RuleExtensions
40 {
50 //[Obsolete("For migration of older apps to Csla 4.x only")]
51 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T,RuleArgs> ruleHandler, Csla.Core.IPropertyInfo primaryProperty, RuleArgs ruleArgs, int priority) where T : BusinessBase<T>
52 {
53 var rule = new Csla.Rules.CommonRules.Lambda(primaryProperty, (o) =>
54 {
55
56 var target = (T)o.Target;
57 using (new ObjectAccessor().BypassPropertyChecks(target))
58 {
59 if (!ruleHandler(target, ruleArgs))
60 {
61 o.Results.Add(new RuleResult(o.Rule.RuleName, o.Rule.PrimaryProperty, ruleArgs.Description) { Severity = ruleArgs.Severity, StopProcessing = ruleArgs.StopProcessing });
62 }
63 else if (ruleArgs.StopProcessing)
64 {
65 o.AddSuccessResult(true);
66 }
67 else
68 {
69 o.AddSuccessResult(false);
70 }
71 ruleArgs.StopProcessing = false;
72 }
73 });
74
75#if NETFX_CORE
76 var methodName = Guid.NewGuid().ToString();
77#else
78 var methodName = ruleHandler.Method.ToString();
79#endif
80 rule.AddQueryParameter("r", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
81 rule.Priority = priority;
82 businessRules.AddRule(rule);
83 }
84
85
93 //[Obsolete("For migration of older apps to Csla 4.x only")]
94 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T, RuleArgs> ruleHandler, Csla.Core.IPropertyInfo primaryProperty) where T : BusinessBase<T>
95 {
96 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), 0);
97 }
98
107 //[Obsolete("For migration of older apps to Csla 4.x only")]
108 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T,RuleArgs> ruleHandler , Csla.Core.IPropertyInfo primaryProperty, int priority) where T : BusinessBase<T>
109 {
110 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), priority);
111 }
112
121 //[Obsolete("For migration of older apps to Csla 4.x only")]
122 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T, RuleArgs> ruleHandler, string primaryPropertyName, int priority) where T : BusinessBase<T>
123 {
124 if (string.IsNullOrEmpty(primaryPropertyName)) throw new ArgumentException("primaryPropertyName");
125
126 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, primaryPropertyName);
127 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), priority);
128 }
129
137 //[Obsolete("For migration of older apps to Csla 4.x only")]
138 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T, RuleArgs> ruleHandler, RuleArgs args) where T : BusinessBase<T>
139 {
140 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, args.PropertyName);
141 AddRule(businessRules, ruleHandler, primaryProperty, args, 0);
142 }
143
152 //[Obsolete("For migration of older apps to Csla 4.x only")]
153 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T, RuleArgs> ruleHandler, RuleArgs args, int priority) where T : BusinessBase<T>
154 {
155 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, args.PropertyName);
156 AddRule(businessRules, ruleHandler, primaryProperty, args, priority);
157 }
158
167 //[Obsolete("For migration of older apps to Csla 4.x only")]
168 public static void AddRule<T>(this BusinessRules businessRules, RuleHandler<T, RuleArgs> ruleHandler, string primaryPropertyName) where T : BusinessBase<T>
169 {
170 if (string.IsNullOrEmpty(primaryPropertyName)) throw new ArgumentException("primaryPropertyName");
171
172 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, primaryPropertyName);
173 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), 0);
174 }
175
176
185 //[Obsolete("For migration of older apps to Csla 4.x only")]
186 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, Csla.Core.IPropertyInfo primaryProperty, RuleArgs ruleArgs, int priority)
187 {
188 var rule = new Csla.Rules.CommonRules.Lambda(primaryProperty, (o) =>
189 {
190
191 var target = (Csla.Core.BusinessBase)o.Target;
192 using (new ObjectAccessor().BypassPropertyChecks(target))
193 {
194 if (!ruleHandler(target, ruleArgs))
195 {
196 o.Results.Add(new RuleResult(o.Rule.RuleName, o.Rule.PrimaryProperty, ruleArgs.Description) { Severity = ruleArgs.Severity, StopProcessing = ruleArgs.StopProcessing });
197 }
198 else if (ruleArgs.StopProcessing)
199 {
200 o.AddSuccessResult(true);
201 }
202 else
203 {
204 o.AddSuccessResult(false);
205 }
206 ruleArgs.StopProcessing = false;
207 }
208 });
209
210#if NETFX_CORE
211 var methodName = Guid.NewGuid().ToString();
212#else
213 var methodName = ruleHandler.Method.ToString();
214#endif
215 rule.AddQueryParameter("r", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
216 rule.Priority = priority;
217 businessRules.AddRule(rule);
218 }
219
220
227 //[Obsolete("For migration of older apps to Csla 4.x only")]
228 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, Csla.Core.IPropertyInfo primaryProperty)
229 {
230 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), 0);
231 }
232
240 //[Obsolete("For migration of older apps to Csla 4.x only")]
241 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, Csla.Core.IPropertyInfo primaryProperty, int priority)
242 {
243 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), priority);
244 }
245
253 //[Obsolete("For migration of older apps to Csla 4.x only")]
254 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, string primaryPropertyName, int priority)
255 {
256 if (string.IsNullOrEmpty(primaryPropertyName)) throw new ArgumentException("primaryPropertyName");
257 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, primaryPropertyName);
258 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), priority);
259 }
260
261
268 //[Obsolete("For migration of older apps to Csla 4.x only")]
269 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, string primaryPropertyName)
270 {
271 if (string.IsNullOrEmpty(primaryPropertyName)) throw new ArgumentException("primaryPropertyName");
272
273 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, primaryPropertyName);
274 AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), 0);
275 }
276
283 //[Obsolete("For migration of older apps to Csla 4.x only")]
284 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, RuleArgs args)
285 {
286 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, args.PropertyName);
287 AddRule(businessRules, ruleHandler, primaryProperty, args, 0);
288 }
289
297 //[Obsolete("For migration of older apps to Csla 4.x only")]
298 public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, RuleArgs args, int priority)
299 {
300 var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, args.PropertyName);
301 AddRule(businessRules, ruleHandler, primaryProperty, args, priority);
302 }
303
304
312 //[Obsolete("For migration of older apps to Csla 4.x only")]
313 public static void AddDependentProperty(this BusinessRules businessRules, IPropertyInfo propertyInfo, IPropertyInfo dependentPropertyInfo, bool isBidirectional)
314 {
315 if (propertyInfo == null) throw new ArgumentNullException("propertyInfo");
316 if (dependentPropertyInfo == null) throw new ArgumentNullException("dependentPropertyInfo");
317
318 businessRules.AddRule(new Dependency(propertyInfo, dependentPropertyInfo));
319 if (isBidirectional)
320 businessRules.AddRule(new Dependency(dependentPropertyInfo, propertyInfo));
321 }
322
329 //[Obsolete("For migration of older apps to Csla 4.x only")]
330 public static void AddDependentProperty(this BusinessRules businessRules, IPropertyInfo propertyInfo, IPropertyInfo dependentPropertyInfo)
331 {
332 AddDependentProperty(businessRules, propertyInfo, dependentPropertyInfo, false);
333 }
334
341 //[Obsolete("For migration of older apps to Csla 4.x only")]
342 public static void AddDependentProperty(this BusinessRules businessRules, string propertyName, string dependentPropertyName)
343 {
344 AddDependentProperty(businessRules, propertyName, dependentPropertyName, false);
345 }
346
354 //[Obsolete("For migration of older apps to Csla 4.x only")]
355 public static void AddDependentProperty(this BusinessRules businessRules, string propertyName, string dependentPropertyName, bool isBidirectional)
356 {
357 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
358 var dependentPropertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, dependentPropertyName);
359
360 AddDependentProperty(businessRules, propertyInfo, dependentPropertyInfo, isBidirectional);
361 }
362
363 #region Add Per-Type Roles
364
378 public static void AllowRead(this BusinessRules businessRules, Core.IPropertyInfo propertyInfo, params string[] roles)
379 {
380 businessRules.AddRule(new IsInRole(AuthorizationActions.ReadProperty, propertyInfo, roles));
381 }
382
396 public static void AllowRead(this BusinessRules businessRules, string propertyName, params string[] roles)
397 {
398 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
399 businessRules.AddRule(new IsInRole(AuthorizationActions.ReadProperty, propertyInfo, roles));
400 }
401
415 public static void DenyRead(this BusinessRules businessRules, Core.IPropertyInfo propertyInfo, params string[] roles)
416 {
417 businessRules.AddRule(new IsNotInRole(AuthorizationActions.ReadProperty, propertyInfo, roles));
418 }
419
433 public static void DenyRead(this BusinessRules businessRules, string propertyName, params string[] roles)
434 {
435 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
436 businessRules.AddRule(new IsNotInRole(AuthorizationActions.ReadProperty, propertyInfo, roles));
437 }
438
452 public static void AllowWrite(this BusinessRules businessRules, Core.IPropertyInfo propertyInfo, params string[] roles)
453 {
454 businessRules.AddRule(new IsInRole(AuthorizationActions.WriteProperty, propertyInfo, roles));
455 }
456
470 public static void AllowWrite(this BusinessRules businessRules, string propertyName, params string[] roles)
471 {
472 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
473 businessRules.AddRule(new IsInRole(AuthorizationActions.WriteProperty, propertyInfo, roles));
474 }
475
489 public static void DenyWrite(this BusinessRules businessRules, Core.IPropertyInfo propertyInfo, params string[] roles)
490 {
491 businessRules.AddRule(new IsNotInRole(AuthorizationActions.WriteProperty, propertyInfo, roles));
492 }
493
507 public static void DenyWrite(this BusinessRules businessRules, string propertyName, params string[] roles)
508 {
509 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
510 businessRules.AddRule(new IsNotInRole(AuthorizationActions.WriteProperty, propertyInfo, roles));
511 }
512
526 public static void AllowExecute(this BusinessRules businessRules, Core.IMemberInfo memberInfo, params string[] roles)
527 {
528 businessRules.AddRule(new IsInRole(AuthorizationActions.ExecuteMethod, memberInfo, roles));
529 }
530
544 public static void AllowExecute(this BusinessRules businessRules, string methodName, params string[] roles)
545 {
546 var methodInfo = new MethodInfo(methodName);
547 businessRules.AddRule(new IsInRole(AuthorizationActions.ReadProperty, methodInfo, roles));
548 }
549
563 public static void DenyExecute(this BusinessRules businessRules, Core.IMemberInfo methodInfo, params string[] roles)
564 {
565 businessRules.AddRule(new IsNotInRole(AuthorizationActions.ReadProperty, methodInfo, roles));
566 }
567
581 public static void DenyExecute(this BusinessRules businessRules, string methodName, params string[] roles)
582 {
583 var methodInfo = new MethodInfo(methodName);
584 businessRules.AddRule(new IsNotInRole(AuthorizationActions.ReadProperty, methodInfo, roles));
585 }
586
587 #endregion
588
589 #region Check Roles
590
600 public static bool HasReadAllowedRoles(this BusinessRules businessRules, string propertyName)
601 {
602 return true;
603 }
604
615 public static bool IsReadAllowed(this BusinessRules businessRules, string propertyName)
616 {
617 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
618 return businessRules.HasPermission(AuthorizationActions.ReadProperty, propertyInfo);
619 }
620
630 public static bool HasReadDeniedRoles(this BusinessRules businessRules, string propertyName)
631 {
632 return true;
633 }
634
645 public static bool IsReadDenied(this BusinessRules businessRules, string propertyName)
646 {
647 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
648 return !businessRules.HasPermission(AuthorizationActions.ReadProperty, propertyInfo);
649 }
650
660 public static bool HasWriteAllowedRoles(this BusinessRules businessRules, string propertyName)
661 {
662 return true;
663 }
664
675 public static bool IsWriteAllowed(this BusinessRules businessRules, string propertyName)
676 {
677 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
678 return businessRules.HasPermission(AuthorizationActions.WriteProperty, propertyInfo);
679 }
680
690 public static bool HasWriteDeniedRoles(this BusinessRules businessRules, string propertyName)
691 {
692 return true;
693 }
694
705 public static bool IsWriteDenied(this BusinessRules businessRules, string propertyName)
706 {
707 var propertyInfo = PropertyHelper.GetRegisteredProperty(businessRules, propertyName);
708 return !businessRules.HasPermission(AuthorizationActions.ReadProperty, propertyInfo);
709 }
710
720 public static bool HasExecuteAllowedRoles(this BusinessRules businessRules, string methodName)
721 {
722 return true;
723 }
724
735 public static bool IsExecuteAllowed(this BusinessRules businessRules, string methodName)
736 {
737 var methodInfo = new MethodInfo(methodName);
738 return businessRules.HasPermission(AuthorizationActions.ExecuteMethod, methodInfo);
739 }
740
750 public static bool HasExecuteDeniedRoles(this BusinessRules businessRules, string methodName)
751 {
752 return true;
753 }
754
765 public static bool IsExecuteDenied(this BusinessRules businessRules, string methodName)
766 {
767 var methodInfo = new MethodInfo(methodName);
768 return businessRules.HasPermission(AuthorizationActions.ReadProperty, methodInfo);
769 }
770
771 #endregion
772 }
773}
This is the non-generic base class from which most business objects will be derived.
Maintains metadata about a method.
Definition: MethodInfo.cs:19
Tracks the business rules for a business object.
static bool HasPermission(AuthorizationActions action, Type objectType)
Checks per-type authorization rules.
void AddRule(IBusinessRuleBase rule)
Associates a business rule with the business object.
A rule that establishes a dependency between two properties.
A business rule defined by a lambda expression.
Contains information about the result of a rule.
Definition: RuleResult.cs:20
Intermediate base class for BusinessBase
Object providing extra information to methods that implement business rules.
Definition: RuleArgs.cs:22
string Description
Set by the rule handler method to describe the broken rule.
Definition: RuleArgs.cs:59
string PropertyName
The name of the property to be validated.
Definition: RuleArgs.cs:33
bool StopProcessing
Gets or sets a value indicating whether this broken rule should stop the processing of subsequent rul...
Definition: RuleArgs.cs:92
Maintains metadata about a property.
AuthorizationActions
Authorization actions.
delegate bool RuleHandler< T, R >(T target, R e)
The delegate definition for CSLA 3.8 generic rule handler
delegate bool RuleHandler(object target, RuleArgs e)
The delegate definition for CSLA 3.8 rule handler