-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboardSecurity): enhance typing and add docs
- Loading branch information
Showing
6 changed files
with
193 additions
and
34 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
plugins/wazuh-core/public/services/dashboard-security/README.md
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,67 @@ | ||
# Dashboard security | ||
|
||
The `dashboardSecurity` service is created in the core plugin and manage the security related to the Wazuh dashboard. | ||
|
||
- Fetch data about the security platform (Wazuh dashboard security enabled or disabled) | ||
- Store information about the current user account data | ||
- administrator | ||
- administrator requirements | ||
- Expose hooks and HOCs for using with ReactJS | ||
|
||
## Get account data | ||
|
||
### Using the service | ||
|
||
```ts | ||
plugins.wazuhCore.dashboardSecurity.account; | ||
``` | ||
|
||
### In ReactJS components | ||
|
||
- hook | ||
|
||
```ts | ||
const MyComponent = props => { | ||
const [dashboardSecurityAccount, setDashboardSecurityAccount] = | ||
getWazuhCorePlugin().hooks.useDashboardSecurityAccount(); | ||
}; | ||
``` | ||
|
||
- HOC | ||
|
||
```ts | ||
const MyComponent = getWazuhCorePlugin().hocs.withDashboardSecurityAccount( | ||
({ dashboardSecurityAccount }) => { | ||
// dashboardSecurityAccount contains the dashboard account data | ||
}, | ||
); | ||
``` | ||
|
||
## Get if the user is an administrator | ||
|
||
### Using the service | ||
|
||
```ts | ||
plugins.wazuhCore.dashboardSecurity.account.administrator; | ||
``` | ||
|
||
### In ReactJS components | ||
|
||
- hook | ||
|
||
```ts | ||
const MyComponent = props => { | ||
const dashboardSecurityAccountAdmin = | ||
getWazuhCorePlugin().hooks.useDashboardSecurityAccountAdmin(); | ||
}; | ||
``` | ||
|
||
- HOC | ||
|
||
```ts | ||
const MyComponent = getWazuhCorePlugin().hocs.withDashboardSecurityAccountAdmin( | ||
({ dashboardSecurityAccountAdmin }) => { | ||
// dashboardSecurityAccountAdmin contains if the user is admin or not | ||
}, | ||
); | ||
``` |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './dashboard-security'; | ||
export * from './types'; |
40 changes: 40 additions & 0 deletions
40
plugins/wazuh-core/public/services/dashboard-security/types.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,40 @@ | ||
import React from 'react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
export interface DashboardSecurityServiceAccount { | ||
administrator: boolean; | ||
administrator_requirements: string | null; | ||
} | ||
|
||
export interface DashboardSecurityServiceSetupReturn { | ||
hooks: { | ||
useDashboardSecurityAccount: () => [ | ||
DashboardSecurityServiceAccount, | ||
(accountData: any) => void, | ||
]; | ||
useDashboardSecurityIsAdmin: () => boolean; | ||
}; | ||
hocs: { | ||
// FIXME: enhance typing | ||
withDashboardSecurityAccount: ( | ||
WrappedComponent: React.ElementType, | ||
) => (props: any) => React.ElementRef; | ||
withDashboardSecurityAccountAdmin: ( | ||
WrappedComponent: React.ElementType, | ||
) => (props: any) => React.ElementRef; | ||
}; | ||
} | ||
|
||
export interface DashboardSecurityServiceSetupDeps { | ||
updateData$: BehaviorSubject<DashboardSecurityServiceAccount>; | ||
} | ||
|
||
export interface DashboardSecurityService { | ||
account: DashboardSecurityServiceAccount; | ||
account$: BehaviorSubject<DashboardSecurityServiceAccount>; | ||
setup: ( | ||
deps: DashboardSecurityServiceSetupDeps, | ||
) => Promise<DashboardSecurityServiceSetupReturn>; | ||
start: () => void; | ||
stop: () => void; | ||
} |
38 changes: 38 additions & 0 deletions
38
plugins/wazuh-core/public/services/dashboard-security/ui/hocs/creator.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,38 @@ | ||
import React from 'react'; | ||
import { DashboardSecurityServiceSetupReturn } from '../../types'; | ||
|
||
export function createDashboardSecurityHOCs({ | ||
useDashboardSecurityAccount, | ||
useDashboardSecurityIsAdmin, | ||
}: DashboardSecurityServiceSetupReturn['hooks']): DashboardSecurityServiceSetupReturn['hocs'] { | ||
const withDashboardSecurityAccount = (WrappedComponent: React.ElementType) => | ||
function WithDashboardSecurityAccount(props: any) { | ||
const dashboardSecurityAccount = useDashboardSecurityAccount(); | ||
|
||
return ( | ||
<WrappedComponent | ||
{...props} | ||
dashboardSecurityAccount={dashboardSecurityAccount} | ||
/> | ||
); | ||
}; | ||
|
||
const withDashboardSecurityAccountAdmin = ( | ||
WrappedComponent: React.ElementType, | ||
) => | ||
function WithDashboardSecurityAccountAdmin(props: any) { | ||
const dashboardSecurityAccountAdmin = useDashboardSecurityIsAdmin(); | ||
|
||
return ( | ||
<WrappedComponent | ||
{...props} | ||
dashboardSecurityAccountAdmin={dashboardSecurityAccountAdmin} | ||
/> | ||
); | ||
}; | ||
|
||
return { | ||
withDashboardSecurityAccount, | ||
withDashboardSecurityAccountAdmin, | ||
}; | ||
} |
23 changes: 23 additions & 0 deletions
23
plugins/wazuh-core/public/services/dashboard-security/ui/hooks/creator.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,23 @@ | ||
import { | ||
DashboardSecurityService, | ||
DashboardSecurityServiceSetupReturn, | ||
} from '../../types'; | ||
|
||
export function createDashboardSecurityHooks({ | ||
account$, | ||
}: { | ||
account$: DashboardSecurityService['account$']; | ||
}): DashboardSecurityServiceSetupReturn['hooks'] { | ||
const setAccount = data => { | ||
account$.next(data); | ||
}; | ||
|
||
return { | ||
useDashboardSecurityAccount: function useDashboardSecurityAccount() { | ||
return [account$.getValue(), setAccount]; | ||
}, | ||
useDashboardSecurityIsAdmin: function useDashboardSecurityIsAdmin() { | ||
return account$.getValue()?.administrator; | ||
}, | ||
}; | ||
} |