import { DEFAULT_CONFIG, tenant } from "@goauthentik/common/api/config"; import { EVENT_API_DRAWER_TOGGLE, EVENT_NOTIFICATION_DRAWER_TOGGLE, EVENT_WS_MESSAGE, } from "@goauthentik/common/constants"; import { configureSentry } from "@goauthentik/common/sentry"; import { UIConfig, UserDisplay, uiConfig } from "@goauthentik/common/ui/config"; import { autoDetectLanguage } from "@goauthentik/common/ui/locale"; import { me } from "@goauthentik/common/users"; import { first } from "@goauthentik/common/utils"; import { WebsocketClient } from "@goauthentik/common/ws"; import { Interface } from "@goauthentik/elements/Base"; import "@goauthentik/elements/messages/MessageContainer"; import "@goauthentik/elements/notifications/APIDrawer"; import "@goauthentik/elements/notifications/NotificationDrawer"; import { getURLParam, updateURLParams } from "@goauthentik/elements/router/RouteMatch"; import "@goauthentik/elements/router/RouterOutlet"; import "@goauthentik/elements/sidebar/Sidebar"; import { DefaultTenant } from "@goauthentik/elements/sidebar/SidebarBrand"; import "@goauthentik/elements/sidebar/SidebarItem"; import { ROUTES } from "@goauthentik/user/Routes"; import { t } from "@lingui/macro"; import { CSSResult, TemplateResult, css, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import PFAvatar from "@patternfly/patternfly/components/Avatar/avatar.css"; import PFBrand from "@patternfly/patternfly/components/Brand/brand.css"; import PFButton from "@patternfly/patternfly/components/Button/button.css"; import PFDrawer from "@patternfly/patternfly/components/Drawer/drawer.css"; import PFDropdown from "@patternfly/patternfly/components/Dropdown/dropdown.css"; import PFNotificationBadge from "@patternfly/patternfly/components/NotificationBadge/notification-badge.css"; import PFPage from "@patternfly/patternfly/components/Page/page.css"; import PFBase from "@patternfly/patternfly/patternfly-base.css"; import PFDisplay from "@patternfly/patternfly/utilities/Display/display.css"; import { CurrentTenant, EventsApi, SessionUser } from "@goauthentik/api"; autoDetectLanguage(); @customElement("ak-interface-user") export class UserInterface extends Interface { @property({ type: Boolean }) notificationDrawerOpen = getURLParam("notificationDrawerOpen", false); @property({ type: Boolean }) apiDrawerOpen = getURLParam("apiDrawerOpen", false); ws: WebsocketClient; @property({ attribute: false }) tenant: CurrentTenant = DefaultTenant; @property({ type: Number }) notificationsCount = 0; @state() me?: SessionUser; @state() config?: UIConfig; static get styles(): CSSResult[] { return [ PFBase, PFDisplay, PFBrand, PFPage, PFAvatar, PFButton, PFDrawer, PFDropdown, PFNotificationBadge, css` .pf-c-page__main, .pf-c-drawer__content, .pf-c-page__drawer { z-index: auto !important; background-color: transparent !important; } .pf-c-page { background-color: transparent; } .background-wrapper { background-color: var(--pf-c-page--BackgroundColor) !important; } .display-none { display: none; } .pf-c-brand { min-height: 48px; height: 48px; } .has-notifications { color: #2b9af3; } .background-wrapper { height: 100vh; width: 100vw; position: absolute; z-index: -1; } `, ]; } constructor() { super(); this.ws = new WebsocketClient(); window.addEventListener(EVENT_NOTIFICATION_DRAWER_TOGGLE, () => { this.notificationDrawerOpen = !this.notificationDrawerOpen; updateURLParams({ notificationDrawerOpen: this.notificationDrawerOpen, }); }); window.addEventListener(EVENT_API_DRAWER_TOGGLE, () => { this.apiDrawerOpen = !this.apiDrawerOpen; updateURLParams({ apiDrawerOpen: this.apiDrawerOpen, }); }); window.addEventListener(EVENT_WS_MESSAGE, () => { this.firstUpdated(); }); configureSentry(true); } async firstUpdated(): Promise { this.tenant = await tenant(); this.me = await me(); this.config = await uiConfig(); const notifications = await new EventsApi(DEFAULT_CONFIG).eventsNotificationsList({ seen: false, ordering: "-created", pageSize: 1, user: this.me.user.pk, }); this.notificationsCount = notifications.pagination.count; } render(): TemplateResult { if (!this.config || !this.me) { return html``; } let userDisplay = ""; switch (this.config.navbar.userDisplay) { case UserDisplay.username: userDisplay = this.me.user.username; break; case UserDisplay.name: userDisplay = this.me.user.name; break; case UserDisplay.email: userDisplay = this.me.user.email || ""; break; default: userDisplay = this.me.user.username; } return html`
${(this.tenant.brandingTitle, DefaultTenant.brandingTitle)}
${this.config.enabledFeatures.apiDrawer ? html`
` : html``} ${this.config.enabledFeatures.notificationDrawer ? html`
` : html``} ${this.config.enabledFeatures.settings ? html`
` : html``}
${this.me.user.isSuperuser ? html` ${t`Admin interface`} ` : html``}
${this.me.original ? html`
${t`Stop impersonation`}
` : html``}
${userDisplay}
${t`Avatar image`}
`; } }