-
Notifications
You must be signed in to change notification settings - Fork 20
fix: check authorities in app adapter [LIBS-370] #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ce2938b
fix(sw): return !ok navigation responses instead of hitting precache
KaiVandivier b5dcd5f
fix(sw): don't use SWR on .json and .action requests
KaiVandivier 858546c
feat(adapter): check user app authorities before rendering app
KaiVandivier b03b858
fix(adapter): use proper app authority name
KaiVandivier b882ed0
feat(adapter): add ui for auth boundary
KaiVandivier ea3ae21
fix(adapter): production env check
KaiVandivier 7f97ca5
refactor: clean up verifyLatestUser
KaiVandivier 1fe6278
chore: fix comment
KaiVandivier 768e48d
refactor: extract useVerifyLatestUser and wrap just app with authBoun…
KaiVandivier 431fa72
fix: notice box text
KaiVandivier 7fb97ef
chore: i18n
KaiVandivier 6ddc7e1
chore: add name and title to pwa example app
KaiVandivier 09b100c
fix: ensure core app name formatting and handle legacy versions
KaiVandivier 0668608
test: fix tests
KaiVandivier d5ed304
Merge branch 'master' into fix/pwa-handle-navigations
KaiVandivier 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
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
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,57 @@ | ||
| import { useConfig } from '@dhis2/app-runtime' | ||
| import { CenteredContent, NoticeBox } from '@dhis2/ui' | ||
| import PropTypes from 'prop-types' | ||
| import React from 'react' | ||
| import i18n from '../locales' | ||
|
|
||
| const IS_PRODUCTION_ENV = process.env.NODE_ENV === 'production' | ||
| const APP_MANAGER_AUTHORITY = 'M_dhis-web-maintenance-appmanager' | ||
| const APP_AUTH_NAME = process.env.REACT_APP_DHIS2_APP_AUTH_NAME | ||
| const LEGACY_APP_AUTH_NAME = process.env.REACT_APP_DHIS2_APP_LEGACY_AUTH_NAME | ||
|
|
||
| const isAppAvailable = ({ userAuthorities, apiVersion }) => { | ||
| // Skip check on dev | ||
| if (!IS_PRODUCTION_ENV) { | ||
| return true | ||
| } | ||
|
|
||
| // On server versions < 35, auth name uses config.title instead of .name | ||
| const requiredAppAuthority = | ||
| apiVersion >= 35 ? APP_AUTH_NAME : LEGACY_APP_AUTH_NAME | ||
|
|
||
| // Check for three possible authorities | ||
| return userAuthorities.some((authority) => | ||
| ['ALL', APP_MANAGER_AUTHORITY, requiredAppAuthority].includes(authority) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Block the app if the user doesn't have the correct permissions to view this | ||
| * app. | ||
| */ | ||
| export function AuthBoundary({ user, children }) { | ||
| const { appName, apiVersion } = useConfig() | ||
|
|
||
| return isAppAvailable({ userAuthorities: user.authorities, apiVersion }) ? ( | ||
| children | ||
| ) : ( | ||
| <CenteredContent> | ||
| <NoticeBox | ||
| error | ||
| title={i18n.t("You don't have access to the {{appName}} app", { | ||
| appName, | ||
| })} | ||
| > | ||
| {i18n.t( | ||
| 'Contact your system administrator for assistance with app access.' | ||
| )} | ||
| </NoticeBox> | ||
| </CenteredContent> | ||
| ) | ||
| } | ||
| AuthBoundary.propTypes = { | ||
| children: PropTypes.node, | ||
| user: PropTypes.shape({ | ||
| authorities: PropTypes.arrayOf(PropTypes.string), | ||
| }), | ||
| } |
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
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,35 @@ | ||
| const { formatUrlSafeAppSlug } = require('./constructAppUrl') | ||
|
|
||
| const APP_AUTH_PREFIX = 'M_' | ||
| const DHIS_WEB = 'dhis-web-' | ||
|
|
||
| /** | ||
| * Returns the string that identifies the 'App view permission' | ||
| * required to view the app | ||
| * | ||
| * Ex: coreApp && name = 'data-visualizer': authName = 'M_dhis-web-data-visualizer' | ||
| * Ex: name = 'pwa-example': authName = 'M_pwaexample' | ||
| * Ex: name = 'BNA Action Tracker': authName = 'M_BNA_Action_Tracker' | ||
| * | ||
| * The 'legacy' parameter specifies server version < 2.35 which uses | ||
| * config.title instead of config.name | ||
| */ | ||
| const formatAppAuthName = ({ config, legacy }) => { | ||
| const appName = legacy ? config.title : config.name | ||
|
|
||
| if (config.coreApp) { | ||
| return APP_AUTH_PREFIX + DHIS_WEB + formatUrlSafeAppSlug(appName) | ||
| } | ||
|
|
||
| // This formatting is drawn from https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java#L494-L499 | ||
| // (replaceAll is only introduced in Node 15) | ||
| return ( | ||
| APP_AUTH_PREFIX + | ||
| appName | ||
| .trim() | ||
| .replace(/[^a-zA-Z0-9\s]/g, '') | ||
| .replace(/\s/g, '_') | ||
| ) | ||
| } | ||
|
|
||
| module.exports = formatAppAuthName |
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,39 @@ | ||
| const formatAppAuthName = require('./formatAppAuthName.js') | ||
|
|
||
| describe('core app handling', () => { | ||
| it('should handle core apps', () => { | ||
| const config = { coreApp: true, name: 'data-visualizer' } | ||
| const formattedAuthName = formatAppAuthName({ config }) | ||
|
|
||
| expect(formattedAuthName).toBe('M_dhis-web-data-visualizer') | ||
| }) | ||
| }) | ||
|
|
||
| describe('non-core app handling', () => { | ||
| it('should handle app names with hyphens', () => { | ||
| const config = { name: 'hyphenated-string-example' } | ||
| const formattedAuthName = formatAppAuthName({ config }) | ||
|
|
||
| expect(formattedAuthName).toBe('M_hyphenatedstringexample') | ||
| }) | ||
|
|
||
| it('should handle app names with capitals and spaces', () => { | ||
| const config = { name: 'Multi Word App Name' } | ||
| const formattedAuthName = formatAppAuthName({ config }) | ||
|
|
||
| expect(formattedAuthName).toBe('M_Multi_Word_App_Name') | ||
| }) | ||
| }) | ||
|
|
||
| describe('legacy app handling', () => { | ||
| it('should use title instead of name if the legacy flag is added', () => { | ||
| const config = { | ||
| name: 'Multi Word App Name', | ||
| title: 'Title of the App', | ||
| } | ||
| const formattedAuthName = formatAppAuthName({ config, legacy: true }) | ||
|
|
||
| expect(formattedAuthName).not.toBe('M_Multi_Word_App_Name') | ||
| expect(formattedAuthName).toBe('M_Title_of_the_App') | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| const config = { | ||
| type: 'app', | ||
| name: 'pwa-example', | ||
| title: 'PWA Example', | ||
|
|
||
| pwa: { | ||
| enabled: true, | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.