Skip to content

Commit

Permalink
ImpersonatingDictionaryObject
Browse files Browse the repository at this point in the history
  • Loading branch information
o.nadymov committed Oct 30, 2023
1 parent 745f57d commit bb0a81f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public async Task LoadAttributesForMaterialNameWithContext()
});
}

[Test]
public async Task LoadAttributesForMaterialNameWithDictionaryContext()
{
// Arrange
var provider = ServiceProvider.GetService<IImpersonatingMetaSystemProvider>();

// Act
var attributeList = await provider.LoadAttributesAsync(_user, nameof(MaterialName), new() { { "_identity", Guid.NewGuid() }, { nameof(MaterialName.Name), "test" } });

// Assert
Assert.Multiple(() =>
{
Assert.That(attributeList, Is.Not.Null);
});
}

[Test]
public async Task LoadAttributesForEmployee()
{
Expand Down Expand Up @@ -303,6 +319,22 @@ public async Task LoadMetaClassForOrderDisplayWithContext()
});
}

[Test]
public async Task LoadMetaClassForOrderDisplayWithDictionaryContext()
{
// Arrange
var provider = ServiceProvider.GetService<IImpersonatingMetaSystemProvider>();

// Act
var metaClass = await provider.LoadMetaClassAsync(_user, nameof(OrderDisplay), new() { { "_identity", Guid.NewGuid() }, { nameof(OrderDisplay.Name), "Заказ тестовый" } });

// Assert
Assert.Multiple(() =>
{
Assert.That(metaClass, Is.Not.Null);
});
}

[Test]
public async Task LoadMetaClassList()
{
Expand Down
20 changes: 20 additions & 0 deletions src/CIS.Service.Client/Models/ImpersonatingDictionaryObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace CIS.Service.Client.Models
{
/// <summary>
/// The persistent object with user impersonating feature.
/// </summary>
public class ImpersonatingDictionaryObject
{
/// <summary>
/// Gets or sets the user.
/// </summary>
public ImpersonatingUser User { get; set; }

/// <summary>
/// Gets or sets the object.
/// </summary>
public Dictionary<string, object> Object { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public interface IImpersonatingMetaSystemProvider
/// </summary>
Task<List<MetaAttribute>> LoadAttributesAsync<T>(ImpersonatingUser user, T contextObject) where T : IdentityObject;

/// <summary>
/// Async loads the available attribute list for the context object and specified user.
/// </summary>
Task<List<MetaAttribute>> LoadAttributesAsync(ImpersonatingUser user, string objectClassName, Dictionary<string, object> contextObject);

/// <summary>
/// Async loads the accessible meta-system info for the specified user.
/// </summary>
Expand All @@ -41,6 +46,11 @@ public interface IImpersonatingMetaSystemProvider
/// </summary>
Task<MetaClass> LoadMetaClassAsync<T>(ImpersonatingUser user, T contextObject) where T : IdentityObject;

/// <summary>
/// Async loads the accessible meta-system info for the context object and specified user.
/// </summary>
Task<MetaClass> LoadMetaClassAsync(ImpersonatingUser user, string objectClassName, Dictionary<string, object> contextObject);

/// <summary>
/// Async loads the accessible meta-system info list for the specified user.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ public async Task<List<MetaAttribute>> LoadAttributesAsync<T>(ImpersonatingUser
return attributeList;
}

public async Task<List<MetaAttribute>> LoadAttributesAsync(ImpersonatingUser user, string objectClassName, Dictionary<string, object> contextObject)
{
if (user == null)
throw new ArgumentNullException(nameof(user));

if (contextObject == null)
throw new ArgumentNullException(nameof(contextObject));


var uri = new Uri(new Uri(Settings.WebAPIEndpointAddress), $"{_impersonatingControllerName}{objectClassName}/LoadAttributesByContext");

var impersonatingContextObject = new ImpersonatingDictionaryObject
{
User = user,
Object = contextObject
};

var jsonModel = JsonHelper.ToJson(impersonatingContextObject);

var attributeList = await InvokeAsync<List<MetaAttribute>>(Settings, uri, HttpMethod.Post, jsonModel);

return attributeList;
}

public Task<MetaClass> LoadMetaClassAsync<T>(ImpersonatingUser user) where T : IdentityObject
=> LoadMetaClassAsync(user, typeof(T).Name);

Expand Down Expand Up @@ -92,6 +116,29 @@ public async Task<MetaClass> LoadMetaClassAsync<T>(ImpersonatingUser user, T con
return metaClass;
}

public async Task<MetaClass> LoadMetaClassAsync(ImpersonatingUser user, string objectClassName, Dictionary<string, object> contextObject)
{
if (user == null)
throw new ArgumentNullException(nameof(user));

if (contextObject == null)
throw new ArgumentNullException(nameof(contextObject));

var uri = new Uri(new Uri(Settings.WebAPIEndpointAddress), $"{_impersonatingControllerName}{objectClassName}/LoadMetaClassByContext");

var impersonatingContextObject = new ImpersonatingDictionaryObject
{
User = user,
Object = contextObject
};

var jsonModel = JsonHelper.ToJson(impersonatingContextObject);

var metaClass = await InvokeAsync<MetaClass>(Settings, uri, HttpMethod.Post, jsonModel);

return metaClass;
}

public async Task<List<MetaClass>> LoadMetaClassListAsync(ImpersonatingUser user, List<string> objectClassNames, bool withAttributes = false)
{
if (user == null)
Expand Down

0 comments on commit bb0a81f

Please # to comment.