From 53e5765956768a53b6943da97bb36a55cdfeabbe Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 26 May 2021 12:52:45 +0100 Subject: [PATCH] Add PolicyDelete page. --- src/allPages.ts | 2 ++ src/api/sys/policies/deletePolicy.ts | 21 ++++++++++++ src/api/sys/{ => policies}/getPolicies.ts | 2 +- src/api/sys/{ => policies}/getPolicy.ts | 2 +- src/pages/Policies/PoliciesHome.tsx | 2 +- src/pages/Policies/PolicyDelete.tsx | 42 +++++++++++++++++++++++ src/pages/Policies/PolicyView.tsx | 9 +++-- src/translations/en.js | 6 ++++ 8 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/api/sys/policies/deletePolicy.ts rename src/api/sys/{ => policies}/getPolicies.ts (84%) rename src/api/sys/{ => policies}/getPolicy.ts (84%) create mode 100644 src/pages/Policies/PolicyDelete.tsx diff --git a/src/allPages.ts b/src/allPages.ts index 75a8326..b212942 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -22,6 +22,7 @@ import { NewTransitEnginePage } from "./pages/Secrets/NewEngines/NewTransitEngin import { NewTransitKeyPage } from "./pages/Secrets/Transit/NewTransitKey"; import { Page } from "./types/Page"; import { PoliciesHomePage } from "./pages/Policies/PoliciesHome"; +import { PolicyDeletePage } from "./pages/Policies/PolicyDelete"; import { PolicyViewPage } from "./pages/Policies/PolicyView"; import { PwGenPage } from "./pages/PwGen"; import { SecretsHomePage } from "./pages/Secrets/SecretsHome"; @@ -58,6 +59,7 @@ export const allPages: pagesList = { POLICIES_HOME: new PoliciesHomePage(), POLICY_VIEW: new PolicyViewPage(), + POLICY_DELETE: new PolicyDeletePage(), ACCESS_HOME: new AccessHomePage(), diff --git a/src/api/sys/policies/deletePolicy.ts b/src/api/sys/policies/deletePolicy.ts new file mode 100644 index 0000000..99c19fb --- /dev/null +++ b/src/api/sys/policies/deletePolicy.ts @@ -0,0 +1,21 @@ +import { appendAPIURL, getHeaders } from "../../apiUtils"; + +type OptionalErrors = { errors?: string[] }; + +export async function deletePolicy(name: string): Promise { + const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { + method: "DELETE", + headers: getHeaders(), + }); + const response = await fetch(request); + let data: OptionalErrors = {}; + try { + data = (await response.json()) as OptionalErrors; + } catch { + // Do Nothing + } + + if ("errors" in data) { + throw new Error(data.errors[0]); + } +} diff --git a/src/api/sys/getPolicies.ts b/src/api/sys/policies/getPolicies.ts similarity index 84% rename from src/api/sys/getPolicies.ts rename to src/api/sys/policies/getPolicies.ts index 5868064..4825b13 100644 --- a/src/api/sys/getPolicies.ts +++ b/src/api/sys/policies/getPolicies.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, getHeaders } from "../../apiUtils"; export async function getPolicies(): Promise { const request = new Request(appendAPIURL("/v1/sys/policies/acl?list=true"), { diff --git a/src/api/sys/getPolicy.ts b/src/api/sys/policies/getPolicy.ts similarity index 84% rename from src/api/sys/getPolicy.ts rename to src/api/sys/policies/getPolicy.ts index 1d5ab5e..04f8a3f 100644 --- a/src/api/sys/getPolicy.ts +++ b/src/api/sys/policies/getPolicy.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, getHeaders } from "../../apiUtils"; export async function getPolicy(name: string): Promise { const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { diff --git a/src/pages/Policies/PoliciesHome.tsx b/src/pages/Policies/PoliciesHome.tsx index d14b572..95bc906 100644 --- a/src/pages/Policies/PoliciesHome.tsx +++ b/src/pages/Policies/PoliciesHome.tsx @@ -1,6 +1,6 @@ import { Margin } from "../../elements/Margin"; import { Page } from "../../types/Page"; -import { getPolicies } from "../../api/sys/getPolicies"; +import { getPolicies } from "../../api/sys/policies/getPolicies"; import { notImplemented, prePageChecks } from "../../pageUtils"; import { render } from "preact"; import i18next from "i18next"; diff --git a/src/pages/Policies/PolicyDelete.tsx b/src/pages/Policies/PolicyDelete.tsx new file mode 100644 index 0000000..b76266d --- /dev/null +++ b/src/pages/Policies/PolicyDelete.tsx @@ -0,0 +1,42 @@ +import { Page } from "../../types/Page"; +import { deletePolicy } from "../../api/sys/policies/deletePolicy"; +import { render } from "preact"; +import { setErrorText } from "../../pageUtils"; +import i18next from "i18next"; + +export class PolicyDeletePage extends Page { + constructor() { + super(); + } + async goBack(): Promise { + await this.router.changePage("POLICY_VIEW"); + } + async render(): Promise { + render( +
+
{i18next.t("policy_delete_text")}
+ +
, + this.router.pageContentElement, + ); + } + + get name(): string { + return i18next.t("policy_delete_title", { + policy: this.state.policyItem, + }); + } +} diff --git a/src/pages/Policies/PolicyView.tsx b/src/pages/Policies/PolicyView.tsx index dedbee5..d0d1f19 100644 --- a/src/pages/Policies/PolicyView.tsx +++ b/src/pages/Policies/PolicyView.tsx @@ -1,7 +1,7 @@ import { CodeBlock } from "../../elements/CodeBlock"; import { Margin } from "../../elements/Margin"; import { Page } from "../../types/Page"; -import { getPolicy } from "../../api/sys/getPolicy"; +import { getPolicy } from "../../api/sys/policies/getPolicy"; import { notImplemented, prePageChecks } from "../../pageUtils"; import { render } from "preact"; import i18next from "i18next"; @@ -26,7 +26,12 @@ export class PolicyViewPage extends Page { {i18next.t("policy_view_edit_btn")} {this.state.policyItem !== "default" && ( - )} diff --git a/src/translations/en.js b/src/translations/en.js index a824c4d..a0e5943 100644 --- a/src/translations/en.js +++ b/src/translations/en.js @@ -297,4 +297,10 @@ module.exports = { policy_view_title: "Policy View ({{policy}})", policy_view_edit_btn: "Edit", policy_view_delete_btn: "Delete", + + // Policy Delete + policy_delete_title: "Delete Policy ({{policy}})", + policy_delete_text: + "Are you sure you want to delete this policy? It can't be reversed and there is a chance that all permissions will break.", + policy_delete_btn: "Delete Policy", };