Skip to content

Commit

Permalink
upd domain object access attribute (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
iatsuta authored Oct 21, 2024
1 parent c6edc30 commit d3234bc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 69 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Framework.Core;
using Framework.SecuritySystem;
using Framework.SecuritySystem;

namespace Framework.Security;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class DomainObjectAccessAttribute : Attribute
{
public DomainObjectAccessAttribute(Type securityRuleType, string name)
: this(securityRuleType.Maybe(v => v.GetSecurityRule(name)))
: this(securityRuleType.GetSecurityRule(name))
{
}

public DomainObjectAccessAttribute(Type domainType, bool isEdit)
: this((isEdit ? SecurityRule.Edit : SecurityRule.View).ToDomain(domainType))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ namespace Framework.Security;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property)]
public class EditDomainObjectAttribute : DomainObjectAccessAttribute
{
public EditDomainObjectAttribute(SecurityRule securityRule)
: base(securityRule)
public EditDomainObjectAttribute(Type securityRuleType, string name)
: base(securityRuleType, name)
{
}

public EditDomainObjectAttribute(Type domainType)
: base(SecurityRule.Edit.ToDomain(domainType))
public EditDomainObjectAttribute(Type domainType, bool isEdit = true)
: base(domainType, isEdit)
{
}

/// <summary>
/// Констуктор с доступом по операции
/// </summary>
public EditDomainObjectAttribute(Type securityRuleType, string name)
: base(securityRuleType, name)
public EditDomainObjectAttribute(SecurityRule securityRule)
: base(securityRule)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public ViewDomainObjectAttribute(Type securityRuleType, string name)
{
}

public ViewDomainObjectAttribute(Type domainType)
: base(SecurityRule.View.ToDomain(domainType))
public ViewDomainObjectAttribute(Type domainType, bool isEdit = false)
: base(domainType, isEdit)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ public static class CustomAttributeProviderExtensions
/// <param name="source">Источник</param>
/// <param name="throwIfNull">Ошибка, если операция отсутствует</param>
/// <returns></returns>
public static SecurityRule GetViewSecurityRule(this ICustomAttributeProvider source, bool throwIfNull = false)
public static SecurityRule? GetViewSecurityRule(this ICustomAttributeProvider source, bool throwIfNull = false)
{
if (source == null) throw new ArgumentNullException(nameof(source));

var res = source.GetViewDomainObjectAttribute().Maybe(attr => attr.SecurityRule);

if (res == null && throwIfNull)
Expand All @@ -33,10 +31,8 @@ public static SecurityRule GetViewSecurityRule(this ICustomAttributeProvider sou
/// <param name="source">Источник</param>
/// <param name="throwIfNull">Ошибка, если операция отсутствует</param>
/// <returns></returns>
public static SecurityRule GetEditSecurityRule(this ICustomAttributeProvider source, bool throwIfNull = false)
public static SecurityRule? GetEditSecurityRule(this ICustomAttributeProvider source, bool throwIfNull = false)
{
if (source == null) throw new ArgumentNullException(nameof(source));

var res = source.GetEditDomainObjectAttribute().Maybe(attr => attr.SecurityRule);

if (res == null && throwIfNull)
Expand All @@ -47,37 +43,35 @@ public static SecurityRule GetEditSecurityRule(this ICustomAttributeProvider sou
return res;
}

public static ViewDomainObjectAttribute GetViewDomainObjectAttribute(this ICustomAttributeProvider source)
public static ViewDomainObjectAttribute? GetViewDomainObjectAttribute(this ICustomAttributeProvider source)
{
if (source == null) throw new ArgumentNullException(nameof(source));

return source.GetCustomAttribute<ViewDomainObjectAttribute>();
}

public static EditDomainObjectAttribute GetEditDomainObjectAttribute(this ICustomAttributeProvider source)
public static EditDomainObjectAttribute? GetEditDomainObjectAttribute(this ICustomAttributeProvider source)
{
if (source == null) throw new ArgumentNullException(nameof(source));

return source.GetCustomAttribute<EditDomainObjectAttribute>();
}

public static DomainObjectAccessAttribute GetDomainObjectAccessAttribute(this ICustomAttributeProvider source)
public static DomainObjectAccessAttribute? GetDomainObjectAccessAttribute(this ICustomAttributeProvider source)
{
return source.GetCustomAttribute<DomainObjectAccessAttribute>();
}

public static DomainObjectAccessAttribute GetDomainObjectAccessAttribute(this ICustomAttributeProvider source, bool isEdit)
public static DomainObjectAccessAttribute? GetDomainObjectAccessAttribute(this ICustomAttributeProvider source, bool isEdit)
{
if (source == null) throw new ArgumentNullException(nameof(source));

return isEdit ? source.GetEditDomainObjectAttribute() : source.GetViewDomainObjectAttribute();
}

public static IEnumerable<DomainObjectAccessAttribute> GetDomainObjectAccessAttributes(this ICustomAttributeProvider source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return from flag in new[] { true, false }

let attr = source.GetDomainObjectAccessAttribute(flag)

where attr != null

return new[] {true, false}.Select(source.GetDomainObjectAccessAttribute).Where(attr => attr != null);
select attr;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,6 @@ internal static SecurityRule GetSecurityRule(this Type securityRuleType, string
}
}

public static Type GetDependencySecuritySourceType(this Type type, bool recurse)
{
if (type == null) throw new ArgumentNullException(nameof(type));

var attr = type.GetCustomAttribute<DependencySecurityAttribute>();

if (attr != null)
{
if (recurse)
{
return attr.SourceType.GetDependencySecuritySourceType(true) ?? attr.SourceType;
}

return attr.SourceType;
}

return null;
}

public static bool HasSecurityNodeInterfaces(this Type sourceType)
{
if (sourceType == null) throw new ArgumentNullException(nameof(sourceType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<PackageId>Luxoft.Framework.Security.LegacyAttributes</PackageId>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[assembly: AssemblyCompany("Luxoft")]
[assembly: AssemblyCopyright("Copyright © Luxoft 2009-2024")]

[assembly: AssemblyVersion("22.5.2.0")]
[assembly: AssemblyFileVersion("22.5.2.0")]
[assembly: AssemblyInformationalVersion("22.5.2.0")]
[assembly: AssemblyVersion("22.5.3.0")]
[assembly: AssemblyFileVersion("22.5.3.0")]
[assembly: AssemblyInformationalVersion("22.5.3.0")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
Expand Down

0 comments on commit d3234bc

Please sign in to comment.