-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from nordeck/nic/feat/disable-features-for-guests
Disable certain homeserver-wide actions (create room, invite user) for guest users
- Loading branch information
Showing
15 changed files
with
393 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
--- | ||
'@nordeck/element-web-guest-module': minor | ||
'@nordeck/synapse-guest-module': minor | ||
--- | ||
|
||
Disable certain homeserver-wide actions (create room, invite user) for guest users. |
This file contains 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 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 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,3 @@ | ||
{ | ||
"src/customisations/ComponentVisibility.ts": "node_modules/@nordeck/element-web-guest-module/customisations/ComponentVisibility.ts" | ||
} |
This file contains 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 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 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
78 changes: 78 additions & 0 deletions
78
element-web-guest-module/customisations/ComponentVisibility.ts
This file contains 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,78 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// This file is exported as-is. It will be used by the Element build as a | ||
// TypeScript file. We don't actually depend on the matrix-react-sdk, though | ||
// all relevant and testable code is imported from the bundled module. | ||
|
||
/* eslint-disable no-undef */ | ||
// @ts-nocheck | ||
|
||
/* | ||
* This file hides the UI features that are also disabled via the Synapse module. | ||
* This should eventually also be moved to the Module API, see also | ||
* https://github.com/matrix-org/matrix-react-sdk-module-api/pull/12 | ||
*/ | ||
|
||
import { | ||
GUEST_MODULE_CONFIG_KEY, | ||
GUEST_MODULE_CONFIG_NAMESPACE, | ||
GuestModuleConfig, | ||
assertValidGuestModuleConfig, | ||
shouldShowComponent as shouldShowComponentShared, | ||
} from '@nordeck/element-web-guest-module'; | ||
import { MatrixClientPeg } from 'matrix-react-sdk/src/MatrixClientPeg'; | ||
import SdkConfig from 'matrix-react-sdk/src/SdkConfig'; | ||
import { UIComponent } from 'matrix-react-sdk/src/settings/UIFeature'; | ||
|
||
export function getConfig(): GuestModuleConfig { | ||
const rawConfig = SdkConfig.get(GUEST_MODULE_CONFIG_NAMESPACE)?.[ | ||
GUEST_MODULE_CONFIG_KEY | ||
]; | ||
|
||
assertValidGuestModuleConfig(rawConfig); | ||
|
||
return rawConfig; | ||
} | ||
|
||
/** | ||
* Determines whether or not the active MatrixClient user should be able to use | ||
* the given UI component. If shown, the user might still not be able to use the | ||
* component depending on their contextual permissions. For example, invite options | ||
* might be shown to the user but they won't have permission to invite users to | ||
* the current room: the button will appear disabled. | ||
* @param {UIComponent} component The component to check visibility for. | ||
* @returns {boolean} True (default) if the user is able to see the component, false | ||
* otherwise. | ||
*/ | ||
function shouldShowComponent(component: UIComponent): boolean { | ||
const config = getConfig(); | ||
const myUserId = MatrixClientPeg.safeGet().getSafeUserId(); | ||
|
||
return shouldShowComponentShared(config, myUserId, component); | ||
} | ||
|
||
// This interface summarises all available customisation points and also marks | ||
// them all as optional. This allows customisers to only define and export the | ||
// customisations they need while still maintaining type safety. | ||
export interface IComponentVisibilityCustomisations { | ||
shouldShowComponent?: typeof shouldShowComponent; | ||
} | ||
|
||
// A real customisation module will define and export one or more of the | ||
// customisation points that make up the interface above. | ||
export const ComponentVisibilityCustomisations: IComponentVisibilityCustomisations = | ||
{ shouldShowComponent }; |
This file contains 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 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 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,63 @@ | ||
/* | ||
* 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 { GuestModuleConfig } from './config'; | ||
import { shouldShowComponent } from './shouldShowComponent'; | ||
|
||
describe('shouldShowComponent', () => { | ||
describe.each` | ||
extra_config | test_guest | ||
${{}} | ${'@guest-asdf'} | ||
${{ guest_user_prefix: '@custom-' }} | ${'@custom-asdf'} | ||
`('with $extra_config config', ({ extra_config, test_guest }) => { | ||
let config: GuestModuleConfig; | ||
|
||
beforeEach(() => { | ||
config = { | ||
...extra_config, | ||
}; | ||
}); | ||
|
||
it.each([ | ||
'UIComponent.sendInvites', | ||
'UIComponent.roomCreation', | ||
'UIComponent.spaceCreation', | ||
'UIComponent.exploreRooms', | ||
'UIComponent.roomOptionsMenu', | ||
])('should reject %s for guests', (component) => { | ||
expect(shouldShowComponent(config, test_guest, component)).toBe(false); | ||
}); | ||
|
||
it.each(['UIComponent.filterContainer', 'UIComponent.addIntegrations'])( | ||
'should accept %s for guests', | ||
(component) => { | ||
expect(shouldShowComponent(config, test_guest, component)).toBe(true); | ||
}, | ||
); | ||
|
||
it.each([ | ||
'UIComponent.sendInvites', | ||
'UIComponent.roomCreation', | ||
'UIComponent.spaceCreation', | ||
'UIComponent.exploreRooms', | ||
'UIComponent.roomOptionsMenu', | ||
'UIComponent.filterContainer', | ||
'UIComponent.addIntegrations', | ||
])('should accept %s for normal users', (component) => { | ||
expect(shouldShowComponent(config, '@userid', component)).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains 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,55 @@ | ||
/* | ||
* 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 { GuestModuleConfig } from './config'; | ||
|
||
const GUEST_INVISIBLE_COMPONENTS = [ | ||
'UIComponent.sendInvites', | ||
'UIComponent.roomCreation', | ||
'UIComponent.spaceCreation', | ||
'UIComponent.exploreRooms', | ||
'UIComponent.roomOptionsMenu', | ||
// TODO: UIComponent.AddIntegrations does hide the whole integrations sidebar, | ||
// where it should only hide the buttons and slash command. If this gets fixed | ||
// in element we can disable it also for guests. | ||
//UIComponent.AddIntegrations, | ||
]; | ||
|
||
function getGuestUserPrefix(config: GuestModuleConfig): string { | ||
return config.guest_user_prefix ?? '@guest-'; | ||
} | ||
|
||
/** | ||
* A function that can be called from a `ComponentVisibility` customisation. It | ||
* returns true, if the `userId` should see the `component`. | ||
* | ||
* @param config - the configuration of the module | ||
* @param userId - the id of the user to check | ||
* @param component - the name of the component that is checked | ||
* @returns true, if the user should see the component | ||
*/ | ||
export function shouldShowComponent( | ||
config: GuestModuleConfig, | ||
userId: string, | ||
component: string, | ||
): boolean { | ||
const components = userId.startsWith(getGuestUserPrefix(config)) | ||
? GUEST_INVISIBLE_COMPONENTS | ||
: []; | ||
|
||
const shouldShow = !components.includes(component); | ||
return shouldShow; | ||
} |
Oops, something went wrong.