events: use pending_user as user when possible (#15238)
* unrelated: dont show nested for user Signed-off-by: Jens Langhammer <jens@goauthentik.io> * unrelated: fix error when no extents in. map Signed-off-by: Jens Langhammer <jens@goauthentik.io> * events: use pending_user when possible Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix for identification stage "fake" user Signed-off-by: Jens Langhammer <jens@goauthentik.io> * better username rendering Signed-off-by: Jens Langhammer <jens@goauthentik.io> --------- Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
		@ -1,9 +1,9 @@
 | 
			
		||||
import { EventWithContext } from "@goauthentik/common/events";
 | 
			
		||||
import { EventUser, EventWithContext } from "@goauthentik/common/events";
 | 
			
		||||
import { truncate } from "@goauthentik/common/utils";
 | 
			
		||||
import { SlottedTemplateResult } from "@goauthentik/elements/types";
 | 
			
		||||
 | 
			
		||||
import { msg, str } from "@lit/localize";
 | 
			
		||||
import { html, nothing } from "lit";
 | 
			
		||||
import { TemplateResult, html, nothing } from "lit";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Given event with a geographical context, format it into a string for display.
 | 
			
		||||
@ -18,31 +18,48 @@ export function EventGeo(event: EventWithContext): SlottedTemplateResult {
 | 
			
		||||
    return html`${parts.join(", ")}`;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function EventUser(
 | 
			
		||||
export function renderEventUser(
 | 
			
		||||
    event: EventWithContext,
 | 
			
		||||
    truncateUsername?: number,
 | 
			
		||||
): SlottedTemplateResult {
 | 
			
		||||
    if (!event.user.username) return html`-`;
 | 
			
		||||
 | 
			
		||||
    let body: SlottedTemplateResult = nothing;
 | 
			
		||||
    const linkOrSpan = (inner: TemplateResult, evu: EventUser) => {
 | 
			
		||||
        return html`${evu.pk && !evu.is_anonymous
 | 
			
		||||
            ? html`<a href="#/identity/users/${evu.pk}">${inner}</a>`
 | 
			
		||||
            : html`<span>${inner}</span>`}`;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if (event.user.is_anonymous) {
 | 
			
		||||
        body = html`<div>${msg("Anonymous user")}</div>`;
 | 
			
		||||
    } else {
 | 
			
		||||
        body = html`<div>
 | 
			
		||||
            <a href="#/identity/users/${event.user.pk}"
 | 
			
		||||
                >${truncateUsername
 | 
			
		||||
                    ? truncate(event.user?.username, truncateUsername)
 | 
			
		||||
                    : event.user?.username}</a
 | 
			
		||||
            >
 | 
			
		||||
        </div>`;
 | 
			
		||||
    }
 | 
			
		||||
    const renderUsername = (evu: EventUser) => {
 | 
			
		||||
        let username = evu.username;
 | 
			
		||||
        if (evu.is_anonymous) {
 | 
			
		||||
            username = msg("Anonymous user");
 | 
			
		||||
        }
 | 
			
		||||
        if (truncateUsername) {
 | 
			
		||||
            return truncate(username, truncateUsername);
 | 
			
		||||
        }
 | 
			
		||||
        return username;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let body: SlottedTemplateResult = nothing;
 | 
			
		||||
    body = html`<div>${linkOrSpan(html`${renderUsername(event.user)}`, event.user)}</div>`;
 | 
			
		||||
 | 
			
		||||
    if (event.user.on_behalf_of) {
 | 
			
		||||
        return html`${body}<small>
 | 
			
		||||
                <a href="#/identity/users/${event.user.on_behalf_of.pk}"
 | 
			
		||||
                    >${msg(str`On behalf of ${event.user.on_behalf_of.username}`)}</a
 | 
			
		||||
                >
 | 
			
		||||
                ${linkOrSpan(
 | 
			
		||||
                    html`${msg(str`On behalf of ${renderUsername(event.user.on_behalf_of)}`)}`,
 | 
			
		||||
                    event.user.on_behalf_of,
 | 
			
		||||
                )}
 | 
			
		||||
            </small>`;
 | 
			
		||||
    }
 | 
			
		||||
    if (event.user.authenticated_as) {
 | 
			
		||||
        return html`${body}<small>
 | 
			
		||||
                ${linkOrSpan(
 | 
			
		||||
                    html`${msg(
 | 
			
		||||
                        str`Authenticated as ${renderUsername(event.user.authenticated_as)}`,
 | 
			
		||||
                    )}`,
 | 
			
		||||
                    event.user.authenticated_as,
 | 
			
		||||
                )}
 | 
			
		||||
            </small>`;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user