1
0
Fork 0

Add PolicyDelete page.

This commit is contained in:
Kitteh 2021-05-26 12:52:45 +01:00
parent 681f617988
commit 53e5765956
8 changed files with 81 additions and 5 deletions

View file

@ -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(),

View file

@ -0,0 +1,21 @@
import { appendAPIURL, getHeaders } from "../../apiUtils";
type OptionalErrors = { errors?: string[] };
export async function deletePolicy(name: string): Promise<void> {
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]);
}
}

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, getHeaders } from "../../apiUtils";
export async function getPolicies(): Promise<string[]> {
const request = new Request(appendAPIURL("/v1/sys/policies/acl?list=true"), {

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, getHeaders } from "../../apiUtils";
export async function getPolicy(name: string): Promise<string> {
const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), {

View file

@ -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";

View file

@ -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<void> {
await this.router.changePage("POLICY_VIEW");
}
async render(): Promise<void> {
render(
<div>
<h5>{i18next.t("policy_delete_text")}</h5>
<button
class="uk-button uk-button-danger"
onClick={async () => {
try {
await deletePolicy(this.state.policyItem);
await this.router.changePage("POLICIES_HOME");
} catch (e: unknown) {
const error = e as Error;
setErrorText(error.message);
}
}}
>
{i18next.t("policy_delete_btn")}
</button>
</div>,
this.router.pageContentElement,
);
}
get name(): string {
return i18next.t("policy_delete_title", {
policy: this.state.policyItem,
});
}
}

View file

@ -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")}
</button>
{this.state.policyItem !== "default" && (
<button class="uk-button uk-button-danger" onClick={notImplemented}>
<button
class="uk-button uk-button-danger"
onClick={async () => {
await this.router.changePage("POLICY_DELETE");
}}
>
{i18next.t("policy_view_delete_btn")}
</button>
)}

View file

@ -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",
};