public static StatUnitViewModel Create( IStatisticalUnit domainEntity, DataAccessPermissions dataAccess, IReadOnlyDictionary <string, bool> mandatoryFields, ActionsEnum ignoredActions) { var properties = GetFilteredProperties(domainEntity.GetType()) .Select(x => PropertyMetadataFactory.Create( x.PropInfo, domainEntity, x.Writable, mandatoryFields.TryGetValue(x.PropInfo.Name, out var mandatory) ? mandatory : (bool?)null)); return(new StatUnitViewModel { StatUnitType = StatisticalUnitsTypeHelper.GetStatUnitMappingType(domainEntity.GetType()), Properties = properties, Permissions = dataAccess.Permissions.Where(x => properties.Any(d => x.PropertyName.EndsWith($".{d.LocalizeKey}"))).ToList() //TODO: Filter By Type (Optimization) }); IEnumerable <(PropertyInfo PropInfo, bool Writable)> GetFilteredProperties(Type type) => type.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(x => dataAccess.HasWriteOrReadPermission(DataAccessAttributesHelper.GetName(type, x.Name)) && x.CanRead && x.CanWrite && (x.GetCustomAttribute <NotMappedForAttribute>(true) == null || !x.GetCustomAttribute <NotMappedForAttribute>(true).Actions.HasFlag(ignoredActions)) ) .OrderBy(x => ((DisplayAttribute)x.GetCustomAttribute(typeof(DisplayAttribute)))?.GetOrder() ?? int.MaxValue) .Select(x => (x, dataAccess.HasWritePermission(DataAccessAttributesHelper.GetName(type, x.Name)))); }
/// <summary> /// Method returning unchanged cropped fields /// </summary> /// <param name = "after"> After </param> /// <param name = "before"> To </param> /// <param name = "userId"> User Id </param> /// <returns> </returns> private async Task <IEnumerable <ChangedField> > CutUnchangedFields <T>(T after, T before, string userId) where T : class, IStatisticalUnit { var unitType = after.GetType(); var daa = await _userService.GetDataAccessAttributes( userId, StatisticalUnitsTypeHelper.GetStatUnitMappingType(unitType)); var cahangedFields = from prop in unitType.GetProperties() let valueBefore = unitType.GetProperty(prop.Name).GetValue(before, null)?.ToString() ?? "" let valueAfter = unitType.GetProperty(prop.Name).GetValue(after, null)?.ToString() ?? "" where prop.Name != nameof(IStatisticalUnit.RegId) && daa.HasWriteOrReadPermission(DataAccessAttributesHelper.GetName(unitType, prop.Name)) && valueAfter != valueBefore select new ChangedField { Name = prop.Name, Before = valueBefore, After = valueAfter }; var result = cahangedFields.ToArray(); foreach (var historyChangedField in result.Where(x => _foreignKeysResolver.Keys.Contains(x.Name)).Select(x => x).ToArray()) { if (historyChangedField != null) { _foreignKeysResolver[historyChangedField.Name](historyChangedField); } } return(result.ToArray()); }
public static object Create <T>(T statUnit, StatUnitTypes type, ISet <string> propNames, bool?isReadonly = null) where T : class { var unitType = StatisticalUnitsTypeHelper.GetStatUnitMappingType(type); if (unitType != typeof(T)) { var currentType = statUnit.GetType(); propNames = new HashSet <string>( currentType.GetProperties() .Where(v => propNames.Contains(DataAccessAttributesHelper.GetName(unitType, v.Name))) .Select(v => DataAccessAttributesHelper.GetName(currentType, v.Name)) .ToList() ); } return(DataAccessResolver.Execute(statUnit, propNames, jo => { jo.Add("type", (int)type); jo.Add("readonly", isReadonly); })); }
public static LinkInfo Create <TParent, TChild>( Expression <Func <TChild, int?> > property, Expression <Func <TChild, TParent> > link) where TParent : class, IStatisticalUnit where TChild : class, IStatisticalUnit { var key = new GenericDataProperty <TChild, int?>(property); var entity = new GenericDataProperty <TChild, TParent>(link); return(new LinkInfo { Type1 = StatisticalUnitsTypeHelper.GetStatUnitMappingType(typeof(TParent)), Type2 = StatisticalUnitsTypeHelper.GetStatUnitMappingType(typeof(TChild)), Getter = key.Getter, Setter = key.Setter, LinkExpression = link, Link = unit => entity.Getter((TChild)unit) }); }
/// <summary> /// Method for obtaining data access attributes /// </summary> /// <param name = "userId"> User Id </param> /// <param name = "type"> User type </param> /// <returns> </returns> public async Task <DataAccessPermissions> GetDataAccessAttributes(string userId, StatUnitTypes?type) { var dataAccess = await( from userRoles in _context.UserRoles join role in _context.Roles on userRoles.RoleId equals role.Id where userRoles.UserId == userId select role.StandardDataAccessArray ) .ToListAsync(); var commonPermissions = new DataAccessPermissions( DataAccessAttributesProvider.CommonAttributes .Select(v => new Permission(v.Name, true, true))); var permissions = DataAccessPermissions.Combine(dataAccess.Append(commonPermissions)); if (type.HasValue) { var name = StatisticalUnitsTypeHelper.GetStatUnitMappingType(type.Value).Name; permissions = permissions.ForType(name); } return(permissions); }
/// <summary> /// Communication context check method /// </summary> /// <param name = "data"> Data </param> /// <param name = "linkMethod"> Communication method </param> /// <param name = "lookupFailureMessage"> Search error message </param> /// <param name = "userId"> User Id </param> /// <returns> </returns> private async Task <bool> LinkContext <T>(T data, MethodInfo linkMethod, string lookupFailureMessage, string userId) where T : LinkSubmitM { LinkInfo info; var reverted = false; if (!LinksMetadata.TryGetValue(Tuple.Create(data.Source1.Type, data.Source2.Type), out info)) { if (!LinksMetadata.TryGetValue(Tuple.Create(data.Source2.Type, data.Source1.Type), out info)) { throw new BadRequestException(lookupFailureMessage); } reverted = true; } var method = linkMethod.MakeGenericMethod( StatisticalUnitsTypeHelper.GetStatUnitMappingType(info.Type1), StatisticalUnitsTypeHelper.GetStatUnitMappingType(info.Type2) ); return(await(Task <bool>) method.Invoke(this, new[] { data, reverted, info.Getter, info.Setter, userId })); }
/// <summary> /// The method of obtaining the domain for the default type /// </summary> /// <param name = "type"> Type of stat. units </param> /// <returns> </returns> private static IStatisticalUnit GetDefaultDomainForType(StatUnitTypes type) => (IStatisticalUnit)Activator.CreateInstance(StatisticalUnitsTypeHelper.GetStatUnitMappingType(type));