src/app/core/user/entity-user/entity-user.component.ts

Description

Display User Account details and configuration related to a given profile Entity.

Example

Metadata

Relationships

Used by

No results matching.

Index

Properties
Inputs

Inputs

entity
Type : Entity

Properties

getUserAccountForDetails
Type : unknown
Default value : computed<UserAccount | null>(() => { const loadedUser = this.user.value(); if (loadedUser) { return loadedUser; } const entity = this.entity(); if (entity) { // stub account object for creating a new account return { userEntityId: entity.getId(), email: "", enabled: true, roles: [], } as UserAccount; } return null; })
user
Type : unknown
Default value : resource({ params: () => { if (!this.userIsPermitted()) { return undefined; } const entity = this.entity(); if (!entity) { return undefined; } return { entity }; }, loader: async ({ params: { entity } }) => { try { const primaryUser = await firstValueFrom( this.userAdminService.getUser(entity.getId()), ); if (primaryUser !== null) { return primaryUser; } return await firstValueFrom( this.userAdminService.getUser(entity.getId(true)), ); } catch (err: unknown) { const error = err as { error?: { message?: string }; message?: string }; this.alertService.addDanger( error?.error?.message || error?.message || $localize`Failed to load user account`, ); return null; } }, })
userIsPermitted
Type : unknown
Default value : computed( () => this.sessionSignal()?.roles.includes( UserAdminService.ACCOUNT_MANAGER_ROLE, ) ?? false, )
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
  resource,
} from "@angular/core";
import { toSignal } from "@angular/core/rxjs-interop";
import { DynamicComponent } from "../../config/dynamic-components/dynamic-component.decorator";
import { AlertService } from "../../alerts/alert.service";
import { SessionSubject } from "../../session/auth/session-info";
import { Entity } from "../../entity/model/entity";
import { UserAdminService } from "../user-admin-service/user-admin.service";
import { UserAccount } from "../user-admin-service/user-account";
import { firstValueFrom } from "rxjs";
import { UserDetailsComponent } from "../user-details/user-details.component";

/**
 * Display User Account details and configuration related to a given profile Entity.
 */
@DynamicComponent("EntityUser")
@DynamicComponent("UserSecurity") // for backwards compatibility. Prefer to use "EntityUser"
@Component({
  selector: "app-entity-user",
  templateUrl: "./entity-user.component.html",
  styleUrls: ["./entity-user.component.scss"],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [UserDetailsComponent],
})
export class EntityUserComponent {
  private readonly userAdminService = inject(UserAdminService);
  private readonly alertService = inject(AlertService);
  private readonly sessionInfo = inject(SessionSubject);
  private readonly sessionSignal = toSignal(this.sessionInfo, {
    initialValue: this.sessionInfo.value,
  });

  entity = input<Entity>();

  userIsPermitted = computed(
    () =>
      this.sessionSignal()?.roles.includes(
        UserAdminService.ACCOUNT_MANAGER_ROLE,
      ) ?? false,
  );

  user = resource({
    params: () => {
      if (!this.userIsPermitted()) {
        return undefined;
      }

      const entity = this.entity();
      if (!entity) {
        return undefined;
      }

      return { entity };
    },
    loader: async ({ params: { entity } }) => {
      try {
        const primaryUser = await firstValueFrom(
          this.userAdminService.getUser(entity.getId()),
        );

        if (primaryUser !== null) {
          return primaryUser;
        }

        return await firstValueFrom(
          this.userAdminService.getUser(entity.getId(true)),
        );
      } catch (err: unknown) {
        const error = err as { error?: { message?: string }; message?: string };
        this.alertService.addDanger(
          error?.error?.message ||
            error?.message ||
            $localize`Failed to load user account`,
        );
        return null;
      }
    },
  });

  getUserAccountForDetails = computed<UserAccount | null>(() => {
    const loadedUser = this.user.value();
    if (loadedUser) {
      return loadedUser;
    }

    const entity = this.entity();
    if (entity) {
      // stub account object for creating a new account
      return {
        userEntityId: entity.getId(),
        email: "",
        enabled: true,
        roles: [],
      } as UserAccount;
    }

    return null;
  });
}
@if (userIsPermitted()) {
  <app-user-details
    [userAccount]="getUserAccountForDetails()"
  ></app-user-details>
} @else {
  <div class="permissions-warning" i18n>
    Your account does not have the required permissions to manage user accounts.
  </div>
}

./entity-user.component.scss

.permissions-warning {
  color: #f44336;
  font-style: italic;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""