This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 812
Change user permission by using a new apply button #12346
Merged
florianduros
merged 7 commits into
develop
from
florianduros/fix/role-permission-selection
Mar 19, 2024
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
239b5c7
Add PowerLevelSelector.tsx.
florianduros ce0da9e
Use `PowerLevelSelector` to render privileged and muted users in `Rol…
florianduros 8d6d935
Update existing tests
florianduros e428a18
Add playwright test
florianduros 898abd5
Merge branch 'develop' into florianduros/fix/role-permission-selection
florianduros f713a3a
Fix typo
florianduros bc81207
Fix typo
florianduros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
playwright/e2e/settings/roles-permissions-room-settings-tab.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * | ||
| * Copyright 2024 The Matrix.org Foundation C.I.C. | ||
| * | ||
| * 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 { Locator } from "@playwright/test"; | ||
|
|
||
| import { test, expect } from "../../element-web-test"; | ||
|
|
||
| test.describe("Roles & Permissions room settings tab", () => { | ||
| const roomName = "Test room"; | ||
|
|
||
| test.use({ | ||
| displayName: "Alice", | ||
| }); | ||
|
|
||
| let settings: Locator; | ||
|
|
||
| test.beforeEach(async ({ user, app }) => { | ||
| await app.client.createRoom({ name: roomName }); | ||
| await app.viewRoomByName(roomName); | ||
| settings = await app.settings.openRoomSettings("Roles & Permissions"); | ||
| }); | ||
|
|
||
| test("should be able to change the role of an user", async ({ page, app, user }) => { | ||
| const privilegedUserSection = settings.locator(".mx_SettingsFieldset").first(); | ||
| const applyButton = privilegedUserSection.getByRole("button", { name: "Apply" }); | ||
|
|
||
| // Alice is admin (100) and the Apply button should be disabled | ||
| await expect(applyButton).toBeDisabled(); | ||
| let combobox = privilegedUserSection.getByRole("combobox", { name: user.userId }); | ||
| await expect(combobox).toHaveValue("100"); | ||
|
|
||
| // Change the role of Alice to Moderator (50) | ||
| await combobox.selectOption("Moderator"); | ||
| await expect(combobox).toHaveValue("50"); | ||
| await applyButton.click(); | ||
|
|
||
| // Reload and check Alice is still Moderator (50) | ||
| await page.reload(); | ||
| settings = await app.settings.openRoomSettings("Roles & Permissions"); | ||
| combobox = privilegedUserSection.getByRole("combobox", { name: user.userId }); | ||
| await expect(combobox).toHaveValue("50"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * | ||
| * Copyright 2024 The Matrix.org Foundation C.I.C. | ||
| * | ||
| * 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. | ||
| * / | ||
| */ | ||
|
|
||
| .mx_PowerLevelSelector_Button { | ||
| align-self: flex-start; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * | ||
| * Copyright 2024 The Matrix.org Foundation C.I.C. | ||
| * | ||
| * 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 React, { useState, JSX, PropsWithChildren } from "react"; | ||
| import { Button } from "@vector-im/compound-web"; | ||
| import { compare } from "matrix-js-sdk/src/utils"; | ||
|
|
||
| import { useMatrixClientContext } from "../../../contexts/MatrixClientContext"; | ||
| import PowerSelector from "../elements/PowerSelector"; | ||
| import { _t } from "../../../languageHandler"; | ||
| import SettingsFieldset from "./SettingsFieldset"; | ||
|
|
||
| /** | ||
| * Display in a fieldset, the power level of the users and allow to change them. | ||
| * The apply button is disabled until the power level of an user is changed. | ||
| * If there is no user to display, the children is displayed instead. | ||
| */ | ||
| interface PowerLevelSelectorProps { | ||
| /** | ||
| * The power levels of the users | ||
| * The key is the user id and the value is the power level | ||
| */ | ||
| userLevels: Record<string, number>; | ||
| /** | ||
| * Whether the user can change the power levels of other users | ||
| */ | ||
| canChangeLevels: boolean; | ||
| /** | ||
| * The current user power level | ||
| */ | ||
| currentUserLevel: number; | ||
| /** | ||
| * The callback when the apply button is clicked | ||
| * @param value - new power level for the user | ||
| * @param userId - the user id | ||
| */ | ||
| onClick: (value: number, userId: string) => void; | ||
| /** | ||
| * Filter the users to display | ||
| * @param user | ||
| */ | ||
| filter: (user: string) => boolean; | ||
| /** | ||
| * The title of the fieldset | ||
| */ | ||
| title: string; | ||
| } | ||
|
|
||
| export function PowerLevelSelector({ | ||
| userLevels, | ||
| canChangeLevels, | ||
| currentUserLevel, | ||
| onClick, | ||
| filter, | ||
| title, | ||
| children, | ||
| }: PropsWithChildren<PowerLevelSelectorProps>): JSX.Element | null { | ||
| const matrixClient = useMatrixClientContext(); | ||
| const [currentPowerLevel, setCurrentPowerLevel] = useState<{ value: number; userId: string } | null>(null); | ||
|
|
||
| // If the power level has changed, we need to enable the apply button | ||
| const powerLevelChanged = Boolean( | ||
| currentPowerLevel && currentPowerLevel.value !== userLevels[currentPowerLevel?.userId], | ||
| ); | ||
|
|
||
| // We sort the users by power level, then we filter them | ||
| const users = Object.keys(userLevels) | ||
| .sort((userA, userB) => sortUser(userA, userB, userLevels)) | ||
| .filter(filter); | ||
|
|
||
| // No user to display, we return the children into fragment to convert it to JSX.Element type | ||
| if (!users.length) return <>{children}</>; | ||
|
|
||
| return ( | ||
| <SettingsFieldset legend={title}> | ||
| {users.map((userId) => { | ||
| // We only want to display users with a valid power level aka an integer | ||
| if (!Number.isInteger(userLevels[userId])) return; | ||
|
|
||
| const isMe = userId === matrixClient.getUserId(); | ||
| // If I can change levels, I can change the level of anyone with a lower level than mine | ||
| const canChange = canChangeLevels && (userLevels[userId] < currentUserLevel || isMe); | ||
|
|
||
| // When the new power level is selected, the fields are rerendered and we need to keep the current value | ||
| const userLevel = currentPowerLevel?.userId === userId ? currentPowerLevel?.value : userLevels[userId]; | ||
|
|
||
| return ( | ||
| <PowerSelector | ||
| value={userLevel} | ||
| disabled={!canChange} | ||
| label={userId} | ||
| key={userId} | ||
| onChange={(value) => setCurrentPowerLevel({ value, userId })} | ||
| /> | ||
| ); | ||
| })} | ||
|
|
||
| <Button | ||
| size="sm" | ||
| kind="primary" | ||
| // mx_Dialog_nonDialogButton is necessary to avoid the Dialog CSS to override the button style | ||
| className="mx_Dialog_nonDialogButton mx_PowerLevelSelector_Button" | ||
| onClick={() => { | ||
| if (currentPowerLevel !== null) { | ||
| onClick(currentPowerLevel.value, currentPowerLevel.userId); | ||
| setCurrentPowerLevel(null); | ||
| } | ||
| }} | ||
| disabled={!powerLevelChanged} | ||
| aria-label={_t("action|apply")} | ||
| > | ||
| {_t("action|apply")} | ||
| </Button> | ||
| </SettingsFieldset> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Sort the users by power level, then by name | ||
| * @param userA | ||
| * @param userB | ||
| * @param userLevels | ||
| */ | ||
| function sortUser(userA: string, userB: string, userLevels: PowerLevelSelectorProps["userLevels"]): number { | ||
| const powerLevelDiff = userLevels[userA] - userLevels[userB]; | ||
| return powerLevelDiff !== 0 ? powerLevelDiff : compare(userA.toLocaleLowerCase(), userB.toLocaleLowerCase()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor grammar fix