Commit for development environment setup

This commit is contained in:
2023-06-19 16:12:33 -04:00
parent be72063a3c
commit bbce2ad0a6
2209 changed files with 1171775 additions and 625 deletions

View File

@@ -0,0 +1,26 @@
namespace Csla.Security
{
/// <summary>
/// The access types supported by authorization
/// as discussed in Chapter 3.
/// </summary>
public enum AccessType
{
/// <summary>
/// Roles allowed to read property.
/// </summary>
ReadAllowed,
/// <summary>
/// Roles denied read access to property.
/// </summary>
ReadDenied,
/// <summary>
/// Roles allowed to set property.
/// </summary>
WriteAllowed,
/// <summary>
/// Roles denied write access to property.
/// </summary>
WriteDenied
}
}

View File

@@ -0,0 +1,324 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace Csla.Security
{
/// <summary>
/// Maintains a list of allowed and denied user roles
/// for each property.
/// </summary>
/// <remarks></remarks>
[Serializable()]
public class AuthorizationRules
{
private Type _businessObjectType;
private AuthorizationRulesManager _typeRules;
private AuthorizationRulesManager _instanceRules;
/// <summary>
/// Creates an instance of the object, initializing
/// it with the business object type.
/// </summary>
/// <param name="businessObjectType">
/// Type of the business object to which the rules
/// apply.
/// </param>
public AuthorizationRules(Type businessObjectType)
{
_businessObjectType = businessObjectType;
}
private AuthorizationRulesManager InstanceRules
{
get
{
if (_instanceRules == null)
_instanceRules = new AuthorizationRulesManager();
return _instanceRules;
}
}
private AuthorizationRulesManager TypeRules
{
get
{
if (_typeRules == null)
_typeRules = SharedAuthorizationRules.GetManager(_businessObjectType, true);
return _typeRules;
}
}
#region Add Per-Instance Roles
/// <summary>
/// Specify the roles allowed to read a given
/// property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles granted read access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of allowed roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void InstanceAllowRead(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = InstanceRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.ReadAllowed.Add(item);
}
/// <summary>
/// Specify the roles denied read access to
/// a given property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles denied read access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of denied roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void InstanceDenyRead(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = InstanceRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.ReadDenied.Add(item);
}
/// <summary>
/// Specify the roles allowed to write a given
/// property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles granted write access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of allowed roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void InstanceAllowWrite(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = InstanceRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.WriteAllowed.Add(item);
}
/// <summary>
/// Specify the roles denied write access to
/// a given property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles denied write access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of denied roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void InstanceDenyWrite(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = InstanceRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.WriteDenied.Add(item);
}
#endregion
#region Add Per-Type Roles
/// <summary>
/// Specify the roles allowed to read a given
/// property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles granted read access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of allowed roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void AllowRead(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = TypeRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.ReadAllowed.Add(item);
}
/// <summary>
/// Specify the roles denied read access to
/// a given property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles denied read access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of denied roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void DenyRead(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = TypeRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.ReadDenied.Add(item);
}
/// <summary>
/// Specify the roles allowed to write a given
/// property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles granted write access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of allowed roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void AllowWrite(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = TypeRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.WriteAllowed.Add(item);
}
/// <summary>
/// Specify the roles denied write access to
/// a given property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="roles">List of roles denied write access.</param>
/// <remarks>
/// This method may be called multiple times, with the roles in
/// each call being added to the end of the list of denied roles.
/// In other words, each call is cumulative, adding more roles
/// to the list.
/// </remarks>
public void DenyWrite(string propertyName, params string[] roles)
{
RolesForProperty currentRoles = TypeRules.GetRolesForProperty(propertyName);
foreach (string item in roles)
currentRoles.WriteDenied.Add(item);
}
#endregion
#region Check Roles
/// <summary>
/// Indicates whether the property has a list
/// of roles granted read access.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool HasReadAllowedRoles(string propertyName)
{
if (InstanceRules.GetRolesForProperty(propertyName).ReadAllowed.Count > 0)
return true;
return TypeRules.GetRolesForProperty(propertyName).ReadAllowed.Count > 0;
}
/// <summary>
/// Indicates whether the current user as defined by
/// <see cref="Csla.ApplicationContext.User" />
/// is explicitly allowed to read the property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool IsReadAllowed(string propertyName)
{
System.Security.Principal.IPrincipal user = ApplicationContext.User;
if (InstanceRules.GetRolesForProperty(propertyName).IsReadAllowed(user))
return true;
return TypeRules.GetRolesForProperty(propertyName).IsReadAllowed(user);
}
/// <summary>
/// Indicates whether the property has a list
/// of roles denied read access.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool HasReadDeniedRoles(string propertyName)
{
if (InstanceRules.GetRolesForProperty(propertyName).ReadDenied.Count > 0)
return true;
return TypeRules.GetRolesForProperty(propertyName).ReadDenied.Count > 0;
}
/// <summary>
/// Indicates whether the current user as defined by
/// <see cref="Csla.ApplicationContext.User" />
/// is explicitly denied read access to the property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool IsReadDenied(string propertyName)
{
System.Security.Principal.IPrincipal user = ApplicationContext.User;
if (InstanceRules.GetRolesForProperty(propertyName).IsReadDenied(user))
return true;
return TypeRules.GetRolesForProperty(propertyName).IsReadDenied(user);
}
/// <summary>
/// Indicates whether the property has a list
/// of roles granted write access.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool HasWriteAllowedRoles(string propertyName)
{
if (InstanceRules.GetRolesForProperty(propertyName).WriteAllowed.Count > 0)
return true;
return TypeRules.GetRolesForProperty(propertyName).WriteAllowed.Count > 0;
}
/// <summary>
/// Indicates whether the current user as defined by
/// <see cref="Csla.ApplicationContext.User" />
/// is explicitly allowed to set the property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool IsWriteAllowed(string propertyName)
{
System.Security.Principal.IPrincipal user = ApplicationContext.User;
if (InstanceRules.GetRolesForProperty(propertyName).IsWriteAllowed(user))
return true;
return TypeRules.GetRolesForProperty(propertyName).IsWriteAllowed(user);
}
/// <summary>
/// Indicates whether the property has a list
/// of roles denied write access.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool HasWriteDeniedRoles(string propertyName)
{
if (InstanceRules.GetRolesForProperty(propertyName).WriteDenied.Count > 0)
return true;
return TypeRules.GetRolesForProperty(propertyName).WriteDenied.Count > 0;
}
/// <summary>
/// Indicates whether the current user as defined by
/// <see cref="Csla.ApplicationContext.User" />
/// is explicitly denied write access to the property.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public bool IsWriteDenied(string propertyName)
{
System.Security.Principal.IPrincipal user = ApplicationContext.User;
if (InstanceRules.GetRolesForProperty(propertyName).IsWriteDenied(user))
return true;
return TypeRules.GetRolesForProperty(propertyName).IsWriteDenied(user);
}
#endregion
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Csla.Security
{
/// <summary>
/// Maintains authorization roles for a business object
/// or business object type.
/// </summary>
public class AuthorizationRulesManager
{
private Dictionary<string, RolesForProperty> _rules;
internal Dictionary<string, RolesForProperty> RulesList
{
get
{
if (_rules == null)
_rules = new Dictionary<string, RolesForProperty>();
return _rules;
}
}
#region Get Roles
internal RolesForProperty GetRolesForProperty(string propertyName)
{
RolesForProperty currentRoles = null;
if (!RulesList.ContainsKey(propertyName))
{
currentRoles = new RolesForProperty();
RulesList.Add(propertyName, currentRoles);
}
else
currentRoles = RulesList[propertyName];
return currentRoles;
}
#endregion
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Security.Principal;
namespace Csla.Security
{
/// <summary>
/// Base class from which custom principal
/// objects should inherit to operate
/// properly with the data portal.
/// </summary>
[Serializable()]
public class BusinessPrincipalBase : IPrincipal
{
private IIdentity _identity;
/// <summary>
/// Returns the user's identity object.
/// </summary>
public virtual IIdentity Identity
{
get { return _identity; }
}
/// <summary>
/// Returns a value indicating whether the
/// user is in a given role.
/// </summary>
/// <param name="role">Name of the role.</param>
public virtual bool IsInRole(string role)
{
return false;
}
/// <summary>
/// Creates an instance of the object.
/// </summary>
/// <param name="identity">Identity object for the user.</param>
protected BusinessPrincipalBase(IIdentity identity)
{
_identity = identity;
}
}
}

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Csla.Security
{
/// <summary>
/// Defines the authorization interface through which an
/// object can indicate which properties the current
/// user can read and write.
/// </summary>
public interface IAuthorizeReadWrite
{
/// <summary>
/// Returns <see langword="true" /> if the user is allowed to write the
/// to the specified property.
/// </summary>
/// <returns><see langword="true" /> if write is allowed.</returns>
/// <param name="propertyName">Name of the property to read.</param>
bool CanWriteProperty(string propertyName);
/// <summary>
/// Returns <see langword="true" /> if the user is allowed to read the
/// specified property.
/// </summary>
/// <returns><see langword="true" /> if read is allowed.</returns>
/// <param name="propertyName">Name of the property to read.</param>
bool CanReadProperty(string propertyName);
}
}

View File

@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.ComponentModel;
namespace Csla.Security
{
/// <summary>
/// Maintains a list of allowed and denied
/// user roles for a specific property.
/// </summary>
/// <remarks></remarks>
[Serializable()]
internal class RolesForProperty
{
private List<string> _readAllowed = new List<string>();
private List<string> _readDenied = new List<string>();
private List<string> _writeAllowed = new List<string>();
private List<string> _writeDenied = new List<string>();
/// <summary>
/// Returns a List(Of string) containing the list
/// of roles allowed read access.
/// </summary>
public List<string> ReadAllowed
{
get { return _readAllowed; }
}
/// <summary>
/// Returns a List(Of string) containing the list
/// of roles denied read access.
/// </summary>
public List<string> ReadDenied
{
get { return _readDenied; }
}
/// <summary>
/// Returns a List(Of string) containing the list
/// of roles allowed write access.
/// </summary>
public List<string> WriteAllowed
{
get { return _writeAllowed; }
}
/// <summary>
/// Returns a List(Of string) containing the list
/// of roles denied write access.
/// </summary>
public List<string> WriteDenied
{
get { return _writeDenied; }
}
/// <summary>
/// Returns <see langword="true" /> if the user is in a role
/// explicitly allowed read access.
/// </summary>
/// <param name="principal">A <see cref="System.Security.Principal.IPrincipal" />
/// representing the user.</param>
/// <returns><see langword="true" /> if the user is allowed read access.</returns>
/// <remarks></remarks>
public bool IsReadAllowed(IPrincipal principal)
{
foreach (string role in ReadAllowed)
if (principal.IsInRole(role))
return true;
return false;
}
/// <summary>
/// Returns <see langword="true" /> if the user is in a role
/// explicitly denied read access.
/// </summary>
/// <param name="principal">A <see cref="System.Security.Principal.IPrincipal" />
/// representing the user.</param>
/// <returns><see langword="true" /> if the user is denied read access.</returns>
/// <remarks></remarks>
public bool IsReadDenied(IPrincipal principal)
{
foreach (string role in ReadDenied)
if (principal.IsInRole(role))
return true;
return false;
}
/// <summary>
/// Returns <see langword="true" /> if the user is in a role
/// explicitly allowed write access.
/// </summary>
/// <param name="principal">A <see cref="System.Security.Principal.IPrincipal" />
/// representing the user.</param>
/// <returns><see langword="true" /> if the user is allowed write access.</returns>
/// <remarks></remarks>
public bool IsWriteAllowed(IPrincipal principal)
{
foreach (string role in WriteAllowed)
if (principal.IsInRole(role))
return true;
return false;
}
/// <summary>
/// Returns <see langword="true" /> if the user is in a role
/// explicitly denied write access.
/// </summary>
/// <param name="principal">A <see cref="System.Security.Principal.IPrincipal" />
/// representing the user.</param>
/// <returns><see langword="true" /> if the user is denied write access.</returns>
/// <remarks></remarks>
public bool IsWriteDenied(IPrincipal principal)
{
foreach (string role in WriteDenied)
if (principal.IsInRole(role))
return true;
return false;
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Csla.Security
{
/// <summary>
/// Maintains a list of all the per-type
/// <see cref="AuthorizationRulesManager"/> objects
/// loaded in memory.
/// </summary>
internal static class SharedAuthorizationRules
{
private static Dictionary<Type, AuthorizationRulesManager> _managers =
new Dictionary<Type, AuthorizationRulesManager>();
/// <summary>
/// Gets the <see cref="AuthorizationRulesManager"/> for the
/// specified object type, optionally creating a new instance
/// of the object if necessary.
/// </summary>
/// <param name="objectType">
/// Type of business object for which the rules apply.
/// </param>
/// <param name="create">Indicates whether to create
/// a new instance of the object if one doesn't exist.</param>
internal static AuthorizationRulesManager GetManager(Type objectType, bool create)
{
AuthorizationRulesManager result = null;
if (!_managers.TryGetValue(objectType, out result) && create)
{
lock (_managers)
{
result = new AuthorizationRulesManager();
_managers.Add(objectType, result);
}
}
return result;
}
/// <summary>
/// Gets a value indicating whether a set of rules
/// have been created for a given <see cref="Type" />.
/// </summary>
/// <param name="objectType">
/// Type of business object for which the rules apply.
/// </param>
/// <returns><see langword="true" /> if rules exist for the type.</returns>
public static bool RulesExistFor(Type objectType)
{
return _managers.ContainsKey(objectType);
}
}
}