Users

Type
Class
Namespace
craft\services
Inherits
craft\services\Users » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

The Users service provides APIs for managing users.

An instance of the Users service is globally accessible in Craft via Craft::$app->users.

View source (opens new window)

# Public Properties

Property Description
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
userPreferences array (opens new window) – The user’s preferences
verificationCodeOnUser craft\elements\User – The user.

# userPreferences

Type
array (opens new window)
Default value
null
Access
Read-only

The user’s preferences

View source (opens new window)

# verificationCodeOnUser

Type
craft\elements\User
Default value
null
Access
Write-only

The user.

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
activateUser() Activates a user, bypassing email verification.
assignUserToDefaultGroup() Assigns a user to the default user group.
assignUserToGroups() Assigns a user to a given list of user groups.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canImpersonate() Returns whether a user is allowed to impersonate another user.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
deleteUserPhoto() Deletes a user’s photo.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getEmailVerifyUrl() Sets a new verification code on a user, and returns their new Email Verification URL.
getPasswordResetUrl() Sets a new verification code on a user, and returns their new Password Reset URL.
getUserById() Returns a user by their ID.
getUserByUid() Returns a user by their UID.
getUserByUsernameOrEmail() Returns a user by their username or email.
getUserPreference() Returns one of a user’s preferences by its key.
getUserPreferences() Returns a user’s preferences.
handleChangedUserFieldLayout() Handle user field layout changes.
handleInvalidLogin() Handles an invalid login for a user.
handleValidLogin() Handles a valid login for a user.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
hasUserShunnedMessage() Returns whether a message is shunned for a user.
init() (opens new window) Initializes the object.
isVerificationCodeValidForUser() Returns whether a verification code is valid for the given user.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
pruneDeletedField() Prune a deleted field from user group layout.
purgeExpiredPendingUsers() Deletes any pending users that have shown zero sense of urgency and are just taking up space.
relocateUserPhoto() Updates the location of a user’s photo.
saveLayout() Save the user field layout
saveUserPhoto() Crops and saves a user’s photo.
saveUserPreferences() Saves a user’s preferences.
sendActivationEmail() Sends a new account activation email for a user, regardless of their status.
sendNewEmailVerifyEmail() Sends a new email verification email to a user, regardless of their status.
sendPasswordResetEmail() Sends a password reset email to a user.
setVerificationCodeOnUser() Sets a new verification code on the user's record.
shunMessageForUser() Shuns a message for a user.
suspendUser() Suspends a user.
trigger() (opens new window) Triggers an event.
unlockUser() Unlocks a user, bypassing the cooldown phase.
unshunMessageForUser() Un-shuns a message for a user.
unsuspendUser() Unsuspends a user.
verifyEmailForUser() If 'unverifiedEmail' is set on the User, then this method will transfer it to the official email property and clear the unverified one.

# activateUser()

Activates a user, bypassing email verification.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user was activated successfully.

Throws

# assignUserToDefaultGroup()

Assigns a user to the default user group.

This method is called toward the end of a public registration request.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user was assigned to the default group.

# assignUserToGroups()

Assigns a user to a given list of user groups.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the users were successfully assigned to the groups.

# canImpersonate()

Since
3.2.0

Returns whether a user is allowed to impersonate another user.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# deleteUserPhoto()

Deletes a user’s photo.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user’s photo was deleted successfully

# getEmailVerifyUrl()

Sets a new verification code on a user, and returns their new Email Verification URL.

View source (opens new window)

Arguments

Returns

string (opens new window) – The new Email Verification URL.

# getPasswordResetUrl()

Sets a new verification code on a user, and returns their new Password Reset URL.

View source (opens new window)

Arguments

Returns

string (opens new window) – The new Password Reset URL.

# getUserById()

Returns a user by their ID.

$user = Craft::$app->users->getUserById($userId);

View source (opens new window)

Arguments

Returns

craft\elements\User, null (opens new window) – The user with the given ID, or null if a user could not be found.

# getUserByUid()

Returns a user by their UID.

$user = Craft::$app->users->getUserByUid($userUid);

View source (opens new window)

Arguments

Returns

craft\elements\User, null (opens new window) – The user with the given UID, or null if a user could not be found.

# getUserByUsernameOrEmail()

Returns a user by their username or email.

$user = Craft::$app->users->getUserByUsernameOrEmail($loginName);

View source (opens new window)

Arguments

Returns

craft\elements\User, null (opens new window) – The user with the given username/email, or null if a user could not be found.

# getUserPreference()

Returns one of a user’s preferences by its key.

View source (opens new window)

Arguments

Returns

mixed – The user’s preference

# getUserPreferences()

Returns a user’s preferences.

View source (opens new window)

Arguments

Returns

array (opens new window) – The user’s preferences

# handleChangedUserFieldLayout()

Handle user field layout changes.

View source (opens new window)

Arguments

# handleInvalidLogin()

Handles an invalid login for a user.

View source (opens new window)

Arguments

# handleValidLogin()

Handles a valid login for a user.

View source (opens new window)

Arguments

# hasUserShunnedMessage()

Returns whether a message is shunned for a user.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user has shunned the message.

