# Pobieranie kontekstu zalogowanego użytkownika

API pozwala na pobranie kontekstu użytkownika aktualnie wypełniającego formularz. Kontekst opisuje użytkowników zalogowanych na formularzu, w aplikacji Dashboard czy aplikacjach klienta zintegrowanych z systemem Eximee oraz informacje o sesji użytkownika niezalogowanego.

{% hint style="warning" %}
Funkcjonalność dostępna od wersji platformy: 4.737.0

Funkcjonalność działa na wnioskach uruchamianych w aplikacji webforms.
{% endhint %}

***

## API

```typescript
interface User {
    v1: {
        getIdentity(): UserIdentity
    }
}


interface UserIdentity {
    userId(): string,
    roles(): string[],
    firstName(): string,
    lastName(): string,
    type(): 'client'|'employee'|'otp',
    system(): string
}
```

{% hint style="info" %}
Pola: firstName, lastName, roles są zależne od sposobu integracji uwierzytelniania w ramach wdrożenia.

Najczęściej pola są dostępne jedynie dla uwierzytelnienia typu Pracownik (employee) w systemach Dashboard lub systemach Klienta zintegrowanych z Eximee.
{% endhint %}

#### Kontekst działania

API jest dostępne z obiektu `api.user.v1` i działa w kontekście formularza.

Obsługuje użytkowników pracujących z formularzem:

* zalogowanych przez kanał Web zgodnie z integracją wdrożenia,
* zalogowanych przez aplikację Dashboard,
* zalogowanych przez autoryzację zintegrowanych systemów wdrożenia,
* niezalogowanych w podstawowym zakresie rozróżnialności użytkowników.

***

## Przykłady użycia

### Pobieranie danych użytkownika

```typescript
function callService(context) {
    const identity = api.user.v1.getIdentity();
    Logger.info("Get user identity: {}", identity)
    return [{
        'userId': identity.userId,
        'firstName': identity.firstName,
        'lastName': identity.lastName,
        'type': identity.type,
        'system': identity.system,
    }];
} 
```

### Przykład użycia w skrypcie

```typescript
function callService(context) {
    const AVAILABLE_ROLES = ["EXIMEE_BROKER_MORTGAGE_DASHBOARD"]
    let identity = api.user.v1.getIdentity()
    let roles = identity.roles
    Logger.info("simpleMortgageExecutionPermissionVerify roles: {}", roles)

    if (!roles) {
        throwBusinessError("error_page_default-*", "Brak uprawnien do uruchomienia procesu")
    }
    let isAvailableRole = roles.some(role => AVAILABLE_ROLES.includes(role.toUpperCase()))

    if (!isAvailableRole) {
        throwBusinessError("error_page_default-*", "Brak odpowiedniej roli do uruchomienia procesu")

    }
    let userId = identity.userId
    return [{ userId }];

    function throwBusinessError(textcontentName, errorDescription) {
        const builder = context.getErrorPageDefinitionBuilder();
        builder.bodyTextContent(textcontentName);
        builder.msg(errorDescription);
        context.throwBusinessException(builder);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.eximee.com/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/pobranie-kontekstu-uzytkownika.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
