updated get permissions to take in the keys and types to get the correct permissions using the context chain
This commit is contained in:
parent
2c28588861
commit
d3a5c24741
2 changed files with 16 additions and 17 deletions
|
|
@ -3,7 +3,7 @@ import { Gate as IGate } from "models/Gate";
|
||||||
//import { Redirect, useHistory, useRouteMatch } from "react-router";
|
//import { Redirect, useHistory, useRouteMatch } from "react-router";
|
||||||
import React, { useCallback, useEffect, useState } from "react";
|
import React, { useCallback, useEffect, useState } from "react";
|
||||||
import PageContainer from "./PageContainer";
|
import PageContainer from "./PageContainer";
|
||||||
import { useGlobalState, useGateAPI, useUserAPI } from "providers";
|
import { useGlobalState, useGateAPI } from "providers";
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
ButtonBase,
|
ButtonBase,
|
||||||
|
|
@ -20,7 +20,7 @@ import {
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { pond } from "protobuf-ts/pond";
|
import { pond } from "protobuf-ts/pond";
|
||||||
import DeviceLinkDrawer from "common/DeviceLinkDrawer";
|
import DeviceLinkDrawer from "common/DeviceLinkDrawer";
|
||||||
import { Component, Device, Scope } from "models";
|
import { Component, Device } from "models";
|
||||||
import GateActions from "gate/GateActions";
|
import GateActions from "gate/GateActions";
|
||||||
import GateDevice from "gate/GateDevice";
|
import GateDevice from "gate/GateDevice";
|
||||||
import ObjectControls from "common/ObjectControls";
|
import ObjectControls from "common/ObjectControls";
|
||||||
|
|
@ -28,7 +28,7 @@ import { Link } from "@mui/icons-material";
|
||||||
import Chat from "chat/Chat";
|
import Chat from "chat/Chat";
|
||||||
import PlaneIcon from "products/AviationIcons/PlaneIcon";
|
import PlaneIcon from "products/AviationIcons/PlaneIcon";
|
||||||
import NotesIcon from "@mui/icons-material/Notes";
|
import NotesIcon from "@mui/icons-material/Notes";
|
||||||
import { useMobile, useSnackbar, useThemeType } from "hooks";
|
import { useMobile, usePermissionAPI, useSnackbar, useThemeType } from "hooks";
|
||||||
import { clone } from "lodash";
|
import { clone } from "lodash";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { makeStyles } from "@mui/styles";
|
import { makeStyles } from "@mui/styles";
|
||||||
|
|
@ -90,7 +90,7 @@ export default function Gate(props: Props) {
|
||||||
const gateID = gateKey ?? useParams<{ gateKey: string }>()?.gateKey ?? "";
|
const gateID = gateKey ?? useParams<{ gateKey: string }>()?.gateKey ?? "";
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const gateAPI = useGateAPI();
|
const gateAPI = useGateAPI();
|
||||||
const userAPI = useUserAPI();
|
const permissionAPI = usePermissionAPI();
|
||||||
const [gate, setGate] = useState<IGate>(IGate.create());
|
const [gate, setGate] = useState<IGate>(IGate.create());
|
||||||
const [devices, setDevices] = useState<Map<string, pond.ComprehensiveDevice>>(
|
const [devices, setDevices] = useState<Map<string, pond.ComprehensiveDevice>>(
|
||||||
new Map<string, pond.ComprehensiveDevice>()
|
new Map<string, pond.ComprehensiveDevice>()
|
||||||
|
|
@ -115,16 +115,10 @@ export default function Gate(props: Props) {
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let key = gateID;
|
permissionAPI.getPermissions(user.id(), [gateID], ["gate"]).then(resp => {
|
||||||
let kind = "gate";
|
setPermissions(pond.EvaluatePermissionsResponse.fromObject(resp.data).permissions)
|
||||||
if (as) {
|
})
|
||||||
key = as;
|
}, [as, gateID, permissionAPI, user]);
|
||||||
kind = "team";
|
|
||||||
}
|
|
||||||
userAPI.getUser(user.id(), { key: key, kind: kind } as Scope).then(resp => {
|
|
||||||
setPermissions(resp.permissions);
|
|
||||||
});
|
|
||||||
}, [as, gateID, userAPI, user]);
|
|
||||||
|
|
||||||
const loadGate = useCallback(() => {
|
const loadGate = useCallback(() => {
|
||||||
let id = gateID;
|
let id = gateID;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import { createContext, PropsWithChildren, useContext } from "react";
|
||||||
import { objectQueryParams, pondURL } from "./pond";
|
import { objectQueryParams, pondURL } from "./pond";
|
||||||
|
|
||||||
export interface IPermissionAPIContext {
|
export interface IPermissionAPIContext {
|
||||||
getPermissions: (user: string, scope: Scope) => Promise<AxiosResponse<any>>;
|
getPermissions: (user: string, keys: string[], types: string[]) => Promise<AxiosResponse<pond.EvaluatePermissionsResponse>>;
|
||||||
removePermissions: (user: string, scope: Scope) => Promise<AxiosResponse<any>>;
|
removePermissions: (user: string, scope: Scope) => Promise<AxiosResponse<any>>;
|
||||||
updatePermissions: (scope: Scope, users: User[] | Team[]) => Promise<AxiosResponse<any>>;
|
updatePermissions: (scope: Scope, users: User[] | Team[]) => Promise<AxiosResponse<any>>;
|
||||||
updateRelativePermissions: (
|
updateRelativePermissions: (
|
||||||
|
|
@ -44,9 +44,12 @@ export default function PermissionProvider(props: PropsWithChildren<Props>) {
|
||||||
const { get, del, put, post } = useHTTP();
|
const { get, del, put, post } = useHTTP();
|
||||||
const [{ as }] = useGlobalState();
|
const [{ as }] = useGlobalState();
|
||||||
|
|
||||||
const getPermissions = (user: string, scope: Scope) => {
|
const getPermissions = (user: string, keys: string[], types: string[]) => {
|
||||||
return new Promise<AxiosResponse>((resolve, reject) => {
|
return new Promise<AxiosResponse>((resolve, reject) => {
|
||||||
get(pondURL("/users/" + user + "/permissions" + objectQueryParams(scope))).then(resp => {
|
get<pond.EvaluatePermissionsResponse>(pondURL(
|
||||||
|
"/users/" + user + "/permissions?types=" + types.toString() + "&keys=" + keys.toString() +
|
||||||
|
(as && "&as=" + as)
|
||||||
|
)).then(resp => {
|
||||||
return resolve(resp)
|
return resolve(resp)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
|
|
@ -54,6 +57,8 @@ export default function PermissionProvider(props: PropsWithChildren<Props>) {
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const removePermissions = (user: string, scope: Scope) => {
|
const removePermissions = (user: string, scope: Scope) => {
|
||||||
return new Promise<AxiosResponse>((resolve, reject) => {
|
return new Promise<AxiosResponse>((resolve, reject) => {
|
||||||
del(pondURL("/users/" + user + "/permissions" + objectQueryParams(scope))).then(resp => {
|
del(pondURL("/users/" + user + "/permissions" + objectQueryParams(scope))).then(resp => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue