Example #1
0
        public async Task TryInteract(DetectorInteractionDto dto)
        {
            await Execute(async() => {
                using (UnitOfWork db = new UnitOfWork())
                {
                    DetectorEntity detector = await db.GetRepo <DetectorEntity>().Get(dto.DetectorId.Value);
                    AccountEntity account   = await db.GetRepo <AccountEntity>().Get(dto.AccountId.Value);

                    DetectorInteractionEventEntity detectorEvent = new DetectorInteractionEventEntity()
                    {
                        account_id  = account.id,
                        detector_id = detector.id,
                        timespan    = DateTime.Now
                    };

                    NotPermittedException ex = null;

                    if (account.Roles.SelectMany(r => r.DetectorPermissions).Any(m => m.id == detector.id))
                    {
                        detectorEvent.log = $"Interaction with Detector #{detector.id} by Account #{account.id}: SUCCESS";
                    }
                    else
                    {
                        detectorEvent.log = $"Interaction with Detector #{detector.id} by Account #{account.id}: ACCESS DENIED";
                        ex = new NotPermittedException(detectorEvent.log);
                    }

                    await db.GetRepo <DetectorInteractionEventEntity>().Create(detectorEvent);
                    await db.Save();

                    if (ex != null)
                    {
                        throw ex;
                    }
                }
            });
        }
Example #2
0
        public async Task <DetectorModel> SetupSettings(AuthorizedDto <DetectorDto> dto)
        {
            return(await Execute(async() => {
                using (UnitOfWork db = new UnitOfWork())
                {
                    IRepo <DetectorEntity> detectorRepo = db.GetRepo <DetectorEntity>();

                    DetectorEntity detectorEntity = await detectorRepo.FirstOrDefault(pi => pi.id == dto.Data.Id.Value);

                    DetectorInteractionEventEntity detectorEvent = new DetectorInteractionEventEntity()
                    {
                        account_id = dto.Session.UserId,
                        detector_id = detectorEntity.id,
                        timespan = DateTime.Now,
                        log = $"Settings setup for Detector #{detectorEntity.id} by {dto.Session.UserId}"
                    };

                    await db.GetRepo <DetectorInteractionEventEntity>().Create(detectorEvent);

                    foreach (DetectorSettingsValueDto settingsValueDto in dto.Data.SettingsValues)
                    {
                        if (settingsValueDto.Id.HasValue)
                        {
                            DetectorSettingsValueEntity settingsValueEntity = detectorEntity.DetectorSettingsValues.First(
                                setting => setting.id == settingsValueDto.Id.Value
                                );

                            DataType dataType = settingsValueEntity.detector_settings_prefab.DataType.name.FromName();
                            DataTypeService.CheckIsDataOfType(settingsValueDto.ValueBase64, dataType);

                            settingsValueEntity.option_data_value_base64 = settingsValueDto.ValueBase64;
                        }
                        else
                        {
                            DetectorSettingsPrefabEntity detectorSettingsPrefab = detectorEntity.DetectorPrefab.DetectorSettingsPrefabs.FirstOrDefault(
                                settingsPrefab => settingsPrefab.id == settingsValueDto.PrefabId.Value
                                );

                            if (detectorSettingsPrefab == null)
                            {
                                throw new NotFoundException("DetectorSettingsPrefab");
                            }

                            DataType dataType = detectorSettingsPrefab.DataType.name.FromName();
                            DataTypeService.CheckIsDataOfType(settingsValueDto.ValueBase64, dataType);

                            DetectorSettingsValueEntity settingWithSameSettingPrefab = detectorEntity.DetectorSettingsValues.FirstOrDefault(
                                setting => setting.detector_settings_prefab_id == settingsValueDto.PrefabId.Value
                                );

                            if (settingWithSameSettingPrefab != null)
                            {
                                settingWithSameSettingPrefab.option_data_value_base64 = settingsValueDto.ValueBase64;
                            }
                            else
                            {
                                DetectorSettingsValueEntity settingsValueEntity = new DetectorSettingsValueEntity()
                                {
                                    detector_id = detectorEntity.id,
                                    option_data_value_base64 = settingsValueDto.ValueBase64,
                                    detector_settings_prefab_id = settingsValueDto.PrefabId.Value
                                };

                                detectorEntity.DetectorSettingsValues.Add(settingsValueEntity);
                            }
                        }
                    }

                    await db.Save();
                    return detectorEntity.ToModel <DetectorModel>();
                }
            }));
        }