переделка работы мапера
This commit is contained in:
parent
6e2e10aa14
commit
1d3d35d106
@ -12,16 +12,19 @@ namespace CoreModels.ModelsSecurity
|
||||
/// <summary>
|
||||
/// Илентификатор роли
|
||||
/// </summary>
|
||||
[CheckRigthForMap]
|
||||
Guid RoleId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Тип операции
|
||||
/// </summary>
|
||||
[CheckRigthForMap]
|
||||
AccessOperation AccessOperation { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Режим доступа
|
||||
/// </summary>
|
||||
[CheckRigthForMap]
|
||||
AccessType AccessType { get; }
|
||||
}
|
||||
}
|
@ -12,16 +12,19 @@ namespace CoreModels.ModelsSecurity
|
||||
/// <summary>
|
||||
/// Ключ настройки
|
||||
/// </summary>
|
||||
[CheckRigthForMap]
|
||||
string Key { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Значение настройки
|
||||
/// </summary>
|
||||
[CheckRigthForMap]
|
||||
string Value { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Описание настройки
|
||||
/// </summary>
|
||||
[CheckRigthForMap]
|
||||
string Description { get; }
|
||||
}
|
||||
}
|
@ -12,16 +12,20 @@ namespace CoreModels.ModelsSecurity
|
||||
{
|
||||
string UserName { get; }
|
||||
|
||||
[CheckRigthForMap]
|
||||
string PasswordHash { get; }
|
||||
|
||||
byte[] Avatar { get; }
|
||||
|
||||
DateTime? DateLastVisit { get; }
|
||||
|
||||
[CheckRigthForMap]
|
||||
bool IsBanned { get; }
|
||||
|
||||
[CheckRigthForMap]
|
||||
DateTime? DateBanned { get; }
|
||||
|
||||
[CheckRigthForMap]
|
||||
int CountAttempt { get; }
|
||||
}
|
||||
}
|
@ -12,8 +12,10 @@ namespace CoreModels.ModelsSecurity
|
||||
[EntityDependency("User", "UserId", "К какой роли относится пользователь")]
|
||||
public interface IUserRoleModel : IId
|
||||
{
|
||||
[CheckRigthForMap]
|
||||
Guid RoleId { get; }
|
||||
|
||||
[CheckRigthForMap]
|
||||
Guid UserId { get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace ModuleTools.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// Признак, гооврящий о том, что нужна проверка доступа к значению свойства при создании одного объекта на основе другого
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class CheckRigthForMapAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using ModuleTools.Attributes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ModuleTools.BusinessLogics
|
||||
@ -53,13 +54,52 @@ namespace ModuleTools.BusinessLogics
|
||||
}
|
||||
var typeFrom = typeof(From);
|
||||
var typeTo = typeof(To);
|
||||
var properties = typeTo.GetProperties();
|
||||
var properties = typeTo.GetProperties().Where(x => x.CanWrite);
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var checkRight = property.GetCustomAttribute<CheckRigthForMapAttribute>();
|
||||
if (!haveRigth && checkRight != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
object value = obj;
|
||||
var customAttribute = property.GetCustomAttribute<MapConfigurationAttribute>();
|
||||
if (customAttribute != null)
|
||||
{
|
||||
if (!haveRigth && !customAttribute.AllowCopyWithoutRigth)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
value = GetValueFromCustomAttribute(customAttribute, typeFrom, obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
var bindingProperty = value.GetType().GetProperty(property.Name);
|
||||
if (bindingProperty != null)
|
||||
{
|
||||
value = bindingProperty.GetValue(obj);
|
||||
}
|
||||
}
|
||||
if (value is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (property.PropertyType.Name.StartsWith("Nullable") && Nullable.GetUnderlyingType(property.PropertyType).IsEnum)
|
||||
{
|
||||
property.SetValue(newObject, Enum.Parse(Nullable.GetUnderlyingType(property.PropertyType), value.ToString()));
|
||||
continue;
|
||||
}
|
||||
property.SetValue(newObject, value);
|
||||
}
|
||||
|
||||
return newObject;
|
||||
}
|
||||
|
||||
private static object GetValueFromCustomAttribute(MapConfigurationAttribute customAttribute, Type typeFrom, object obj)
|
||||
{
|
||||
object value = obj;
|
||||
if (customAttribute.IsDifficle)
|
||||
{
|
||||
var props = customAttribute.PropertyNameFromModel.Split('.');
|
||||
@ -136,30 +176,7 @@ namespace ModuleTools.BusinessLogics
|
||||
value = bindingProperty.GetValue(obj);
|
||||
}
|
||||
}
|
||||
if ((haveRigth && !customAttribute.AllowCopyWithoutRigth) || customAttribute.AllowCopyWithoutRigth)
|
||||
{
|
||||
if (property.PropertyType.Name.StartsWith("Nullable"))
|
||||
{
|
||||
if (Nullable.GetUnderlyingType(property.PropertyType).IsEnum)
|
||||
{
|
||||
property.SetValue(newObject, Enum.Parse(Nullable.GetUnderlyingType(property.PropertyType), value.ToString()));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
property.SetValue(newObject, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var bindingProperty = value.GetType().GetProperty(property.Name);
|
||||
if (bindingProperty != null)
|
||||
{
|
||||
property.SetValue(newObject, bindingProperty.GetValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newObject;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
@ -18,7 +18,6 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
public class AccessViewModel : ElementViewModel, IAccessModel
|
||||
{
|
||||
[ViewModelControlElementProperty("Роль", ControlType.ControlGuid, MustHaveValue = true, ReadOnly = true, ControlTypeObject = "SecurityWindowsDesktop.EntityControls.ControlRoleList, SecurityWindowsDesktop")]
|
||||
[MapConfiguration("RoleId", AllowCopyWithoutRigth = false)]
|
||||
public Guid RoleId { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Роль", ColumnWidth = 100)]
|
||||
@ -26,14 +25,12 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
public string RoleName { get; set; }
|
||||
|
||||
[ViewModelControlElementProperty("Операция", ControlType.ControlEnum, MustHaveValue = true)]
|
||||
[MapConfiguration("AccessOperation", AllowCopyWithoutRigth = false)]
|
||||
public AccessOperation AccessOperation { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Операция")]
|
||||
public string AccessOperationTitle => AccessOperation.ToString("G");
|
||||
|
||||
[ViewModelControlElementProperty("Тип", ControlType.ControlEnum, MustHaveValue = true)]
|
||||
[MapConfiguration("AccessType", AllowCopyWithoutRigth = false)]
|
||||
public AccessType AccessType { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Тип", ColumnWidth = 150)]
|
||||
|
@ -18,17 +18,14 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
{
|
||||
[ViewModelControlListProperty("Ключ")]
|
||||
[ViewModelControlElementProperty("Ключ", ControlType.ControlString, MustHaveValue = true, ReadOnly = true)]
|
||||
[MapConfiguration("Key", AllowCopyWithoutRigth = false)]
|
||||
public string Key { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Значение")]
|
||||
[ViewModelControlElementProperty("Значение", ControlType.ControlString, MustHaveValue = true)]
|
||||
[MapConfiguration("Value", AllowCopyWithoutRigth = false)]
|
||||
public string Value { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Описание")]
|
||||
[ViewModelControlElementProperty("Описание", ControlType.ControlText, Height = 200)]
|
||||
[MapConfiguration("Description", AllowCopyWithoutRigth = false)]
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
@ -18,7 +18,6 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
public class UserRoleViewModel : ElementViewModel, IUserRoleModel
|
||||
{
|
||||
[ViewModelControlElementProperty("Пользователь", ControlType.ControlGuid, MustHaveValue = true, ReadOnly = false, ControlTypeObject = "SecurityWindowsDesktop.EntityControls.ControlUserList, SecurityWindowsDesktop")]
|
||||
[MapConfiguration("UserId", AllowCopyWithoutRigth = false)]
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Пользователь")]
|
||||
@ -26,7 +25,6 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
public string UserName { get; set; }
|
||||
|
||||
[ViewModelControlElementProperty("Роль", ControlType.ControlGuid, MustHaveValue = true, ReadOnly = false, ControlTypeObject = "SecurityWindowsDesktop.EntityControls.ControlRoleList, SecurityWindowsDesktop")]
|
||||
[MapConfiguration("RoleId", AllowCopyWithoutRigth = false)]
|
||||
public Guid RoleId { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Роль")]
|
||||
|
@ -23,7 +23,6 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
[ViewModelControlElementProperty("Логин", ControlType.ControlString, MustHaveValue = true)]
|
||||
public string UserName { get; set; }
|
||||
|
||||
[MapConfiguration("PasswordHash", AllowCopyWithoutRigth = false)]
|
||||
public string PasswordHash { get; set; }
|
||||
|
||||
[ViewModelControlElementProperty("Фото", ControlType.ControlImage, Width = 200, Height = 200)]
|
||||
@ -33,21 +32,18 @@ namespace SecurityBusinessLogic.ViewModels
|
||||
public DateTime? DateLastVisit { get; set; }
|
||||
|
||||
[ViewModelControlElementProperty("Заблокирован", ControlType.ControlBool, MustHaveValue = true)]
|
||||
[MapConfiguration("IsBanned", AllowCopyWithoutRigth = false)]
|
||||
public bool IsBanned { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Блокир.", ColumnWidth = 80)]
|
||||
public string Banned => IsBanned ? "Да" : "Нет";
|
||||
|
||||
[ViewModelControlElementProperty("Дата блокировки", ControlType.ControlDateTime, ReadOnly = true)]
|
||||
[MapConfiguration("DateBanned", AllowCopyWithoutRigth = false)]
|
||||
public DateTime? DateBanned { get; set; }
|
||||
|
||||
[ViewModelControlListProperty("Дата Б.", ColumnWidth = 100)]
|
||||
public string DateBannedTitle => DateBanned.HasValue ? DateBanned.Value.ToShortDateString() : string.Empty;
|
||||
|
||||
[ViewModelControlElementProperty("Попытки входа", ControlType.ControlInt, ReadOnly = true)]
|
||||
[MapConfiguration("CountAttempt", AllowCopyWithoutRigth = false)]
|
||||
public int CountAttempt { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user