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.
HtmlExtensions.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="HtmlExtensions.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Html extension methods providing support for HTML rendering based on security permissions in an application.</summary>
7//-----------------------------------------------------------------------
8#if NETSTANDARD2_0 || NET5_0 || NETCORE3_1
9using System;
10using System.Linq;
11using Microsoft.AspNetCore.Html;
12using Microsoft.AspNetCore.Mvc.Rendering;
13using Microsoft.AspNetCore.Mvc.ViewFeatures;
14
15namespace Csla.Web.Mvc
16{
20 public static class HtmlExtensions
21 {
30 public static HtmlString InformationFor<T>(
31 this IHtmlHelper<T> htmlHelper,
32 System.Linq.Expressions.Expression<Func<T, object>> expression)
33 {
34 var result = string.Empty;
35 var model = htmlHelper.ViewData.Model;
36 System.Reflection.PropertyInfo reflectedPropertyInfo = Csla.Reflection.Reflect<T>.GetProperty(expression);
37 var propertyName = reflectedPropertyInfo.Name;
38 if (model is Csla.Core.BusinessBase bb)
39 result = bb.BrokenRulesCollection.ToString(",", Rules.RuleSeverity.Information, propertyName);
40 return new HtmlString(result);
41 }
42
51 public static HtmlString WarningFor<T>(
52 this IHtmlHelper<T> htmlHelper,
53 System.Linq.Expressions.Expression<Func<T, object>> expression)
54 {
55 var result = string.Empty;
56 var model = htmlHelper.ViewData.Model;
57 System.Reflection.PropertyInfo reflectedPropertyInfo = Csla.Reflection.Reflect<T>.GetProperty(expression);
58 var propertyName = reflectedPropertyInfo.Name;
59 if (model is Csla.Core.BusinessBase bb)
60 result = bb.BrokenRulesCollection.ToString(",", Rules.RuleSeverity.Warning, propertyName);
61 return new HtmlString(result);
62 }
63
72 public static HtmlString ErrorFor<T>(
73 this IHtmlHelper<T> htmlHelper,
74 System.Linq.Expressions.Expression<Func<T, object>> expression)
75 {
76 var result = string.Empty;
77 var model = htmlHelper.ViewData.Model;
78 System.Reflection.PropertyInfo reflectedPropertyInfo = Csla.Reflection.Reflect<T>.GetProperty(expression);
79 var propertyName = reflectedPropertyInfo.Name;
80 if (model is Csla.Core.BusinessBase bb)
81 result = bb.BrokenRulesCollection.ToString(",", Rules.RuleSeverity.Error, propertyName);
82 return new HtmlString(result);
83 }
84
94 public static HtmlString HasPermission(
95 this IHtmlHelper htmlHelper,
96 Csla.Rules.AuthorizationActions action,
97 Type objectType,
98 HtmlString granted,
99 HtmlString denied)
100 {
101 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
102 return granted;
103 else
104 return denied;
105 }
106
116 public static HtmlString HasPermission(
117 this IHtmlHelper htmlHelper,
118 Csla.Rules.AuthorizationActions action,
119 Type objectType,
120 HtmlString granted,
121 string denied)
122 {
123 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
124 return granted;
125 else
126 return new HtmlString(denied);
127 }
128
138 public static HtmlString HasPermission(
139 this IHtmlHelper htmlHelper,
140 Csla.Rules.AuthorizationActions action,
141 Type objectType,
142 string granted,
143 string denied)
144 {
145 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
146 return new HtmlString(granted);
147 else
148 return new HtmlString(denied);
149 }
150
161 public static HtmlString HasPermission(
162 this IHtmlHelper htmlHelper,
163 Csla.Rules.AuthorizationActions action,
164 object target,
165 Csla.Core.IMemberInfo member,
166 HtmlString granted,
167 HtmlString denied)
168 {
169 var instance = target as Csla.Security.IAuthorizeReadWrite;
170 if (instance == null) return denied;
171
172 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
173 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
174 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
175 return granted;
176 else
177 return denied;
178 }
179
190 public static HtmlString HasPermission(
191 this IHtmlHelper htmlHelper,
192 Csla.Rules.AuthorizationActions action,
193 object target,
194 Csla.Core.IMemberInfo member,
195 HtmlString granted,
196 string denied)
197 {
198 var instance = target as Csla.Security.IAuthorizeReadWrite;
199 if (instance == null) return new HtmlString(denied);
200
201 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
202 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
203 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
204 return granted;
205 else
206 return new HtmlString(denied);
207 }
208
219 public static HtmlString HasPermission(
220 this IHtmlHelper htmlHelper,
221 Csla.Rules.AuthorizationActions action,
222 object target,
223 Csla.Core.IMemberInfo member,
224 string granted,
225 string denied)
226 {
227 var instance = target as Csla.Security.IAuthorizeReadWrite;
228 if (instance == null) return new HtmlString(denied);
229
230 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
231 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
232 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
233 return new HtmlString(granted);
234 else
235 return new HtmlString(denied);
236 }
237
246 public static HtmlString HasPermission(
247 this IHtmlHelper htmlHelper,
248 Csla.Rules.AuthorizationActions action,
249 Type objectType,
250 Func<IHtmlHelper, HtmlString> grantedAction)
251 {
252 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
253 return grantedAction.Invoke(htmlHelper);
254 else
255 return HtmlString.Empty;
256 }
257
268 public static HtmlString HasPermission(
269 this IHtmlHelper htmlHelper,
270 Csla.Rules.AuthorizationActions action,
271 object target,
272 Csla.Core.IMemberInfo member,
273 Func<IHtmlHelper, HtmlString> grantedAction,
274 Func<IHtmlHelper, HtmlString> denieddAction)
275 {
276 var instance = target as Csla.Security.IAuthorizeReadWrite;
277 if (instance == null) return denieddAction.Invoke(htmlHelper);
278
279 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
280 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
281 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
282 return grantedAction.Invoke(htmlHelper);
283 else
284 return denieddAction.Invoke(htmlHelper);
285 }
286 }
287}
288#else
289using System;
290using System.Collections.Generic;
291using System.Linq;
292using System.Text;
293using System.Web.Mvc;
294using System.Web.Mvc.Html;
295using System.Web.Routing;
296
297namespace Csla.Web.Mvc
298{
302 public static class HtmlExtensions
303 {
313 public static MvcHtmlString HasPermission(
314 this HtmlHelper htmlHelper,
315 Csla.Rules.AuthorizationActions action,
316 Type objectType,
317 MvcHtmlString granted,
318 MvcHtmlString denied)
319 {
320 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
321 return granted;
322 else
323 return denied;
324 }
325
335 public static MvcHtmlString HasPermission(
336 this HtmlHelper htmlHelper,
337 Csla.Rules.AuthorizationActions action,
338 Type objectType,
339 MvcHtmlString granted,
340 string denied)
341 {
342 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
343 return granted;
344 else
345 return MvcHtmlString.Create(denied);
346 }
347
357 public static MvcHtmlString HasPermission(
358 this HtmlHelper htmlHelper,
359 Csla.Rules.AuthorizationActions action,
360 Type objectType,
361 string granted,
362 string denied)
363 {
364 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
365 return MvcHtmlString.Create(granted);
366 else
367 return MvcHtmlString.Create(denied);
368 }
369
380 public static MvcHtmlString HasPermission(
381 this HtmlHelper htmlHelper,
382 Csla.Rules.AuthorizationActions action,
383 object target,
384 Csla.Core.IMemberInfo member,
385 MvcHtmlString granted,
386 MvcHtmlString denied)
387 {
388 var instance = target as Csla.Security.IAuthorizeReadWrite;
389 if (instance == null) return denied;
390
391 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
392 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
393 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
394 return granted;
395 else
396 return denied;
397 }
398
409 public static MvcHtmlString HasPermission(
410 this HtmlHelper htmlHelper,
411 Csla.Rules.AuthorizationActions action,
412 object target,
413 Csla.Core.IMemberInfo member,
414 MvcHtmlString granted,
415 string denied)
416 {
417 var instance = target as Csla.Security.IAuthorizeReadWrite;
418 if (instance == null) return MvcHtmlString.Create(denied);
419
420 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
421 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
422 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
423 return granted;
424 else
425 return MvcHtmlString.Create(denied);
426 }
427
438 public static MvcHtmlString HasPermission(
439 this HtmlHelper htmlHelper,
440 Csla.Rules.AuthorizationActions action,
441 object target,
442 Csla.Core.IMemberInfo member,
443 string granted,
444 string denied)
445 {
446 var instance = target as Csla.Security.IAuthorizeReadWrite;
447 if (instance == null) return MvcHtmlString.Create(denied);
448
449 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
450 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
451 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
452 return MvcHtmlString.Create(granted);
453 else
454 return MvcHtmlString.Create(denied);
455 }
456
465 public static MvcHtmlString HasPermission(
466 this HtmlHelper htmlHelper,
467 Csla.Rules.AuthorizationActions action,
468 Type objectType,
469 Func<HtmlHelper, MvcHtmlString> grantedAction)
470 {
471 if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
472 return grantedAction.Invoke(htmlHelper);
473 else
474 return MvcHtmlString.Empty;
475 }
476
487 public static MvcHtmlString HasPermission(
488 this HtmlHelper htmlHelper,
489 Csla.Rules.AuthorizationActions action,
490 object target,
491 Csla.Core.IMemberInfo member,
492 Func<HtmlHelper, MvcHtmlString> grantedAction,
493 Func<HtmlHelper, MvcHtmlString> denieddAction)
494 {
495 var instance = target as Csla.Security.IAuthorizeReadWrite;
496 if (instance == null) return denieddAction.Invoke(htmlHelper);
497
498 if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
499 (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
500 (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
501 return grantedAction.Invoke(htmlHelper);
502 else
503 return denieddAction.Invoke(htmlHelper);
504 }
505 }
506}
507#endif
This is the non-generic base class from which most business objects will be derived.
Tracks the business rules for a business object.
static bool HasPermission(AuthorizationActions action, Type objectType)
Checks per-type authorization rules.
Maintains metadata about a method or property.
Definition: IMemberInfo.cs:19
Defines the authorization interface through which an object can indicate which properties the current...