копирование поля в другой обхект при наличии парв

This commit is contained in:
kotcheshir73 2021-03-28 20:26:25 +04:00
parent 8578045f56
commit 3a29225011
8 changed files with 54 additions and 37 deletions

View File

@ -21,15 +21,22 @@ namespace ModuleTools.Attributes
/// </summary> /// </summary>
public bool IsDifficle { get; set; } public bool IsDifficle { get; set; }
/// <summary>
/// Можно копировать поле даже при доступе без прав
/// </summary>
public bool AllowCopyWithoutRigth { get; set; }
/// <summary> /// <summary>
/// Настройка для полей сущности правил маппинга /// Настройка для полей сущности правил маппинга
/// </summary> /// </summary>
/// <param name="propertyNameFromMModel">Название свойства с класса, из которого извлекаем данные</param> /// <param name="propertyNameFromMModel">Название свойства с класса, из которого извлекаем данные</param>
/// <param name="isDifficle">Сложное свойство (свойствое в другом классе-свойстве)</param> /// <param name="isDifficle">Сложное свойство (свойствое в другом классе-свойстве)</param>
public MapConfigurationAttribute(string propertyNameFromMModel, bool isDifficle = false) /// <param name="allowCopyWithoutRigth">Можно копировать поле даже при доступе без прав</param>
public MapConfigurationAttribute(string propertyNameFromMModel, bool isDifficle = false, bool allowCopyWithoutRigth = true)
{ {
PropertyNameFromModel = propertyNameFromMModel; PropertyNameFromModel = propertyNameFromMModel;
IsDifficle = isDifficle; IsDifficle = isDifficle;
AllowCopyWithoutRigth = allowCopyWithoutRigth;
} }
} }
} }

View File

