Skip to content

Commit 0565eee

Browse files
authored
Do not allow editing read-only properties (#17915)
1 parent cce056b commit 0565eee

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

src/Umbraco.Core/Services/ContentEditingServiceBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ private void RemoveMissingProperties(ContentEditingModelBase contentEditingModel
458458
IDataValueEditor dataValueEditor = dataEditor.GetValueEditor();
459459
if (dataValueEditor.IsReadOnly)
460460
{
461-
return null;
461+
// read-only property editor - get and return the current value
462+
return content.GetValue(propertyType.Alias, culture, segment);
462463
}
463464

464465
IDataType? dataType = await _dataTypeService.GetAsync(propertyType.DataTypeKey);

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEditingServiceTests.Update.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,95 @@ public async Task Cannot_Update_Variant_With_Incorrect_Culture_Casing()
333333
Assert.IsFalse(result.Success);
334334
Assert.AreEqual(ContentEditingOperationStatus.InvalidCulture, result.Status);
335335
}
336+
337+
[Test]
338+
public async Task Cannot_Update_Invariant_Readonly_Property_Value()
339+
{
340+
var content = await CreateInvariantContent();
341+
content.SetValue("label", "The initial label value");
342+
ContentService.Save(content);
343+
344+
var updateModel = new ContentUpdateModel
345+
{
346+
InvariantName = "Updated Name",
347+
InvariantProperties = new[]
348+
{
349+
new PropertyValueModel { Alias = "label", Value = "The updated label value" }
350+
}
351+
};
352+
353+
var result = await ContentEditingService.UpdateAsync(content.Key, updateModel, Constants.Security.SuperUserKey);
354+
Assert.Multiple(() =>
355+
{
356+
Assert.IsTrue(result.Success);
357+
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
358+
Assert.IsNotNull(result.Result.Content);
359+
});
360+
361+
// re-get and validate
362+
content = await ContentEditingService.GetAsync(content.Key);
363+
Assert.IsNotNull(content);
364+
Assert.Multiple(() =>
365+
{
366+
Assert.AreEqual("Updated Name", content.Name);
367+
Assert.AreEqual("The initial label value", content.GetValue<string>("label"));
368+
});
369+
}
370+
371+
[Test]
372+
public async Task Cannot_Update_Variant_Readonly_Property_Value()
373+
{
374+
var content = await CreateVariantContent();
375+
content.SetValue("variantLabel", "The initial English label value", "en-US");
376+
content.SetValue("variantLabel", "The initial Danish label value", "da-DK");
377+
ContentService.Save(content);
378+
379+
var updateModel = new ContentUpdateModel
380+
{
381+
InvariantProperties = new[]
382+
{
383+
new PropertyValueModel { Alias = "invariantTitle", Value = "The updated invariant title" }
384+
},
385+
Variants = new []
386+
{
387+
new VariantModel
388+
{
389+
Culture = "en-US",
390+
Name = "Updated English Name",
391+
Properties = new []
392+
{
393+
new PropertyValueModel { Alias = "variantLabel", Value = "The updated English label value" }
394+
}
395+
},
396+
new VariantModel
397+
{
398+
Culture = "da-DK",
399+
Name = "Updated Danish Name",
400+
Properties = new []
401+
{
402+
new PropertyValueModel { Alias = "variantLabel", Value = "The updated Danish label value" }
403+
}
404+
}
405+
}
406+
};
407+
408+
var result = await ContentEditingService.UpdateAsync(content.Key, updateModel, Constants.Security.SuperUserKey);
409+
Assert.Multiple(() =>
410+
{
411+
Assert.IsTrue(result.Success);
412+
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
413+
Assert.IsNotNull(result.Result.Content);
414+
});
415+
416+
// re-get and validate
417+
content = await ContentEditingService.GetAsync(content.Key);
418+
Assert.IsNotNull(content);
419+
Assert.Multiple(() =>
420+
{
421+
Assert.AreEqual("Updated English Name", content.GetCultureName("en-US"));
422+
Assert.AreEqual("Updated Danish Name", content.GetCultureName("da-DK"));
423+
Assert.AreEqual("The initial English label value", content.GetValue<string>("variantLabel", "en-US"));
424+
Assert.AreEqual("The initial Danish label value", content.GetValue<string>("variantLabel", "da-DK"));
425+
});
426+
}
336427
}

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEditingServiceTestsBase.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ protected IContentType CreateInvariantContentType(params ITemplate[] templates)
3838
.WithAlias("text")
3939
.WithName("Text")
4040
.WithVariations(ContentVariation.Nothing)
41+
.Done()
42+
.AddPropertyType()
43+
.WithAlias("label")
44+
.WithName("Label")
45+
.WithDataTypeId(Constants.DataTypes.LabelString)
46+
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Label)
47+
.WithVariations(ContentVariation.Nothing)
4148
.Done();
4249

4350
foreach (var template in templates)
@@ -83,6 +90,13 @@ protected async Task<IContentType> CreateVariantContentType()
8390
.WithName("Invariant Title")
8491
.WithVariations(ContentVariation.Nothing)
8592
.Done()
93+
.AddPropertyType()
94+
.WithAlias("variantLabel")
95+
.WithName("Variant Label")
96+
.WithDataTypeId(Constants.DataTypes.LabelString)
97+
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.Label)
98+
.WithVariations(ContentVariation.Culture)
99+
.Done()
86100
.Build();
87101
contentType.AllowedAsRoot = true;
88102
ContentTypeService.Save(contentType);

0 commit comments

Comments
 (0)