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 React, { useCallback, useEffect, useState } from "react";
|
||||
import PageContainer from "./PageContainer";
|
||||
import { useGlobalState, useGateAPI, useUserAPI } from "providers";
|
||||
import { useGlobalState, useGateAPI } from "providers";
|
||||
import {
|
||||
Box,
|
||||
ButtonBase,
|
||||
|
|
@ -20,7 +20,7 @@ import {
|
|||
} from "@mui/material";
|
||||
import { pond } from "protobuf-ts/pond";
|
||||
import DeviceLinkDrawer from "common/DeviceLinkDrawer";
|
||||
import { Component, Device, Scope } from "models";
|
||||
import { Component, Device } from "models";
|
||||
import GateActions from "gate/GateActions";
|
||||
import GateDevice from "gate/GateDevice";
|
||||
import ObjectControls from "common/ObjectControls";
|
||||
|
|
@ -28,7 +28,7 @@ import { Link } from "@mui/icons-material";
|
|||
import Chat from "chat/Chat";
|
||||
import PlaneIcon from "products/AviationIcons/PlaneIcon";
|
||||
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 { useNavigate, useParams } from "react-router-dom";
|
||||
import { makeStyles } from "@mui/styles";
|
||||
|
|
@ -90,7 +90,7 @@ export default function Gate(props: Props) {
|
|||
const gateID = gateKey ?? useParams<{ gateKey: string }>()?.gateKey ?? "";
|
||||
const classes = useStyles();
|
||||
const gateAPI = useGateAPI();
|
||||
const userAPI = useUserAPI();
|
||||
const permissionAPI = usePermissionAPI();
|
||||
const [gate, setGate] = useState<IGate>(IGate.create());
|
||||
const [devices, setDevices] = useState<Map<string, pond.ComprehensiveDevice>>(
|
||||
new Map<string, pond.ComprehensiveDevice>()
|
||||
|
|
@ -115,16 +115,10 @@ export default function Gate(props: Props) {
|
|||
};
|
||||
|
||||
useEffect(() => {
|
||||
let key = gateID;
|
||||
let kind = "gate";
|
||||
if (as) {
|
||||
key = as;
|
||||
kind = "team";
|
||||
}
|
||||
userAPI.getUser(user.id(), { key: key, kind: kind } as Scope).then(resp => {
|
||||
setPermissions(resp.permissions);
|
||||
});
|
||||
}, [as, gateID, userAPI, user]);
|
||||
permissionAPI.getPermissions(user.id(), [gateID], ["gate"]).then(resp => {
|
||||
setPermissions(pond.EvaluatePermissionsResponse.fromObject(resp.data).permissions)
|
||||
})
|
||||
}, [as, gateID, permissionAPI, user]);
|
||||
|
||||
const loadGate = useCallback(() => {
|
||||
let id = gateID;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import { createContext, PropsWithChildren, useContext } from "react";
|
|||
import { objectQueryParams, pondURL } from "./pond";
|
||||
|
||||
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>>;
|
||||
updatePermissions: (scope: Scope, users: User[] | Team[]) => Promise<AxiosResponse<any>>;
|
||||
updateRelativePermissions: (
|
||||
|
|
@ -44,9 +44,12 @@ export default function PermissionProvider(props: PropsWithChildren<Props>) {
|
|||
const { get, del, put, post } = useHTTP();
|
||||
const [{ as }] = useGlobalState();
|
||||
|
||||
const getPermissions = (user: string, scope: Scope) => {
|
||||
const getPermissions = (user: string, keys: string[], types: string[]) => {
|
||||
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)
|
||||
}).catch(err => {
|
||||
return reject(err)
|
||||
|
|
@ -54,6 +57,8 @@ export default function PermissionProvider(props: PropsWithChildren<Props>) {
|
|||
})
|
||||
};
|
||||
|
||||
|
||||
|
||||
const removePermissions = (user: string, scope: Scope) => {
|
||||
return new Promise<AxiosResponse>((resolve, reject) => {
|
||||
del(pondURL("/users/" + user + "/permissions" + objectQueryParams(scope))).then(resp => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue