forked from gchq/CyberChef
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Re: gchq#172 - Moves section filtering to higher order
- Loading branch information
Showing
3 changed files
with
75 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* A helper function that filters all the sections based on current users permissions | ||
* Checks each sections displayData for hideForUsers, showForUsers and hideForGuests | ||
* Returns an array of sections that the current logged in user has permissions for | ||
*/ | ||
|
||
// Import helper functions from auth, to get current user, and check if guest | ||
import { getCurrentUser, isLoggedInAsGuest } from '@/utils/Auth'; | ||
|
||
/* Helper function, checks if a given username appears in a user array */ | ||
const determineVisibility = (visibilityList, cUsername) => { | ||
let isFound = false; | ||
visibilityList.forEach((userInList) => { | ||
if (userInList.toLowerCase() === cUsername) isFound = true; | ||
}); | ||
return isFound; | ||
}; | ||
|
||
/* Returns false if this section should not be rendered for the current user/ guest */ | ||
const isSectionVisibleToUser = (displayData, currentUser, isGuest) => { | ||
// Checks if user explicitly has access to a certain section | ||
const checkVisiblity = () => { | ||
if (!currentUser) return true; | ||
const hideFor = displayData.hideForUsers || []; | ||
const cUsername = currentUser.user.toLowerCase(); | ||
return !determineVisibility(hideFor, cUsername); | ||
}; | ||
// Checks if user is explicitly prevented from viewing a certain section | ||
const checkHiddenability = () => { | ||
if (!currentUser) return true; | ||
const cUsername = currentUser.user.toLowerCase(); | ||
const showForUsers = displayData.showForUsers || []; | ||
if (showForUsers.length < 1) return true; | ||
return determineVisibility(showForUsers, cUsername); | ||
}; | ||
// Checks if the current user is a guest, and if section allows for guests | ||
const checkIfHideForGuest = () => { | ||
const hideForGuest = displayData.hideForGuests; | ||
return !(hideForGuest && isGuest); | ||
}; | ||
return checkVisiblity() && checkHiddenability() && checkIfHideForGuest(); | ||
}; | ||
|
||
/* Putting it all together, the function to export */ | ||
const filterSectionVisibility = (sections) => { | ||
const currentUser = getCurrentUser(); // Get current user object | ||
const isGuest = isLoggedInAsGuest(); // Check if current user is a guest | ||
// const sectionsToReturn = []; | ||
// sections.forEach((currentSection) => { | ||
// const displayData = currentSection.displayData || {}; | ||
// if (isSectionVisibleToUser(displayData, currentUser, isGuest)) { | ||
// sectionsToReturn.push(currentSection); | ||
// } | ||
// }); | ||
|
||
const filteredSections = sections.filter((currentSection) => { | ||
const displayData = currentSection.displayData || {}; | ||
return isSectionVisibleToUser(displayData, currentUser, isGuest); | ||
}); | ||
|
||
return filteredSections; | ||
}; | ||
|
||
export default filterSectionVisibility; |
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