From 7ee25bc7f7f75e169cd935a4527cb35d08dff747 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 26 May 2021 13:10:46 +0100 Subject: [PATCH] Add PolicyNew page. --- src/allPages.ts | 2 + src/api/sys/policies/createOrUpdatePolicy.ts | 27 +++++++++ src/pages/Policies/PoliciesHome.tsx | 9 ++- src/pages/Policies/PolicyNew.tsx | 59 ++++++++++++++++++++ src/translations/en.js | 6 ++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/api/sys/policies/createOrUpdatePolicy.ts create mode 100644 src/pages/Policies/PolicyNew.tsx diff --git a/src/allPages.ts b/src/allPages.ts index b212942..7ba26e6 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -23,6 +23,7 @@ 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 { PolicyNewPage } from "./pages/Policies/PolicyNew"; import { PolicyViewPage } from "./pages/Policies/PolicyView"; import { PwGenPage } from "./pages/PwGen"; import { SecretsHomePage } from "./pages/Secrets/SecretsHome"; @@ -59,6 +60,7 @@ export const allPages: pagesList = { POLICIES_HOME: new PoliciesHomePage(), POLICY_VIEW: new PolicyViewPage(), + POLICY_NEW: new PolicyNewPage(), POLICY_DELETE: new PolicyDeletePage(), ACCESS_HOME: new AccessHomePage(), diff --git a/src/api/sys/policies/createOrUpdatePolicy.ts b/src/api/sys/policies/createOrUpdatePolicy.ts new file mode 100644 index 0000000..906c23e --- /dev/null +++ b/src/api/sys/policies/createOrUpdatePolicy.ts @@ -0,0 +1,27 @@ +import { appendAPIURL, getHeaders } from "../../apiUtils"; + +type OptionalErrors = { errors?: string[] }; + +export async function createOrUpdatePolicy(name: string, policy_data: string): Promise { + const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { + method: "POST", + headers: { + "Content-Type": "application/json", + ...getHeaders(), + }, + body: JSON.stringify({ policy: policy_data }, null, 0), + }); + + 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/pages/Policies/PoliciesHome.tsx b/src/pages/Policies/PoliciesHome.tsx index 95bc906..8d51e7d 100644 --- a/src/pages/Policies/PoliciesHome.tsx +++ b/src/pages/Policies/PoliciesHome.tsx @@ -1,7 +1,7 @@ import { Margin } from "../../elements/Margin"; import { Page } from "../../types/Page"; import { getPolicies } from "../../api/sys/policies/getPolicies"; -import { notImplemented, prePageChecks } from "../../pageUtils"; +import { prePageChecks } from "../../pageUtils"; import { render } from "preact"; import i18next from "i18next"; @@ -25,7 +25,12 @@ export class PoliciesHomePage extends Page { render(

-

diff --git a/src/pages/Policies/PolicyNew.tsx b/src/pages/Policies/PolicyNew.tsx new file mode 100644 index 0000000..a9dd461 --- /dev/null +++ b/src/pages/Policies/PolicyNew.tsx @@ -0,0 +1,59 @@ +import { Form } from "../../elements/Form"; +import { Margin } from "../../elements/Margin"; +import { Page } from "../../types/Page"; +import { createOrUpdatePolicy } from "../../api/sys/policies/createOrUpdatePolicy"; +import { getPolicies } from "../../api/sys/policies/getPolicies"; +import { render } from "preact"; +import { setErrorText } from "../../pageUtils"; +import i18next from "i18next"; + +export class PolicyNewPage extends Page { + constructor() { + super(); + } + async goBack(): Promise { + await this.router.changePage("POLICIES_HOME"); + } + async render(): Promise { + render( +
+
{ + const name = formData.get("name") as string; + if ((await getPolicies()).includes(name)) { + setErrorText(i18next.t("policy_new_already_exists")); + return; + } + + try { + await createOrUpdatePolicy(name, " "); + this.state.policyItem = name; + await this.router.changePage("POLICY_VIEW"); + } catch (e: unknown) { + const error = e as Error; + setErrorText(error.message); + } + }} + > + + + +

+ +

+
, + this.router.pageContentElement, + ); + } + + get name(): string { + return i18next.t("policy_new_title"); + } +} diff --git a/src/translations/en.js b/src/translations/en.js index a0e5943..4221393 100644 --- a/src/translations/en.js +++ b/src/translations/en.js @@ -303,4 +303,10 @@ module.exports = { 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", + + // Policy New + policy_new_title: "Create New Policy", + policy_new_name_placeholder: "Policy Name", + policy_new_create_btn: "Create", + policy_new_already_exists: "This policy already exists.", };