Skip to content

Commit

Permalink
web/admin: refactor property mappings forms (#10810)
Browse files Browse the repository at this point in the history
  • Loading branch information
rissson authored Aug 7, 2024
1 parent bbc567e commit 9343e34
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 418 deletions.
47 changes: 46 additions & 1 deletion web/src/admin/property-mappings/BasePropertyMappingForm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
import { docLink } from "@goauthentik/common/global";
import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror";
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { ifDefined } from "lit/directives/if-defined.js";

interface PropertyMapping {
name: string;
expression: string;
}

export abstract class BasePropertyMappingForm<T extends PropertyMapping> extends ModelForm<
T,
string
> {
docLink(): string {
return "/docs/property-mappings/expression?utm_source=authentik";
}

export abstract class BasePropertyMappingForm<T> extends ModelForm<T, string> {
getSuccessMessage(): string {
return this.instance
? msg("Successfully updated mapping.")
: msg("Successfully created mapping.");
}

renderExtraFields(): TemplateResult {
return html``;
}

renderForm(): TemplateResult {
return html` <ak-form-element-horizontal label=${msg("Name")} required name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
${this.renderExtraFields()}
<ak-form-element-horizontal label=${msg("Expression")} required name="expression">
<ak-codemirror
mode=${CodeMirrorMode.Python}
value="${ifDefined(this.instance?.expression)}"
>
</ak-codemirror>
<p class="pf-c-form__helper-text">
${msg("Expression using Python.")}
<a target="_blank" rel="noopener noreferrer" href="${docLink(this.docLink())}">
${msg("See documentation for a list of all variables.")}
</a>
</p>
</ak-form-element-horizontal>`;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { BasePropertyMappingForm } from "@goauthentik/admin/property-mappings/BasePropertyMappingForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { docLink } from "@goauthentik/common/global";
import "@goauthentik/elements/CodeMirror";
import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror";
import "@goauthentik/elements/forms/HorizontalFormElement";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";

import { GoogleWorkspaceProviderMapping, PropertymappingsApi } from "@goauthentik/api";

Expand Down Expand Up @@ -38,38 +33,6 @@ export class PropertyMappingGoogleWorkspaceForm extends BasePropertyMappingForm<
});
}
}

renderForm(): TemplateResult {
return html` <ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${msg("Expression")}
?required=${true}
name="expression"
>
<ak-codemirror
mode=${CodeMirrorMode.Python}
value="${ifDefined(this.instance?.expression)}"
>
</ak-codemirror>
<p class="pf-c-form__helper-text">
${msg("Expression using Python.")}
<a
target="_blank"
rel="noopener noreferrer"
href="${docLink("/docs/property-mappings/expression?utm_source=authentik")}"
>
${msg("See documentation for a list of all variables.")}
</a>
</p>
</ak-form-element-horizontal>`;
}
}

declare global {
Expand Down
39 changes: 0 additions & 39 deletions web/src/admin/property-mappings/PropertyMappingLDAPSourceForm.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { BasePropertyMappingForm } from "@goauthentik/admin/property-mappings/BasePropertyMappingForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { docLink } from "@goauthentik/common/global";
import "@goauthentik/elements/CodeMirror";
import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror";
import "@goauthentik/elements/forms/HorizontalFormElement";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";

import { LDAPSourcePropertyMapping, PropertymappingsApi } from "@goauthentik/api";

Expand All @@ -32,40 +27,6 @@ export class PropertyMappingLDAPSourceForm extends BasePropertyMappingForm<LDAPS
});
}
}

renderForm(): TemplateResult {
return html` <ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${msg("Expression")}
?required=${true}
name="expression"
>
<ak-codemirror
mode=${CodeMirrorMode.Python}
value="${ifDefined(this.instance?.expression)}"
>
</ak-codemirror>
<p class="pf-c-form__helper-text">
${msg("Expression using Python.")}
<a
target="_blank"
rel="noopener noreferrer"
href="${docLink(
"/docs/sources/property-mappings/expression?utm_source=authentik",
)}"
>
${msg("See documentation for a list of all variables.")}
</a>
</p>
</ak-form-element-horizontal>`;
}
}

declare global {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { BasePropertyMappingForm } from "@goauthentik/admin/property-mappings/BasePropertyMappingForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { docLink } from "@goauthentik/common/global";
import "@goauthentik/elements/CodeMirror";
import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror";
import "@goauthentik/elements/forms/HorizontalFormElement";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";

import { MicrosoftEntraProviderMapping, PropertymappingsApi } from "@goauthentik/api";

Expand Down Expand Up @@ -38,38 +33,6 @@ export class PropertyMappingMicrosoftEntraForm extends BasePropertyMappingForm<M
});
}
}

