Commit 9fb4d037d99f42fdb0d294ce2ed2e59d232232fd
1 parent
f7260db4
2026-11-13
Showing
16 changed files
with
401 additions
and
178 deletions
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/DashboardScopeHelper.cs
| ... | ... | @@ -203,7 +203,7 @@ public static class DashboardScopeHelper |
| 203 | 203 | db, labelQuery, regionSchema); |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - return LabelRegionScopeHelper.ApplyLabelLocationListFilter( | |
| 206 | + return await LabelRegionScopeHelper.ApplyLabelLocationListFilterAsync( | |
| 207 | 207 | db, labelQuery, scopedLocationIds, regionSchema); |
| 208 | 208 | } |
| 209 | 209 | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/LabelMultipleOptionManagePermissionHelper.cs
| ... | ... | @@ -6,85 +6,21 @@ using Yi.Framework.SqlSugarCore.Abstractions; |
| 6 | 6 | namespace FoodLabeling.Application.Helpers; |
| 7 | 7 | |
| 8 | 8 | /// <summary> |
| 9 | -/// Multiple Option 编辑/删除权限: | |
| 10 | -/// System Admin 可管理全部; | |
| 11 | -/// Company Admin 仅可管理「非 System Admin 创建」且「适用本公司」的多选项。 | |
| 9 | +/// Multiple Option 编辑/删除权限,与 <see cref="LabelTemplateManagePermissionHelper"/> / 标签分类等规则一致。 | |
| 12 | 10 | /// </summary> |
| 13 | 11 | public static class LabelMultipleOptionManagePermissionHelper |
| 14 | 12 | { |
| 15 | - private const string ScopeAll = "ALL"; | |
| 16 | - | |
| 17 | - public static async Task<bool> CanManageAsync( | |
| 18 | - ICurrentUser currentUser, | |
| 19 | - ISqlSugarDbContext dbContext, | |
| 20 | - IReadOnlyList<string>? partnerIds, | |
| 21 | - bool createdBySystemAdmin) | |
| 22 | - { | |
| 23 | - if (ReportsRoleHelper.IsAdminRole(currentUser)) | |
| 24 | - { | |
| 25 | - return true; | |
| 26 | - } | |
| 27 | - | |
| 28 | - if (!await TeamMemberRoleHelper.IsScopedAdminUserAsync(dbContext.SqlSugarClient, currentUser)) | |
| 29 | - { | |
| 30 | - return false; | |
| 31 | - } | |
| 32 | - | |
| 33 | - if (createdBySystemAdmin) | |
| 34 | - { | |
| 35 | - return false; | |
| 36 | - } | |
| 37 | - | |
| 38 | - var optionPartnerIds = (partnerIds ?? Array.Empty<string>()) | |
| 39 | - .Select(x => x?.Trim()) | |
| 40 | - .Where(x => !string.IsNullOrWhiteSpace(x)) | |
| 41 | - .Select(x => x!) | |
| 42 | - .ToList(); | |
| 43 | - | |
| 44 | - if (optionPartnerIds.Count == 0 || | |
| 45 | - optionPartnerIds.Any(id => | |
| 46 | - string.Equals(id, ScopeAll, StringComparison.OrdinalIgnoreCase))) | |
| 47 | - { | |
| 48 | - return false; | |
| 49 | - } | |
| 50 | - | |
| 51 | - var scope = await PartnerScopeHelper.ResolvePartnerScopeAsync(currentUser, dbContext); | |
| 52 | - if (scope.IsUnrestricted) | |
| 53 | - { | |
| 54 | - return true; | |
| 55 | - } | |
| 56 | - | |
| 57 | - var allowed = scope.AllowedPartnerIds ?? Array.Empty<string>(); | |
| 58 | - return optionPartnerIds.Any(pid => | |
| 59 | - allowed.Any(a => string.Equals(a, pid, StringComparison.OrdinalIgnoreCase))); | |
| 60 | - } | |
| 61 | - | |
| 62 | 13 | public static async Task EnsureCanManageAsync( |
| 63 | 14 | ICurrentUser currentUser, |
| 64 | 15 | ISqlSugarDbContext dbContext, |
| 65 | 16 | FlLabelMultipleOptionDbEntity entity) |
| 66 | 17 | { |
| 67 | - var createdBySystemAdmin = false; | |
| 68 | - if (!string.IsNullOrWhiteSpace(entity.CreatorId) && | |
| 69 | - Guid.TryParse(entity.CreatorId.Trim(), out var creatorGuid)) | |
| 70 | - { | |
| 71 | - createdBySystemAdmin = await TeamMemberRoleHelper.IsSystemAdminUserAsync( | |
| 72 | - dbContext.SqlSugarClient, creatorGuid); | |
| 73 | - } | |
| 74 | - | |
| 75 | - var scopeMap = await LabelEntityPartnerScopeHelper.BuildPartnerScopeDisplayMapAsync( | |
| 76 | - dbContext.SqlSugarClient, | |
| 18 | + await PartnerScopedManagePermissionHelper.EnsureCanManagePartnerEntityAsync( | |
| 19 | + currentUser, | |
| 20 | + dbContext, | |
| 77 | 21 | LabelEntityPartnerScopeHelper.LabelEntityPartnerKind.MultipleOption, |
| 78 | - new List<string> { entity.Id }); | |
| 79 | - scopeMap.TryGetValue(entity.Id, out var scope); | |
| 80 | - var partnerIds = scope?.PartnerIds ?? new List<string>(); | |
| 81 | - | |
| 82 | - if (await CanManageAsync(currentUser, dbContext, partnerIds, createdBySystemAdmin)) | |
| 83 | - { | |
| 84 | - return; | |
| 85 | - } | |
| 86 | - | |
| 87 | - throw new UserFriendlyException( | |
| 22 | + entity.Id, | |
| 23 | + entity.CreatorId, | |
| 88 | 24 | "You are not authorized to edit or delete this Multiple Option Set."); |
| 89 | 25 | } |
| 90 | 26 | } | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/LabelRegionScopeHelper.cs
| ... | ... | @@ -617,7 +617,7 @@ public static class LabelRegionScopeHelper |
| 617 | 617 | /// <summary> |
| 618 | 618 | /// ??? Region?<c>fl_group.Id</c>?? scoped ????? |
| 619 | 619 | /// </summary> |
| 620 | - public static ISugarQueryable<FlLabelDbEntity> ApplyLabelRegionListFilter( | |
| 620 | + public static async Task<ISugarQueryable<FlLabelDbEntity>> ApplyLabelRegionListFilterAsync( | |
| 621 | 621 | ISqlSugarClient db, |
| 622 | 622 | ISugarQueryable<FlLabelDbEntity> query, |
| 623 | 623 | string groupId, |
| ... | ... | @@ -630,14 +630,18 @@ public static class LabelRegionScopeHelper |
| 630 | 630 | return query.Where(_ => false); |
| 631 | 631 | } |
| 632 | 632 | |
| 633 | + var scopedGroupIds = schema.HasLabelRegionTable | |
| 634 | + ? await LocationScopeBindingHelper.ResolveGroupIdsFromLocationIdsAsync(db, scopedLocationIds) | |
| 635 | + : new List<string>(); | |
| 636 | + | |
| 633 | 637 | if (!schema.HasLabelRegionTable && !schema.HasAppliedRegionTypeColumn) |
| 634 | 638 | { |
| 635 | - return ApplyLabelLocationMatchFilter(query, scopedLocationIds, schema); | |
| 639 | + return ApplyLabelLocationMatchFilter(query, scopedLocationIds, scopedGroupIds, schema); | |
| 636 | 640 | } |
| 637 | 641 | |
| 638 | 642 | if (!schema.HasLabelRegionTable) |
| 639 | 643 | { |
| 640 | - return ApplyLabelLocationMatchFilter(query, scopedLocationIds, schema); | |
| 644 | + return ApplyLabelLocationMatchFilter(query, scopedLocationIds, scopedGroupIds, schema); | |
| 641 | 645 | } |
| 642 | 646 | |
| 643 | 647 | if (!schema.HasAppliedRegionTypeColumn) |
| ... | ... | @@ -772,7 +776,7 @@ public static class LabelRegionScopeHelper |
| 772 | 776 | /// 按 scoped 门店过滤。 |
| 773 | 777 | /// <c>AppliedRegionType=ALL</c> 且无门店快照时为全局;有快照时须与 scoped 门店有交集(对齐 <see cref="LabelAppliesToLocationAsync"/>)。 |
| 774 | 778 | /// </summary> |
| 775 | - public static ISugarQueryable<FlLabelDbEntity> ApplyLabelLocationListFilter( | |
| 779 | + public static async Task<ISugarQueryable<FlLabelDbEntity>> ApplyLabelLocationListFilterAsync( | |
| 776 | 780 | ISqlSugarClient db, |
| 777 | 781 | ISugarQueryable<FlLabelDbEntity> query, |
| 778 | 782 | List<string> scopedLocationIds, |
| ... | ... | @@ -783,7 +787,11 @@ public static class LabelRegionScopeHelper |
| 783 | 787 | return ApplyLabelAllRegionOnlyFilter(db, query, schema); |
| 784 | 788 | } |
| 785 | 789 | |
| 786 | - return ApplyLabelLocationMatchFilter(query, scopedLocationIds, schema); | |
| 790 | + var scopedGroupIds = schema.HasLabelRegionTable | |
| 791 | + ? await LocationScopeBindingHelper.ResolveGroupIdsFromLocationIdsAsync(db, scopedLocationIds) | |
| 792 | + : new List<string>(); | |
| 793 | + | |
| 794 | + return ApplyLabelLocationMatchFilter(query, scopedLocationIds, scopedGroupIds, schema); | |
| 787 | 795 | } |
| 788 | 796 | |
| 789 | 797 | /// <summary> |
| ... | ... | @@ -981,58 +989,92 @@ public static class LabelRegionScopeHelper |
| 981 | 989 | private static ISugarQueryable<FlLabelDbEntity> ApplyLabelLocationMatchFilter( |
| 982 | 990 | ISugarQueryable<FlLabelDbEntity> query, |
| 983 | 991 | List<string> scopedLocationIds, |
| 992 | + IReadOnlyList<string> scopedGroupIds, | |
| 984 | 993 | LabelRegionSchemaHelper.LabelRegionSchemaStatus schema) |
| 985 | 994 | { |
| 995 | + var includeRegionMatch = schema.HasLabelRegionTable && scopedGroupIds.Count > 0; | |
| 996 | + var specifiedBranch = BuildSpecifiedLocationRegionMatchSql( | |
| 997 | + schema.HasLabelLocationTable, | |
| 998 | + includeRegionMatch); | |
| 999 | + | |
| 986 | 1000 | // 对齐 BuildAppLabelingTreeStoreFilterSql / LabelAppliesToLocationAsync: |
| 987 | 1001 | // ALL 无门店快照 → 全局;ALL 有快照 → 须命中 scoped 门店(不可无条件放行,否则跨公司泄漏)。 |
| 1002 | + // SPECIFIED 仅 fl_label_region、无门店快照(Scoped ALL)→ 按 scoped 门店所属 Region 命中。 | |
| 988 | 1003 | if (schema.HasAppliedRegionTypeColumn && schema.HasLabelLocationTable) |
| 989 | 1004 | { |
| 990 | 1005 | return query.Where( |
| 991 | - """ | |
| 992 | - ( | |
| 993 | - (AppliedRegionType = @all AND ( | |
| 994 | - ((LocationId IS NULL OR LocationId = '') | |
| 995 | - AND NOT EXISTS (SELECT 1 FROM fl_label_location ll0 WHERE ll0.LabelId = fl_label.Id)) | |
| 996 | - OR LocationId IN (@locs) | |
| 997 | - OR EXISTS (SELECT 1 FROM fl_label_location ll WHERE ll.LabelId = fl_label.Id AND ll.LocationId IN (@locs)) | |
| 998 | - )) | |
| 999 | - OR (AppliedRegionType <> @all AND ( | |
| 1000 | - LocationId IN (@locs) | |
| 1001 | - OR EXISTS (SELECT 1 FROM fl_label_location ll WHERE ll.LabelId = fl_label.Id AND ll.LocationId IN (@locs)) | |
| 1002 | - )) | |
| 1003 | - ) | |
| 1004 | - """, | |
| 1005 | - new { all = AppliedRegionAll, locs = scopedLocationIds }); | |
| 1006 | + $""" | |
| 1007 | + ( | |
| 1008 | + (AppliedRegionType = @all AND ( | |
| 1009 | + ((LocationId IS NULL OR LocationId = '') | |
| 1010 | + AND NOT EXISTS (SELECT 1 FROM fl_label_location ll0 WHERE ll0.LabelId = fl_label.Id)) | |
| 1011 | + OR LocationId IN (@locs) | |
| 1012 | + OR EXISTS (SELECT 1 FROM fl_label_location ll WHERE ll.LabelId = fl_label.Id AND ll.LocationId IN (@locs)) | |
| 1013 | + )) | |
| 1014 | + OR (AppliedRegionType <> @all AND ({specifiedBranch})) | |
| 1015 | + ) | |
| 1016 | + """, | |
| 1017 | + new | |
| 1018 | + { | |
| 1019 | + all = AppliedRegionAll, | |
| 1020 | + locs = scopedLocationIds, | |
| 1021 | + scopedGroupIds | |
| 1022 | + }); | |
| 1006 | 1023 | } |
| 1007 | 1024 | |
| 1008 | 1025 | if (schema.HasAppliedRegionTypeColumn) |
| 1009 | 1026 | { |
| 1027 | + var specifiedOnlyLocations = includeRegionMatch | |
| 1028 | + ? specifiedBranch | |
| 1029 | + : "LocationId IN (@locs)"; | |
| 1010 | 1030 | return query.Where( |
| 1011 | - """ | |
| 1012 | - ( | |
| 1013 | - (AppliedRegionType = @all AND ( | |
| 1014 | - (LocationId IS NULL OR LocationId = '') | |
| 1015 | - OR LocationId IN (@locs) | |
| 1016 | - )) | |
| 1017 | - OR (AppliedRegionType <> @all AND LocationId IN (@locs)) | |
| 1018 | - ) | |
| 1019 | - """, | |
| 1020 | - new { all = AppliedRegionAll, locs = scopedLocationIds }); | |
| 1031 | + $""" | |
| 1032 | + ( | |
| 1033 | + (AppliedRegionType = @all AND ( | |
| 1034 | + (LocationId IS NULL OR LocationId = '') | |
| 1035 | + OR LocationId IN (@locs) | |
| 1036 | + )) | |
| 1037 | + OR (AppliedRegionType <> @all AND ({specifiedOnlyLocations})) | |
| 1038 | + ) | |
| 1039 | + """, | |
| 1040 | + new | |
| 1041 | + { | |
| 1042 | + all = AppliedRegionAll, | |
| 1043 | + locs = scopedLocationIds, | |
| 1044 | + scopedGroupIds | |
| 1045 | + }); | |
| 1021 | 1046 | } |
| 1022 | 1047 | |
| 1023 | 1048 | if (schema.HasLabelLocationTable) |
| 1024 | 1049 | { |
| 1025 | 1050 | return query.Where( |
| 1026 | - """ | |
| 1027 | - (LocationId IN (@locs) | |
| 1028 | - OR EXISTS (SELECT 1 FROM fl_label_location ll WHERE ll.LabelId = fl_label.Id AND ll.LocationId IN (@locs))) | |
| 1051 | + $""" | |
| 1052 | + ({specifiedBranch}) | |
| 1029 | 1053 | """, |
| 1030 | - new { locs = scopedLocationIds }); | |
| 1054 | + new { locs = scopedLocationIds, scopedGroupIds }); | |
| 1031 | 1055 | } |
| 1032 | 1056 | |
| 1033 | 1057 | return query.Where(l => scopedLocationIds.Contains(l.LocationId)); |
| 1034 | 1058 | } |
| 1035 | 1059 | |
| 1060 | + private static string BuildSpecifiedLocationRegionMatchSql(bool hasLabelLocationTable, bool includeRegionMatch) | |
| 1061 | + { | |
| 1062 | + var parts = new List<string> { "LocationId IN (@locs)" }; | |
| 1063 | + if (hasLabelLocationTable) | |
| 1064 | + { | |
| 1065 | + parts.Add( | |
| 1066 | + "EXISTS (SELECT 1 FROM fl_label_location ll WHERE ll.LabelId = fl_label.Id AND ll.LocationId IN (@locs))"); | |
| 1067 | + } | |
| 1068 | + | |
| 1069 | + if (includeRegionMatch) | |
| 1070 | + { | |
| 1071 | + parts.Add( | |
| 1072 | + "EXISTS (SELECT 1 FROM fl_label_region lr WHERE lr.LabelId = fl_label.Id AND lr.GroupId IN (@scopedGroupIds))"); | |
| 1073 | + } | |
| 1074 | + | |
| 1075 | + return string.Join(" OR ", parts); | |
| 1076 | + } | |
| 1077 | + | |
| 1036 | 1078 | private static List<string> MergeExplicitLocationIds(string? locationId, IReadOnlyList<string>? locationIds) |
| 1037 | 1079 | { |
| 1038 | 1080 | var merged = LocationScopeBindingHelper.NormalizeIds(locationIds); | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/LabelTemplateManagePermissionHelper.cs
| ... | ... | @@ -61,7 +61,8 @@ public static class LabelTemplateManagePermissionHelper |
| 61 | 61 | creatorPartners, |
| 62 | 62 | template.CreatorId, |
| 63 | 63 | viewerTier, |
| 64 | - creatorTier); | |
| 64 | + creatorTier, | |
| 65 | + allowSelfCreatedWithoutConcretePartner: true); | |
| 65 | 66 | } |
| 66 | 67 | |
| 67 | 68 | /// <summary> |
| ... | ... | @@ -103,23 +104,37 @@ public static class LabelTemplateManagePermissionHelper |
| 103 | 104 | return false; |
| 104 | 105 | } |
| 105 | 106 | |
| 107 | + // 创建者角色更高(System/Company 相对 Regional/Location)→ 下级不可管。 | |
| 106 | 108 | if (IsCreatorRoleSuperiorToViewer(creatorRoleTier, viewerRoleTier)) |
| 107 | 109 | { |
| 108 | 110 | return false; |
| 109 | 111 | } |
| 110 | 112 | |
| 113 | + var creatorPartners = NormalizeIds(creatorPartnerIds); | |
| 114 | + var allowed = NormalizeIds(allowedPartnerIds); | |
| 115 | + | |
| 116 | + // 上级编辑下级创建:只要创建人落在查看者公司范围内即可。 | |
| 117 | + // 下级常把 Region/Location 设为 ALL,权限用 partnerIds 会被算成空而误拒。 | |
| 118 | + if (IsViewerRoleSuperiorToCreator(viewerRoleTier, creatorRoleTier)) | |
| 119 | + { | |
| 120 | + if (creatorPartners.Count == 0 || allowed.Count == 0) | |
| 121 | + { | |
| 122 | + return false; | |
| 123 | + } | |
| 124 | + | |
| 125 | + return creatorPartners.Any(pid => ContainsId(allowed, pid)); | |
| 126 | + } | |
| 127 | + | |
| 111 | 128 | if (!MeetsPartnerScopeForManage(partnerIds, allowedPartnerIds, allowEmptyPartnerIds: false)) |
| 112 | 129 | { |
| 113 | 130 | return false; |
| 114 | 131 | } |
| 115 | 132 | |
| 116 | - var creatorPartners = NormalizeIds(creatorPartnerIds); | |
| 117 | 133 | if (creatorPartners.Count == 0) |
| 118 | 134 | { |
| 119 | 135 | return false; |
| 120 | 136 | } |
| 121 | 137 | |
| 122 | - var allowed = NormalizeIds(allowedPartnerIds); | |
| 123 | 138 | return creatorPartners.Any(pid => ContainsId(allowed, pid)); |
| 124 | 139 | } |
| 125 | 140 | |
| ... | ... | @@ -161,6 +176,19 @@ public static class LabelTemplateManagePermissionHelper |
| 161 | 176 | return creatorRoleTier.Value < viewerRoleTier.Value; |
| 162 | 177 | } |
| 163 | 178 | |
| 179 | + /// <summary>查看者层级更高(数值更小)于创建者 → 上级可管下级创建的项目。</summary> | |
| 180 | + private static bool IsViewerRoleSuperiorToCreator( | |
| 181 | + TeamMemberListAccessHelper.MemberRoleTier? viewerRoleTier, | |
| 182 | + TeamMemberListAccessHelper.MemberRoleTier? creatorRoleTier) | |
| 183 | + { | |
| 184 | + if (creatorRoleTier is null || viewerRoleTier is null) | |
| 185 | + { | |
| 186 | + return false; | |
| 187 | + } | |
| 188 | + | |
| 189 | + return viewerRoleTier.Value < creatorRoleTier.Value; | |
| 190 | + } | |
| 191 | + | |
| 164 | 192 | private static bool MeetsPartnerScopeForManage( |
| 165 | 193 | IReadOnlyList<string>? partnerIds, |
| 166 | 194 | IReadOnlyList<string>? allowedPartnerIds, |
| ... | ... | @@ -197,9 +225,12 @@ public static class LabelTemplateManagePermissionHelper |
| 197 | 225 | ISqlSugarDbContext dbContext, |
| 198 | 226 | FlLabelTemplateDbEntity template) |
| 199 | 227 | { |
| 228 | + var creatorIdForManage = PartnerScopedManagePermissionHelper.ResolveCreatorIdForPermission( | |
| 229 | + template.Id, template.CreatorId, template.LastModifierId); | |
| 230 | + | |
| 200 | 231 | var createdBySystemAdmin = false; |
| 201 | - if (!string.IsNullOrWhiteSpace(template.CreatorId) && | |
| 202 | - Guid.TryParse(template.CreatorId.Trim(), out var creatorGuid)) | |
| 232 | + if (!string.IsNullOrWhiteSpace(creatorIdForManage) && | |
| 233 | + Guid.TryParse(creatorIdForManage.Trim(), out var creatorGuid)) | |
| 203 | 234 | { |
| 204 | 235 | createdBySystemAdmin = await TeamMemberRoleHelper.IsSystemAdminUserAsync( |
| 205 | 236 | dbContext.SqlSugarClient, creatorGuid); |
| ... | ... | @@ -212,14 +243,14 @@ public static class LabelTemplateManagePermissionHelper |
| 212 | 243 | var partnerIds = scope?.PartnerIds ?? new List<string>(); |
| 213 | 244 | |
| 214 | 245 | var (viewerTier, creatorTier) = await ResolveManageRoleTiersAsync( |
| 215 | - dbContext.SqlSugarClient, currentUser, template.CreatorId); | |
| 246 | + dbContext.SqlSugarClient, currentUser, creatorIdForManage); | |
| 216 | 247 | |
| 217 | 248 | var isScopedAdmin = await TeamMemberRoleHelper.IsScopedAdminUserAsync( |
| 218 | 249 | dbContext.SqlSugarClient, currentUser); |
| 219 | 250 | var partnerScope = await PartnerScopeHelper.ResolvePartnerScopeAsync(currentUser, dbContext); |
| 220 | 251 | IReadOnlyList<string>? creatorPartnerIds = null; |
| 221 | 252 | if (!createdBySystemAdmin && |
| 222 | - Guid.TryParse(template.CreatorId?.Trim(), out var ownerGuid)) | |
| 253 | + Guid.TryParse(creatorIdForManage?.Trim(), out var ownerGuid)) | |
| 223 | 254 | { |
| 224 | 255 | creatorPartnerIds = await PartnerScopeHelper.ResolvePartnerIdsForUserAsync( |
| 225 | 256 | dbContext.SqlSugarClient, ownerGuid); |
| ... | ... | @@ -232,9 +263,10 @@ public static class LabelTemplateManagePermissionHelper |
| 232 | 263 | createdBySystemAdmin, |
| 233 | 264 | partnerScope.AllowedPartnerIds, |
| 234 | 265 | creatorPartnerIds, |
| 235 | - template.CreatorId, | |
| 266 | + creatorIdForManage, | |
| 236 | 267 | viewerTier, |
| 237 | - creatorTier)) | |
| 268 | + creatorTier, | |
| 269 | + allowSelfCreatedWithoutConcretePartner: true)) | |
| 238 | 270 | { |
| 239 | 271 | return; |
| 240 | 272 | } | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/PartnerScopedManagePermissionHelper.cs
| ... | ... | @@ -29,16 +29,22 @@ public static class PartnerScopedManagePermissionHelper |
| 29 | 29 | public static async Task<ListEvaluationContext> BuildListContextAsync( |
| 30 | 30 | ICurrentUser currentUser, |
| 31 | 31 | ISqlSugarDbContext dbContext, |
| 32 | - IEnumerable<string?> creatorIds) | |
| 32 | + IEnumerable<string?> creatorIds, | |
| 33 | + IEnumerable<string?>? fallbackUserIds = null) | |
| 33 | 34 | { |
| 35 | + // 合并 CreatorId + LastModifierId:脏数据 CreatorId=实体Id 时,矫正后仍能命中公司/层级 map。 | |
| 36 | + var allUserIds = (creatorIds ?? Array.Empty<string?>()) | |
| 37 | + .Concat(fallbackUserIds ?? Array.Empty<string?>()) | |
| 38 | + .ToList(); | |
| 39 | + | |
| 34 | 40 | var isScopedAdmin = await TeamMemberRoleHelper.IsScopedAdminUserAsync( |
| 35 | 41 | dbContext.SqlSugarClient, currentUser); |
| 36 | 42 | var currentPartnerScope = await PartnerScopeHelper.ResolvePartnerScopeAsync(currentUser, dbContext); |
| 37 | 43 | var systemAdminCreatorIds = await TeamMemberRoleHelper.ResolveSystemAdminCreatorIdSetAsync( |
| 38 | - dbContext.SqlSugarClient, creatorIds); | |
| 44 | + dbContext.SqlSugarClient, allUserIds); | |
| 39 | 45 | |
| 40 | 46 | var creatorPartnerMap = new Dictionary<Guid, IReadOnlyList<string>>(); |
| 41 | - foreach (var creatorId in creatorIds | |
| 47 | + foreach (var creatorId in allUserIds | |
| 42 | 48 | .Select(id => Guid.TryParse(id?.Trim(), out var g) ? g : (Guid?)null) |
| 43 | 49 | .Where(g => g.HasValue) |
| 44 | 50 | .Select(g => g!.Value) |
| ... | ... | @@ -53,7 +59,7 @@ public static class PartnerScopedManagePermissionHelper |
| 53 | 59 | dbContext.SqlSugarClient, creatorId); |
| 54 | 60 | } |
| 55 | 61 | |
| 56 | - var creatorGuids = creatorIds | |
| 62 | + var creatorGuids = allUserIds | |
| 57 | 63 | .Select(id => Guid.TryParse(id?.Trim(), out var g) ? g : (Guid?)null) |
| 58 | 64 | .Where(g => g.HasValue) |
| 59 | 65 | .Select(g => g!.Value) |
| ... | ... | @@ -80,12 +86,44 @@ public static class PartnerScopedManagePermissionHelper |
| 80 | 86 | }; |
| 81 | 87 | } |
| 82 | 88 | |
| 89 | + /// <summary> | |
| 90 | + /// 当库里误将 <c>CreatorId</c> 写成实体主键时,用 <c>LastModifierId</c> 参与权限判定(常见于历史脏数据)。 | |
| 91 | + /// </summary> | |
| 92 | + public static string? ResolveCreatorIdForPermission( | |
| 93 | + string? entityId, | |
| 94 | + string? creatorId, | |
| 95 | + string? lastModifierId) | |
| 96 | + { | |
| 97 | + if (string.IsNullOrWhiteSpace(creatorId)) | |
| 98 | + { | |
| 99 | + return creatorId; | |
| 100 | + } | |
| 101 | + | |
| 102 | + var normalizedCreator = creatorId.Trim(); | |
| 103 | + if (string.IsNullOrWhiteSpace(entityId) | |
| 104 | + || !string.Equals(normalizedCreator, entityId.Trim(), StringComparison.OrdinalIgnoreCase)) | |
| 105 | + { | |
| 106 | + return normalizedCreator; | |
| 107 | + } | |
| 108 | + | |
| 109 | + if (!string.IsNullOrWhiteSpace(lastModifierId) | |
| 110 | + && !string.Equals(lastModifierId.Trim(), entityId.Trim(), StringComparison.OrdinalIgnoreCase)) | |
| 111 | + { | |
| 112 | + return lastModifierId.Trim(); | |
| 113 | + } | |
| 114 | + | |
| 115 | + return normalizedCreator; | |
| 116 | + } | |
| 117 | + | |
| 83 | 118 | public static bool EvaluateCanManage( |
| 84 | 119 | ICurrentUser currentUser, |
| 85 | 120 | ListEvaluationContext context, |
| 86 | 121 | string? creatorId, |
| 87 | - IReadOnlyList<string>? partnerIds) | |
| 122 | + IReadOnlyList<string>? partnerIds, | |
| 123 | + string? entityId = null, | |
| 124 | + string? lastModifierId = null) | |
| 88 | 125 | { |
| 126 | + creatorId = ResolveCreatorIdForPermission(entityId, creatorId, lastModifierId); | |
| 89 | 127 | var createdBySystemAdmin = |
| 90 | 128 | Guid.TryParse(creatorId?.Trim(), out var creatorGuid) && |
| 91 | 129 | context.SystemAdminCreatorIds.Contains(creatorGuid); |
| ... | ... | @@ -198,10 +236,15 @@ public static class PartnerScopedManagePermissionHelper |
| 198 | 236 | locationIdsForEcho: null, |
| 199 | 237 | partnerIdsForEcho); |
| 200 | 238 | |
| 239 | + var creatorForManage = ResolveCreatorIdForPermission( | |
| 240 | + entityId, | |
| 241 | + entityScope.CreatorId ?? creatorId, | |
| 242 | + entityScope.LastModifierId); | |
| 243 | + | |
| 201 | 244 | await EnsureCanManageAsync( |
| 202 | 245 | currentUser, |
| 203 | 246 | dbContext, |
| 204 | - entityScope.CreatorId ?? creatorId, | |
| 247 | + creatorForManage, | |
| 205 | 248 | partnerIdsForManage, |
| 206 | 249 | unauthorizedMessage); |
| 207 | 250 | } |
| ... | ... | @@ -322,6 +365,8 @@ public static class PartnerScopedManagePermissionHelper |
| 322 | 365 | public string AppliedPartnerType { get; init; } = AllScopeBindingHelper.ScopeAll; |
| 323 | 366 | |
| 324 | 367 | public string? CreatorId { get; init; } |
| 368 | + | |
| 369 | + public string? LastModifierId { get; init; } | |
| 325 | 370 | } |
| 326 | 371 | |
| 327 | 372 | private static async Task<PartnerScopedEntityScopeRow?> LoadPartnerScopedEntityScopeAsync( |
| ... | ... | @@ -339,7 +384,8 @@ public static class PartnerScopedManagePermissionHelper |
| 339 | 384 | AvailabilityType = x.AvailabilityType, |
| 340 | 385 | AppliedRegionType = x.AppliedRegionType, |
| 341 | 386 | AppliedPartnerType = x.AppliedPartnerType, |
| 342 | - CreatorId = x.CreatorId | |
| 387 | + CreatorId = x.CreatorId, | |
| 388 | + LastModifierId = x.LastModifierId | |
| 343 | 389 | }) |
| 344 | 390 | .FirstAsync(); |
| 345 | 391 | return productCategory; |
| ... | ... | @@ -351,7 +397,8 @@ public static class PartnerScopedManagePermissionHelper |
| 351 | 397 | AvailabilityType = x.AvailabilityType, |
| 352 | 398 | AppliedRegionType = x.AppliedRegionType, |
| 353 | 399 | AppliedPartnerType = x.AppliedPartnerType, |
| 354 | - CreatorId = x.CreatorId | |
| 400 | + CreatorId = x.CreatorId, | |
| 401 | + LastModifierId = x.LastModifierId | |
| 355 | 402 | }) |
| 356 | 403 | .FirstAsync(); |
| 357 | 404 | return labelCategory; |
| ... | ... | @@ -363,10 +410,24 @@ public static class PartnerScopedManagePermissionHelper |
| 363 | 410 | AvailabilityType = x.AvailabilityType, |
| 364 | 411 | AppliedRegionType = x.AppliedRegionType, |
| 365 | 412 | AppliedPartnerType = x.AppliedPartnerType, |
| 366 | - CreatorId = x.CreatorId | |
| 413 | + CreatorId = x.CreatorId, | |
| 414 | + LastModifierId = x.LastModifierId | |
| 367 | 415 | }) |
| 368 | 416 | .FirstAsync(); |
| 369 | 417 | return labelType; |
| 418 | + case LabelEntityPartnerScopeHelper.LabelEntityPartnerKind.MultipleOption: | |
| 419 | + var multipleOption = await db.Queryable<FlLabelMultipleOptionDbEntity>() | |
| 420 | + .Where(x => x.Id == entityId && !x.IsDeleted) | |
| 421 | + .Select(x => new PartnerScopedEntityScopeRow | |
| 422 | + { | |
| 423 | + AvailabilityType = x.AvailabilityType, | |
| 424 | + AppliedRegionType = x.AppliedRegionType, | |
| 425 | + AppliedPartnerType = x.AppliedPartnerType, | |
| 426 | + CreatorId = x.CreatorId, | |
| 427 | + LastModifierId = x.LastModifierId | |
| 428 | + }) | |
| 429 | + .FirstAsync(); | |
| 430 | + return multipleOption; | |
| 370 | 431 | default: |
| 371 | 432 | return null; |
| 372 | 433 | } |
| ... | ... | @@ -394,6 +455,11 @@ public static class PartnerScopedManagePermissionHelper |
| 394 | 455 | .Where(x => x.LabelTypeId == entityId) |
| 395 | 456 | .Select(x => x.LocationId) |
| 396 | 457 | .ToListAsync(); |
| 458 | + case LabelEntityPartnerScopeHelper.LabelEntityPartnerKind.MultipleOption: | |
| 459 | + return await db.Queryable<FlLabelMultipleOptionLocationDbEntity>() | |
| 460 | + .Where(x => x.MultipleOptionId == entityId) | |
| 461 | + .Select(x => x.LocationId) | |
| 462 | + .ToListAsync(); | |
| 397 | 463 | default: |
| 398 | 464 | return new List<string>(); |
| 399 | 465 | } | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/ProductCategoryImportHelper.cs
0 → 100644
| 1 | +using FoodLabeling.Application.Services.DbModels; | |
| 2 | +using SqlSugar; | |
| 3 | + | |
| 4 | +namespace FoodLabeling.Application.Helpers; | |
| 5 | + | |
| 6 | +/// <summary> | |
| 7 | +/// 产品批量导入:分类解析与空 <c>CategoryCode</c> 回填(产品仅落库 <c>CategoryId</c>)。 | |
| 8 | +/// </summary> | |
| 9 | +public static class ProductCategoryImportHelper | |
| 10 | +{ | |
| 11 | + /// <summary> | |
| 12 | + /// 分类 <c>CategoryCode</c> 为空时,按主键生成稳定编码,避免列表/报表仅展示空编码而关联实际已通过 Id 建立。 | |
| 13 | + /// </summary> | |
| 14 | + public static async Task EnsureImportableCategoryCodeAsync(ISqlSugarClient db, FlProductCategoryDbEntity category) | |
| 15 | + { | |
| 16 | + if (category is null || !string.IsNullOrWhiteSpace(category.CategoryCode)) | |
| 17 | + { | |
| 18 | + return; | |
| 19 | + } | |
| 20 | + | |
| 21 | + var code = BuildStableCategoryCode(category.Id); | |
| 22 | + var exists = await db.Queryable<FlProductCategoryDbEntity>() | |
| 23 | + .AnyAsync(x => !x.IsDeleted && x.Id != category.Id && x.CategoryCode == code); | |
| 24 | + if (exists) | |
| 25 | + { | |
| 26 | + code = $"CAT_{category.Id.Replace("-", string.Empty, StringComparison.Ordinal).ToLowerInvariant()}"; | |
| 27 | + } | |
| 28 | + | |
| 29 | + await db.Updateable<FlProductCategoryDbEntity>() | |
| 30 | + .SetColumns(x => new FlProductCategoryDbEntity { CategoryCode = code }) | |
| 31 | + .Where(x => x.Id == category.Id) | |
| 32 | + .ExecuteCommandAsync(); | |
| 33 | + category.CategoryCode = code; | |
| 34 | + } | |
| 35 | + | |
| 36 | + private static string BuildStableCategoryCode(string categoryId) | |
| 37 | + { | |
| 38 | + var compact = categoryId.Replace("-", string.Empty, StringComparison.Ordinal).ToLowerInvariant(); | |
| 39 | + var suffix = compact.Length <= 16 ? compact : compact[..16]; | |
| 40 | + return $"CAT_{suffix}"; | |
| 41 | + } | |
| 42 | +} | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/ProductCategoryNameMatchHelper.cs
| ... | ... | @@ -5,7 +5,7 @@ using SqlSugar; |
| 5 | 5 | namespace FoodLabeling.Application.Helpers; |
| 6 | 6 | |
| 7 | 7 | /// <summary> |
| 8 | -/// 产品分类名称匹配(导入 Excel 与库中简繁/空格差异)。 | |
| 8 | +/// 产品分类名称匹配(导入 Excel 与库中简繁/空格差异;支持名称、编码、展示文案)。 | |
| 9 | 9 | /// </summary> |
| 10 | 10 | public static class ProductCategoryNameMatchHelper |
| 11 | 11 | { |
| ... | ... | @@ -20,15 +20,11 @@ public static class ProductCategoryNameMatchHelper |
| 20 | 20 | return new List<FlProductCategoryDbEntity>(); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - var categories = await db.Queryable<FlProductCategoryDbEntity>() | |
| 24 | - .Where(x => !x.IsDeleted) | |
| 25 | - .ToListAsync(); | |
| 26 | - | |
| 27 | - categories = await NarrowCategoriesByPartnerAsync(db, categories, partnerId); | |
| 28 | - | |
| 23 | + var categories = await LoadPartnerScopedCategoriesAsync(db, partnerId); | |
| 29 | 24 | var key = NormalizeNameKey(n); |
| 25 | + | |
| 30 | 26 | var exact = categories |
| 31 | - .Where(c => NormalizeNameKey(c.CategoryName) == key) | |
| 27 | + .Where(c => MatchesImportCategoryCell(c, key, n)) | |
| 32 | 28 | .ToList(); |
| 33 | 29 | if (exact.Count > 0) |
| 34 | 30 | { |
| ... | ... | @@ -47,28 +43,46 @@ public static class ProductCategoryNameMatchHelper |
| 47 | 43 | .ToList(); |
| 48 | 44 | } |
| 49 | 45 | |
| 50 | - private static async Task<List<FlProductCategoryDbEntity>> NarrowCategoriesByPartnerAsync( | |
| 46 | + private static async Task<List<FlProductCategoryDbEntity>> LoadPartnerScopedCategoriesAsync( | |
| 51 | 47 | ISqlSugarClient db, |
| 52 | - List<FlProductCategoryDbEntity> categories, | |
| 53 | 48 | string? partnerId) |
| 54 | 49 | { |
| 55 | - var pid = partnerId?.Trim(); | |
| 56 | - if (string.IsNullOrWhiteSpace(pid)) | |
| 50 | + var query = db.Queryable<FlProductCategoryDbEntity>().Where(x => !x.IsDeleted); | |
| 51 | + var scopedPartnerIds = await LabelEntityPartnerScopeHelper.ResolveScopedPartnerIdsForListAsync(db, partnerId); | |
| 52 | + if (scopedPartnerIds is not null) | |
| 57 | 53 | { |
| 58 | - return categories; | |
| 54 | + query = await LabelEntityPartnerScopeHelper.ApplyProductCategoryPartnerListFilterAsync( | |
| 55 | + db, query, scopedPartnerIds); | |
| 59 | 56 | } |
| 60 | 57 | |
| 61 | - var linkedIds = await db.Queryable<FlProductCategoryPartnerDbEntity>() | |
| 62 | - .Where(x => x.PartnerId == pid) | |
| 63 | - .Select(x => x.CategoryId) | |
| 64 | - .ToListAsync(); | |
| 65 | - var linkedSet = new HashSet<string>(linkedIds, StringComparer.OrdinalIgnoreCase); | |
| 58 | + return await query.ToListAsync(); | |
| 59 | + } | |
| 66 | 60 | |
| 67 | - return categories | |
| 68 | - .Where(c => | |
| 69 | - AllScopeBindingHelper.IsDeclaredAll(c.AppliedPartnerType) | |
| 70 | - || linkedSet.Contains(c.Id)) | |
| 71 | - .ToList(); | |
| 61 | + private static bool MatchesImportCategoryCell(FlProductCategoryDbEntity category, string normalizedKey, string rawCell) | |
| 62 | + { | |
| 63 | + if (NormalizeNameKey(category.CategoryName) == normalizedKey) | |
| 64 | + { | |
| 65 | + return true; | |
| 66 | + } | |
| 67 | + | |
| 68 | + if (!string.IsNullOrWhiteSpace(category.DisplayText) | |
| 69 | + && NormalizeNameKey(category.DisplayText) == normalizedKey) | |
| 70 | + { | |
| 71 | + return true; | |
| 72 | + } | |
| 73 | + | |
| 74 | + var code = category.CategoryCode?.Trim(); | |
| 75 | + if (string.IsNullOrWhiteSpace(code)) | |
| 76 | + { | |
| 77 | + return false; | |
| 78 | + } | |
| 79 | + | |
| 80 | + if (NormalizeNameKey(code) == normalizedKey) | |
| 81 | + { | |
| 82 | + return true; | |
| 83 | + } | |
| 84 | + | |
| 85 | + return string.Equals(code, rawCell.Trim(), StringComparison.OrdinalIgnoreCase); | |
| 72 | 86 | } |
| 73 | 87 | |
| 74 | 88 | private static string NormalizeNameKey(string? raw) | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/TeamMemberListAccessHelper.cs
| ... | ... | @@ -45,6 +45,11 @@ public static class TeamMemberListAccessHelper |
| 45 | 45 | return MemberRoleTier.Staff; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | + if (await TeamMemberRoleHelper.IsSystemAdminUserAsync(db, currentUser.Id.Value)) | |
| 49 | + { | |
| 50 | + return null; | |
| 51 | + } | |
| 52 | + | |
| 48 | 53 | if (await TeamMemberRoleHelper.IsCompanyAdminUserAsync(db, currentUser)) |
| 49 | 54 | { |
| 50 | 55 | return MemberRoleTier.System; |
| ... | ... | @@ -56,8 +61,8 @@ public static class TeamMemberListAccessHelper |
| 56 | 61 | return MemberRoleTier.Company; |
| 57 | 62 | } |
| 58 | 63 | |
| 59 | - // Location Admin:仅可见比 Regional 更低的角色(不含 Regional / Company / System)。 | |
| 60 | - if (await TeamMemberRoleHelper.IsLocationAdminUserAsync(db, currentUser)) | |
| 64 | + // Location Admin / Location Manager:仅可见比 Regional 更低的角色。 | |
| 65 | + if (await TeamMemberRoleHelper.IsLocationTierUserAsync(db, currentUser)) | |
| 61 | 66 | { |
| 62 | 67 | return MemberRoleTier.Regional; |
| 63 | 68 | } |
| ... | ... | @@ -218,7 +223,9 @@ public static class TeamMemberListAccessHelper |
| 218 | 223 | |
| 219 | 224 | if (roleRows.Any(r => |
| 220 | 225 | TeamMemberRoleHelper.IsLocationAdminRoleToken(r.RoleName) |
| 221 | - || TeamMemberRoleHelper.IsLocationAdminRoleToken(r.RoleCode))) | |
| 226 | + || TeamMemberRoleHelper.IsLocationAdminRoleToken(r.RoleCode) | |
| 227 | + || TeamMemberRoleHelper.IsLocationManagerRoleToken(r.RoleName) | |
| 228 | + || TeamMemberRoleHelper.IsLocationManagerRoleToken(r.RoleCode))) | |
| 222 | 229 | { |
| 223 | 230 | return MemberRoleTier.Location; |
| 224 | 231 | } | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/TeamMemberListScopeHelper.cs
| ... | ... | @@ -75,7 +75,7 @@ public static class TeamMemberListScopeHelper |
| 75 | 75 | var isRegionalOrLocationAdmin = |
| 76 | 76 | currentUser.Id.HasValue && |
| 77 | 77 | (await TeamMemberRoleHelper.IsRegionalAdminUserAsync(db, currentUser) |
| 78 | - || await TeamMemberRoleHelper.IsLocationAdminUserAsync(db, currentUser)); | |
| 78 | + || await TeamMemberRoleHelper.IsLocationTierUserAsync(db, currentUser)); | |
| 79 | 79 | |
| 80 | 80 | var allowedLocIds = isRegionalOrLocationAdmin |
| 81 | 81 | ? await LocationScopeBindingHelper.ResolveAppAccessibleLocationIdsForUserAsync( | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/TeamMemberRoleHelper.cs
| ... | ... | @@ -70,7 +70,8 @@ public static class TeamMemberRoleHelper |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | if (IsCompanyAdminRoleName(roleName) || IsCompanyAdminRoleToken(roleName) |
| 73 | - || IsRegionalAdminRoleToken(roleName) || IsLocationAdminRoleToken(roleName)) | |
| 73 | + || IsRegionalAdminRoleToken(roleName) || IsLocationAdminRoleToken(roleName) | |
| 74 | + || IsLocationManagerRoleToken(roleName)) | |
| 74 | 75 | { |
| 75 | 76 | return false; |
| 76 | 77 | } |
| ... | ... | @@ -202,6 +203,54 @@ public static class TeamMemberRoleHelper |
| 202 | 203 | || string.Equals(name, FoodLabelingRoleCodes.LocationAdmin, StringComparison.OrdinalIgnoreCase); |
| 203 | 204 | } |
| 204 | 205 | |
| 206 | + /// <summary> | |
| 207 | + /// RoleCode / RoleName 是否 Location Manager(库内常见 RoleCode <c>Manager</c>,与 Location Admin 数据范围同级)。 | |
| 208 | + /// </summary> | |
| 209 | + public static bool IsLocationManagerRoleToken(string? value) | |
| 210 | + { | |
| 211 | + if (string.IsNullOrWhiteSpace(value) || IsCompanyAdminRoleToken(value) || IsLocationAdminRoleToken(value)) | |
| 212 | + { | |
| 213 | + return false; | |
| 214 | + } | |
| 215 | + | |
| 216 | + var key = NormalizeRoleKey(value); | |
| 217 | + if (key == "manager" || key.Contains("locationmanager", StringComparison.Ordinal)) | |
| 218 | + { | |
| 219 | + return true; | |
| 220 | + } | |
| 221 | + | |
| 222 | + var name = value.Trim(); | |
| 223 | + return string.Equals(name, "Location Manager", StringComparison.OrdinalIgnoreCase); | |
| 224 | + } | |
| 225 | + | |
| 226 | + /// <summary>Location Admin 或 Location Manager(Team Member 列表可见性 / 门店范围,不含保存时 Location Admin 专属规则)。</summary> | |
| 227 | + public static bool IsLocationTierRoleToken(string? value) => | |
| 228 | + IsLocationAdminRoleToken(value) || IsLocationManagerRoleToken(value); | |
| 229 | + | |
| 230 | + /// <summary>当前用户是否为 Location Admin / Location Manager(JWT + 库内 Role)。</summary> | |
| 231 | + public static async Task<bool> IsLocationTierUserAsync(ISqlSugarClient db, ICurrentUser currentUser) | |
| 232 | + { | |
| 233 | + if (currentUser.Id is null) | |
| 234 | + { | |
| 235 | + return false; | |
| 236 | + } | |
| 237 | + | |
| 238 | + if (IsRoleFromClaims(currentUser, IsLocationTierRoleToken)) | |
| 239 | + { | |
| 240 | + return true; | |
| 241 | + } | |
| 242 | + | |
| 243 | + return await IsLocationTierUserAsync(db, currentUser.Id.Value); | |
| 244 | + } | |
| 245 | + | |
| 246 | + /// <summary>指定用户是否为 Location Admin / Location Manager。</summary> | |
| 247 | + public static async Task<bool> IsLocationTierUserAsync(ISqlSugarClient db, Guid userId) | |
| 248 | + { | |
| 249 | + var roleRows = await LoadUserRoleRowsAsync(db, userId); | |
| 250 | + return roleRows.Any(r => | |
| 251 | + IsLocationTierRoleToken(r.RoleName) || IsLocationTierRoleToken(r.RoleCode)); | |
| 252 | + } | |
| 253 | + | |
| 205 | 254 | public static async Task<bool> IsRegionalAdminUserAsync(ISqlSugarClient db, ICurrentUser currentUser) |
| 206 | 255 | { |
| 207 | 256 | if (currentUser.Id is null) |
| ... | ... | @@ -251,12 +300,12 @@ public static class TeamMemberRoleHelper |
| 251 | 300 | await RoleMatchesAsync(db, roleId, IsLocationAdminRoleToken); |
| 252 | 301 | |
| 253 | 302 | /// <summary> |
| 254 | - /// Company / Regional / Location Admin(功能权限可与 Company Admin 对齐,差异在数据范围)。 | |
| 303 | + /// Company / Regional / Location Admin / Location Manager(功能权限可与 Company Admin 对齐,差异在数据范围)。 | |
| 255 | 304 | /// </summary> |
| 256 | 305 | public static async Task<bool> IsScopedAdminUserAsync(ISqlSugarClient db, ICurrentUser currentUser) => |
| 257 | 306 | await IsCompanyAdminUserAsync(db, currentUser) |
| 258 | 307 | || await IsRegionalAdminUserAsync(db, currentUser) |
| 259 | - || await IsLocationAdminUserAsync(db, currentUser); | |
| 308 | + || await IsLocationTierUserAsync(db, currentUser); | |
| 260 | 309 | |
| 261 | 310 | /// <summary> |
| 262 | 311 | /// 解析会话 scopeLevel:system > company > regional > location > staff。 |
| ... | ... | @@ -279,7 +328,7 @@ public static class TeamMemberRoleHelper |
| 279 | 328 | return OrgScopeLevels.Regional; |
| 280 | 329 | } |
| 281 | 330 | |
| 282 | - if (await IsLocationAdminUserAsync(db, currentUser)) | |
| 331 | + if (await IsLocationTierUserAsync(db, currentUser)) | |
| 283 | 332 | { |
| 284 | 333 | return OrgScopeLevels.Location; |
| 285 | 334 | } | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/LabelAppService.cs
| ... | ... | @@ -88,12 +88,12 @@ public class LabelAppService : ApplicationService, ILabelAppService |
| 88 | 88 | } |
| 89 | 89 | else if (!string.IsNullOrWhiteSpace(filterGroupId)) |
| 90 | 90 | { |
| 91 | - labelIdsQuery = LabelRegionScopeHelper.ApplyLabelRegionListFilter( | |
| 91 | + labelIdsQuery = await LabelRegionScopeHelper.ApplyLabelRegionListFilterAsync( | |
| 92 | 92 | db, labelIdsQuery, filterGroupId, scopedLocationIds, regionSchema); |
| 93 | 93 | } |
| 94 | 94 | else |
| 95 | 95 | { |
| 96 | - labelIdsQuery = LabelRegionScopeHelper.ApplyLabelLocationListFilter( | |
| 96 | + labelIdsQuery = await LabelRegionScopeHelper.ApplyLabelLocationListFilterAsync( | |
| 97 | 97 | db, labelIdsQuery, scopedLocationIds, regionSchema); |
| 98 | 98 | } |
| 99 | 99 | } |
| ... | ... | @@ -111,7 +111,7 @@ public class LabelAppService : ApplicationService, ILabelAppService |
| 111 | 111 | } |
| 112 | 112 | else |
| 113 | 113 | { |
| 114 | - labelIdsQuery = LabelRegionScopeHelper.ApplyLabelLocationListFilter( | |
| 114 | + labelIdsQuery = await LabelRegionScopeHelper.ApplyLabelLocationListFilterAsync( | |
| 115 | 115 | db, labelIdsQuery, scopedLocationIds, regionSchema); |
| 116 | 116 | } |
| 117 | 117 | } |
| ... | ... | @@ -187,6 +187,7 @@ public class LabelAppService : ApplicationService, ILabelAppService |
| 187 | 187 | l.LocationId, |
| 188 | 188 | l.PartnerId, |
| 189 | 189 | l.CreatorId, |
| 190 | + l.LastModifierId, | |
| 190 | 191 | LabelCategoryName = c.CategoryName, |
| 191 | 192 | LabelTypeName = t.TypeName, |
| 192 | 193 | TemplateName = tpl.TemplateName, |
| ... | ... | @@ -259,7 +260,10 @@ public class LabelAppService : ApplicationService, ILabelAppService |
| 259 | 260 | }); |
| 260 | 261 | |
| 261 | 262 | var manageCtx = await PartnerScopedManagePermissionHelper.BuildListContextAsync( |
| 262 | - CurrentUser, _dbContext, orderedLabels.Select(x => x.CreatorId)); | |
| 263 | + CurrentUser, | |
| 264 | + _dbContext, | |
| 265 | + orderedLabels.Select(x => x.CreatorId), | |
| 266 | + orderedLabels.Select(x => x.LastModifierId)); | |
| 263 | 267 | |
| 264 | 268 | var items = new List<LabelGetListOutputDto>(orderedLabels.Count); |
| 265 | 269 | foreach (var x in orderedLabels) |
| ... | ... | @@ -287,7 +291,12 @@ public class LabelAppService : ApplicationService, ILabelAppService |
| 287 | 291 | Guid.TryParse(x.CreatorId?.Trim(), out var creatorGuid) && |
| 288 | 292 | manageCtx.SystemAdminCreatorIds.Contains(creatorGuid); |
| 289 | 293 | var canManage = PartnerScopedManagePermissionHelper.EvaluateCanManage( |
| 290 | - CurrentUser, manageCtx, x.CreatorId, partnerIdsForManage); | |
| 294 | + CurrentUser, | |
| 295 | + manageCtx, | |
| 296 | + x.CreatorId, | |
| 297 | + partnerIdsForManage, | |
| 298 | + x.Id, | |
| 299 | + x.LastModifierId); | |
| 291 | 300 | items.Add(new LabelGetListOutputDto |
| 292 | 301 | { |
| 293 | 302 | Id = x.LabelCode ?? string.Empty, | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/LabelCategoryAppService.cs
| ... | ... | @@ -77,7 +77,7 @@ public class LabelCategoryAppService : ApplicationService, ILabelCategoryAppServ |
| 77 | 77 | ids); |
| 78 | 78 | var scopeMap = await BuildCategoryScopeMapAsync(entities, partnerScopeMap); |
| 79 | 79 | var manageCtx = await PartnerScopedManagePermissionHelper.BuildListContextAsync( |
| 80 | - CurrentUser, _dbContext, entities.Select(x => x.CreatorId)); | |
| 80 | + CurrentUser, _dbContext, entities.Select(x => x.CreatorId), entities.Select(x => x.LastModifierId)); | |
| 81 | 81 | |
| 82 | 82 | var items = entities.Select(x => |
| 83 | 83 | { |
| ... | ... | @@ -97,7 +97,12 @@ public class LabelCategoryAppService : ApplicationService, ILabelCategoryAppServ |
| 97 | 97 | Guid.TryParse(x.CreatorId?.Trim(), out var creatorGuid) && |
| 98 | 98 | manageCtx.SystemAdminCreatorIds.Contains(creatorGuid); |
| 99 | 99 | var canManage = PartnerScopedManagePermissionHelper.EvaluateCanManage( |
| 100 | - CurrentUser, manageCtx, x.CreatorId, partnerIdsForManage); | |
| 100 | + CurrentUser, | |
| 101 | + manageCtx, | |
| 102 | + x.CreatorId, | |
| 103 | + partnerIdsForManage, | |
| 104 | + x.Id, | |
| 105 | + x.LastModifierId); | |
| 101 | 106 | return new LabelCategoryGetListOutputDto |
| 102 | 107 | { |
| 103 | 108 | Id = x.Id, | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/LabelMultipleOptionAppService.cs
| ... | ... | @@ -66,9 +66,8 @@ public class LabelMultipleOptionAppService : ApplicationService, ILabelMultipleO |
| 66 | 66 | LabelEntityPartnerScopeHelper.LabelEntityPartnerKind.MultipleOption, |
| 67 | 67 | entities.Select(x => x.Id).ToList()); |
| 68 | 68 | |
| 69 | - var systemAdminCreatorIds = await TeamMemberRoleHelper.ResolveSystemAdminCreatorIdSetAsync( | |
| 70 | - _dbContext.SqlSugarClient, | |
| 71 | - entities.Select(x => x.CreatorId)); | |
| 69 | + var manageCtx = await PartnerScopedManagePermissionHelper.BuildListContextAsync( | |
| 70 | + CurrentUser, _dbContext, entities.Select(x => x.CreatorId), entities.Select(x => x.LastModifierId)); | |
| 72 | 71 | |
| 73 | 72 | var items = new List<LabelMultipleOptionGetListOutputDto>(entities.Count); |
| 74 | 73 | foreach (var x in entities) |
| ... | ... | @@ -76,11 +75,23 @@ public class LabelMultipleOptionAppService : ApplicationService, ILabelMultipleO |
| 76 | 75 | scopeMap.TryGetValue(x.Id, out var scope); |
| 77 | 76 | partnerScopeMap.TryGetValue(x.Id, out var partnerScope); |
| 78 | 77 | var partnerIds = partnerScope?.PartnerIds ?? new List<string>(); |
| 78 | + var partnerIdsForManage = PartnerScopedManagePermissionHelper.ResolvePartnerScopedEntityPartnerIdsForManageFromEcho( | |
| 79 | + x.AvailabilityType, | |
| 80 | + x.AppliedRegionType, | |
| 81 | + partnerScope?.AppliedPartnerType ?? x.AppliedPartnerType, | |
| 82 | + scope?.RegionIds, | |
| 83 | + scope?.LocationIds, | |
| 84 | + partnerIds); | |
| 79 | 85 | var createdBySystemAdmin = |
| 80 | 86 | Guid.TryParse(x.CreatorId?.Trim(), out var creatorGuid) && |
| 81 | - systemAdminCreatorIds.Contains(creatorGuid); | |
| 82 | - var canManage = await LabelMultipleOptionManagePermissionHelper.CanManageAsync( | |
| 83 | - CurrentUser, _dbContext, partnerIds, createdBySystemAdmin); | |
| 87 | + manageCtx.SystemAdminCreatorIds.Contains(creatorGuid); | |
| 88 | + var canManage = PartnerScopedManagePermissionHelper.EvaluateCanManage( | |
| 89 | + CurrentUser, | |
| 90 | + manageCtx, | |
| 91 | + x.CreatorId, | |
| 92 | + partnerIdsForManage, | |
| 93 | + x.Id, | |
| 94 | + x.LastModifierId); | |
| 84 | 95 | |
| 85 | 96 | items.Add(new LabelMultipleOptionGetListOutputDto |
| 86 | 97 | { | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/LabelTemplateAppService.cs
| ... | ... | @@ -122,19 +122,21 @@ public class LabelTemplateAppService : ApplicationService, ILabelTemplateAppServ |
| 122 | 122 | ? fromElements! |
| 123 | 123 | : (itemsDisplay?.Items ?? FoodLabelingDisplayConsts.NotAvailable); |
| 124 | 124 | var partnerIds = scope?.PartnerIds ?? new List<string>(); |
| 125 | + var creatorIdForManage = PartnerScopedManagePermissionHelper.ResolveCreatorIdForPermission( | |
| 126 | + x.Id, x.CreatorId, x.LastModifierId); | |
| 125 | 127 | var createdBySystemAdmin = |
| 126 | - Guid.TryParse(x.CreatorId?.Trim(), out var creatorGuid) && | |
| 128 | + Guid.TryParse(creatorIdForManage?.Trim(), out var creatorGuid) && | |
| 127 | 129 | systemAdminCreatorIds.Contains(creatorGuid); |
| 128 | 130 | IReadOnlyList<string>? creatorPartnerIds = null; |
| 129 | 131 | if (!createdBySystemAdmin && |
| 130 | - Guid.TryParse(x.CreatorId?.Trim(), out var ownerGuid) && | |
| 132 | + Guid.TryParse(creatorIdForManage?.Trim(), out var ownerGuid) && | |
| 131 | 133 | creatorPartnerMap.TryGetValue(ownerGuid, out var mappedCreatorPartners)) |
| 132 | 134 | { |
| 133 | 135 | creatorPartnerIds = mappedCreatorPartners; |
| 134 | 136 | } |
| 135 | 137 | |
| 136 | 138 | TeamMemberListAccessHelper.MemberRoleTier? creatorRoleTier = null; |
| 137 | - if (Guid.TryParse(x.CreatorId?.Trim(), out var parsedCreatorGuid) | |
| 139 | + if (Guid.TryParse(creatorIdForManage?.Trim(), out var parsedCreatorGuid) | |
| 138 | 140 | && creatorRoleTierMap.TryGetValue(parsedCreatorGuid, out var mappedCreatorTier)) |
| 139 | 141 | { |
| 140 | 142 | creatorRoleTier = mappedCreatorTier; |
| ... | ... | @@ -147,9 +149,10 @@ public class LabelTemplateAppService : ApplicationService, ILabelTemplateAppServ |
| 147 | 149 | createdBySystemAdmin, |
| 148 | 150 | currentPartnerScope.AllowedPartnerIds, |
| 149 | 151 | creatorPartnerIds, |
| 150 | - x.CreatorId, | |
| 152 | + creatorIdForManage, | |
| 151 | 153 | viewerRoleTier, |
| 152 | - creatorRoleTier); | |
| 154 | + creatorRoleTier, | |
| 155 | + allowSelfCreatedWithoutConcretePartner: true); | |
| 153 | 156 | |
| 154 | 157 | items.Add(new LabelTemplateGetListOutputDto |
| 155 | 158 | { | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/LabelTypeAppService.cs
| ... | ... | @@ -74,7 +74,7 @@ public class LabelTypeAppService : ApplicationService, ILabelTypeAppService |
| 74 | 74 | LabelEntityPartnerScopeHelper.LabelEntityPartnerKind.Type, |
| 75 | 75 | ids); |
| 76 | 76 | var manageCtx = await PartnerScopedManagePermissionHelper.BuildListContextAsync( |
| 77 | - CurrentUser, _dbContext, entities.Select(x => x.CreatorId)); | |
| 77 | + CurrentUser, _dbContext, entities.Select(x => x.CreatorId), entities.Select(x => x.LastModifierId)); | |
| 78 | 78 | |
| 79 | 79 | var items = entities.Select(x => |
| 80 | 80 | { |
| ... | ... | @@ -94,7 +94,12 @@ public class LabelTypeAppService : ApplicationService, ILabelTypeAppService |
| 94 | 94 | Guid.TryParse(x.CreatorId?.Trim(), out var creatorGuid) && |
| 95 | 95 | manageCtx.SystemAdminCreatorIds.Contains(creatorGuid); |
| 96 | 96 | var canManage = PartnerScopedManagePermissionHelper.EvaluateCanManage( |
| 97 | - CurrentUser, manageCtx, x.CreatorId, partnerIdsForManage); | |
| 97 | + CurrentUser, | |
| 98 | + manageCtx, | |
| 99 | + x.CreatorId, | |
| 100 | + partnerIdsForManage, | |
| 101 | + x.Id, | |
| 102 | + x.LastModifierId); | |
| 98 | 103 | return new LabelTypeGetListOutputDto |
| 99 | 104 | { |
| 100 | 105 | Id = x.Id, |
| ... | ... | @@ -532,12 +537,12 @@ public class LabelTypeAppService : ApplicationService, ILabelTypeAppService |
| 532 | 537 | } |
| 533 | 538 | else if (!string.IsNullOrWhiteSpace(filterGroupId)) |
| 534 | 539 | { |
| 535 | - labelQuery = LabelRegionScopeHelper.ApplyLabelRegionListFilter( | |
| 540 | + labelQuery = await LabelRegionScopeHelper.ApplyLabelRegionListFilterAsync( | |
| 536 | 541 | db, labelQuery, filterGroupId, scopedLocationIds, regionSchema); |
| 537 | 542 | } |
| 538 | 543 | else |
| 539 | 544 | { |
| 540 | - labelQuery = LabelRegionScopeHelper.ApplyLabelLocationListFilter( | |
| 545 | + labelQuery = await LabelRegionScopeHelper.ApplyLabelLocationListFilterAsync( | |
| 541 | 546 | db, labelQuery, scopedLocationIds, regionSchema); |
| 542 | 547 | } |
| 543 | 548 | } |
| ... | ... | @@ -554,7 +559,7 @@ public class LabelTypeAppService : ApplicationService, ILabelTypeAppService |
| 554 | 559 | } |
| 555 | 560 | else |
| 556 | 561 | { |
| 557 | - labelQuery = LabelRegionScopeHelper.ApplyLabelLocationListFilter( | |
| 562 | + labelQuery = await LabelRegionScopeHelper.ApplyLabelLocationListFilterAsync( | |
| 558 | 563 | db, labelQuery, scopedLocationIds, regionSchema); |
| 559 | 564 | } |
| 560 | 565 | } | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/ProductAppService.cs
| ... | ... | @@ -1115,7 +1115,9 @@ public class ProductAppService : ApplicationService, IProductAppService |
| 1115 | 1115 | throw new UserFriendlyException($"Multiple product categories match \"{n}\". Use a unique name in the system."); |
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | - return matches[0].Id; | |
| 1118 | + var matched = matches[0]; | |
| 1119 | + await ProductCategoryImportHelper.EnsureImportableCategoryCodeAsync(_dbContext.SqlSugarClient, matched); | |
| 1120 | + return matched.Id; | |
| 1119 | 1121 | } |
| 1120 | 1122 | |
| 1121 | 1123 | private async Task<string?> ResolvePartnerIdFromImportCellAsync(string cell) | ... | ... |