@ -7,6 +7,11 @@ namespace ModuleTools.BindingModels
/// </summary> /// </summary>
public class GetBindingModel : AccessBindingModel public class GetBindingModel : AccessBindingModel
{ {
/// <summary>
/// Наличие прав на операцию
/// </summary>
public bool HaveRight { get; set; }
/// <summary> /// <summary>
/// Идентификатор получаемой записи (для одной записи) /// Идентификатор получаемой записи (для одной записи)
/// </summary> /// </summary>

View File

@ -87,8 +87,8 @@ namespace ModuleTools.BusinessLogics
Errors.Clear(); Errors.Clear();
try try
{ {
var fullView = NoAccess(model, AccessType.View); model.HaveRight = NoAccess(model, AccessType.View);
if (fullView && !_allowSimpleView) if (model.HaveRight && !_allowSimpleView)
{ {
throw new MethodAccessException("Нет прав на получение списка"); throw new MethodAccessException("Нет прав на получение списка");
} }
@ -118,8 +118,8 @@ namespace ModuleTools.BusinessLogics
Errors.Clear(); Errors.Clear();
try try
{ {
var fullView = NoAccess(model, AccessType.View); model.HaveRight = NoAccess(model, AccessType.View);
if (fullView && !_allowSimpleView) if (model.HaveRight && !_allowSimpleView)
{ {
throw new MethodAccessException("Нет прав на получение списка"); throw new MethodAccessException("Нет прав на получение списка");
} }

View File

@ -15,8 +15,9 @@ namespace ModuleTools.BusinessLogics
/// <typeparam name="From"></typeparam> /// <typeparam name="From"></typeparam>
/// <typeparam name="To"></typeparam> /// <typeparam name="To"></typeparam>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <param name="haveRigth"></param>
/// <returns></returns> /// <returns></returns>
public static To MapToClass<From, To>(From obj) where To : class => FillObject(obj, (To)Activator.CreateInstance(typeof(To))); public static To MapToClass<From, To>(From obj, bool haveRigth) where To : class => FillObject(obj, (To)Activator.CreateInstance(typeof(To)), haveRigth);
/// <summary> /// <summary>
/// Преобразование из одного класса в другой /// Преобразование из одного класса в другой
@ -26,7 +27,7 @@ namespace ModuleTools.BusinessLogics
/// <param name="obj"></param> /// <param name="obj"></param>
/// <param name="newObject"></param> /// <param name="newObject"></param>
/// <returns></returns> /// <returns></returns>
public static To MapToClass<From, To>(From obj, To newObject) where To : class => FillObject(obj, newObject); public static To MapToClass<From, To>(From obj, To newObject, bool haveRigth) where To : class => FillObject(obj, newObject, haveRigth);
/// <summary> /// <summary>
/// Заполнение объекта /// Заполнение объекта
@ -35,8 +36,9 @@ namespace ModuleTools.BusinessLogics
/// <typeparam name="To"></typeparam> /// <typeparam name="To"></typeparam>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <param name="newObject"></param> /// <param name="newObject"></param>
/// <param name="haveRigth"></param>
/// <returns></returns> /// <returns></returns>
private static To FillObject<From, To>(From obj, To newObject) private static To FillObject<From, To>(From obj, To newObject, bool haveRigth)
where To : class where To : class
{ {
if (obj == null) if (obj == null)
@ -90,9 +92,12 @@ namespace ModuleTools.BusinessLogics
{ {
continue; continue;
} }
if ((haveRigth && !customAttribute.AllowCopyWithoutRigth) || customAttribute.AllowCopyWithoutRigth)
{
property.SetValue(newObject, value); property.SetValue(newObject, value);
} }
} }
}
return newObject; return newObject;
} }

View File

@ -25,10 +25,10 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var exsistEntity = context.Accesses.FirstOrDefault(x => x.AccessOperation == model.Operation && x.RoleId == model.RoleId && x.AccessType == model.AccessType); var exsistEntity = context.Accesses.FirstOrDefault(x => x.AccessOperation == model.Operation && x.RoleId == model.RoleId && x.AccessType == model.AccessType);
if (exsistEntity == null) if (exsistEntity == null)
{ {
var entity = Mapper.MapToClass<AccessSetBindingModel, Access>(model); var entity = Mapper.MapToClass<AccessSetBindingModel, Access>(model, true);
context.Accesses.Add(entity); context.Accesses.Add(entity);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity, true));
} }
else else
{ {
@ -36,7 +36,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
exsistEntity.IsDeleted = false; exsistEntity.IsDeleted = false;
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(exsistEntity)); return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(exsistEntity, true));
} }
else else
{ {
@ -79,7 +79,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
} }
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity, model.HaveRight));
} }
var query = context.Accesses.Where(x => !x.IsDeleted).AsQueryable(); var query = context.Accesses.Where(x => !x.IsDeleted).AsQueryable();
@ -107,7 +107,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new AccessListViewModel var result = new AccessListViewModel
{ {
MaxCount = countPages, MaxCount = countPages,
List = query.Select(Mapper.MapToClass<Access, AccessViewModel>).ToList() List = query.Select(x => Mapper.MapToClass<Access, AccessViewModel>(x, model.HaveRight)).ToList()
}; };
return OperationResultModel.Success(result); return OperationResultModel.Success(result);
@ -126,11 +126,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
} }
entity = Mapper.MapToClass(model, entity); entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity, true));
} }
} }
} }

View File

