-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(organizations): update page access permissions TASK-977 (#5219)
## Description For various pages in account settings, we previously used a wrapper component to verify whether the user was the owner of their org before allowing them to access the page. With the organizations project, this permissions check needs to allow for checking multiple role options. We also need to be able to check whether an org is an mmo for the members page. This PR overhauls the old wrapper component to allow for these checks.
- Loading branch information
1 parent
be9db29
commit 70a8673
Showing
3 changed files
with
84 additions
and
43 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
jsapp/js/account/organizations/requireOrgOwner.component.tsx
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
jsapp/js/account/organizations/validateOrgPermissions.component.tsx
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,49 @@ | ||
import React, {Suspense, useEffect} from 'react'; | ||
import {useNavigate} from 'react-router-dom'; | ||
import LoadingSpinner from 'js/components/common/loadingSpinner'; | ||
import {ACCOUNT_ROUTES} from 'js/account/routes.constants'; | ||
import {useOrganizationQuery} from 'js/account/stripe.api'; | ||
import {OrganizationUserRole} from '../stripe.types'; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
validRoles?: OrganizationUserRole[]; | ||
mmoOnly?: boolean; | ||
redirect?: boolean; | ||
} | ||
|
||
/** | ||
* Use to handle display of pages that should only be accessible to certain user roles | ||
* or members of MMOs. Defaults to allowing access for all users, so you must supply | ||
* any restrictions. | ||
*/ | ||
export const ValidateOrgPermissions = ({ | ||
children, | ||
validRoles = undefined, | ||
mmoOnly = false, | ||
redirect = true, | ||
}: Props) => { | ||
const navigate = useNavigate(); | ||
const orgQuery = useOrganizationQuery(); | ||
const hasValidRole = validRoles ? validRoles.includes( | ||
orgQuery.data?.request_user_role ?? OrganizationUserRole.member | ||
) : true; | ||
const hasValidOrg = mmoOnly ? orgQuery.data?.is_mmo : true; | ||
|
||
// Redirect to Account Settings if conditions not met | ||
useEffect(() => { | ||
if ( | ||
redirect && | ||
orgQuery.data && | ||
(!hasValidRole || !hasValidOrg) | ||
) { | ||
navigate(ACCOUNT_ROUTES.ACCOUNT_SETTINGS); | ||
} | ||
}, [redirect, orgQuery.data, navigate]); | ||
|
||
return redirect && hasValidRole && hasValidOrg ? ( | ||
<Suspense fallback={null}>{children}</Suspense> | ||
) : ( | ||
<LoadingSpinner /> | ||
); | ||
}; |
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