-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
327 additions
and
61 deletions.
There are no files selected for viewing
19 changes: 18 additions & 1 deletion
19
cmd/api/src/test/integration/harnesses/adinboundcontrolharness.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,90 @@ | ||
// Copyright 2024 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { PermissionsAuthority, PermissionsName, PermissionsSpec } from 'bh-shared-ui'; | ||
import { renderHook } from 'src/test-utils'; | ||
import usePermissions from './usePermissions'; | ||
|
||
describe('usePermissions', () => { | ||
const checkPermissions = (permissions: { has: PermissionsSpec[]; needs: PermissionsSpec[] }) => { | ||
return renderHook(() => usePermissions(permissions.needs), { | ||
initialState: { | ||
auth: { | ||
user: { | ||
roles: [ | ||
{ | ||
permissions: permissions.has, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}).result.current; | ||
}; | ||
|
||
const manageClientsPermission = { | ||
authority: PermissionsAuthority.CLIENTS, | ||
name: PermissionsName.MANAGE_CLIENTS, | ||
}; | ||
|
||
const createTokenPermission = { | ||
authority: PermissionsAuthority.AUTH, | ||
name: PermissionsName.CREATE_TOKEN, | ||
}; | ||
|
||
const manageAppConfigPermission = { | ||
authority: PermissionsAuthority.APP, | ||
name: PermissionsName.MANAGE_APP_CONFIG, | ||
}; | ||
|
||
const allPermissions = [manageClientsPermission, createTokenPermission, manageAppConfigPermission]; | ||
|
||
it('returns true if the user has a required permission', () => { | ||
const hasPermissions = checkPermissions({ | ||
has: [manageClientsPermission], | ||
needs: [manageClientsPermission], | ||
}); | ||
|
||
expect(hasPermissions).toBe(true); | ||
}); | ||
|
||
it('returns true if the user has multiple required permissions', () => { | ||
const hasPermissions = checkPermissions({ | ||
has: allPermissions, | ||
needs: allPermissions, | ||
}); | ||
|
||
expect(hasPermissions).toBe(true); | ||
}); | ||
|
||
it('returns false if the user does not have a matching permission', () => { | ||
const hasPermissions = checkPermissions({ | ||
has: [manageClientsPermission], | ||
needs: [createTokenPermission], | ||
}); | ||
|
||
expect(hasPermissions).toBe(false); | ||
}); | ||
|
||
it('returns false if the user is missing one of many required permissions', () => { | ||
const hasPermissions = checkPermissions({ | ||
has: [manageClientsPermission, createTokenPermission], | ||
needs: allPermissions, | ||
}); | ||
|
||
expect(hasPermissions).toBe(false); | ||
}); | ||
}); |
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,46 @@ | ||
// Copyright 2024 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { getSelfResponse } from 'src/ducks/auth/types'; | ||
import { PermissionsSpec } from 'bh-shared-ui'; | ||
|
||
const usePermissions: (permissions: PermissionsSpec[]) => boolean = (permissions) => { | ||
const [hasAllPermissions, setHasAllPermissions] = useState<boolean>(false); | ||
const auth: { user: getSelfResponse } = useSelector((state: any) => state.auth); | ||
|
||
const doesUserHavePermissions = useCallback( | ||
(user: getSelfResponse): boolean => { | ||
const userPermissions = user.roles.map((role) => role.permissions).flat(); | ||
|
||
return permissions.every((permission) => { | ||
return userPermissions.some((userPermission) => { | ||
return userPermission.authority === permission.authority && userPermission.name === permission.name; | ||
}); | ||
}); | ||
}, | ||
[permissions] | ||
); | ||
|
||
useEffect(() => { | ||
setHasAllPermissions(auth.user && doesUserHavePermissions(auth.user)); | ||
}, [auth, doesUserHavePermissions]); | ||
|
||
return hasAllPermissions; | ||
}; | ||
|
||
export default usePermissions; |
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,50 @@ | ||
// Copyright 2024 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export enum PermissionsAuthority { | ||
APP = 'app', | ||
RISKS = 'risks', | ||
AUTH = 'auth', | ||
CLIENTS = 'clients', | ||
COLLECTION = 'collection', | ||
GRAPHDB = 'graphdb', | ||
SAVED_QUERIES = 'saved_queries', | ||
} | ||
|
||
export enum PermissionsName { | ||
READ_APP_CONFIG = 'ReadAppConfig', | ||
WRITE_APP_CONFIG = 'WriteAppConfig', | ||
GENERATE_REPORT = 'GenerateReport', | ||
MANAGE_RISKS = 'ManageRisks', | ||
CREATE_TOKEN = 'CreateToken', | ||
MANAGE_APP_CONFIG = 'ManageAppConfig', | ||
MANAGE_PROVIDERS = 'ManageProviders', | ||
MANAGE_SELF = 'ManageSelf', | ||
MANAGE_USERS = 'ManageUsers', | ||
MANAGE_CLIENTS = 'Manage', | ||
READ_CLIENTS = 'Read', | ||
CLIENT_TASKING = 'Tasking', | ||
MANAGE_COLLECTION_JOBS = 'ManageJobs', | ||
READ_GRAPHDB = 'Read', | ||
WRITE_GRAPHDB = 'Write', | ||
READ_SAVED_QUERIES = 'Read', | ||
WRITE_SAVED_QUERIES = 'Write', | ||
} | ||
|
||
export type PermissionsSpec = { | ||
authority: PermissionsAuthority; | ||
name: PermissionsName; | ||
}; |