From 5c2b0121ba536084205b5e36e3288b96c919dfa8 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Tue, 10 May 2022 11:03:31 +0200 Subject: [PATCH] Create new table in database for usergroup languages --- .../Models/ContentEditing/UserGroupSave.cs | 6 ++++ .../Models/Mapping/UserMapDefinition.cs | 10 ++++++ .../Persistence/Constants-DatabaseSchema.cs | 1 + .../Install/DatabaseSchemaCreator.cs | 3 +- .../Migrations/Upgrade/UmbracoPlan.cs | 3 ++ .../V_10_0_0/AddUserGroup2LanguageTable.cs | 22 +++++++++++++ .../Persistence/Dtos/UserGroup2LanguageDto.cs | 20 ++++++++++++ .../Persistence/Dtos/UserGroupDto.cs | 5 +++ .../Persistence/Factories/UserGroupFactory.cs | 11 ++++++- .../Implement/UserGroupRepository.cs | 32 +++++++++++++++++++ 10 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_0_0/AddUserGroup2LanguageTable.cs create mode 100644 src/Umbraco.Infrastructure/Persistence/Dtos/UserGroup2LanguageDto.cs diff --git a/src/Umbraco.Core/Models/ContentEditing/UserGroupSave.cs b/src/Umbraco.Core/Models/ContentEditing/UserGroupSave.cs index 1bf7923817..0974a849be 100644 --- a/src/Umbraco.Core/Models/ContentEditing/UserGroupSave.cs +++ b/src/Umbraco.Core/Models/ContentEditing/UserGroupSave.cs @@ -51,6 +51,12 @@ namespace Umbraco.Cms.Core.Models.ContentEditing [DataMember(Name = "assignedPermissions")] public IDictionary>? AssignedPermissions { get; set; } + /// + /// The ids of allowed languages + /// + [DataMember(Name = "allowedLanguages")] + public IEnumerable? AllowedLanguages { get; set; } + /// /// The real persisted user group /// diff --git a/src/Umbraco.Core/Models/Mapping/UserMapDefinition.cs b/src/Umbraco.Core/Models/Mapping/UserMapDefinition.cs index 728ce73a1f..bf196b1593 100644 --- a/src/Umbraco.Core/Models/Mapping/UserMapDefinition.cs +++ b/src/Umbraco.Core/Models/Mapping/UserMapDefinition.cs @@ -104,6 +104,15 @@ namespace Umbraco.Cms.Core.Models.Mapping } } + target.ClearAllowedLanguages(); + if (source.AllowedLanguages is not null) + { + foreach (var language in source.AllowedLanguages) + { + target.AddAllowedLanguage(language); + } + } + } // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate @@ -367,6 +376,7 @@ namespace Umbraco.Cms.Core.Models.Mapping var allLanguages = _localizationService.GetAllLanguages(); var applicableLanguages = Enumerable.Empty(); + if (sourceAllowedLanguages.Any()) { applicableLanguages = allLanguages.Where(x => sourceAllowedLanguages.Contains(x.Id)); diff --git a/src/Umbraco.Core/Persistence/Constants-DatabaseSchema.cs b/src/Umbraco.Core/Persistence/Constants-DatabaseSchema.cs index de5b8c04ae..042028ce10 100644 --- a/src/Umbraco.Core/Persistence/Constants-DatabaseSchema.cs +++ b/src/Umbraco.Core/Persistence/Constants-DatabaseSchema.cs @@ -54,6 +54,7 @@ namespace Umbraco.Cms.Core public const string UserGroup2App = TableNamePrefix + "UserGroup2App"; public const string UserGroup2Node = TableNamePrefix + "UserGroup2Node"; public const string UserGroup2NodePermission = TableNamePrefix + "UserGroup2NodePermission"; + public const string UserGroup2Language = TableNamePrefix + "UserGroup2Language"; public const string ExternalLogin = TableNamePrefix + "ExternalLogin"; public const string TwoFactorLogin = TableNamePrefix + "TwoFactorLogin"; public const string ExternalLoginToken = TableNamePrefix + "ExternalLoginToken"; diff --git a/src/Umbraco.Infrastructure/Migrations/Install/DatabaseSchemaCreator.cs b/src/Umbraco.Infrastructure/Migrations/Install/DatabaseSchemaCreator.cs index d4527909e9..dd2cb0161c 100644 --- a/src/Umbraco.Infrastructure/Migrations/Install/DatabaseSchemaCreator.cs +++ b/src/Umbraco.Infrastructure/Migrations/Install/DatabaseSchemaCreator.cs @@ -86,7 +86,8 @@ namespace Umbraco.Cms.Infrastructure.Migrations.Install typeof(LogViewerQueryDto), typeof(ContentVersionCleanupPolicyDto), typeof(UserGroup2NodeDto), - typeof(CreatedPackageSchemaDto) + typeof(CreatedPackageSchemaDto), + typeof(UserGroup2LanguageDto) }; private readonly IUmbracoDatabase _database; diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs index 37c2ab6c0e..47eafa9f95 100644 --- a/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs @@ -289,6 +289,9 @@ namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade // TO 10.0.0 To("{B7E0D53C-2B0E-418B-AB07-2DDE486E225F}"); + + // TO 10.1.0 + To("{D0B3D29D-F4D5-43E3-BA67-9D49256F3266}"); } } } diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_0_0/AddUserGroup2LanguageTable.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_0_0/AddUserGroup2LanguageTable.cs new file mode 100644 index 0000000000..db7d0c6fe1 --- /dev/null +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_0_0/AddUserGroup2LanguageTable.cs @@ -0,0 +1,22 @@ +using Umbraco.Cms.Infrastructure.Persistence.Dtos; +using Umbraco.Extensions; + +namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_10_0_0; + +public class AddUserGroup2LanguageTable : MigrationBase +{ + public AddUserGroup2LanguageTable(IMigrationContext context) : base(context) + { + } + + protected override void Migrate() + { + IEnumerable tables = SqlSyntax.GetTablesInSchema(Context.Database); + if (tables.InvariantContains(UserGroup2LanguageDto.TableName)) + { + return; + } + + Create.Table().Do(); + } +} diff --git a/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroup2LanguageDto.cs b/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroup2LanguageDto.cs new file mode 100644 index 0000000000..773e74b9bd --- /dev/null +++ b/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroup2LanguageDto.cs @@ -0,0 +1,20 @@ +using NPoco; +using Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations; + +namespace Umbraco.Cms.Infrastructure.Persistence.Dtos; + +[TableName(Cms.Core.Constants.DatabaseSchema.Tables.UserGroup2Language)] +[ExplicitColumns] +public class UserGroup2LanguageDto +{ + public const string TableName = Cms.Core.Constants.DatabaseSchema.Tables.UserGroup2Language; + + [Column("userGroupId")] + [PrimaryKeyColumn(AutoIncrement = false, Name = "PK_userGroup2language", OnColumns = "userGroupId, languageId")] + [ForeignKey(typeof(UserGroupDto))] + public int UserGroupId { get; set; } + + [Column("languageId")] + [ForeignKey(typeof(LanguageDto))] + public int LanguageId { get; set; } +} diff --git a/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroupDto.cs b/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroupDto.cs index afbda3cc9a..a9c368b39b 100644 --- a/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroupDto.cs +++ b/src/Umbraco.Infrastructure/Persistence/Dtos/UserGroupDto.cs @@ -14,6 +14,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Dtos public UserGroupDto() { UserGroup2AppDtos = new List(); + UserGroup2LanguageDtos = new List(); } [Column("id")] @@ -63,6 +64,10 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Dtos [Reference(ReferenceType.Many, ReferenceMemberName = "UserGroupId")] public List UserGroup2AppDtos { get; set; } + [ResultColumn] + [Reference(ReferenceType.Many, ReferenceMemberName = "UserGroupId")] + public List UserGroup2LanguageDtos { get; set; } + /// /// This is only relevant when this column is included in the results (i.e. GetUserGroupsWithUserCounts) /// diff --git a/src/Umbraco.Infrastructure/Persistence/Factories/UserGroupFactory.cs b/src/Umbraco.Infrastructure/Persistence/Factories/UserGroupFactory.cs index 9672e0e3a9..f3d74d8ec2 100644 --- a/src/Umbraco.Infrastructure/Persistence/Factories/UserGroupFactory.cs +++ b/src/Umbraco.Infrastructure/Persistence/Factories/UserGroupFactory.cs @@ -12,7 +12,11 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Factories { public static IUserGroup BuildEntity(IShortStringHelper shortStringHelper, UserGroupDto dto) { - var userGroup = new UserGroup(shortStringHelper, dto.UserCount, dto.Alias, dto.Name, + var userGroup = new UserGroup( + shortStringHelper, + dto.UserCount, + dto.Alias, + dto.Name, dto.DefaultPermissions.IsNullOrWhiteSpace() ? Enumerable.Empty() : dto.DefaultPermissions!.ToCharArray().Select(x => x.ToString(CultureInfo.InvariantCulture)).ToList(), @@ -34,6 +38,11 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Factories } } + foreach (UserGroup2LanguageDto language in dto.UserGroup2LanguageDtos) + { + userGroup.AddAllowedLanguage(language.LanguageId); + } + userGroup.ResetDirtyProperties(false); return userGroup; } diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserGroupRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserGroupRepository.cs index 7a3ca69db1..f2a90933bf 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserGroupRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserGroupRepository.cs @@ -184,6 +184,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement if (dto == null) return null; + dto.UserGroup2LanguageDtos = GetUserGroupLanguages(id); var userGroup = UserGroupFactory.BuildEntity(_shortStringHelper, dto); return userGroup; @@ -303,6 +304,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement entity.Id = id; PersistAllowedSections(entity); + PersistAllowedLanguages(entity); entity.ResetDirtyProperties(); } @@ -316,6 +318,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement Database.Update(userGroupDto); PersistAllowedSections(entity); + PersistAllowedLanguages(entity); entity.ResetDirtyProperties(); } @@ -339,6 +342,35 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement } } + private void PersistAllowedLanguages(IUserGroup entity) + { + var userGroup = entity; + + // First delete all + Database.Delete("WHERE UserGroupId = @UserGroupId", new { UserGroupId = userGroup.Id }); + + // Then re-add any associated with the group + foreach (var language in userGroup.AllowedLanguages) + { + var dto = new UserGroup2LanguageDto + { + UserGroupId = userGroup.Id, + LanguageId = language, + }; + + Database.Insert(dto); + } + } + + private List GetUserGroupLanguages(int userGroupId) + { + Sql query = Sql() + .Select() + .From() + .Where(x => x.UserGroupId == userGroupId); + return Database.Fetch(query); + } + #endregion ///