renderForm(): TemplateResult {
return html` <ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${msg("Expression")}
?required=${true}
name="expression"
>
<ak-codemirror
mode=${CodeMirrorMode.Python}
value="${ifDefined(this.instance?.expression)}"
>
</ak-codemirror>
<p class="pf-c-form__helper-text">
${msg("Expression using Python.")}
<a
target="_blank"
rel="noopener noreferrer"
href="${docLink("/docs/property-mappings/expression?utm_source=authentik")}"
>
${msg("See documentation for a list of all variables.")}
</a>
</p>
</ak-form-element-horizontal>`;
}
}

declare global {
Expand Down
43 changes: 0 additions & 43 deletions web/src/admin/property-mappings/PropertyMappingNotification.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { docLink } from "@goauthentik/common/global";
import "@goauthentik/elements/CodeMirror";
import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror";
import "@goauthentik/elements/forms/HorizontalFormElement";
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";

import { NotificationWebhookMapping, PropertymappingsApi } from "@goauthentik/api";

Expand All @@ -20,12 +15,6 @@ export class PropertyMappingNotification extends ModelForm<NotificationWebhookMa
});
}

getSuccessMessage(): string {
return this.instance
? msg("Successfully updated mapping.")
: msg("Successfully created mapping.");
}

async send(data: NotificationWebhookMapping): Promise<NotificationWebhookMapping> {
if (this.instance) {
return new PropertymappingsApi(DEFAULT_CONFIG).propertymappingsNotificationUpdate({
Expand All @@ -38,38 +27,6 @@ export class PropertyMappingNotification extends ModelForm<NotificationWebhookMa
});
}
}

renderForm(): TemplateResult {
return html` <ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${msg("Expression")}
?required=${true}
name="expression"
>
<ak-codemirror
mode=${CodeMirrorMode.Python}
value="${ifDefined(this.instance?.expression)}"
>
</ak-codemirror>
<p class="pf-c-form__helper-text">
${msg("Expression using Python.")}
<a
target="_blank"
rel="noopener noreferrer"
href="${docLink("/docs/property-mappings/expression?utm_source=authentik")}"
>
${msg("See documentation for a list of all variables.")}
</a>
</p>
</ak-form-element-horizontal>`;
}
}

declare global {
Expand Down
39 changes: 0 additions & 39 deletions web/src/admin/property-mappings/PropertyMappingOAuthSourceForm.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { BasePropertyMappingForm } from "@goauthentik/admin/property-mappings/BasePropertyMappingForm";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { docLink } from "@goauthentik/common/global";
import "@goauthentik/elements/CodeMirror";
import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror";
import "@goauthentik/elements/forms/HorizontalFormElement";

import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";

import { OAuthSourcePropertyMapping, PropertymappingsApi } from "@goauthentik/api";

Expand All @@ -32,40 +27,6 @@ export class PropertyMappingOAuthSourceForm extends BasePropertyMappingForm<OAut
});
}
}

renderForm(): TemplateResult {
return html` <ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
class="pf-c-form-control"
required
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${msg("Expression")}
?required=${true}
name="expression"
>
<ak-codemirror
mode=${CodeMirrorMode.Python}
value="${ifDefined(this.instance?.expression)}"
>
</ak-codemirror>
<p class="pf-c-form__helper-text">
${msg("Expression using Python.")}
<a
target="_blank"
rel="noopener noreferrer"
href="${docLink(
"/docs/sources/property-mappings/expression?utm_source=authentik",
)}"
>
${msg("See documentation for a list of all variables.")}
</a>
</p>
</ak-form-element-horizontal>`;
}
}

declare global {
Expand Down
4 changes: 2 additions & 2 deletions web/src/admin/property-mappings/PropertyMappingRACForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class PropertyMappingRACForm extends ModelForm<RACPropertyMapping, string

renderForm(): TemplateResult {
return html`
<ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<ak-form-element-horizontal label=${msg("Name")} required name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
Expand Down Expand Up @@ -148,7 +148,7 @@ export class PropertyMappingRACForm extends ModelForm<RACPropertyMapping, string
<div slot="body" class="pf-c-form">
<ak-form-element-horizontal
label=${msg("Expression")}
?required=${true}
required
name="expression"
>
<ak-codemirror
Expand Down
Loading

0 comments on commit 9343e34

Please sign in to comment.