# UserSwitch

> Backs the user-impersonation workflow by swapping the active session identity to another user.

[API reference](/docs/api/index.md) › [Debug](/docs/extensions/debug/reference/index.md) › [models](/docs/extensions/debug/reference/models/index.md) › UserSwitch

| class | `yii\debug\models\UserSwitch` |
| --- | --- |
| Extends | [`yii\base\Model`](/docs/api/yii/base/model/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\StaticInstanceInterface`](/docs/api/yii/base/staticinstanceinterface/index.md), [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`yii\base\Arrayable`](/docs/api/yii/base/arrayable/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Uses traits | [`yii\base\ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md), [`yii\base\StaticInstanceTrait`](/docs/api/yii/base/staticinstancetrait/index.md) |
| Source | [`src/models/UserSwitch.php`](https://github.com/yii2-extensions/debug/blob/main/src/models/UserSwitch.php#L24) |

Backs the user-impersonation workflow by swapping the active session identity to another user.

Preserves the original identity in the session (`main_user`) so [`reset()`](/docs/extensions/debug/reference/models/userswitch/index.md#method-reset) can restore it once the impersonator
is done. Every accessor resolves the user component lazily through [`getUser()`](/docs/extensions/debug/reference/models/userswitch/index.md#method-getUser) so unit tests can inject a
pre-built [`User`](/docs/api/yii/web/user/index.md) instance into [`$userComponent`](/docs/extensions/debug/reference/models/userswitch/index.md#property-userComponent).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-activeValidators"></span>[`$activeValidators`](/docs/api/yii/base/model/index.md#property-activeValidators) read-only | [`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`[]` | The validators applicable to the current [`$scenario`](/docs/api/yii/base/model/index.md#property-scenario). | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-attributes"></span>[`$attributes`](/docs/api/yii/base/model/index.md#property-attributes) | `array<string, mixed>` | Attribute values (name => value). | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-errors"></span>[`$errors`](/docs/api/yii/base/model/index.md#property-errors) read-only | `array<string, string[]>` | Errors for all attributes. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-firstErrors"></span>[`$firstErrors`](/docs/api/yii/base/model/index.md#property-firstErrors) read-only | `array<string, string>` | The first errors. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-iterator"></span>[`$iterator`](/docs/api/yii/base/model/index.md#property-iterator) read-only | [`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<string, mixed>` | An iterator for traversing the items in the list. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-mainUser"></span>`$mainUser` read-only | [`yii\web\User`](/docs/api/yii/web/user/index.md) | User component carrying the original identity. | `UserSwitch` |
| <span id="property-scenario"></span>[`$scenario`](/docs/api/yii/base/model/index.md#property-scenario) | `string` | The scenario that this model is in. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="property-user"></span>`$user` | [`yii\web\User`](/docs/api/yii/web/user/index.md) | Bound user component. | `UserSwitch` |
| <span id="property-userByIdentity"></span>`$userByIdentity` write-only | [`yii\web\IdentityInterface`](/docs/api/yii/web/identityinterface/index.md) | Identity to impersonate. | `UserSwitch` |
| <span id="property-userComponent"></span>`$userComponent` | `string\|`[`yii\web\User`](/docs/api/yii/web/user/index.md) | Component ID of the user component, or a [`User`](/docs/api/yii/web/user/index.md) instance to operate on directly. Default: `'user'`. | `UserSwitch` |
| <span id="property-validators"></span>[`$validators`](/docs/api/yii/base/model/index.md#property-validators) read-only | [`ArrayObject`](https://www.php.net/manual/en/class.arrayobject.php)`<int,` [`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`>\|`[`yii\validators\Validator`](/docs/api/yii/validators/validator/index.md)`[]` | All the validators declared in the model. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/model/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-activeAttributes"></span>[`activeAttributes()`](/docs/api/yii/base/model/index.md#method-activeAttributes) | Returns the attribute names that are subject to validation in the current scenario. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-addError"></span>[`addError()`](/docs/api/yii/base/model/index.md#method-addError) | Adds a new error to the specified attribute. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-addErrors"></span>[`addErrors()`](/docs/api/yii/base/model/index.md#method-addErrors) | Adds a list of errors. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-afterValidate"></span>[`afterValidate()`](/docs/api/yii/base/model/index.md#method-afterValidate) | This method is invoked after validation ends. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attributeHints"></span>[`attributeHints()`](/docs/api/yii/base/model/index.md#method-attributeHints) | Returns the attribute hints. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| [`attributeLabels()`](#method-attributeLabels) | Returns the labels shown for the switch form attributes. | `UserSwitch` |
| <span id="method-attributes"></span>[`attributes()`](/docs/api/yii/base/model/index.md#method-attributes) | Returns the list of attribute names. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-beforeValidate"></span>[`beforeValidate()`](/docs/api/yii/base/model/index.md#method-beforeValidate) | This method is invoked before validation starts. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-clearErrors"></span>[`clearErrors()`](/docs/api/yii/base/model/index.md#method-clearErrors) | Removes errors for all attributes or a single attribute. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-createValidators"></span>[`createValidators()`](/docs/api/yii/base/model/index.md#method-createValidators) | Creates validator objects based on the validation rules specified in [`rules()`](/docs/api/yii/base/model/index.md#method-rules). | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-extraFields"></span>[`extraFields()`](/docs/api/yii/base/arrayabletrait/index.md#method-extraFields) | Returns the list of fields that can be expanded further and returned by [`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray). | [`yii\base\ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-fields"></span>[`fields()`](/docs/api/yii/base/model/index.md#method-fields) | Returns the list of fields that should be returned by default by [`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray) when no specific fields are specified. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-formName"></span>[`formName()`](/docs/api/yii/base/model/index.md#method-formName) | Returns the form name that this model class should use. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-generateAttributeLabel"></span>[`generateAttributeLabel()`](/docs/api/yii/base/model/index.md#method-generateAttributeLabel) | Generates a user friendly attribute label based on the give attribute name. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getActiveValidators"></span>[`getActiveValidators()`](/docs/api/yii/base/model/index.md#method-getActiveValidators) | Returns the validators applicable to the current [`$scenario`](/docs/api/yii/base/model/index.md#property-scenario). | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getAttributeHint"></span>[`getAttributeHint()`](/docs/api/yii/base/model/index.md#method-getAttributeHint) | Returns the text hint for the specified attribute. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getAttributeLabel"></span>[`getAttributeLabel()`](/docs/api/yii/base/model/index.md#method-getAttributeLabel) | Returns the text label for the specified attribute. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getAttributes"></span>[`getAttributes()`](/docs/api/yii/base/model/index.md#method-getAttributes) | Returns attribute values. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getErrors"></span>[`getErrors()`](/docs/api/yii/base/model/index.md#method-getErrors) | Returns the errors for all attributes or a single attribute. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getErrorSummary"></span>[`getErrorSummary()`](/docs/api/yii/base/model/index.md#method-getErrorSummary) | Returns the errors for all attributes as a one-dimensional array. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getFirstError"></span>[`getFirstError()`](/docs/api/yii/base/model/index.md#method-getFirstError) | Returns the first error of the specified attribute. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getFirstErrors"></span>[`getFirstErrors()`](/docs/api/yii/base/model/index.md#method-getFirstErrors) | Returns the first error of every attribute in the model. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-getIterator"></span>[`getIterator()`](/docs/api/yii/base/model/index.md#method-getIterator) | Returns an iterator for traversing the attributes in the model. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| [`getMainUser()`](#method-getMainUser) | Returns the main user; the original identity captured on the first switch. | `UserSwitch` |
| <span id="method-getScenario"></span>[`getScenario()`](/docs/api/yii/base/model/index.md#method-getScenario) | Returns the scenario that this model is used in. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| [`getUser()`](#method-getUser) | Returns the user component bound to this switch model, resolving it lazily on first call. | `UserSwitch` |
| <span id="method-getValidators"></span>[`getValidators()`](/docs/api/yii/base/model/index.md#method-getValidators) | Returns all the validators declared in [`rules()`](/docs/api/yii/base/model/index.md#method-rules). | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-hasErrors"></span>[`hasErrors()`](/docs/api/yii/base/model/index.md#method-hasErrors) | Returns a value indicating whether there is any validation error. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-instance"></span>[`instance()`](/docs/api/yii/base/staticinstancetrait/index.md#method-instance) static | Returns static class instance, which can be used to obtain meta information. | [`yii\base\StaticInstanceTrait`](/docs/api/yii/base/staticinstancetrait/index.md) |
| <span id="method-isAttributeActive"></span>[`isAttributeActive()`](/docs/api/yii/base/model/index.md#method-isAttributeActive) | Returns a value indicating whether the attribute is active in the current scenario. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-isAttributeRequired"></span>[`isAttributeRequired()`](/docs/api/yii/base/model/index.md#method-isAttributeRequired) | Returns a value indicating whether the attribute is required. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-isAttributeSafe"></span>[`isAttributeSafe()`](/docs/api/yii/base/model/index.md#method-isAttributeSafe) | Returns a value indicating whether the attribute is safe for massive assignments. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| [`isMainUser()`](#method-isMainUser) | Returns whether the current identity is the main user (or a guest). | `UserSwitch` |
| <span id="method-load"></span>[`load()`](/docs/api/yii/base/model/index.md#method-load) | Populates the model with input data. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-loadMultiple"></span>[`loadMultiple()`](/docs/api/yii/base/model/index.md#method-loadMultiple) static | Populates a set of models with the data from end user. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-offsetExists"></span>[`offsetExists()`](/docs/api/yii/base/model/index.md#method-offsetExists) | Returns whether there is an element at the specified offset. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-offsetGet"></span>[`offsetGet()`](/docs/api/yii/base/model/index.md#method-offsetGet) | Returns the element at the specified offset. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-offsetSet"></span>[`offsetSet()`](/docs/api/yii/base/model/index.md#method-offsetSet) | Sets the element at the specified offset. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-offsetUnset"></span>[`offsetUnset()`](/docs/api/yii/base/model/index.md#method-offsetUnset) | Sets the element value at the specified offset to null. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-onUnsafeAttribute"></span>[`onUnsafeAttribute()`](/docs/api/yii/base/model/index.md#method-onUnsafeAttribute) | This method is invoked when an unsafe attribute is being massively assigned. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| [`reset()`](#method-reset) | Restores the session to the main user captured before the first switch. | `UserSwitch` |
| [`rules()`](#method-rules) | Declares the validation rules applied to the switch form. | `UserSwitch` |
| <span id="method-safeAttributes"></span>[`safeAttributes()`](/docs/api/yii/base/model/index.md#method-safeAttributes) | Returns the attribute names that are safe to be massively assigned in the current scenario. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-scenarios"></span>[`scenarios()`](/docs/api/yii/base/model/index.md#method-scenarios) | Returns a list of scenarios and the corresponding active attributes. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-setAttributes"></span>[`setAttributes()`](/docs/api/yii/base/model/index.md#method-setAttributes) | Sets the attribute values in a massive way. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-setScenario"></span>[`setScenario()`](/docs/api/yii/base/model/index.md#method-setScenario) | Sets the scenario for the model. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| [`setUser()`](#method-setUser) | Switches the session to the given user and tracks the main user id when impersonating. | `UserSwitch` |
| [`setUserByIdentity()`](#method-setUserByIdentity) | Switches the session to the user identified by `$identity`. | `UserSwitch` |
| <span id="method-toArray"></span>[`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray) | Converts the model into an array. | [`yii\base\ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-validate"></span>[`validate()`](/docs/api/yii/base/model/index.md#method-validate) | Performs the data validation. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-validateMultiple"></span>[`validateMultiple()`](/docs/api/yii/base/model/index.md#method-validateMultiple) static | Validates multiple models. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="method-extractFieldsFor"></span>[`extractFieldsFor()`](/docs/api/yii/base/arrayabletrait/index.md#method-extractFieldsFor) protected | Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id". | [`yii\base\ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-extractRootFields"></span>[`extractRootFields()`](/docs/api/yii/base/arrayabletrait/index.md#method-extractRootFields) protected | Extracts the root field names from nested fields. | [`yii\base\ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |
| <span id="method-resolveFields"></span>[`resolveFields()`](/docs/api/yii/base/arrayabletrait/index.md#method-resolveFields) protected | Determines which fields can be returned by [`toArray()`](/docs/api/yii/base/arrayabletrait/index.md#method-toArray). | [`yii\base\ArrayableTrait`](/docs/api/yii/base/arrayabletrait/index.md) |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_BEFORE_VALIDATE"></span>[`EVENT_BEFORE_VALIDATE`](/docs/api/yii/base/model/index.md#event-EVENT_BEFORE_VALIDATE) | [`yii\base\ModelEvent`](/docs/api/yii/base/modelevent/index.md) | An event raised at the beginning of [`validate()`](/docs/api/yii/base/model/index.md#method-validate). Event name: `'beforeValidate'`. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |
| <span id="event-EVENT_AFTER_VALIDATE"></span>[`EVENT_AFTER_VALIDATE`](/docs/api/yii/base/model/index.md#event-EVENT_AFTER_VALIDATE) | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | An event raised at the end of [`validate()`](/docs/api/yii/base/model/index.md#method-validate) Event name: `'afterValidate'`. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-SCENARIO_DEFAULT"></span>[`SCENARIO_DEFAULT`](/docs/api/yii/base/model/index.md#const-SCENARIO_DEFAULT) | `'default'` | The name of the default scenario. | [`yii\base\Model`](/docs/api/yii/base/model/index.md) |

## Method details

### attributeLabels() <span id="method-attributeLabels"></span>

```php
public function attributeLabels(): array<string, string>
```

Returns the labels shown for the switch form attributes.

**Returns**

`array<string, string>`: Form labels keyed by attribute name.

### getMainUser() <span id="method-getMainUser"></span>

```php
public function getMainUser(): yii\web\User
```

Returns the main user; the original identity captured on the first switch.

Reads the captured id from the session when present; otherwise treats the current identity as the main one.

**Returns**

[`yii\web\User`](/docs/api/yii/web/user/index.md): User component carrying the original identity.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when the user component cannot be resolved.

### getUser() <span id="method-getUser"></span>

```php
public function getUser(): yii\web\User
```

Returns the user component bound to this switch model, resolving it lazily on first call.

**Returns**

[`yii\web\User`](/docs/api/yii/web/user/index.md): Bound user component.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when the configured component ID does not resolve to a [`User`](/docs/api/yii/web/user/index.md) instance.

### isMainUser() <span id="method-isMainUser"></span>

```php
public function isMainUser(): bool
```

Returns whether the current identity is the main user (or a guest).

**Returns**

`bool`: `true` when the session runs as the main user or as a guest; `false` while impersonating.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when the user component cannot be resolved.

### reset() <span id="method-reset"></span>

```php
public function reset(): void
```

Restores the session to the main user captured before the first switch.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when the user component cannot be resolved.

### rules() <span id="method-rules"></span>

```php
public function rules(): array<int, array<int|string, mixed>>
```

Declares the validation rules applied to the switch form.

**Returns**

`array<int, array<int|string, mixed>>`: Validation rules consumed by [`Model::validate()`](/docs/api/yii/base/model/index.md#method-validate).

### setUser() <span id="method-setUser"></span>

```php
public function setUser(yii\web\User $user): void
```

Switches the session to the given user and tracks the main user id when impersonating.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$user` | [`yii\web\User`](/docs/api/yii/web/user/index.md) | User component carrying the identity to switch to. |

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when the user component cannot be resolved.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): when the supplied user has no identity attached.

### setUserByIdentity() <span id="method-setUserByIdentity"></span>

```php
public function setUserByIdentity(yii\web\IdentityInterface $identity): void
```

Switches the session to the user identified by `$identity`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$identity` | [`yii\web\IdentityInterface`](/docs/api/yii/web/identityinterface/index.md) | Identity to impersonate. |

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when the user component cannot be resolved.