|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/mendixlabs/mxcli/model" |
| 10 | + "github.com/mendixlabs/mxcli/sdk/security" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + autoDocumentRoleName = "User" |
| 15 | + autoDocumentRoleDescription = "Auto-created default role for mxcli document access" |
| 16 | +) |
| 17 | + |
| 18 | +// defaultDocumentAccessRoles returns a conservative fallback role set for newly |
| 19 | +// created pages/microflows when the target module has no module roles at all. |
| 20 | +// |
| 21 | +// Mendix accepts document access only when it references a role from the same |
| 22 | +// module; using an existing role from another module causes CE0148 on freshly |
| 23 | +// created documents. To keep mx-check green, auto-create a local `User` module |
| 24 | +// role only for modules that currently have zero roles. Modules that already |
| 25 | +// manage their own roles keep the existing "no access by default" behavior. |
| 26 | +func defaultDocumentAccessRoles(ctx *ExecContext, module *model.Module) []model.ID { |
| 27 | + if module == nil { |
| 28 | + return nil |
| 29 | + } |
| 30 | + |
| 31 | + ms, err := ctx.Backend.GetModuleSecurity(module.ID) |
| 32 | + if err != nil || ms == nil { |
| 33 | + return nil |
| 34 | + } |
| 35 | + if moduleUsesAutoDocumentRole(ms) { |
| 36 | + return []model.ID{model.ID(module.Name + "." + autoDocumentRoleName)} |
| 37 | + } |
| 38 | + if len(ms.ModuleRoles) > 0 { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + if err := ctx.Backend.AddModuleRole(ms.ID, autoDocumentRoleName, autoDocumentRoleDescription); err != nil { |
| 43 | + return nil |
| 44 | + } |
| 45 | + return []model.ID{model.ID(module.Name + "." + autoDocumentRoleName)} |
| 46 | +} |
| 47 | + |
| 48 | +func moduleUsesAutoDocumentRole(ms *security.ModuleSecurity) bool { |
| 49 | + if ms == nil { |
| 50 | + return false |
| 51 | + } |
| 52 | + return len(ms.ModuleRoles) == 1 && |
| 53 | + ms.ModuleRoles[0].Name == autoDocumentRoleName && |
| 54 | + ms.ModuleRoles[0].Description == autoDocumentRoleDescription |
| 55 | +} |
| 56 | + |
| 57 | +func remapDocumentAccessRoles(ctx *ExecContext, targetModule *model.Module, currentRoles []model.ID) []model.ID { |
| 58 | + if targetModule == nil { |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + ms, err := ctx.Backend.GetModuleSecurity(targetModule.ID) |
| 63 | + if err != nil || ms == nil { |
| 64 | + return nil |
| 65 | + } |
| 66 | + if len(ms.ModuleRoles) == 0 || moduleUsesAutoDocumentRole(ms) { |
| 67 | + return defaultDocumentAccessRoles(ctx, targetModule) |
| 68 | + } |
| 69 | + |
| 70 | + targetRoleNames := make(map[string]bool, len(ms.ModuleRoles)) |
| 71 | + for _, role := range ms.ModuleRoles { |
| 72 | + targetRoleNames[role.Name] = true |
| 73 | + } |
| 74 | + |
| 75 | + var remapped []model.ID |
| 76 | + seen := make(map[string]bool) |
| 77 | + for _, qualifiedRole := range currentRoles { |
| 78 | + roleName := string(qualifiedRole) |
| 79 | + if idx := strings.LastIndex(roleName, "."); idx >= 0 { |
| 80 | + roleName = roleName[idx+1:] |
| 81 | + } |
| 82 | + if !targetRoleNames[roleName] { |
| 83 | + continue |
| 84 | + } |
| 85 | + targetQualifiedRole := targetModule.Name + "." + roleName |
| 86 | + if seen[targetQualifiedRole] { |
| 87 | + continue |
| 88 | + } |
| 89 | + seen[targetQualifiedRole] = true |
| 90 | + remapped = append(remapped, model.ID(targetQualifiedRole)) |
| 91 | + } |
| 92 | + |
| 93 | + return remapped |
| 94 | +} |
| 95 | + |
| 96 | +func documentRoleStrings(roles []model.ID) []string { |
| 97 | + values := make([]string, 0, len(roles)) |
| 98 | + for _, role := range roles { |
| 99 | + values = append(values, string(role)) |
| 100 | + } |
| 101 | + return values |
| 102 | +} |
| 103 | + |
| 104 | +func cloneRoleIDs(roles []model.ID) []model.ID { |
| 105 | + if len(roles) == 0 { |
| 106 | + return nil |
| 107 | + } |
| 108 | + cloned := make([]model.ID, len(roles)) |
| 109 | + copy(cloned, roles) |
| 110 | + return cloned |
| 111 | +} |
| 112 | + |
| 113 | +// pruneInvalidUserRoles removes user roles that no longer have any non-System |
| 114 | +// module role assignments. Mendix rejects those roles with CE0157. |
| 115 | +func pruneInvalidUserRoles(ctx *ExecContext, ps *security.ProjectSecurity) error { |
| 116 | + if latest, err := ctx.Backend.GetProjectSecurity(); err == nil { |
| 117 | + ps = latest |
| 118 | + } else if ps == nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + for _, userRole := range ps.UserRoles { |
| 123 | + hasNonSystemRole := false |
| 124 | + for _, moduleRole := range userRole.ModuleRoles { |
| 125 | + if !strings.HasPrefix(moduleRole, "System.") { |
| 126 | + hasNonSystemRole = true |
| 127 | + break |
| 128 | + } |
| 129 | + } |
| 130 | + if hasNonSystemRole { |
| 131 | + continue |
| 132 | + } |
| 133 | + if err := ctx.Backend.RemoveUserRole(ps.ID, userRole.Name); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + if !ctx.Quiet { |
| 137 | + fmt.Fprintf(ctx.Output, "Dropped invalid user role: %s\n", userRole.Name) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
0 commit comments