From f815a942741fea980981c994c6fb1f989f167941 Mon Sep 17 00:00:00 2001 From: Sara Ahmadi Date: Fri, 13 Nov 2026 18:06:27 +0100 Subject: [PATCH] refactor(graph): keep the unified role labels in one place Assisted-by: Claude Code:claude-opus-5-5 --- opencloud/README.md | 6 ++-- services/graph/README.md | 2 ++ services/graph/pkg/command/unified_roles.go | 30 ++-------------- services/graph/pkg/unifiedrole/roles.go | 31 +++++++++++++++++ services/graph/pkg/unifiedrole/roles_test.go | 30 ++++++++++++++++ .../web/assets/themes/opencloud/theme.json | 8 ----- services/web/pkg/theme/service_test.go | 28 ++++++++++++++- services/web/pkg/theme/theme.go | 34 +++++++++---------- 8 files changed, 112 insertions(+), 57 deletions(-) diff --git a/opencloud/README.md b/opencloud/README.md index 80bc8dce94..666ca61d4d 100644 --- a/opencloud/README.md +++ b/opencloud/README.md @@ -115,8 +115,8 @@ opencloud graph list-unified-roles The output of this command includes the following information for each role: -* `Name`\ - The human readable name of the role. +* `Label`\ + A short, stable name of the role. * `UID`\ The unique identifier of the role. * `Enabled`\ @@ -130,7 +130,7 @@ The output of this command includes the following information for each role: ```bash +----------------------------+--------------------------------------+----------+--------------------------------+--------------------------------+------------------------------------------+ -| NAME | UID | ENABLED | DESCRIPTION | CONDITION | ALLOWED RESOURCE ACTIONS | +| LABEL | UID | ENABLED | DESCRIPTION | CONDITION | ALLOWED RESOURCE ACTIONS | +----------------------------+--------------------------------------+----------+--------------------------------+--------------------------------+------------------------------------------+ | Viewer | b1e2218d-eef8-4d4c-b82d-0f1a1b48f3b5 | enabled | View and download. | exists @Resource.File | libre.graph/driveItem/path/read | | | | | | exists @Resource.Folder | libre.graph/driveItem/quota/read | diff --git a/services/graph/README.md b/services/graph/README.md index f310aec7f3..48ca6ff87e 100644 --- a/services/graph/README.md +++ b/services/graph/README.md @@ -176,6 +176,8 @@ opencloud graph list-unified-roles The output of this command includes the following information for each role: +* `Label`\ + A short, stable name of the role. * `UID`\ The unique identifier of the role. * `Enabled`\ diff --git a/services/graph/pkg/command/unified_roles.go b/services/graph/pkg/command/unified_roles.go index c279f14136..0157f7ca44 100644 --- a/services/graph/pkg/command/unified_roles.go +++ b/services/graph/pkg/command/unified_roles.go @@ -16,32 +16,6 @@ import ( "github.com/spf13/cobra" ) -var ( - unifiedRolesNames = map[string]string{ - unifiedrole.UnifiedRoleViewerID: "Viewer", - unifiedrole.UnifiedRoleViewerWithVersionsID: "ViewerWithVersions", - unifiedrole.UnifiedRoleViewerListGrantsID: "ViewerListGrants", - unifiedrole.UnifiedRoleSpaceViewerID: "SpaceViewer", - unifiedrole.UnifiedRoleSpaceViewerWithVersionsID: "SpaceViewerWithVersions", - unifiedrole.UnifiedRoleEditorID: "Editor", - unifiedrole.UnifiedRoleEditorWithVersionsID: "EditorWithVersions", - unifiedrole.UnifiedRoleEditorListGrantsID: "EditorListGrants", - unifiedrole.UnifiedRoleEditorListGrantsWithVersionsID: "EditorListGrantsWithVersions", - unifiedrole.UnifiedRoleSpaceEditorID: "SpaceEditor", - unifiedrole.UnifiedRoleSpaceEditorWithoutVersionsID: "SpaceEditorWithoutVersions", - unifiedrole.UnifiedRoleSpaceEditorWithoutTrashbinID: "SpaceEditorWithoutTrashbin", - unifiedrole.UnifiedRoleSpaceEditorWithoutVersionsWithoutTrashbinID: "SpaceEditorWithoutVersionsWithoutTrashbin", - unifiedrole.UnifiedRoleFileEditorID: "FileEditor", - unifiedrole.UnifiedRoleFileEditorWithVersionsID: "FileEditorWithVersions", - unifiedrole.UnifiedRoleFileEditorListGrantsID: "FileEditorListGrants", - unifiedrole.UnifiedRoleFileEditorListGrantsWithVersionsID: "FileEditorListGrantsWithVersions", - unifiedrole.UnifiedRoleEditorLiteID: "EditorLite", - unifiedrole.UnifiedRoleManagerID: "SpaceManager", - unifiedrole.UnifiedRoleSecureViewerID: "SecureViewer", - unifiedrole.UnifiedRoleDeniedID: "Denied", - } -) - // UnifiedRoles bundles available commands for unified roles func UnifiedRoles(cfg *config.Config) []*cobra.Command { cmds := []*cobra.Command{ @@ -88,7 +62,7 @@ func listUnifiedRoles(cfg *config.Config) *cobra.Command { } tbl := tablewriter.NewTable(os.Stdout, opt) - headers := []string{"Name", "UID", "Enabled", "Description", "Condition", "Allowed resource actions"} + headers := []string{"Label", "UID", "Enabled", "Description", "Condition", "Allowed resource actions"} tbl.Header(headers) for _, definition := range unifiedrole.GetRoles(unifiedrole.RoleFilterAll()) { @@ -96,7 +70,7 @@ func listUnifiedRoles(cfg *config.Config) *cobra.Command { const disabled = "disabled" rows := [][]string{ - {unifiedRolesNames[definition.GetId()], definition.GetId(), disabled, definition.GetDescription()}, + {unifiedrole.GetUnifiedRoleLabel(definition.GetId()), definition.GetId(), disabled, definition.GetDescription()}, } if slices.Contains(cfg.UnifiedRoles.AvailableRoles, definition.GetId()) { rows[0][2] = enabled diff --git a/services/graph/pkg/unifiedrole/roles.go b/services/graph/pkg/unifiedrole/roles.go index e67bbdbcee..b6888d39e7 100644 --- a/services/graph/pkg/unifiedrole/roles.go +++ b/services/graph/pkg/unifiedrole/roles.go @@ -236,6 +236,32 @@ var ( // UnifiedRole FullDenial, Role DisplayName (resolves directly) _deniedUnifiedRoleDisplayName = l10n.Template("Cannot access") + // unifiedRoleLabels maps the IDs of the built-in roles to short, stable labels, + // e.g. for the CLI and the theme. Every built-in role must have a label. + unifiedRoleLabels = map[string]string{ + UnifiedRoleViewerID: "Viewer", + UnifiedRoleViewerWithVersionsID: "ViewerWithVersions", + UnifiedRoleViewerListGrantsID: "ViewerListGrants", + UnifiedRoleSpaceViewerID: "SpaceViewer", + UnifiedRoleSpaceViewerWithVersionsID: "SpaceViewerWithVersions", + UnifiedRoleEditorID: "Editor", + UnifiedRoleEditorWithVersionsID: "EditorWithVersions", + UnifiedRoleEditorListGrantsID: "EditorListGrants", + UnifiedRoleEditorListGrantsWithVersionsID: "EditorListGrantsWithVersions", + UnifiedRoleSpaceEditorID: "SpaceEditor", + UnifiedRoleSpaceEditorWithoutVersionsID: "SpaceEditorWithoutVersions", + UnifiedRoleSpaceEditorWithoutTrashbinID: "SpaceEditorWithoutTrashbin", + UnifiedRoleSpaceEditorWithoutVersionsWithoutTrashbinID: "SpaceEditorWithoutVersionsWithoutTrashbin", + UnifiedRoleFileEditorID: "FileEditor", + UnifiedRoleFileEditorWithVersionsID: "FileEditorWithVersions", + UnifiedRoleFileEditorListGrantsID: "FileEditorListGrants", + UnifiedRoleFileEditorListGrantsWithVersionsID: "FileEditorListGrantsWithVersions", + UnifiedRoleEditorLiteID: "EditorLite", + UnifiedRoleManagerID: "SpaceManager", + UnifiedRoleSecureViewerID: "SecureViewer", + UnifiedRoleDeniedID: "Denied", + } + // legacyNames contains the legacy role names. legacyNames = map[string]string{ UnifiedRoleViewerID: conversions.RoleViewer, @@ -751,6 +777,11 @@ func GetRolesByPermissions(roleSet []*libregraph.UnifiedRoleDefinition, actions return weightRoles(roles, constraints, descending) } +// GetUnifiedRoleLabel returns the label of the role with the given ID, or an empty string for an unknown ID. +func GetUnifiedRoleLabel(unifiedRoleID string) string { + return unifiedRoleLabels[unifiedRoleID] +} + // GetLegacyRoleName returns the legacy role name for the provided role func GetLegacyRoleName(role libregraph.UnifiedRoleDefinition) string { return legacyNames[role.GetId()] diff --git a/services/graph/pkg/unifiedrole/roles_test.go b/services/graph/pkg/unifiedrole/roles_test.go index d6d910f008..7f38373367 100644 --- a/services/graph/pkg/unifiedrole/roles_test.go +++ b/services/graph/pkg/unifiedrole/roles_test.go @@ -294,6 +294,36 @@ func TestGetAllowedResourceActions(t *testing.T) { } } +func TestGetUnifiedRoleLabel(t *testing.T) { + tests := map[string]struct { + unifiedRoleID string + expectedLabel string + }{ + "empty id": {unifiedRoleID: "", expectedLabel: ""}, + "unknown id": {unifiedRoleID: "unknown", expectedLabel: ""}, + "viewer": {unifiedRoleID: unifiedrole.UnifiedRoleViewerID, expectedLabel: "Viewer"}, + "manager": {unifiedRoleID: unifiedrole.UnifiedRoleManagerID, expectedLabel: "SpaceManager"}, + "denied": {unifiedRoleID: unifiedrole.UnifiedRoleDeniedID, expectedLabel: "Denied"}, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + NewWithT(t).Expect(unifiedrole.GetUnifiedRoleLabel(tc.unifiedRoleID)).To(Equal(tc.expectedLabel)) + }) + } +} + +func TestGetUnifiedRoleLabel_EveryRoleHasALabel(t *testing.T) { + g := NewWithT(t) + labels := map[string]string{} + for _, role := range unifiedrole.GetRoles(unifiedrole.RoleFilterAll()) { + label := unifiedrole.GetUnifiedRoleLabel(role.GetId()) + g.Expect(label).ToNot(BeEmpty(), "role %s has no label", role.GetId()) + g.Expect(labels).ToNot(HaveKey(label), "label %q is used by the roles %s and %s", label, labels[label], role.GetId()) + labels[label] = role.GetId() + } +} + func TestLocalizeRole_English(t *testing.T) { g := NewWithT(t) original := unifiedrole.RoleViewer diff --git a/services/web/assets/themes/opencloud/theme.json b/services/web/assets/themes/opencloud/theme.json index ed4392e339..32999bbd9f 100644 --- a/services/web/assets/themes/opencloud/theme.json +++ b/services/web/assets/themes/opencloud/theme.json @@ -11,35 +11,27 @@ }, "shareRoles": { "b1e2218d-eef8-4d4c-b82d-0f1a1b48f3b5": { - "name": "UnifiedRoleViewer", "iconName": "eye" }, "a8d5fe5e-96e3-418d-825b-534dbdf22b99": { - "label": "UnifiedRoleSpaceViewer", "iconName": "eye" }, "2d00ce52-1fc2-4dbc-8b95-a73b73395f5a": { - "label": "UnifiedRoleFileEditor", "iconName": "pencil" }, "fb6c3e19-e378-47e5-b277-9732f9de6e21": { - "label": "UnifiedRoleEditor", "iconName": "pencil" }, "58c63c02-1d89-4572-916a-870abc5a1b7d": { - "label": "UnifiedRoleSpaceEditor", "iconName": "pencil" }, "312c0871-5ef7-4b3a-85b6-0e4074c64049": { - "label": "UnifiedRoleManager", "iconName": "user-star" }, "1c996275-f1c9-4e71-abdf-a42f6495e960": { - "label": "UnifiedRoleUploader", "iconName": "upload" }, "aa97fe03-7980-45ac-9e50-b325749fd7e6": { - "label": "UnifiedRoleSecureView", "iconName": "shield" } } diff --git a/services/web/pkg/theme/service_test.go b/services/web/pkg/theme/service_test.go index 70af28bbc7..86cae933e1 100644 --- a/services/web/pkg/theme/service_test.go +++ b/services/web/pkg/theme/service_test.go @@ -15,6 +15,7 @@ import ( "github.com/opencloud-eu/opencloud/pkg/x/io/fsx" "github.com/opencloud-eu/opencloud/services/graph/mocks" "github.com/opencloud-eu/opencloud/services/graph/pkg/unifiedrole" + "github.com/opencloud-eu/opencloud/services/web" "github.com/opencloud-eu/opencloud/services/web/pkg/theme" ) @@ -71,7 +72,32 @@ func TestService_Get(t *testing.T) { // brandingTheme assert.Equal(t, jsonData.Get("_branding").String(), "_branding") // themeDefaults - assert.Equal(t, jsonData.Get("common.shareRoles."+unifiedrole.UnifiedRoleViewerID+".name").String(), "UnifiedRoleViewer") + assert.Equal(t, "Viewer", jsonData.Get("common.shareRoles."+unifiedrole.UnifiedRoleViewerID+".label").String()) + assert.False(t, jsonData.Get("common.shareRoles."+unifiedrole.UnifiedRoleViewerID+".name").Exists()) +} + +func TestService_Get_BuiltInThemeShareRoleLabels(t *testing.T) { + service, err := theme.NewService( + theme.ServiceOptions{}. + WithThemeFS(fsx.NewFallbackFS(fsx.NewMemMapFs(), fsx.NewBasePathFs(fsx.FromIOFS(web.Assets), "assets/themes"))). + WithGatewaySelector(mocks.NewSelectable[gateway.GatewayAPIClient](t)), + ) + assert.NoError(t, err) + + r := httptest.NewRequest(http.MethodGet, "/", nil) + r.SetPathValue("id", "opencloud") + w := httptest.NewRecorder() + service.Get(w, r) + assert.Equal(t, http.StatusOK, w.Code) + + shareRoles := gjson.Get(w.Body.String(), "common.shareRoles") + assert.True(t, shareRoles.IsObject()) + shareRoles.ForEach(func(id, role gjson.Result) bool { + assert.NotEmpty(t, unifiedrole.GetUnifiedRoleLabel(id.String()), "unknown role %s", id.String()) + assert.Equal(t, unifiedrole.GetUnifiedRoleLabel(id.String()), role.Get("label").String(), "label of role %s", id.String()) + assert.False(t, role.Get("name").Exists(), "role %s has a name key", id.String()) + return true + }) } func TestService_GetConcurrent(t *testing.T) { diff --git a/services/web/pkg/theme/theme.go b/services/web/pkg/theme/theme.go index a6f05c62b8..4247742d3b 100644 --- a/services/web/pkg/theme/theme.go +++ b/services/web/pkg/theme/theme.go @@ -18,71 +18,71 @@ var themeDefaults = KV{ "common": KV{ "shareRoles": KV{ unifiedrole.UnifiedRoleViewerID: KV{ - "name": "UnifiedRoleViewer", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleViewerID), "iconName": "eye", }, unifiedrole.UnifiedRoleViewerWithVersionsID: KV{ - "name": "UnifiedRoleViewerWithVersions", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleViewerWithVersionsID), "iconName": "eye", }, unifiedrole.UnifiedRoleViewerListGrantsID: KV{ - "name": "UnifiedRoleViewerListGrants", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleViewerListGrantsID), "iconName": "eye", }, unifiedrole.UnifiedRoleSpaceViewerID: KV{ - "label": "UnifiedRoleSpaceViewer", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleSpaceViewerID), "iconName": "eye", }, unifiedrole.UnifiedRoleSpaceViewerWithVersionsID: KV{ - "label": "UnifiedRoleSpaceViewerWithVersions", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleSpaceViewerWithVersionsID), "iconName": "eye", }, unifiedrole.UnifiedRoleFileEditorID: KV{ - "label": "UnifiedRoleFileEditor", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleFileEditorID), "iconName": "pencil", }, unifiedrole.UnifiedRoleFileEditorWithVersionsID: KV{ - "label": "UnifiedRoleFileEditorWithVersions", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleFileEditorWithVersionsID), "iconName": "pencil", }, unifiedrole.UnifiedRoleFileEditorListGrantsID: KV{ - "label": "UnifiedRoleFileEditorListGrants", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleFileEditorListGrantsID), "iconName": "pencil", }, unifiedrole.UnifiedRoleEditorID: KV{ - "label": "UnifiedRoleEditor", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleEditorID), "iconName": "pencil", }, unifiedrole.UnifiedRoleEditorWithVersionsID: KV{ - "label": "UnifiedRoleEditorWithVersions", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleEditorWithVersionsID), "iconName": "pencil", }, unifiedrole.UnifiedRoleEditorListGrantsID: KV{ - "label": "UnifiedRoleEditorListGrants", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleEditorListGrantsID), "iconName": "pencil", }, unifiedrole.UnifiedRoleSpaceEditorID: KV{ - "label": "UnifiedRoleSpaceEditor", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleSpaceEditorID), "iconName": "pencil", }, unifiedrole.UnifiedRoleSpaceEditorWithoutVersionsID: KV{ - "label": "UnifiedRoleSpaceEditorWithoutVersions", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleSpaceEditorWithoutVersionsID), "iconName": "pencil", }, unifiedrole.UnifiedRoleManagerID: KV{ - "label": "UnifiedRoleManager", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleManagerID), "iconName": "user-star", }, unifiedrole.UnifiedRoleEditorLiteID: KV{ - "label": "UnifiedRoleEditorLite", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleEditorLiteID), "iconName": "upload", }, unifiedrole.UnifiedRoleSecureViewerID: KV{ - "label": "UnifiedRoleSecureView", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleSecureViewerID), "iconName": "shield", }, unifiedrole.UnifiedRoleDeniedID: KV{ - "label": "UnifiedRoleFullDenial", + "label": unifiedrole.GetUnifiedRoleLabel(unifiedrole.UnifiedRoleDeniedID), "iconName": "stop-circle", }, },