#17842: Exception when content type name contains special chars

Work Items: 17842

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-05-27 14:59:45 -07:00
parent 5677c4dba9
commit fd041c3c2e
2 changed files with 24 additions and 3 deletions

View File

@@ -54,11 +54,15 @@ namespace Orchard.ContentTypes.Controllers {
ModelState.AddModelError("DisplayName", T("The Display Name name can't be empty.").ToString());
}
if ( _contentDefinitionService.GetTypes().Any(t => String.Equals(t.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase)) ) {
if (_contentDefinitionService.GetTypes().Any(t => String.Equals(t.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase))) {
ModelState.AddModelError("Name", T("A type with the same Id already exists.").ToString());
}
if ( _contentDefinitionService.GetTypes().Any(t => String.Equals(t.DisplayName.Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase)) ) {
if (!String.IsNullOrWhiteSpace(viewModel.Name) && !ContentDefinitionService.IsLetter(viewModel.Name[0])) {
ModelState.AddModelError("Name", T("The technical name must start with a letter.").ToString());
}
if (_contentDefinitionService.GetTypes().Any(t => String.Equals(t.DisplayName.Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase))) {
ModelState.AddModelError("DisplayName", T("A type with the same Name already exists.").ToString());
}

View File

@@ -71,6 +71,11 @@ namespace Orchard.ContentTypes.Services {
if(String.IsNullOrWhiteSpace(name)) {
name = GenerateContentTypeNameFromDisplayName(displayName);
}
else {
if(!IsLetter(name[0])) {
throw new ArgumentException("Content type name must start with a letter", "name");
}
}
while ( _contentDefinitionManager.GetTypeDefinition(name) != null )
name = VersionName(name);
@@ -235,11 +240,23 @@ namespace Orchard.ContentTypes.Services {
name = dissallowed.Replace(name, String.Empty);
name = name.Trim();
// don't allow non A-Z chars as first letter, as they are not allowed in prefixes
if(name.Length > 0) {
if (!IsLetter(name[0])) {
name = name.Substring(1);
}
}
if (name.Length > 128)
name = name.Substring(0, 128);
return name;
}
public static bool IsLetter(char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
//gratuitously stolen from the RoutableService
public string GenerateContentTypeNameFromDisplayName(string displayName) {
displayName = SafeName(displayName);