Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Make user role mappings configurable #11441

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/IConfigOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ export interface IConfigOptions {
client_id: string;
}
>;

custom_power_level_roles?: {
[level: string]: {
[lang: string]: string;
};
};
}

export interface ISsoRedirectOptions {
Expand Down
23 changes: 22 additions & 1 deletion src/Roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,30 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { _t } from "./languageHandler";
import { safeSet } from "matrix-js-sdk/src/utils";

import SdkConfig from "./SdkConfig";
import { _t, pickBestLanguage } from "./languageHandler";

export function levelRoleMap(usersDefault: number): Record<number | "undefined", string> {
const customPowerLevelRoles = SdkConfig.get().custom_power_level_roles;
if (customPowerLevelRoles) {
const mappings: Record<number | "undefined", string> = {
undefined: _t("Default"),
};

Object.entries(customPowerLevelRoles).forEach(([levelString, langs]) => {
const lang = pickBestLanguage(Object.keys(langs));
if (lang && langs[lang]) {
safeSet(mappings, parseInt(levelString), langs[lang]);
}
});

if (Object.keys(mappings).length > 1) {
return mappings;
}
}

return {
undefined: _t("Default"),
0: _t("Restricted"),
Expand Down
6 changes: 5 additions & 1 deletion src/components/views/elements/PowerSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import Field from "./Field";
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
import { getKeyBindingsManager } from "../../../KeyBindingsManager";
import { objectHasDiff } from "../../../utils/objects";
import SettingsStore from "../../../settings/SettingsStore";
import { UIFeature } from "../../../settings/UIFeature";

const CUSTOM_VALUE = "SELECT_VALUE_CUSTOM";

Expand Down Expand Up @@ -173,7 +175,9 @@ export default class PowerSelector<K extends undefined | string> extends React.C
text: Roles.textualPowerLevel(level, this.props.usersDefault),
};
});
options.push({ value: CUSTOM_VALUE, text: _t("Custom level") });
if (SettingsStore.getValue(UIFeature.CustomPowerLevel)) {
options.push({ value: CUSTOM_VALUE, text: _t("Custom level") });
}
const optionsElements = options.map((op) => {
return (
<option value={op.value} key={op.value} data-testid={`power-level-option-${op.value}`}>
Expand Down
4 changes: 4 additions & 0 deletions src/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,10 @@ export const SETTINGS: { [setting: string]: ISetting } = {
supportedLevels: LEVELS_UI_FEATURE,
default: true,
},
[UIFeature.CustomPowerLevel]: {
supportedLevels: LEVELS_UI_FEATURE,
default: true,
},

// Electron-specific settings, they are stored by Electron and set/read over an IPC.
// We store them over there are they are necessary to know before the renderer process launches.
Expand Down
1 change: 1 addition & 0 deletions src/settings/UIFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const enum UIFeature {
RoomHistorySettings = "UIFeature.roomHistorySettings",
TimelineEnableRelativeDates = "UIFeature.timelineEnableRelativeDates",
BulkUnverifiedSessionsReminder = "UIFeature.BulkUnverifiedSessionsReminder",
CustomPowerLevel = "UIFeature.customPowerLevel",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

export enum UIComponent {
Expand Down
91 changes: 91 additions & 0 deletions test/Roles-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2023 Nordeck IT + Consulting GmbH.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { levelRoleMap } from "../src/Roles";
import SdkConfig from "../src/SdkConfig";
import { setLanguage } from "../src/languageHandler";

describe("levelRoleMap", () => {
beforeEach(async () => {
await setLanguage("en");
});

it("returns default role labels", async () => {
expect(levelRoleMap(0)).toEqual({
undefined: "Default",
0: "Default",
50: "Moderator",
100: "Admin",
});
});

it("returns a restricted level with custom default user level", async () => {
expect(levelRoleMap(10)).toEqual({
undefined: "Default",
0: "Restricted",
10: "Default",
50: "Moderator",
100: "Admin",
});
});

it("returns custom levels if present in the configuration", async () => {
SdkConfig.put({
custom_power_level_roles: {
"0": { en: "Regular" },
"1": { en: "Mod" },
"2": { en: "Admin" },
},
});

expect(levelRoleMap(0)).toEqual({
undefined: "Default",
0: "Regular",
1: "Mod",
2: "Admin",
});
});

it("returns custom levels in the correct language", async () => {
SdkConfig.put({
custom_power_level_roles: {
"0": { en: "Regular", de: "Normal" },
"1": { "en": "Mod", "de-DE": "Moderator" },
"2": { en: "Admin" },
},
});

await setLanguage("de");

expect(levelRoleMap(0)).toEqual({
undefined: "Standard",
0: "Normal",
1: "Moderator",
2: "Admin",
});
});

it("returns default levels if custom configuration is empty", async () => {
SdkConfig.put({ custom_power_level_roles: { "0": {} } });

expect(levelRoleMap(0)).toEqual({
undefined: "Default",
0: "Default",
50: "Moderator",
100: "Admin",
});
});
});