/// <summary>
 /// Initializes a new instance of the <see cref="SecurityConfiguration"/> class.
 /// </summary>
 /// <param name="applicationEntitlements">The application entitlements list.</param>
 /// <param name="pageEntitlements">The page entitlements list.</param>
 /// <param name="controlEntitlements">The control entitlements list.</param>
 /// <param name="metadataEntitlements">The metadata entitlements list.</param>
 public SecurityConfiguration(ApplicationEntitlementList applicationEntitlements = null, PageEntitlementList pageEntitlements = null, ControlEntitlementList controlEntitlements = null, MetadataEntitlementList metadataEntitlements = null)
 {
     this.ApplicationEntitlements = applicationEntitlements ?? new ApplicationEntitlementList();
     this.PageEntitlements = pageEntitlements ?? new PageEntitlementList();
     this.ControlEntitlements = controlEntitlements ?? new ControlEntitlementList();
     this.MetadataEntitlements = metadataEntitlements ?? new MetadataEntitlementList();
 }
 public void CreatesEntitlement()
 {
     MetadataEntitlementList m = new MetadataEntitlementList()
                                     {
                                         { "foo", AccessLevel.Write }
                                     };
     Assert.AreEqual(1, m.Count);
     MetadataEntitlement e = m.First();
     Assert.AreEqual("foo", e.ControlName);
     Assert.AreEqual("foo", e.Key);
     Assert.AreEqual(AccessLevel.Write, e.AccessLevel);
 }
            public void CopyConstructor()
            {
                List<MetadataEntitlement> e = new List<MetadataEntitlement>
                {
                    new MetadataEntitlement(),
                    new MetadataEntitlement(),
                    new MetadataEntitlement(),
                    new MetadataEntitlement(),
                    new MetadataEntitlement()
                };

                MetadataEntitlementList m = new MetadataEntitlementList(e);
                Assert.AreEqual(e.Count, m.Count);
            }
        /// <summary>
        /// Imports the products.
        /// </summary>
        /// <param name="sessionData">The session data.</param>
        /// <param name="migrationData">Contains the products to import.</param>
        /// <param name="systemRoleMap">A map of role ids, where the old id is the key and the new id is the value.</param>
        /// <param name="organisationMap">A map of organisation ids, where the old id is the key and the new id is the value.</param>
        /// <param name="validationResults">A list of validation results.</param>
        /// <param name="migrationResults">A list to which import results will be appended.</param>
        private void ImportProducts(SessionData sessionData, MigrationData migrationData, Dictionary<string, string> systemRoleMap, Dictionary<string, string> organisationMap, MigrationDataResults validationResults, MigrationDataResults migrationResults)
        {
            const string importedMessage = "Imported via Workbench";
            Dictionary<string, string> validatorMap = this.ImportValidators(sessionData.UserId, migrationData.Validators);
            Dictionary<string, string> serviceEndpointMap = this.ImportServiceEndpoints(sessionData, migrationData, validationResults, migrationResults);
            Dictionary<string, string> dataSetMap = this.ImportDataSets(sessionData, migrationData, validationResults, migrationResults);

            SystemSettings settings = this.manager.GetSystemSettings(sessionData.UserId);
            if (settings.EnableFormApprovals)
            {
                migrationData.Products.ForEach(p => p.BranchName = Constants.DefaultImportBranch);
            }

            foreach (ProductDefinition product in migrationData.Products)
            {
                if (!validationResults.GetNotificationsFor(product).CanImport)
                {
                    continue;
                }

                ProductDataSourceConfiguration dataSourceConfiguration = migrationData.ProductDataSourceConfigurations[product.Id];

                if (organisationMap.ContainsKey(product.OrganisationId))
                {
                    product.OrganisationId = organisationMap[product.OrganisationId];
                }

                WorkflowConfigurationContainer workflowConfiguration = migrationData.WorkflowConfigurations[product.Id];

                ApplicationEntitlementList applicationEntitlements = new ApplicationEntitlementList(migrationData.Security.SelectMany(s => s.ApplicationEntitlements).Where(e => e.ProductId == product.Id));
                PageEntitlementList pageEntitlements = new PageEntitlementList(migrationData.Security.SelectMany(s => s.PageEntitlements).Where(e => e.ProductId == product.Id));
                ControlEntitlementList controlEntitlements = new ControlEntitlementList(migrationData.Security.SelectMany(s => s.ControlEntitlements).Where(e => e.ProductId == product.Id));
                MetadataEntitlementList metadataEntitlements = new MetadataEntitlementList(migrationData.Security.SelectMany(s => s.MetadataEntitlements).Where(e => e.ProductId == product.Id));
                SecurityConfiguration securityConfiguration = new SecurityConfiguration(applicationEntitlements, pageEntitlements, controlEntitlements, metadataEntitlements);

                foreach (KeyValuePair<string, string> kvp in systemRoleMap)
                {
                    foreach (ApplicationEntitlement entitlement in applicationEntitlements.Where(e => e.RoleId == kvp.Key))
                    {
                        entitlement.RoleId = systemRoleMap[entitlement.RoleId];
                    }

                    foreach (PageEntitlement entitlement in pageEntitlements.Where(e => e.RoleId == kvp.Key))
                    {
                        entitlement.RoleId = systemRoleMap[entitlement.RoleId];
                    }

                    foreach (ControlEntitlement entitlement in controlEntitlements.Where(e => e.RoleId == kvp.Key))
                    {
                        entitlement.RoleId = systemRoleMap[entitlement.RoleId];
                    }

                    foreach (MetadataEntitlement entitlement in metadataEntitlements.Where(e => e.RoleId == kvp.Key))
                    {
                        entitlement.RoleId = systemRoleMap[entitlement.RoleId];
                    }

                    foreach (EmailWorkflowTransitionAction emailTransition in workflowConfiguration.Configuration.States.SelectMany(s => s.Transitions).SelectMany(t => t.TransitionActionList).OfType<EmailWorkflowTransitionAction>().Where(ewt => ewt.PdfRenderRole == kvp.Key))
                    {
                        emailTransition.PdfRenderRole = systemRoleMap[emailTransition.PdfRenderRole];
                    }
                }

                string mappedId;

                // Update control validators with new IDs
                IEnumerable<ControlRegexValidator> controlValidators = product.FormDefinition.Pages.AllControls.AllValidators();
                foreach (ControlRegexValidator validator in controlValidators)
                {
                    bool available = validatorMap.TryGetValue(validator.ValidatorId, out mappedId);
                    if (available)
                    {
                        validator.ValidatorId = mappedId;
                    }
                }

                // Update prebuilt option sources with new IDs
                List<ControlWithOptions> prebuiltOptionControls = product.FormDefinition.Pages.AllControls.FindAllRecursive<ControlWithOptions>(x => x.OptionSource.Type == OptionSourceType.Prebuilt);
                foreach (ControlWithOptions prebuiltOptionControl in prebuiltOptionControls)
                {
                    bool available = dataSetMap.TryGetValue(((PrebuiltOptionSource)prebuiltOptionControl.OptionSource).DataSetId, out mappedId);
                    if (available)
                    {
                        ((PrebuiltOptionSource)prebuiltOptionControl.OptionSource).DataSetId = mappedId;
                    }
                }

                // Update service endpoints with new IDs
                this.UpdateServiceEndpoints(product, dataSourceConfiguration, workflowConfiguration.Configuration, serviceEndpointMap);

                product.EditComment = importedMessage;
                ProductVariation productVariation = null;
                if (product.IsChildForm)
                {
                    productVariation = migrationData.ProductVariations.Exists(p => p.ProductId == product.Id)
                        ? migrationData.ProductVariations.First(p => p.ProductId == product.Id)
                        : new ProductVariation
                        {
                            AddedControls = new List<int>(),
                            EditedControls = new List<int>(),
                            RemovedControls = new List<int>()
                        };
                }

                Product savedProduct = this.manager.SaveProduct(sessionData, product, workflowConfiguration.Configuration, securityConfiguration, dataSourceConfiguration, productVariation);
                migrationResults.AddNotification(savedProduct, MessageType.Information, string.Format(Messages.Import_ProductSuccess, savedProduct.Name, savedProduct.Version));
            }
        }