Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter user management groups by the organization of the admin #47

Merged
merged 3 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/midgard/layout/Container/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Items from "../../pages/Items/Items";
import SensorsGateway from "../../pages/SensorsGateway/SensorsGateway";
import Shipment from "../../pages/Shipment/Shipment";
import Dashboard from "../../pages/Dashboard/Dashboard";
import { checkForGlobalAdmin } from "../../utils/utilMethods";
import { checkForAdmin, checkForGlobalAdmin } from "../../utils/utilMethods";
import { isMobile } from "../../utils/mediaQuery";

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -93,7 +93,7 @@ function ContainerDashboard({ location, history, data }) {
render={() => <Redirect to={routes.DASHBOARD} />}
/>
<Route path={routes.DASHBOARD} component={Dashboard} />
{checkForGlobalAdmin(userData) && (
{(checkForAdmin(userData) || checkForGlobalAdmin(userData)) && (
<Route path={routes.USER_MANAGEMENT} component={UserManagement} />
)}
<Route path={routes.CUSTODIANS} component={Custodians} />
Expand Down
8 changes: 4 additions & 4 deletions src/midgard/layout/NavBar/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { makeStyles, useTheme } from "@material-ui/core/styles";
import { AppContext } from "midgard/context/App.context";
import { NAVIGATION_ITEMS } from "./NavBarConstants";
import { isMobile } from "../../utils/mediaQuery";
import { checkForGlobalAdmin } from "../../utils/utilMethods";
import { checkForAdmin, checkForGlobalAdmin } from "../../utils/utilMethods";

const useStyles = makeStyles((theme) => ({
drawer: {
Expand Down Expand Up @@ -60,9 +60,9 @@ const useStyles = makeStyles((theme) => ({
}));

const getFilteredNavItems = (userData, navItems) => {
let isGlobalAdmin = checkForGlobalAdmin(userData);

if (isGlobalAdmin) return navItems;
let isAdmin = checkForAdmin(userData) || checkForGlobalAdmin(userData);
if (isAdmin) return navItems;
else
return navItems.filter((nav) => {
return nav.id !== "user_management";
Expand Down
2 changes: 1 addition & 1 deletion src/midgard/pages/Register/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Register({ dispatch, loading, history, loaded, error }) {
username: username.value,
email: email.value,
password: password.value,
organization: { name: organization_name.value },
organization_name: organization_name.value,
first_name: first_name.value,
last_name: last_name.value,
};
Expand Down
24 changes: 21 additions & 3 deletions src/midgard/pages/UserManagement/Users/Users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useContext } from 'react'
import { PermissionsTable } from 'midgard/components/PermissionsTable/PermissionsTable';
import Crud from 'midgard/modules/crud/Crud';
import { getCoreGroups } from 'midgard/redux/coregroup/actions/coregroup.actions'
Expand All @@ -13,6 +13,8 @@ import Box from "@material-ui/core/Box";
import { makeStyles } from '@material-ui/core/styles';
import { rem } from 'polished';
import { Typography } from '@material-ui/core';
import { checkForGlobalAdmin, checkForAdmin } from "midgard/utils/utilMethods";
import { UserContext } from "midgard/context/User.context";

const useStyles = makeStyles((theme) => ({
btnPermission: {
Expand All @@ -29,7 +31,10 @@ function Users({ location, history, data, dispatch }) {
const [menu, setMenu] = useState({ row: null, element: null });
const [coreGroupsLoaded, setCoreGroupsLoaded] = useState(false);
const [permissions, setPermissions] = useState([]);

// to get currently logged in user
const user = useContext(UserContext);
const isOrganizationAdmin = checkForAdmin(user);

useEffect(() => {
if (!coreGroupsLoaded) {
dispatch(getCoreGroups());
Expand Down Expand Up @@ -139,10 +144,23 @@ function Users({ location, history, data, dispatch }) {
});
}
return (
isOrganizationAdmin ?
<PermissionsTable
columns={[
{ label: 'Full name', prop: 'name', template: (row) => {return <Typography variant="body1" style={!row.is_active? {'color': '#aaa'}: null}>{row.first_name} {row.last_name}</Typography>}, flex: '1' },
{ label: 'Email', prop: 'email', flex: '2', template: (row) => {return <Typography variant="body2" style={!row.is_active? {'color': '#aaa'}: null}>{row.email}</Typography>}},
{ label: 'Last activity', prop: 'activity', template: (row) => {return <Typography variant="caption" style={{'color': '#aaa'}}>Today</Typography>}, flex: '1' },
{ label: 'Permissions', prop: 'permission', template: (row) => permissionsTemplate(row, crud, classes), flex: '2' },
{ label: 'Actions', prop: 'options', template: (row) => actionsTemplate(row, crud), flex: '1' },
]}
rows={crud.getData()}
/>
:
<PermissionsTable
columns={[
{ label: 'Full name', prop: 'name', template: (row) => {return <Typography variant="body1" style={!row.is_active? {'color': '#aaa'}: null}>{row.first_name} {row.last_name}</Typography>}, flex: '1' },
{ label: 'Email', prop: 'email', flex: '2', template: (row) => {return <Typography variant="body2" style={!row.is_active? {'color': '#aaa'}: null}> {row.email}</Typography>}},
{ label: 'Email', prop: 'email', flex: '2', template: (row) => {return <Typography variant="body2" style={!row.is_active? {'color': '#aaa'}: null}>{row.email}</Typography>}},
{ label: 'Organization name', prop: 'organization', flex: '2', template: (row) => {return <Typography variant="body2" style={!row.is_active? {'color': '#aaa'}: null}>{row.organization.name}</Typography>}},
{ label: 'Last activity', prop: 'activity', template: (row) => {return <Typography variant="caption" style={{'color': '#aaa'}}>Today</Typography>}, flex: '1' },
{ label: 'Permissions', prop: 'permission', template: (row) => permissionsTemplate(row, crud, classes), flex: '2' },
{ label: 'Actions', prop: 'options', template: (row) => actionsTemplate(row, crud), flex: '1' },
Expand Down
17 changes: 16 additions & 1 deletion src/midgard/utils/utilMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,29 @@ export const checkForGlobalAdmin = (userData) => {
let isGlobalAdmin = false;
if (userData && userData.core_groups) {
userData.core_groups.map((group) => {
if (group.is_global) {
if (group.is_global &&
Object.keys(group.permissions).every((permission) => group.permissions[permission] === true)) {
isGlobalAdmin = true;
}
});
}
return isGlobalAdmin;
};

export const checkForAdmin = (userData) => {
let isAdmin = false;
if (userData && userData.core_groups) {
userData.core_groups.map((group) => {
if (group.is_org_level &&
Object.keys(group.permissions).every((permission) => group.permissions[permission] === true) &&
!group.is_global) {
isAdmin = true;
}
});
}
return isAdmin;
}

export const setOptionsData = (options, fieldName) => {
let result = null;
let optionKeys = Object.keys(options);
Expand Down