import { gettext } from "django";
import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
import { until } from "lit-html/directives/until";
import { Event, EventContext } from "../../api/Events";
import { Flow } from "../../api/Flows";
import { COMMON_STYLES } from "../../common/styles";
import "../../elements/Spinner";
import "../../elements/Expand";
import { SpinnerSize } from "../../elements/Spinner";
@customElement("ak-event-info")
export class EventInfo extends LitElement {
@property({attribute: false})
event?: Event;
static get styles(): CSSResult[] {
return COMMON_STYLES.concat(
css`
code {
display: block;
white-space: pre-wrap;
}
.pf-l-flex {
justify-content: space-between;
}
.pf-l-flex__item {
min-width: 25%;
}
`
);
}
getModelInfo(context: EventContext): TemplateResult {
if (context === null) {
return html`-`;
}
return html`
- ${gettext("UID")}: ${context.pk as string}
- ${gettext("Name")}: ${context.name as string}
- ${gettext("App")}: ${context.app as string}
- ${gettext("Model Name")}: ${context.model_name as string}
`;
}
defaultResponse(): TemplateResult {
return html`
${gettext("Context")}
${JSON.stringify(this.event?.context, null, 4)}
${gettext("User")}
${JSON.stringify(this.event?.user, null, 4)}
`;
}
render(): TemplateResult {
if (!this.event) {
return html``;
}
switch (this.event?.action) {
case "model_created":
case "model_updated":
case "model_deleted":
return html`
${gettext("Affected model:")}
${this.getModelInfo(this.event.context.model as EventContext)}
`;
case "authorize_application":
return html`
${gettext("Authorized application:")}
${this.getModelInfo(this.event.context.authorized_application as EventContext)}
${gettext("Using flow")}
${until(Flow.list({
flow_uuid: this.event.context.flow as string,
}).then(resp => {
return html`${resp.results[0].name}`;
}), html``)}
${this.defaultResponse()}`;
case "login_failed":
return html`
${gettext(`Attempted to log in as ${this.event.context.username}`)}
${this.defaultResponse()}`;
case "secret_view":
return html`
${gettext("Secret:")}
${this.getModelInfo(this.event.context.secret as EventContext)}`;
case "property_mapping_exception":
return html`
${gettext("Exception")}
${this.event.context.message || this.event.context.error}
${gettext("Expression")}
${this.event.context.expression}
${this.defaultResponse()}`;
case "policy_exception":
return html`
${gettext("Binding")}
${this.getModelInfo(this.event.context.binding as EventContext)}
${gettext("Request")}
- ${gettext("Object")}: ${this.getModelInfo((this.event.context.request as EventContext).obj as EventContext)}
- ${gettext("Context")}:
${JSON.stringify((this.event.context.request as EventContext).context, null, 4)}
${gettext("Exception")}
${this.event.context.message || this.event.context.error}
${this.defaultResponse()}`;
case "policy_execution":
return html`
${gettext("Binding")}
${this.getModelInfo(this.event.context.binding as EventContext)}
${gettext("Request")}
- ${gettext("Object")}: ${this.getModelInfo((this.event.context.request as EventContext).obj as EventContext)}
- ${gettext("Context")}:
${JSON.stringify((this.event.context.request as EventContext).context, null, 4)}
${gettext("Result")}
- ${gettext("Passing")}: ${(this.event.context.result as EventContext).passing}
- ${gettext("Messages")}:
${((this.event.context.result as EventContext).messages as string[]).map(msg => {
return html`- ${msg}
`;
})}
${this.defaultResponse()}`;
case "configuration_error":
return html`${this.event.context.message}
${this.defaultResponse()}`;
case "update_available":
return html`${gettext("New version available!")}
${this.event.context.new_version}
`;
// Action types which typically don't record any extra context.
// If context is not empty, we fall to the default response.
case "login":
if ("using_source" in this.event.context) {
return html`
${gettext("Using source")}
${this.getModelInfo(this.event.context.using_source as EventContext)}
`;
}
return this.defaultResponse();
case "logout":
if (this.event.context === {}) {
return html`${gettext("No additional data available.")}`;
}
return this.defaultResponse();
default:
return this.defaultResponse();
}
}
}