-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rbac): display administration nav to authorized users
- Loading branch information
Showing
12 changed files
with
2,278 additions
and
2,190 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
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,54 @@ | ||
import { | ||
ConfigApi, | ||
createApiRef, | ||
IdentityApi, | ||
} from '@backstage/core-plugin-api'; | ||
|
||
// @public | ||
export type RBACAPI = { | ||
getUserAuthorization: () => Promise<{ status: string }>; | ||
getRoles: () => Promise<any>; | ||
}; | ||
|
||
export type Options = { | ||
configApi: ConfigApi; | ||
identityApi: IdentityApi; | ||
}; | ||
|
||
// @public | ||
export const rbacApiRef = createApiRef<RBACAPI>({ | ||
id: 'plugin.rbac.service', | ||
}); | ||
|
||
export class RBACBackendClient implements RBACAPI { | ||
// @ts-ignore | ||
private readonly configApi: ConfigApi; | ||
private readonly identityApi: IdentityApi; | ||
|
||
constructor(options: Options) { | ||
this.configApi = options.configApi; | ||
this.identityApi = options.identityApi; | ||
} | ||
|
||
async getUserAuthorization() { | ||
const { token: idToken } = await this.identityApi.getCredentials(); | ||
const backendUrl = this.configApi.getString('backend.baseUrl'); | ||
const jsonResponse = await fetch(`${backendUrl}/api/permission/`, { | ||
headers: { | ||
...(idToken && { Authorization: `Bearer ${idToken}` }), | ||
}, | ||
}); | ||
return jsonResponse.json(); | ||
} | ||
|
||
async getRoles() { | ||
const { token: idToken } = await this.identityApi.getCredentials(); | ||
const backendUrl = this.configApi.getString('backend.baseUrl'); | ||
const jsonResponse = await fetch(`${backendUrl}/api/permission/roles`, { | ||
headers: { | ||
...(idToken && { Authorization: `Bearer ${idToken}` }), | ||
}, | ||
}); | ||
return jsonResponse.json(); | ||
} | ||
} |
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,28 @@ | ||
import React from 'react'; | ||
import { useAsync } from 'react-use'; | ||
|
||
import { SidebarItem } from '@backstage/core-components'; | ||
import { IconComponent, useApi } from '@backstage/core-plugin-api'; | ||
|
||
import AdminPanelSettingsOutlinedIcon from '@mui/icons-material/AdminPanelSettingsOutlined'; | ||
|
||
import { rbacApiRef } from '../api/RBACBackendClient'; | ||
|
||
export const Administration = () => { | ||
const rbacApi = useApi(rbacApiRef); | ||
const { loading: isUserLoading, value: result } = useAsync( | ||
async () => await rbacApi.getUserAuthorization(), | ||
[], | ||
); | ||
|
||
if (!isUserLoading) { | ||
return result?.status === 'Authorized' ? ( | ||
<SidebarItem | ||
text="Administration" | ||
to="rbac" | ||
icon={AdminPanelSettingsOutlinedIcon as IconComponent} | ||
/> | ||
) : null; | ||
} | ||
return null; | ||
}; |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
export { RbacPage } from './RbacPage'; | ||
export { Administration } from './Administration'; |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { rbacPlugin, RbacPage } from './plugin'; | ||
export { rbacPlugin, RbacPage, Administration } from './plugin'; |
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 |
---|---|---|
@@ -1,21 +1,52 @@ | ||
import { | ||
configApiRef, | ||
createApiFactory, | ||
createComponentExtension, | ||
createPlugin, | ||
createRoutableExtension, | ||
identityApiRef, | ||
} from '@backstage/core-plugin-api'; | ||
|
||
import { rbacApiRef, RBACBackendClient } from './api/RBACBackendClient'; | ||
import { rootRouteRef } from './routes'; | ||
|
||
export const rbacPlugin = createPlugin({ | ||
id: 'rbac', | ||
routes: { | ||
root: rootRouteRef, | ||
}, | ||
apis: [ | ||
createApiFactory({ | ||
api: rbacApiRef, | ||
deps: { | ||
configApi: configApiRef, | ||
identityApi: identityApiRef, | ||
}, | ||
factory: ({ configApi, identityApi }) => | ||
new RBACBackendClient({ configApi, identityApi }), | ||
}), | ||
], | ||
}); | ||
|
||
/** | ||
* @public | ||
*/ | ||
export const RbacPage = rbacPlugin.provide( | ||
createRoutableExtension({ | ||
name: 'RbacPage', | ||
component: () => import('./components/RbacPage').then(m => m.RbacPage), | ||
component: () => import('./components').then(m => m.RbacPage), | ||
mountPoint: rootRouteRef, | ||
}), | ||
); | ||
|
||
/** | ||
* @public | ||
*/ | ||
export const Administration = rbacPlugin.provide( | ||
createComponentExtension({ | ||
name: 'Administration', | ||
component: { | ||
lazy: () => import('./components').then(m => m.Administration), | ||
}, | ||
}), | ||
); |
Oops, something went wrong.