// REGISTATION public static void Register(IConfigurationEditor instance) { if (FindInstance(instance.ID) == null) { _repository.Register(instance); } }
public static IConfigurationEditor[] ExportInstances() { int count = _repository.Count(); IConfigurationEditor[] result = new IConfigurationEditor[count]; for (int i = 0; i < count; i++) { result[i] = _repository.GetItem(i); } return result; }
public override IDataEditor Build() { var name = _name ?? Guid.NewGuid().ToString(); var alias = _alias ?? name.ToCamelCase(); IDictionary <string, object> defaultConfiguration = _defaultConfiguration ?? new Dictionary <string, object>(); IConfigurationEditor explicitConfigurationEditor = _explicitConfigurationEditorBuilder.Build(); IDataValueEditor explicitValueEditor = _explicitValueEditorBuilder.Build(); return(new DataEditor( Mock.Of <IDataValueEditorFactory>()) { Alias = alias, Name = name, DefaultConfiguration = defaultConfiguration, ExplicitConfigurationEditor = explicitConfigurationEditor, ExplicitValueEditor = explicitValueEditor }); }
public void AddConfigurationEditor(IConfigurationEditor editor) { string ID = editor.GetID(); System.Windows.Forms.MenuItem menuItem = new System.Windows.Forms.MenuItem(ID); menuItem.Click += new System.EventHandler(this.MainWindow_ConfigurationEditorSelect); menuItem.Tag = editor; string category = editor.GetCategory(); if ((category == "") || (category == null)) { menuItemConfigurationEditors.MenuItems.Add(menuItem); } else { System.Windows.Forms.MenuItem categoryItem = findCategoryConfigurationEditor(category); if (categoryItem == null) { categoryItem = new System.Windows.Forms.MenuItem(category); menuItemConfigurationEditors.MenuItems.Add(categoryItem); } categoryItem.MenuItems.Add(menuItem); } }
private IEnumerable <DataTypeConfigurationFieldDisplay> MapPreValues(IDataType dataType, MapperContext context) { // in v7 it was apparently fine to have an empty .EditorAlias here, in which case we would map onto // an empty fields list, which made no sense since there would be nothing to map to - and besides, // a datatype without an editor alias is a serious issue - v8 wants an editor here if (string.IsNullOrWhiteSpace(dataType.EditorAlias) || !_propertyEditors.TryGet(dataType.EditorAlias, out IDataEditor? editor)) { throw new InvalidOperationException( $"Could not find a property editor with alias \"{dataType.EditorAlias}\"."); } IConfigurationEditor configurationEditor = editor.GetConfigurationEditor(); var fields = context .MapEnumerable <ConfigurationField, DataTypeConfigurationFieldDisplay>(configurationEditor.Fields) .WhereNotNull().ToList(); IDictionary <string, object> configurationDictionary = configurationEditor.ToConfigurationEditor(dataType.Configuration); MapConfigurationFields(dataType, fields, configurationDictionary); return(fields); }
public static void Execute(IConfigurationEditor instance) { instance.Execute(); }
protected override void Migrate() { // drop and create columns Delete.Column("pk").FromTable("cmsDataType").Do(); // rename the table Rename.Table("cmsDataType").To(Constants.DatabaseSchema.Tables.DataType).Do(); // create column AddColumn <DataTypeDto>(Constants.DatabaseSchema.Tables.DataType, "config"); Execute.Sql(Sql().Update <DataTypeDto>(u => u.Set(x => x.Configuration, string.Empty))).Do(); // renames Execute.Sql(Sql() .Update <DataTypeDto>(u => u.Set(x => x.EditorAlias, "Umbraco.ColorPicker")) .Where <DataTypeDto>(x => x.EditorAlias == "Umbraco.ColorPickerAlias")).Do(); // from preValues to configuration... Sql <ISqlContext> sql = Sql() .Select <DataTypeDto>() .AndSelect <PreValueDto>(x => x.Id, x => x.Alias, x => x.SortOrder, x => x.Value) .From <DataTypeDto>() .InnerJoin <PreValueDto>().On <DataTypeDto, PreValueDto>((left, right) => left.NodeId == right.NodeId) .OrderBy <DataTypeDto>(x => x.NodeId) .AndBy <PreValueDto>(x => x.SortOrder); IEnumerable <IGrouping <int, PreValueDto> > dtos = Database.Fetch <PreValueDto>(sql).GroupBy(x => x.NodeId); foreach (IGrouping <int, PreValueDto> group in dtos) { DataTypeDto?dataType = Database.Fetch <DataTypeDto>(Sql() .Select <DataTypeDto>() .From <DataTypeDto>() .Where <DataTypeDto>(x => x.NodeId == group.Key)).First(); // check for duplicate aliases var aliases = group.Select(x => x.Alias).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); if (aliases.Distinct().Count() != aliases.Length) { throw new InvalidOperationException( $"Cannot migrate prevalues for datatype id={dataType.NodeId}, editor={dataType.EditorAlias}: duplicate alias."); } // handle null/empty aliases var index = 0; var dictionary = group.ToDictionary(x => string.IsNullOrWhiteSpace(x.Alias) ? index++.ToString() : x.Alias); // migrate the preValues to configuration IPreValueMigrator migrator = _preValueMigrators.GetMigrator(dataType.EditorAlias) ?? new DefaultPreValueMigrator(); var config = migrator.GetConfiguration(dataType.NodeId, dataType.EditorAlias, dictionary); var json = _configurationEditorJsonSerializer.Serialize(config); // validate - and kill the migration if it fails var newAlias = migrator.GetNewAlias(dataType.EditorAlias); if (newAlias == null) { if (!_legacyAliases.Contains(dataType.EditorAlias)) { _logger.LogWarning( "Skipping validation of configuration for data type {NodeId} : {EditorAlias}." + " Please ensure that the configuration is valid. The site may fail to start and / or load data types and run.", dataType.NodeId, dataType.EditorAlias); } } else if (!_propertyEditors.TryGet(newAlias, out IDataEditor? propertyEditor)) { if (!_legacyAliases.Contains(newAlias)) { _logger.LogWarning( "Skipping validation of configuration for data type {NodeId} : {NewEditorAlias} (was: {EditorAlias})" + " because no property editor with that alias was found." + " Please ensure that the configuration is valid. The site may fail to start and / or load data types and run.", dataType.NodeId, newAlias, dataType.EditorAlias); } } else { IConfigurationEditor configEditor = propertyEditor.GetConfigurationEditor(); try { var _ = configEditor.FromDatabase(json, _configurationEditorJsonSerializer); } catch (Exception e) { _logger.LogWarning( e, "Failed to validate configuration for data type {NodeId} : {NewEditorAlias} (was: {EditorAlias})." + " Please fix the configuration and ensure it is valid. The site may fail to start and / or load data types and run.", dataType.NodeId, newAlias, dataType.EditorAlias); } } // update dataType.Configuration = _configurationEditorJsonSerializer.Serialize(config); Database.Update(dataType); } }
// REGISTATION public static void Register(IConfigurationEditor instance) { _repository.Register(instance); }
public void OnActionExecuting(ActionExecutingContext context) { var dataType = (DataTypeSave?)context.ActionArguments["dataType"]; if (dataType is not null) { dataType.Name = dataType.Name?.CleanForXss('[', ']', '(', ')', ':'); dataType.Alias = dataType.Alias == null ? dataType.Name ! : dataType.Alias.CleanForXss('[', ']', '(', ')', ':'); } // get the property editor, ensuring that it exits if (!_propertyEditorCollection.TryGet(dataType?.EditorAlias, out IDataEditor? propertyEditor)) { var message = $"Property editor \"{dataType?.EditorAlias}\" was not found."; context.Result = new UmbracoProblemResult(message, HttpStatusCode.NotFound); return; } if (dataType is null) { return; } // assign dataType.PropertyEditor = propertyEditor; // validate that the data type exists, or create one if required IDataType?persisted; switch (dataType.Action) { case ContentSaveAction.Save: persisted = _dataTypeService.GetDataType(Convert.ToInt32(dataType.Id)); if (persisted == null) { var message = $"Data type with id {dataType.Id} was not found."; context.Result = new UmbracoProblemResult(message, HttpStatusCode.NotFound); return; } // map the model to the persisted instance _umbracoMapper.Map(dataType, persisted); break; case ContentSaveAction.SaveNew: // create the persisted model from mapping the saved model persisted = _umbracoMapper.Map <IDataType>(dataType); ((DataType?)persisted)?.ResetIdentity(); break; default: context.Result = new UmbracoProblemResult($"Data type action {dataType.Action} was not found.", HttpStatusCode.NotFound); return; } // assign (so it's available in the action) dataType.PersistedDataType = persisted; // validate the configuration // which is posted as a set of fields with key (string) and value (object) IConfigurationEditor configurationEditor = propertyEditor.GetConfigurationEditor(); if (dataType.ConfigurationFields is not null) { foreach (DataTypeConfigurationFieldSave field in dataType.ConfigurationFields) { ConfigurationField?editorField = configurationEditor.Fields.SingleOrDefault(x => x.Key == field.Key); if (editorField == null) { continue; } // run each IValueValidator (with null valueType and dataTypeConfiguration: not relevant here) foreach (IValueValidator validator in editorField.Validators) { foreach (ValidationResult result in validator.Validate(field.Value, null, null)) { context.ModelState.AddValidationError(result, "Properties", field.Key ?? string.Empty); } } } } if (context.ModelState.IsValid == false) { // if it is not valid, do not continue and return the model state context.Result = new ValidationErrorResult(context.ModelState); } }
public void CanParseManifest_PropertyEditors() { const string json = @"{'propertyEditors': [ { alias: 'Test.Test1', name: 'Test 1', editor: { view: '~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html', valueType: 'int', hideLabel: true, validation: { 'required': true, 'Regex': '\\d*' } }, prevalues: { fields: [ { label: 'Some config 1', key: 'key1', view: '~/App_Plugins/MyPackage/PropertyEditors/Views/pre-val1.html', validation: { required: true } }, { label: 'Some config 2', key: 'key2', view: '~/App_Plugins/MyPackage/PropertyEditors/Views/pre-val2.html' } ] } }, { alias: 'Test.Test2', name: 'Test 2', isParameterEditor: true, defaultConfig: { key1: 'some default val' }, editor: { view: '~/App_Plugins/MyPackage/PropertyEditors/MyEditor.html', valueType: 'int', validation: { required : true, regex : '\\d*' } } } ]}"; PackageManifest manifest = _parser.ParseManifest(json); Assert.AreEqual(2, manifest.PropertyEditors.Length); IDataEditor editor = manifest.PropertyEditors[1]; Assert.IsTrue((editor.Type & EditorType.MacroParameter) > 0); Assert.IsNotEmpty(editor.DefaultConfiguration); Assert.AreEqual("some default val", editor.DefaultConfiguration["key1"]); editor = manifest.PropertyEditors[0]; Assert.AreEqual("Test.Test1", editor.Alias); Assert.AreEqual("Test 1", editor.Name); Assert.IsFalse((editor.Type & EditorType.MacroParameter) > 0); IDataValueEditor valueEditor = editor.GetValueEditor(); Assert.AreEqual(_ioHelper.ResolveUrl("/App_Plugins/MyPackage/PropertyEditors/MyEditor.html"), valueEditor.View); Assert.AreEqual("int", valueEditor.ValueType); Assert.IsTrue(valueEditor.HideLabel); // these two don't make much sense here //// valueEditor.RegexValidator; //// valueEditor.RequiredValidator; List <IValueValidator> validators = valueEditor.Validators; Assert.AreEqual(2, validators.Count); IValueValidator validator = validators[0]; var v1 = validator as RequiredValidator; Assert.IsNotNull(v1); Assert.AreEqual("Required", v1.ValidationName); validator = validators[1]; var v2 = validator as RegexValidator; Assert.IsNotNull(v2); Assert.AreEqual("Regex", v2.ValidationName); Assert.AreEqual("\\d*", v2.Configuration); // this is not part of the manifest IDictionary <string, object> preValues = editor.GetConfigurationEditor().DefaultConfiguration; Assert.IsEmpty(preValues); IConfigurationEditor preValueEditor = editor.GetConfigurationEditor(); Assert.IsNotNull(preValueEditor); Assert.IsNotNull(preValueEditor.Fields); Assert.AreEqual(2, preValueEditor.Fields.Count); ConfigurationField f = preValueEditor.Fields[0]; Assert.AreEqual("key1", f.Key); Assert.AreEqual("Some config 1", f.Name); Assert.AreEqual(_ioHelper.ResolveUrl("/App_Plugins/MyPackage/PropertyEditors/Views/pre-val1.html"), f.View); List <IValueValidator> fvalidators = f.Validators; Assert.IsNotNull(fvalidators); Assert.AreEqual(1, fvalidators.Count); var fv = fvalidators[0] as RequiredValidator; Assert.IsNotNull(fv); Assert.AreEqual("Required", fv.ValidationName); f = preValueEditor.Fields[1]; Assert.AreEqual("key2", f.Key); Assert.AreEqual("Some config 2", f.Name); Assert.AreEqual(_ioHelper.ResolveUrl("/App_Plugins/MyPackage/PropertyEditors/Views/pre-val2.html"), f.View); fvalidators = f.Validators; Assert.IsNotNull(fvalidators); Assert.AreEqual(0, fvalidators.Count); }