mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 18:16:47 -05:00
Merge pull request #3992 from asaah262/refactor/graph-unified-role-labels
refactor(graph): keep the unified role labels in one place
This commit is contained in:
8 files changed
+112
-57
No files matched your search
+3
-3
@@ -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 |
|
||||
|
||||
@@ -260,6 +260,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`\
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user