@ -24,10 +24,10 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var exsistEntity = context.EnviromentSettings.FirstOrDefault(x => x.Key == model.Key); var exsistEntity = context.EnviromentSettings.FirstOrDefault(x => x.Key == model.Key);
if (exsistEntity == null) if (exsistEntity == null)
{ {
var entity = Mapper.MapToClass<EnviromentSettingSetBindingModel, EnviromentSetting>(model); var entity = Mapper.MapToClass<EnviromentSettingSetBindingModel, EnviromentSetting>(model, true);
context.EnviromentSettings.Add(entity); context.EnviromentSettings.Add(entity);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, true));
} }
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem); return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
} }
@ -61,7 +61,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
} }
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, model.HaveRight));
} }
if (model.Key.IsNotEmpty()) if (model.Key.IsNotEmpty())
@ -71,7 +71,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
} }
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, model.HaveRight));
} }
var query = context.EnviromentSettings.AsQueryable(); var query = context.EnviromentSettings.AsQueryable();
@ -89,7 +89,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new EnviromentSettingListViewModel var result = new EnviromentSettingListViewModel
{ {
MaxCount = countPages, MaxCount = countPages,
List = query.Select(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>).ToList() List = query.Select(x => Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(x, model.HaveRight)).ToList()
}; };
return OperationResultModel.Success(result); return OperationResultModel.Success(result);
@ -104,11 +104,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
} }
entity = Mapper.MapToClass(model, entity); entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<EnviromentSetting, EnviromentSettingViewModel>(entity, true));
} }
} }
} }

View File

@ -23,10 +23,10 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var exsistEntity = context.Roles.FirstOrDefault(x => x.RoleName == model.RoleName); var exsistEntity = context.Roles.FirstOrDefault(x => x.RoleName == model.RoleName);
if (exsistEntity == null) if (exsistEntity == null)
{ {
var entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model); var entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, true);
context.Roles.Add(entity); context.Roles.Add(entity);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
} }
else else
{ {
@ -34,7 +34,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
exsistEntity.IsDeleted = false; exsistEntity.IsDeleted = false;
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity)); return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity, true));
} }
else else
{ {
@ -103,7 +103,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
} }
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, model.HaveRight));
} }
var query = context.Roles.Where(x => !x.IsDeleted).AsQueryable(); var query = context.Roles.Where(x => !x.IsDeleted).AsQueryable();
@ -121,7 +121,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new RoleListViewModel var result = new RoleListViewModel
{ {
MaxCount = countPages, MaxCount = countPages,
List = query.Select(Mapper.MapToClass<Role, RoleViewModel>).ToList() List = query.Select(x => Mapper.MapToClass<Role, RoleViewModel>(x, model.HaveRight)).ToList()
}; };
return OperationResultModel.Success(result); return OperationResultModel.Success(result);
@ -140,11 +140,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
} }
entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, entity); entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, entity, true);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
} }
} }
} }

View File

@ -23,10 +23,10 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var exsistEntity = context.Users.FirstOrDefault(x => x.UserName == model.Login); var exsistEntity = context.Users.FirstOrDefault(x => x.UserName == model.Login);
if (exsistEntity == null) if (exsistEntity == null)
{ {
var entity = Mapper.MapToClass<UserSetBindingModel, User>(model); var entity = Mapper.MapToClass<UserSetBindingModel, User>(model, true);
context.Users.Add(entity); context.Users.Add(entity);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity, true));
} }
else else
{ {
@ -34,7 +34,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
exsistEntity.IsDeleted = false; exsistEntity.IsDeleted = false;
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(exsistEntity)); return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(exsistEntity, true));
} }
else else
{ {
@ -95,7 +95,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
} }
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity, model.HaveRight));
} }
var query = context.Users.Where(x => !x.IsDeleted).AsQueryable(); var query = context.Users.Where(x => !x.IsDeleted).AsQueryable();
@ -113,7 +113,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new UserListViewModel var result = new UserListViewModel
{ {
MaxCount = countPages, MaxCount = countPages,
List = query.Select(Mapper.MapToClass<User, UserViewModel>).ToList() List = query.Select(x => Mapper.MapToClass<User, UserViewModel>(x, model.HaveRight)).ToList()
}; };
return OperationResultModel.Success(result); return OperationResultModel.Success(result);
@ -132,11 +132,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{ {
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete); return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
} }
entity = Mapper.MapToClass(model, entity); entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges(); context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity)); return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity, true));
} }
} }
} }