# B2B Context & Permissions

## B2B Context Extension

In Shopware, the `SalesChannelContext` handles basic session data. We have expanded this by adding a `b2bPlatformContext` extension. This allows you to retrieve B2B-specific data directly from the user context, such as:

* Identity: Which specific employee of a customer is currently logged in?
* Roles: Is the user an administrator or a standard employee?
* Sales Representatives: Is a sales representative logged in, and are they currently masquerading as (logged into) a customer account?

### Usage & Safety

The `b2bPlatformContext` is globally accessible. However, since it is only populated when a B2B user is authenticated, you should always verify its existence before access to avoid errors:

```php
if (!$context->hasExtension('b2bPlatformContext')) {
    return; 
}
```

## B2B Context Trait

The `B2bContextTrait` provides a suite of helper functions to easily distinguish between different user types in your PHP code. Whether you need to identify a standard end customer, a corporate employee, or a sales agent, these methods simplify the logic.

### Available Methods

|                                           |                                                                                                                                                                                               |
| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isEmployeePermissionEnabled`             | Checks if an employee has a specific permission without throwing exceptions. Returns `true` if permission is granted, `false` otherwise.                                                      |
| `checkB2bPlatformPermission`              | Validates that the current user has all required permissions. Throws `InsufficientEmployeePermissionException` if any permissions are missing. Admin employees automatically pass all checks. |
| `getEmployeeRole`                         | Retrieves the employee role for the current user context. Returns `null` for sales representatives, customer admins, or when no role is assigned.                                             |
| `hasB2bPlatformContext`                   | Checks if B2B platform context exists in the sales channel context. Returns `true` if available, `false` otherwise.                                                                           |
| `getB2bPlatformContext`                   | Retrieves the B2B platform context. Throws `B2bPlatformContextException` if context doesn't exist.                                                                                            |
| `getB2bPlatformContextOrNull`             | Safely retrieves B2B platform context without throwing exceptions. Returns B2bPlatformContext or null if not available.                                                                       |
| `isLoggedInAsEmployee`                    | Checks if the current user is logged in as an employee. Returns `true` if user has employee context.                                                                                          |
| `isLoggedInAsSalesRepresentative`         | Checks if the current user is logged in as a sales representative. Returns `true` if sales representative context exists.                                                                     |
| `getEmployee`                             | Retrieves the current employee entity from the B2B platform context. Returns EmployeeEntity or null if not available.                                                                         |
| `isSalesRepresentativeLoggedInAsCustomer` | Checks if a sales representative is impersonating a customer. Returns `true` if sales rep is logged in as a different customer (customer ID differs from sales rep ID).                       |
| `customerIsSalesRepresentative`           | Checks if the current customer has sales representative privileges. Returns `true` if customer has custom field set to `true`. `b2b_sales_representative`                                     |
| `customerIsSupervisor`                    | Checks if the current customer is a B2B supervisor (elevated sales representative). Returns `true` if customer is both a sales representative and has custom field set to `b2b_supervisor`    |


---

# 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.b2b-sellers.com/b2b-platform/developer-guide/first-steps/b2b-context-and-permissions.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.