# isVerificationCodeValidForUser()

Returns whether a verification code is valid for the given user.

This method first checks if the code has expired past the config3:verificationCodeDuration (opens new window) config setting. If it is still valid, then, the checks the validity of the contents of the code.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the code is still valid.

# pruneDeletedField()

Prune a deleted field from user group layout.

View source (opens new window)

Arguments

# purgeExpiredPendingUsers()

Deletes any pending users that have shown zero sense of urgency and are just taking up space.

This method will check the config3:purgePendingUsersDuration (opens new window) config setting, and if it is set to a valid duration, it will delete any user accounts that were created that duration ago, and have still not activated their account.

View source (opens new window)

# relocateUserPhoto()

Since
3.5.14

Updates the location of a user’s photo.

View source (opens new window)

Arguments

# saveLayout()

Save the user field layout

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# saveUserPhoto()

Crops and saves a user’s photo.

View source (opens new window)

Arguments

Throws

# saveUserPreferences()

Saves a user’s preferences.

View source (opens new window)

Arguments

# sendActivationEmail()

Sends a new account activation email for a user, regardless of their status.

A new verification code will generated for the user overwriting any existing one.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the email was sent successfully.

# sendNewEmailVerifyEmail()

Sends a new email verification email to a user, regardless of their status.

A new verification code will generated for the user overwriting any existing one.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the email was sent successfully.

# sendPasswordResetEmail()

Sends a password reset email to a user.

A new verification code be will generated for the user, overwriting any existing one.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the email was sent successfully.

# setVerificationCodeOnUser()

Sets a new verification code on the user's record.

View source (opens new window)

Arguments

Returns

string (opens new window) – The user’s brand new verification code.

# shunMessageForUser()

Shuns a message for a user.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the message was shunned successfully.

# suspendUser()

Suspends a user.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user was suspended successfully.

Throws

# unlockUser()

Unlocks a user, bypassing the cooldown phase.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user was unlocked successfully.

Throws

# unshunMessageForUser()

Un-shuns a message for a user.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the message was un-shunned successfully.

# unsuspendUser()

Unsuspends a user.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the user was unsuspended successfully.

Throws

# verifyEmailForUser()

If 'unverifiedEmail' is set on the User, then this method will transfer it to the official email property and clear the unverified one.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# Constants

Constant Description
CONFIG_USERLAYOUT_KEY
CONFIG_USERS_KEY

# Events

# EVENT_AFTER_ACTIVATE_USER

Type
craft\events\UserEvent

The event that is triggered after a user is activated.

# EVENT_AFTER_ASSIGN_USER_TO_DEFAULT_GROUP

Type
craft\events\UserAssignGroupEvent

The event that is triggered after a user is assigned to the default user group.

# EVENT_AFTER_ASSIGN_USER_TO_GROUPS

Type
craft\events\UserGroupsAssignEvent

The event that is triggered after a user is assigned to some user groups.

# EVENT_AFTER_LOCK_USER

Type
craft\events\UserEvent

The event that is triggered after a user is locked.

# EVENT_AFTER_SUSPEND_USER

Type
craft\events\UserEvent

The event that is triggered after a user is suspended.

# EVENT_AFTER_UNLOCK_USER

Type
craft\events\UserEvent

The event that is triggered after a user is unlocked.

# EVENT_AFTER_UNSUSPEND_USER

Type
craft\events\UserEvent

The event that is triggered after a user is unsuspended.

# EVENT_AFTER_VERIFY_EMAIL

Type
craft\events\UserEvent

The event that is triggered after a user's email is verified.

# EVENT_BEFORE_ACTIVATE_USER

Type
craft\events\UserEvent

The event that is triggered before a user is activated.

You may set craft\events\UserEvent::$isValid to false to prevent the user from getting activated.

# EVENT_BEFORE_ASSIGN_USER_TO_DEFAULT_GROUP

Type
craft\events\UserAssignGroupEvent

The event that is triggered before a user is assigned to the default user group.

You may set craft\events\UserAssignGroupEvent::$isValid to false to prevent the user from getting assigned to the default user group.

# EVENT_BEFORE_ASSIGN_USER_TO_GROUPS

Type
craft\events\UserGroupsAssignEvent

The event that is triggered before a user is assigned to some user groups.

You may set craft\events\UserGroupsAssignEvent::$isValid to false to prevent the user from getting assigned to the groups.

# EVENT_BEFORE_SUSPEND_USER

Type
craft\events\UserEvent

The event that is triggered before a user is suspended.

You may set craft\events\UserEvent::$isValid to false to prevent the user from getting suspended.

# EVENT_BEFORE_UNLOCK_USER

Type
craft\events\UserEvent

The event that is triggered before a user is unlocked.

You may set craft\events\UserEvent::$isValid to false to prevent the user from getting unlocked.

# EVENT_BEFORE_UNSUSPEND_USER

Type
craft\events\UserEvent

The event that is triggered before a user is unsuspended.

You may set craft\events\UserEvent::$isValid to false to prevent the user from getting unsuspended.

# EVENT_BEFORE_VERIFY_EMAIL

Type
craft\events\UserEvent

The event that is triggered before a user's email is verified.