Esempio n. 1
0
        /// <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());
        }
Esempio n. 2
0
        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))));
        }
Esempio n. 3
0
 /// <summary>
 /// Метод  обработки условии к доступу данных
 /// </summary>
 private static void DataAccessCondition <TSource, TDestionation>(
     IMappingExpression <TSource, TDestionation> mapping)
     where TSource : IStatUnitM
     where TDestionation : IStatisticalUnit
 =>
 mapping.ForAllMembers(v => v.Condition((src, dst) =>
 {
     var name = DataAccessAttributesHelper.GetName(dst.GetType(), v.DestinationMember.Name);
     return(DataAccessAttributesProvider.Find(name) == null ||
            (src.DataAccess?.HasWritePermission(name) ?? false));
 }));
Esempio n. 4
0
        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); }));
        }
Esempio n. 5
0
 private static List <DataAccessAttributeM> ToDataAccessAttributeM(IEnumerable <PropertyInfo> properties)
 => properties.Select(v =>
 {
     var displayAttribute = v.GetCustomAttribute <DisplayAttribute>();
     return(Tuple.Create(
                displayAttribute?.GetOrder() ?? int.MaxValue,
                new DataAccessAttributeM
     {
         Name = DataAccessAttributesHelper.GetName(typeof(T), v.Name),
         GroupName = displayAttribute?.GroupName,
         LocalizeKey = v.Name,
     }));
 })
 .OrderBy(v => v.Item1)
 .Select(v => v.Item2)
 .ToList();
Esempio n. 6
0
        public void CreateTest()
        {
            using (var context = CreateDbContext())
            {
                var submitData =
                    new RoleSubmitM
                {
                    Name               = "Role",
                    Description        = "Description",
                    StandardDataAccess = new DataAccessModel()
                    {
                        LocalUnit = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <LegalUnit>("ForeignCapitalShare"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                        LegalUnit = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <LocalUnit>("LegalUnitIdDate"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                        EnterpriseGroup = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <EnterpriseGroup>("LiqReason"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                        EnterpriseUnit = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <EnterpriseUnit>("Employees"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                    },
                    AccessToSystemFunctions = new List <int> {
                        1, 2, 3
                    },
                    ActiviyCategoryIds = new int[] {}
                };

                var  role     = new RoleService(context).Create(submitData);
                var  expected = typeof(Exception);
                Type actual   = null;
                try
                {
                    new RoleService(context).Create(submitData);
                }
                catch (Exception e)
                {
                    actual = e.GetType();
                }

                Assert.Equal(role.Name,
                             context.Roles.Single(
                                 x =>
                                 x.Name == submitData.Name && x.Status == RoleStatuses.Active &&
                                 x.Description == submitData.Description &&
                                 x.StandardDataAccess == JsonConvert.SerializeObject(submitData.StandardDataAccess.ToPermissionsModel()) &&
                                 x.AccessToSystemFunctions == "1,2,3"
                                 ).Name);
                Assert.Equal(expected, actual);
            }
        }
Esempio n. 7
0
        public void EditTest()
        {
            using (var context = CreateDbContext())
            {
                var role = new Role
                {
                    AccessToSystemFunctionsArray = new List <int> {
                        1, 3
                    },
                    Name = "Role Name",
                    StandardDataAccessArray = new DataAccessPermissions(
                        new List <string> {
                        "LocalUnit.1", "LegalUnit.2", "EnterpriseUnit.3", "EnterpriseGroup.4"
                    }
                        .Select(x => new Permission(x, true, true)))
                    ,
                    Status = RoleStatuses.Active
                };
                context.Roles.Add(role);
                context.SaveChanges();
                context.Entry(role).State = EntityState.Detached;

                var daa = new DataAccessModel()
                {
                    LocalUnit = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <LegalUnit>("ForeignCapitalShare"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                    LegalUnit = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <LocalUnit>("LegalUnitIdDate"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                    EnterpriseGroup = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <EnterpriseGroup>("LiqReason"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                    EnterpriseUnit = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <EnterpriseUnit>("Employees"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                };

                var roleData = new RoleSubmitM
                {
                    Name = "Edited Role Name",
                    AccessToSystemFunctions = new List <int> {
                        1, 2, 3
                    },
                    StandardDataAccess = daa,
                    Description        = "After Edit",
                    ActiviyCategoryIds = new int[] { }
                };

                new RoleService(context).Edit(role.Id, roleData);
                var single = context.Roles.Single(x => x.Id == role.Id);

                Assert.Equal(roleData.Name, single.Name);
                Assert.Equal(role.Status, single.Status);
                Assert.Equal(roleData.Description, single.Description);
                Assert.Equal(roleData.AccessToSystemFunctions, single.AccessToSystemFunctionsArray);
                Assert.Equal(JsonConvert.SerializeObject(roleData.StandardDataAccess.ToPermissionsModel()), single.StandardDataAccess);
            }
        }