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

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>
public bool IsDifficle { get; set; }
/// <summary>
/// Можно копировать поле даже при доступе без прав
/// </summary>
public bool AllowCopyWithoutRigth { get; set; }
/// <summary>
/// Настройка для полей сущности правил маппинга
/// </summary>
/// <param name="propertyNameFromMModel">Название свойства с класса, из которого извлекаем данные</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;
IsDifficle = isDifficle;
AllowCopyWithoutRigth = allowCopyWithoutRigth;
}
}
}

View File

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

View File

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

View File

@ -15,8 +15,9 @@ namespace ModuleTools.BusinessLogics
/// <typeparam name="From"></typeparam>
/// <typeparam name="To"></typeparam>
/// <param name="obj"></param>
/// <param name="haveRigth"></param>
/// <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>
/// Преобразование из одного класса в другой
@ -26,7 +27,7 @@ namespace ModuleTools.BusinessLogics
/// <param name="obj"></param>
/// <param name="newObject"></param>
/// <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>
/// Заполнение объекта
@ -35,8 +36,9 @@ namespace ModuleTools.BusinessLogics
/// <typeparam name="To"></typeparam>
/// <param name="obj"></param>
/// <param name="newObject"></param>
/// <param name="haveRigth"></param>
/// <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
{
if (obj == null)
@ -90,9 +92,12 @@ namespace ModuleTools.BusinessLogics
{
continue;
}
if ((haveRigth && !customAttribute.AllowCopyWithoutRigth) || customAttribute.AllowCopyWithoutRigth)
{
property.SetValue(newObject, value);
}
}
}
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);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<AccessSetBindingModel, Access>(model);
var entity = Mapper.MapToClass<AccessSetBindingModel, Access>(model, true);
context.Accesses.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity));
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(entity, true));
}
else
{
@ -36,7 +36,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(exsistEntity));
return OperationResultModel.Success(Mapper.MapToClass<Access, AccessViewModel>(exsistEntity, true));
}
else
{
@ -79,7 +79,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
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();
@ -107,7 +107,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new AccessListViewModel
{
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);
@ -126,11 +126,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity);
entity = Mapper.MapToClass(model, entity, true);
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);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<EnviromentSettingSetBindingModel, EnviromentSetting>(model);
var entity = Mapper.MapToClass<EnviromentSettingSetBindingModel, EnviromentSetting>(model, true);
context.EnviromentSettings.Add(entity);
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);
}
@ -61,7 +61,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
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())
@ -71,7 +71,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
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();
@ -89,7 +89,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new EnviromentSettingListViewModel
{
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);
@ -104,11 +104,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
entity = Mapper.MapToClass(model, entity);
entity = Mapper.MapToClass(model, entity, true);
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);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model);
var entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, true);
context.Roles.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity));
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(entity, true));
}
else
{
@ -34,7 +34,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity));
return OperationResultModel.Success(Mapper.MapToClass<Role, RoleViewModel>(exsistEntity, true));
}
else
{
@ -103,7 +103,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
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();
@ -121,7 +121,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new RoleListViewModel
{
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);
@ -140,11 +140,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, entity);
entity = Mapper.MapToClass<RoleSetBindingModel, Role>(model, entity, true);
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);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<UserSetBindingModel, User>(model);
var entity = Mapper.MapToClass<UserSetBindingModel, User>(model, true);
context.Users.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity));
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity, true));
}
else
{
@ -34,7 +34,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(exsistEntity));
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(exsistEntity, true));
}
else
{
@ -95,7 +95,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
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();
@ -113,7 +113,7 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
var result = new UserListViewModel
{
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);
@ -132,11 +132,11 @@ namespace SecurityImplementation.SecurityDatabaseImplementation
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity);
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity));
return OperationResultModel.Success(Mapper.MapToClass<User, UserViewModel>(entity, true));
}
}
}