web: add first() to improve defaults

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-03 11:41:11 +02:00
parent 42cb55d78a
commit 43f37e4776
8 changed files with 31 additions and 15 deletions

View File

@ -6,7 +6,7 @@ import { DEFAULT_CONFIG } from "../../api/Config";
import { Form } from "../../elements/forms/Form"; import { Form } from "../../elements/forms/Form";
import { until } from "lit-html/directives/until"; import { until } from "lit-html/directives/until";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
import { groupBy } from "../../utils"; import { first, groupBy } from "../../utils";
import "../../elements/forms/HorizontalFormElement"; import "../../elements/forms/HorizontalFormElement";
@customElement("ak-policy-binding-form") @customElement("ak-policy-binding-form")
@ -130,7 +130,7 @@ export class PolicyBindingForm extends Form<PolicyBinding> {
label=${gettext("Timeout")} label=${gettext("Timeout")}
?required=${true} ?required=${true}
name="timeout"> name="timeout">
<input type="number" value="${this.binding?.timeout || 30}" class="pf-c-form-control" required> <input type="number" value="${first(this.binding?.timeout, 30)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
</form>`; </form>`;
} }

View File

@ -7,6 +7,7 @@ import { Form } from "../../../elements/forms/Form";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
import "../../../elements/forms/HorizontalFormElement"; import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/FormGroup"; import "../../../elements/forms/FormGroup";
import { first } from "../../../utils";
@customElement("ak-policy-dummy-form") @customElement("ak-policy-dummy-form")
export class DummyPolicyForm extends Form<DummyPolicy> { export class DummyPolicyForm extends Form<DummyPolicy> {
@ -78,14 +79,14 @@ export class DummyPolicyForm extends Form<DummyPolicy> {
label=${gettext("Wait (min)")} label=${gettext("Wait (min)")}
?required=${true} ?required=${true}
name="waitMin"> name="waitMin">
<input type="number" value="${this.policy?.waitMin || 1}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.waitMin, 1)}" class="pf-c-form-control" required>
<p class="pf-c-form__helper-text">${gettext("The policy takes a random time to execute. This controls the minimum time it will take.")}</p> <p class="pf-c-form__helper-text">${gettext("The policy takes a random time to execute. This controls the minimum time it will take.")}</p>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Wait (max)")} label=${gettext("Wait (max)")}
?required=${true} ?required=${true}
name="waitMax"> name="waitMax">
<input type="number" value="${this.policy?.waitMax || 5}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.waitMax, 5)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
</div> </div>
</ak-form-group> </ak-form-group>

View File

@ -7,6 +7,7 @@ import { Form } from "../../../elements/forms/Form";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
import "../../../elements/forms/HorizontalFormElement"; import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/FormGroup"; import "../../../elements/forms/FormGroup";
import { first } from "../../../utils";
@customElement("ak-policy-hibp-form") @customElement("ak-policy-hibp-form")
export class HaveIBeenPwnedPolicyForm extends Form<HaveIBeenPwendPolicy> { export class HaveIBeenPwnedPolicyForm extends Form<HaveIBeenPwendPolicy> {
@ -76,7 +77,7 @@ export class HaveIBeenPwnedPolicyForm extends Form<HaveIBeenPwendPolicy> {
label=${gettext("Allowed count")} label=${gettext("Allowed count")}
?required=${true} ?required=${true}
name="allowedCount"> name="allowedCount">
<input type="number" value="${ifDefined(this.policy?.allowedCount || 0)}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.allowedCount, 0)}" class="pf-c-form-control" required>
<p class="pf-c-form__helper-text">${gettext("Allow up to N occurrences in the HIBP database.")}</p> <p class="pf-c-form__helper-text">${gettext("Allow up to N occurrences in the HIBP database.")}</p>
</ak-form-element-horizontal> </ak-form-element-horizontal>
</div> </div>

View File

@ -7,6 +7,7 @@ import { Form } from "../../../elements/forms/Form";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
import "../../../elements/forms/HorizontalFormElement"; import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/FormGroup"; import "../../../elements/forms/FormGroup";
import { first } from "../../../utils";
@customElement("ak-policy-password-form") @customElement("ak-policy-password-form")
export class PasswordPolicyForm extends Form<PasswordPolicy> { export class PasswordPolicyForm extends Form<PasswordPolicy> {
@ -77,25 +78,25 @@ export class PasswordPolicyForm extends Form<PasswordPolicy> {
label=${gettext("Minimum length")} label=${gettext("Minimum length")}
?required=${true} ?required=${true}
name="lengthMin"> name="lengthMin">
<input type="number" value="${ifDefined(this.policy?.lengthMin || 10)}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.lengthMin, 10)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Minimum amount of Uppercase Characters")} label=${gettext("Minimum amount of Uppercase Characters")}
?required=${true} ?required=${true}
name="amountUppercase"> name="amountUppercase">
<input type="number" value="${ifDefined(this.policy?.amountUppercase || 2)}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.amountUppercase, 2)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Minimum amount of Lowercase Characters")} label=${gettext("Minimum amount of Lowercase Characters")}
?required=${true} ?required=${true}
name="amountLowercase"> name="amountLowercase">
<input type="number" value="${ifDefined(this.policy?.amountLowercase || 2)}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.amountLowercase, 2)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Minimum amount of Symbols Characters")} label=${gettext("Minimum amount of Symbols Characters")}
?required=${true} ?required=${true}
name="amountSymbols"> name="amountSymbols">
<input type="number" value="${ifDefined(this.policy?.amountSymbols || 2)}" class="pf-c-form-control" required> <input type="number" value="${first(this.policy?.amountSymbols, 2)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Error message")} label=${gettext("Error message")}

View File

@ -8,6 +8,7 @@ import { ifDefined } from "lit-html/directives/if-defined";
import "../../../elements/forms/HorizontalFormElement"; import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/FormGroup"; import "../../../elements/forms/FormGroup";
import { until } from "lit-html/directives/until"; import { until } from "lit-html/directives/until";
import { first } from "../../../utils";
@customElement("ak-stage-authenticator-static-form") @customElement("ak-stage-authenticator-static-form")
export class AuthenticatorStaticStageForm extends Form<AuthenticatorStaticStage> { export class AuthenticatorStaticStageForm extends Form<AuthenticatorStaticStage> {
@ -61,7 +62,7 @@ export class AuthenticatorStaticStageForm extends Form<AuthenticatorStaticStage>
label=${gettext("Token count")} label=${gettext("Token count")}
?required=${true} ?required=${true}
name="tokenCount"> name="tokenCount">
<input type="text" value="${ifDefined(this.stage?.tokenCount || 6)}" class="pf-c-form-control" required> <input type="text" value="${first(this.stage?.tokenCount, 6)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Configuration flow")} label=${gettext("Configuration flow")}

View File

@ -7,6 +7,7 @@ import { Form } from "../../../elements/forms/Form";
import { ifDefined } from "lit-html/directives/if-defined"; import { ifDefined } from "lit-html/directives/if-defined";
import "../../../elements/forms/HorizontalFormElement"; import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/FormGroup"; import "../../../elements/forms/FormGroup";
import { first } from "../../../utils";
@customElement("ak-stage-email-form") @customElement("ak-stage-email-form")
export class EmailStageForm extends Form<EmailStage> { export class EmailStageForm extends Form<EmailStage> {
@ -65,7 +66,7 @@ export class EmailStageForm extends Form<EmailStage> {
label=${gettext("SMTP Port")} label=${gettext("SMTP Port")}
?required=${true} ?required=${true}
name="port"> name="port">
<input type="number" value="${ifDefined(this.stage?.port || 25)}" class="pf-c-form-control" required> <input type="number" value="${first(this.stage?.port, 25)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("SMTP Username")} label=${gettext("SMTP Username")}
@ -99,7 +100,7 @@ export class EmailStageForm extends Form<EmailStage> {
label=${gettext("Timeout")} label=${gettext("Timeout")}
?required=${true} ?required=${true}
name="timeout"> name="timeout">
<input type="number" value="${ifDefined(this.stage?.timeout || 30)}" class="pf-c-form-control" required> <input type="number" value="${first(this.stage?.timeout, 30)}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("From address")} label=${gettext("From address")}
@ -140,14 +141,14 @@ export class EmailStageForm extends Form<EmailStage> {
label=${gettext("Token expiry")} label=${gettext("Token expiry")}
?required=${true} ?required=${true}
name="tokenExpiry"> name="tokenExpiry">
<input type="number" value="${ifDefined(this.stage?.tokenExpiry || 30)}" class="pf-c-form-control" required> <input type="number" value="${first(this.stage?.tokenExpiry, 30)}" class="pf-c-form-control" required>
<p class="pf-c-form__helper-text">${gettext("Time in minutes the token sent is valid.")}</p> <p class="pf-c-form__helper-text">${gettext("Time in minutes the token sent is valid.")}</p>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Subject")} label=${gettext("Subject")}
?required=${true} ?required=${true}
name="subject"> name="subject">
<input type="text" value="${ifDefined(this.stage?.subject || "authentik")}" class="pf-c-form-control" required> <input type="text" value="${first(this.stage?.subject, "authentik")}" class="pf-c-form-control" required>
</ak-form-element-horizontal> </ak-form-element-horizontal>
<ak-form-element-horizontal <ak-form-element-horizontal
label=${gettext("Template")} label=${gettext("Template")}

View File

@ -8,6 +8,7 @@ import { ifDefined } from "lit-html/directives/if-defined";
import "../../../elements/forms/HorizontalFormElement"; import "../../../elements/forms/HorizontalFormElement";
import "../../../elements/forms/FormGroup"; import "../../../elements/forms/FormGroup";
import { until } from "lit-html/directives/until"; import { until } from "lit-html/directives/until";
import { first } from "../../../utils";
@customElement("ak-stage-password-form") @customElement("ak-stage-password-form")
export class PasswordStageForm extends Form<PasswordStage> { export class PasswordStageForm extends Form<PasswordStage> {
@ -103,7 +104,7 @@ export class PasswordStageForm extends Form<PasswordStage> {
label=${gettext("Failed attempts before cancel")} label=${gettext("Failed attempts before cancel")}
?required=${true} ?required=${true}
name="failedAttemptsBeforeCancel"> name="failedAttemptsBeforeCancel">
<input type="number" value="${ifDefined(this.stage?.failedAttemptsBeforeCancel || 5)}" class="pf-c-form-control" required> <input type="number" value="${first(this.stage?.failedAttemptsBeforeCancel, 5)}" class="pf-c-form-control" required>
<p class="pf-c-form__helper-text">${gettext("How many attempts a user has before the flow is canceled. To lock the user out, use a reputation policy and a user_write stage.")}</p> <p class="pf-c-form__helper-text">${gettext("How many attempts a user has before the flow is canceled. To lock the user out, use a reputation policy and a user_write stage.")}</p>
</ak-form-element-horizontal> </ak-form-element-horizontal>
</div> </div>

View File

@ -69,3 +69,13 @@ export function groupBy<T>(objects: T[], callback: (obj: T) => string): Array<[s
}); });
return Array.from(m); return Array.from(m);
} }
export function first<T>(...args: Array<T | undefined | null>): T {
for (let index = 0; index < args.length; index++) {
const element = args[index];
if (element !== undefined && element !== null) {
return element;
}
}
throw new Error(`No compatible arg given: ${args}`);
}