<commit_before>﻿#if !NETSTANDARD && !NETSTANDARD2_0
//-----------------------------------------------------------------------
// <copyright file="HtmlExtensions.cs" company="Marimer LLC">
//     Copyright (c) Marimer LLC. All rights reserved.
//     Website: https://cslanet.com
// </copyright>
// <summary>Html extension methods providing support for HTML rendering based on security permissions in an application.</summary>
//-----------------------------------------------------------------------<commit_msg>#905 Bring HtmlExtensions forward to NS2<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="HtmlExtensions.cs" company="Marimer LLC">
//     Copyright (c) Marimer LLC. All rights reserved.
//     Website: https://cslanet.com
// </copyright>
// <summary>Html extension methods providing support for HTML rendering based on security permissions in an application.</summary>
//-----------------------------------------------------------------------
#if NETSTANDARD2_0
using System;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace Csla.Web.Mvc
{
  /// <summary>
  /// Html extension methods providing support for HTML rendering based on security permissions in an application.
  /// </summary>
  public static class HtmlExtensions
  {
    /// <summary>
    /// Conditionally render HTML output according to the provided authorization action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="objectType">CSLA object type for which the action is applied.</param>
    /// <param name="granted">The rendered HTML output for granted users.</param>
    /// <param name="denied">The rendered HTML output for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    Type objectType,
                                    HtmlString granted,
                                    HtmlString denied)
    {
      if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
        return granted;
      else
        return denied;
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="objectType">CSLA object type for which the action is applied.</param>
    /// <param name="granted">The rendered HTML output for granted users.</param>
    /// <param name="denied">The rendered HTML output for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    Type objectType,
                                    HtmlString granted,
                                    string denied)
    {
      if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
        return granted;
      else
        return new HtmlString(denied);
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="objectType">CSLA object type for which the action is applied.</param>
    /// <param name="granted">The rendered HTML output for granted users.</param>
    /// <param name="denied">The rendered HTML output for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    Type objectType,
                                    string granted,
                                    string denied)
    {
      if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
        return new HtmlString(granted);
      else
        return new HtmlString(denied);
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization member action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="target">CSLA object instance.</param>
    /// <param name="member">CSLA object member.</param>
    /// <param name="granted">The rendered HTML output for granted users.</param>
    /// <param name="denied">The rendered HTML output for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    object target,
                                    Csla.Core.IMemberInfo member,
                                    HtmlString granted,
                                    HtmlString denied)
    {
      var instance = target as Csla.Security.IAuthorizeReadWrite;
      if (instance == null) return denied;

      if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
        return granted;
      else
        return denied;
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization member action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="target">CSLA object instance.</param>
    /// <param name="member">CSLA object member.</param>
    /// <param name="granted">The rendered HTML output for granted users.</param>
    /// <param name="denied">The rendered HTML output for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    object target,
                                    Csla.Core.IMemberInfo member,
                                    HtmlString granted,
                                    string denied)
    {
      var instance = target as Csla.Security.IAuthorizeReadWrite;
      if (instance == null) return new HtmlString(denied);

      if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
        return granted;
      else
        return new HtmlString(denied);
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization member action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="target">CSLA object instance.</param>
    /// <param name="member">CSLA object member.</param>
    /// <param name="granted">The rendered HTML output for granted users.</param>
    /// <param name="denied">The rendered HTML output for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    object target,
                                    Csla.Core.IMemberInfo member,
                                    string granted,
                                    string denied)
    {
      var instance = target as Csla.Security.IAuthorizeReadWrite;
      if (instance == null) return new HtmlString(denied);

      if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
        return new HtmlString(granted);
      else
        return new HtmlString(denied);
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="objectType">CSLA object type for which the action is applied.</param>
    /// <param name="grantedAction">The rendered HTML helper action for granted users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    Type objectType,
                                    Func<HtmlHelper, HtmlString> grantedAction)
    {
      if (Csla.Rules.BusinessRules.HasPermission(action, objectType))
        return grantedAction.Invoke(htmlHelper);
      else
        return HtmlString.Empty;
    }

    /// <summary>
    /// Conditionally render HTML output according to the provided authorization member action and underlyiong object type.
    /// </summary>
    /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
    /// <param name="action">AuthorizationActions for which the authorization is required.</param>
    /// <param name="target">CSLA object instance.</param>
    /// <param name="member">CSLA object member.</param>
    /// <param name="grantedAction">The rendered HTML helper action for granted users.</param>
    /// <param name="denieddAction">The rendered HTML helper action for denied users.</param>
    /// <returns>The appropriate HTML rendered output.</returns>
    public static HtmlString HasPermission(
                                    this HtmlHelper htmlHelper,
                                    Csla.Rules.AuthorizationActions action,
                                    object target,
                                    Csla.Core.IMemberInfo member,
                                    Func<HtmlHelper, HtmlString> grantedAction,
                                    Func<HtmlHelper, HtmlString> denieddAction)
    {
      var instance = target as Csla.Security.IAuthorizeReadWrite;
      if (instance == null) return denieddAction.Invoke(htmlHelper);

      if ((action == Rules.AuthorizationActions.ReadProperty && instance.CanReadProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.WriteProperty && instance.CanWriteProperty(member.Name)) ||
          (action == Rules.AuthorizationActions.ExecuteMethod && instance.CanExecuteMethod(member.Name)))
        return grantedAction.Invoke(htmlHelper);
      else
        return denieddAction.Invoke(htmlHelper);
    }
  }
}
#else
{'fraction_non_alphanumeric': 0.11919898283534647, 'fraction_numerical': 0.0006357279084551812, 'mean_word_length': 2.0354558610709117, 'pattern_counts': {'":': 0, '<': 134, '<?xml version=': 0, '>': 134, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 55, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5890265', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2793, 'n_words': 871}