Skip to content

Commit c99137a

Browse files
Organization user affiliation error (canada-ca#2520)
* add isAdmin check to Users Tab * add API support
1 parent c932fe9 commit c99137a

4 files changed

Lines changed: 59 additions & 11 deletions

File tree

frontend/mocking/faked_schema.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export const getTypeNames = () => gql`
9696
): SharedUser
9797
9898
# Query used to check if the user has an admin role.
99-
isUserAdmin: Boolean
99+
isUserAdmin(
100+
# The organization that you want to check for admin permissions on.
101+
orgId: ID!
102+
): Boolean
100103
101104
# Query used to check if the user has a super admin role.
102105
isUserSuperAdmin: Boolean

frontend/src/OrganizationDetails.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
Tabs,
1515
useToast,
1616
} from '@chakra-ui/core'
17-
import { ORG_DETAILS_PAGE } from './graphql/queries'
17+
import { ORG_DETAILS_PAGE, IS_USER_ADMIN } from './graphql/queries'
1818
import { Link as RouteLink, useParams } from 'react-router-dom'
1919
import { OrganizationSummary } from './OrganizationSummary'
2020
import { ErrorBoundary } from 'react-error-boundary'
@@ -46,6 +46,29 @@ export default function OrganizationDetails() {
4646
},
4747
})
4848

49+
const orgId = data?.organization?.id
50+
const { data: isAdminData } = useQuery(IS_USER_ADMIN, {
51+
skip: !orgId,
52+
variables: { orgId: orgId },
53+
54+
onError: (error) => {
55+
const [_, message] = error.message.split(': ')
56+
toast({
57+
title: 'Error',
58+
description: message,
59+
status: 'error',
60+
duration: 9000,
61+
isClosable: true,
62+
position: 'top-left',
63+
})
64+
},
65+
})
66+
67+
let isAdmin = false
68+
if (isAdminData?.isUserAdmin) {
69+
isAdmin = isAdminData.isUserAdmin
70+
}
71+
4972
let orgName = ''
5073
if (data?.organization) {
5174
orgName = data.organization.name
@@ -85,9 +108,11 @@ export default function OrganizationDetails() {
85108
<Tab>
86109
<Trans>Domains</Trans>
87110
</Tab>
88-
<Tab>
89-
<Trans>Users</Trans>
90-
</Tab>
111+
{isAdmin && (
112+
<Tab>
113+
<Trans>Users</Trans>
114+
</Tab>
115+
)}
91116
</TabList>
92117

93118
<TabPanels>
@@ -107,11 +132,13 @@ export default function OrganizationDetails() {
107132
<OrganizationDomains orgSlug={orgSlug} domainsPerPage={10} />
108133
</ErrorBoundary>
109134
</TabPanel>
110-
<TabPanel>
111-
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
112-
<OrganizationAffiliations orgSlug={orgSlug} usersPerPage={10} />
113-
</ErrorBoundary>
114-
</TabPanel>
135+
{isAdmin && (
136+
<TabPanel>
137+
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
138+
<OrganizationAffiliations orgSlug={orgSlug} usersPerPage={10} />
139+
</ErrorBoundary>
140+
</TabPanel>
141+
)}
115142
</TabPanels>
116143
</Tabs>
117144
</Layout>

frontend/src/__tests__/OrganizationDetails.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MockedProvider } from '@apollo/client/testing'
44
import { MemoryRouter, Route } from 'react-router-dom'
55
import { theme, ThemeProvider } from '@chakra-ui/core'
66
import { UserVarProvider } from '../UserState'
7-
import { ORG_DETAILS_PAGE } from '../graphql/queries'
7+
import { ORG_DETAILS_PAGE, IS_USER_ADMIN } from '../graphql/queries'
88
import { I18nProvider } from '@lingui/react'
99
import { setupI18n } from '@lingui/core'
1010
import OrganizationDetails from '../OrganizationDetails'
@@ -110,6 +110,17 @@ describe('<OrganizationDetails />', () => {
110110
},
111111
},
112112
},
113+
{
114+
request: {
115+
query: IS_USER_ADMIN,
116+
variables: { orgId: 'ODk3MDg5MzI2MA==' },
117+
},
118+
result: {
119+
data: {
120+
isUserAdmin: true,
121+
},
122+
},
123+
},
113124
]
114125

115126
const { getByText } = render(
@@ -139,6 +150,7 @@ describe('<OrganizationDetails />', () => {
139150

140151
await waitFor(() => {
141152
expect(getByText(name)).toBeInTheDocument()
153+
expect(getByText('Users')).toBeInTheDocument()
142154
})
143155
})
144156
})

frontend/src/graphql/queries.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,3 +1368,9 @@ export const IS_USER_SUPER_ADMIN = gql`
13681368
isUserSuperAdmin
13691369
}
13701370
`
1371+
1372+
export const IS_USER_ADMIN = gql`
1373+
query IsUserAdmin($orgId: ID!) {
1374+
isUserAdmin(orgId: $orgId)
1375+
}
1376+
`

0 commit comments

Comments
 (0)