Files
Umbraco-CMS/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DomainRepository.cs

208 lines
8.1 KiB
C#
Raw Normal View History

using System;
2015-01-21 17:03:56 +11:00
using System.Collections.Generic;
using System.Data;
2015-01-21 17:03:56 +11:00
using System.Linq;
2020-09-17 09:42:55 +02:00
using Microsoft.Extensions.Logging;
using NPoco;
Merge remote-tracking branch 'origin/v8/8.16' into v9/feature/merge_v8_11082021 # Conflicts: # .github/CONTRIBUTING.md # build/NuSpecs/UmbracoCms.Core.nuspec # build/NuSpecs/UmbracoCms.Web.nuspec # build/NuSpecs/UmbracoCms.nuspec # src/SolutionInfo.cs # src/Umbraco.Core/Cache/AppCaches.cs # src/Umbraco.Core/Cache/AppPolicedCacheDictionary.cs # src/Umbraco.Core/Cache/DeepCloneAppCache.cs # src/Umbraco.Core/Cache/WebCachingAppCache.cs # src/Umbraco.Core/CompositionExtensions.cs # src/Umbraco.Core/Models/Identity/BackOfficeIdentityUser.cs # src/Umbraco.Core/Models/PropertyGroupCollection.cs # src/Umbraco.Core/Models/PropertyTypeCollection.cs # src/Umbraco.Core/Persistence/Repositories/Implement/ExternalLoginRepository.cs # src/Umbraco.Core/ReadLock.cs # src/Umbraco.Core/Routing/SiteDomainMapper.cs # src/Umbraco.Core/UpgradeableReadLock.cs # src/Umbraco.Core/WriteLock.cs # src/Umbraco.Examine/ExamineExtensions.cs # src/Umbraco.Infrastructure/Examine/UmbracoFieldDefinitionCollection.cs # src/Umbraco.Infrastructure/Persistence/Dtos/ContentTypeDto.cs # src/Umbraco.Infrastructure/Persistence/Dtos/DictionaryDto.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberGroupRepository.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/TemplateRepository.cs # src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs # src/Umbraco.Infrastructure/Services/IdKeyMap.cs # src/Umbraco.Infrastructure/Services/Implement/ContentService.cs # src/Umbraco.ModelsBuilder.Embedded/PureLiveModelFactory.cs # src/Umbraco.Tests/App.config # src/Umbraco.Web.BackOffice/Controllers/EntityController.cs # src/Umbraco.Web.UI.Client/package.json # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/da.xml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en.xml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en_us.xml # src/Umbraco.Web.UI/Umbraco.Web.UI.csproj # src/Umbraco.Web.UI/Umbraco/config/lang/cy.xml # src/Umbraco.Web.UI/web.Template.config # src/Umbraco.Web/CacheHelperExtensions.cs # src/Umbraco.Web/Editors/RelationTypeController.cs # src/Umbraco.Web/Logging/WebProfilerProvider.cs # src/Umbraco.Web/Models/Mapping/MemberMapDefinition.cs # src/Umbraco.Web/PublishedCache/NuCache/MemberCache.cs # src/Umbraco.Web/Routing/ContentFinderByConfigured404.cs # src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs # src/Umbraco.Web/Security/BackOfficeUserManager.cs # src/Umbraco.Web/Umbraco.Web.csproj
2021-08-11 19:11:35 +02:00
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
2022-01-13 17:44:11 +00:00
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Extensions;
2015-01-21 17:03:56 +11:00
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
2015-01-21 17:03:56 +11:00
{
// TODO: We need to get a readonly ISO code for the domain assigned
2020-12-22 10:30:16 +11:00
internal class DomainRepository : EntityRepositoryBase<int, IDomain>, IDomainRepository
2015-01-21 17:03:56 +11:00
{
2020-09-17 09:42:55 +02:00
public DomainRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger<DomainRepository> logger)
2017-12-14 17:04:44 +01:00
: base(scopeAccessor, cache, logger)
2017-05-12 14:49:44 +02:00
{ }
2015-01-21 17:03:56 +11:00
protected override IRepositoryCachePolicy<IDomain, int> CreateCachePolicy()
2015-01-21 17:03:56 +11:00
{
return new FullDataSetRepositoryCachePolicy<IDomain, int>(GlobalIsolatedCache, ScopeAccessor, GetEntityId, /*expires:*/ false);
2015-01-21 17:03:56 +11:00
}
protected override IDomain? PerformGet(int id)
2015-01-21 17:03:56 +11:00
{
//use the underlying GetAll which will force cache all domains
return GetMany()?.FirstOrDefault(x => x.Id == id);
2015-01-21 17:03:56 +11:00
}
protected override IEnumerable<IDomain> PerformGetAll(params int[]? ids)
2015-01-21 17:03:56 +11:00
{
var sql = GetBaseQuery(false).Where<DomainDto>(x => x.Id > 0);
if (ids?.Any() ?? false)
2015-01-21 17:03:56 +11:00
{
sql.WhereIn<DomainDto>(x => x.Id, ids);
2015-01-21 17:03:56 +11:00
}
return Database.Fetch<DomainDto>(sql).Select(ConvertFromDto);
2015-01-21 17:03:56 +11:00
}
protected override IEnumerable<IDomain> PerformGetByQuery(IQuery<IDomain> query)
{
throw new NotSupportedException("This repository does not support this method");
}
2017-09-22 18:48:58 +02:00
protected override Sql<ISqlContext> GetBaseQuery(bool isCount)
2015-01-21 17:03:56 +11:00
{
var sql = Sql();
if (isCount)
{
sql.SelectCount().From<DomainDto>();
}
else
{
sql.Select("umbracoDomain.*, umbracoLanguage.languageISOCode")
.From<DomainDto>()
.LeftJoin<LanguageDto>()
.On<DomainDto, LanguageDto>(dto => dto.DefaultLanguage, dto => dto.Id);
}
2017-07-20 11:21:28 +02:00
2015-01-21 17:03:56 +11:00
return sql;
}
protected override string GetBaseWhereClause()
{
return $"{Constants.DatabaseSchema.Tables.Domain}.id = @id";
2015-01-21 17:03:56 +11:00
}
protected override IEnumerable<string> GetDeleteClauses()
{
var list = new List<string>
{
"DELETE FROM umbracoDomain WHERE id = @id"
};
return list;
2015-01-21 17:03:56 +11:00
}
protected override void PersistNewItem(IDomain entity)
{
var exists = Database.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoDomain WHERE domainName = @domainName", new { domainName = entity.DomainName });
if (exists > 0) throw new DuplicateNameException(string.Format("The domain name {0} is already assigned", entity.DomainName));
2015-01-21 17:03:56 +11:00
if (entity.RootContentId.HasValue)
2015-01-21 17:03:56 +11:00
{
var contentExists = Database.ExecuteScalar<int>($"SELECT COUNT(*) FROM {Cms.Core.Constants.DatabaseSchema.Tables.Content} WHERE nodeId = @id", new { id = entity.RootContentId.Value });
if (contentExists == 0) throw new NullReferenceException("No content exists with id " + entity.RootContentId.Value);
2015-01-21 17:03:56 +11:00
}
if (entity.LanguageId.HasValue)
{
var languageExists = Database.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoLanguage WHERE id = @id", new { id = entity.LanguageId.Value });
if (languageExists == 0) throw new NullReferenceException("No language exists with id " + entity.LanguageId.Value);
}
entity.AddingEntity();
var factory = new DomainModelFactory();
var dto = factory.BuildDto(entity);
2015-01-21 17:03:56 +11:00
var id = Convert.ToInt32(Database.Insert(dto));
entity.Id = id;
2015-01-21 17:03:56 +11:00
//if the language changed, we need to resolve the ISO code!
if (entity.LanguageId.HasValue)
{
((UmbracoDomain)entity).LanguageIsoCode = Database.ExecuteScalar<string>("SELECT languageISOCode FROM umbracoLanguage WHERE id=@langId", new { langId = entity.LanguageId });
}
entity.ResetDirtyProperties();
2015-01-21 17:03:56 +11:00
}
protected override void PersistUpdatedItem(IDomain entity)
2015-01-21 17:03:56 +11:00
{
entity.UpdatingEntity();
2015-01-21 17:03:56 +11:00
var exists = Database.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoDomain WHERE domainName = @domainName AND umbracoDomain.id <> @id",
new { domainName = entity.DomainName, id = entity.Id });
//ensure there is no other domain with the same name on another entity
if (exists > 0) throw new DuplicateNameException(string.Format("The domain name {0} is already assigned", entity.DomainName));
2015-01-21 17:03:56 +11:00
if (entity.RootContentId.HasValue)
2015-01-21 17:03:56 +11:00
{
var contentExists = Database.ExecuteScalar<int>($"SELECT COUNT(*) FROM {Cms.Core.Constants.DatabaseSchema.Tables.Content} WHERE nodeId = @id", new { id = entity.RootContentId.Value });
if (contentExists == 0) throw new NullReferenceException("No content exists with id " + entity.RootContentId.Value);
}
2015-01-21 17:03:56 +11:00
if (entity.LanguageId.HasValue)
{
var languageExists = Database.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoLanguage WHERE id = @id", new { id = entity.LanguageId.Value });
if (languageExists == 0) throw new NullReferenceException("No language exists with id " + entity.LanguageId.Value);
2015-01-21 17:03:56 +11:00
}
var factory = new DomainModelFactory();
var dto = factory.BuildDto(entity);
2015-01-21 17:03:56 +11:00
Database.Update(dto);
2015-01-21 17:03:56 +11:00
//if the language changed, we need to resolve the ISO code!
if (entity.WasPropertyDirty("LanguageId"))
{
((UmbracoDomain)entity).LanguageIsoCode = Database.ExecuteScalar<string>("SELECT languageISOCode FROM umbracoLanguage WHERE id=@langId", new {langId = entity.LanguageId});
}
entity.ResetDirtyProperties();
}
2015-01-21 17:03:56 +11:00
public IDomain? GetByName(string domainName)
{
return GetMany()?.FirstOrDefault(x => x.DomainName.InvariantEquals(domainName));
2015-01-21 17:03:56 +11:00
}
public bool Exists(string domainName)
2015-01-21 17:03:56 +11:00
{
return GetMany()?.Any(x => x.DomainName.InvariantEquals(domainName)) ?? false;
}
2015-01-21 17:03:56 +11:00
2022-04-20 08:42:06 +02:00
public IEnumerable<IDomain> GetAll(bool includeWildcards)
{
2022-04-20 08:42:06 +02:00
return GetMany().Where(x => includeWildcards || x.IsWildcard == false);
}
2015-01-21 17:03:56 +11:00
2022-04-20 08:42:06 +02:00
public IEnumerable<IDomain> GetAssignedDomains(int contentId, bool includeWildcards)
{
2022-04-20 08:42:06 +02:00
return GetMany()
.Where(x => x.RootContentId == contentId)
.Where(x => includeWildcards || x.IsWildcard == false);
}
2015-01-21 17:03:56 +11:00
private IDomain ConvertFromDto(DomainDto dto)
{
var factory = new DomainModelFactory();
var entity = factory.BuildEntity(dto);
return entity;
2017-07-20 11:21:28 +02:00
}
2015-01-21 17:03:56 +11:00
internal class DomainModelFactory
{
2017-07-20 11:21:28 +02:00
public IDomain BuildEntity(DomainDto dto)
2015-01-21 17:03:56 +11:00
{
var domain = new UmbracoDomain(dto.DomainName, dto.IsoCode)
{
Id = dto.Id,
LanguageId = dto.DefaultLanguage,
RootContentId = dto.RootStructureId
};
2017-11-10 11:27:12 +01:00
// reset dirty initial properties (U4-1946)
2015-01-21 17:03:56 +11:00
domain.ResetDirtyProperties(false);
return domain;
}
public DomainDto BuildDto(IDomain entity)
2015-01-21 17:03:56 +11:00
{
var dto = new DomainDto { DefaultLanguage = entity.LanguageId, DomainName = entity.DomainName, Id = entity.Id, RootStructureId = entity.RootContentId };
2015-01-21 17:03:56 +11:00
return dto;
}
}
}
2017-07-20 11:21:28 +02:00
}