UsersController

Type
Class
Namespace
craft\controllers
Inherits
craft\controllers\UsersController » craft\web\Controller » yii\web\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable, yii\base\ViewContextInterface
Since
3.0

The UsersController class is a controller that handles various user account related tasks such as logging-in, impersonating a user, logging out, forgetting passwords, setting passwords, validating accounts, activating accounts, creating users, saving users, processing user avatars, deleting, suspending and un-suspending users.

Note that all actions in the controller, except actionLogin(), actionLogout(), actionGetRemainingSessionTime(), actionSendPasswordResetEmail(), actionSetPassword(), actionVerifyEmail() and actionSaveUser() require an authenticated Craft session via $allowAnonymous.

View source

Public Properties

PropertyDescription
$actionyii\base\Action – The action that is currently being executed.
$actionParamsarray – The parameters bound to the current action.
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$defaultActionstring – The ID of the action that is used when the action ID is not specified in the request.
$enableCsrfValidationboolean – Whether to enable CSRF validation for the actions in this controller.
$idstring – The ID of this controller.
$layoutnull, string, false – The name of the layout to be applied to this controller's views.
$moduleyii\base\Module – The module that this controller belongs to.
$modulesyii\base\Module – All ancestor modules that this controller is located within.
$routestring – The route (module ID, controller ID and action ID) of the current request.
$uniqueIdstring – The controller ID that is prefixed with the module ID (if any).
$viewyii\base\View, yii\web\View – The view object that can be used to render views or view files.
$viewPathstring – The view path that may be prefixed to a relative view name.

Protected Properties

PropertyDescription
$allowAnonymousboolean, string[] – Whether this controller’s actions can be accessed anonymously If set to false, you are required to be logged in to execute any of the given controller's actions.

$allowAnonymous

Type
boolean, string[]

Whether this controller’s actions can be accessed anonymously If set to false, you are required to be logged in to execute any of the given controller's actions. If set to true, anonymous access is allowed for all of the given controller's actions. If the value is an array of action IDs, then you must be logged in for any actions except for the ones in the array list. If you have a controller that where the majority of actions allow anonymous access, but you only want require login on a few, you can set this to true and call requireLogin() in the individual methods.

View source

Signature

protected boolean, string[] $allowAnonymous = ['login', 'logout', 'get-remaining-session-time', 'send-password-reset-email', 'send-activation-email', 'save-user', 'set-password', 'verify-email']

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to null.
actionActivateUser()Manually activates a user account. Only admins have access.
actionDeleteUser()Deletes a user.
actionDeleteUserPhoto()Delete all the photos for current user.
actionEditUser()Edit a user account.
actionGetElevatedSessionTimeout()Returns how many seconds are left in the current elevated user session.
actionGetPasswordResetUrl()Generates a new verification code for a given user, and returns its URL.
actionGetRemainingSessionTime()Returns how many seconds are left in the current user session.
actionImpersonate()Logs a user in for impersonation. Requires you to be an administrator.
actionLogin()Displays the login template, and handles login post requests.
actionLogout()
actionSaveFieldLayout()Saves the user field layout.
actionSaveUser()Provides an endpoint for saving a user account.
actionSendActivationEmail()Sends a new activation email to a user.
actionSendPasswordResetEmail()Sends a password reset email.
actionSetPassword()Sets a user's password once they've verified they have access to their email.
actionStartElevatedSession()Starts an elevated user session.
actionSuspendUser()Suspends a user.
actionUnlockUser()Unlocks a user, bypassing the cooldown phase.
actionUnsuspendUser()Unsuspends a user.
actionUploadUserPhoto()Upload a user photo.
actionUserContentSummary()Returns a summary of the content that is owned by a given user ID(s).
actionVerifyEmail()Verifies that a user has access to an email address.
actionVerifyPassword()Verifies a password for a user.
actions()Declares external actions for the controller.
afterAction()This method is invoked right after an action is executed.
asErrorJson()Responds to the request with a JSON error message.
asJson()Send data formatted as JSON.
asJsonP()Sets the response format of the given data as JSONP.
asRaw()Sets the response format of the given data as RAW.
asXml()Send data formatted as XML.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
beforeAction()This method is invoked right before an action is executed.
behaviors()Returns a list of behaviors that this component should behave as.
bindActionParams()Binds the parameters to the action.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
createAction()Creates an action based on the given action ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
findLayoutFile()Finds the applicable layout file.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getModules()Returns all ancestor modules of this controller.
getRoute()Returns the route of the current request.
getUniqueId()Returns the unique ID of the controller.
getView()Returns the view object that can be used to render views or view files.
getViewPath()
goBack()Redirects the browser to the last visited page.
goHome()Redirects the browser to the home page.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
redirect()Redirects the browser to the specified URL.
redirectToPostedUrl()Redirects to the URI specified in the POST.
refresh()Refreshes the current page.
render()Renders a view and applies layout if available.
renderAjax()Renders a view in response to an AJAX request.
renderContent()Renders a static string by applying a layout.
renderFile()Renders a view file.
renderPartial()Renders a view without applying layout.
renderTemplate()Renders a template.
requireAcceptsJson()Throws a 400 error if the request doesn't accept JSON.
requireAdmin()Throws a 403 error if the current user is not an admin.
requireAuthorization()Checks whether the current user can perform a given action, and ends the request with a 403 error if they don’t.
requireElevatedSession()Requires that the user has an elevated session.
requireLogin()Redirects the user to the login template if they're not logged in.
requirePermission()Checks whether the current user has a given permission, and ends the request with a 403 error if they don’t.
requirePostRequest()Throws a 400 error if this isn’t a POST request
requireToken()Throws a 400 error if the current request doesn’t have a valid token.
run()Runs a request specified in terms of a route.
runAction()Runs an action within this controller with the specified action ID and parameters.
setView()Sets the view object to be used by this controller.
setViewPath()Sets the directory that contains the view files.
trigger()Triggers an event.

actionActivateUser()

Manually activates a user account. Only admins have access.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionActivateUser ( )

actionDeleteUser()

Deletes a user.

View source

Returns

yii\web\Response, null

Signature

public yii\web\Response, null actionDeleteUser ( )

actionDeleteUserPhoto()

Delete all the photos for current user.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionDeleteUserPhoto ( )

actionEditUser()

Edit a user account.

View source

Arguments

  • $userId (integer, string, null) – The user’s ID, if any, or a string that indicates the user to be edited ('current' or 'client').
  • $user (craft\elements\User, null) – The user being edited, if there were any validation errors.

Returns

yii\web\Response

Throws

Signature

public yii\web\Response actionEditUser ( $userId = null, craft\elements\User $user = null )

actionGetElevatedSessionTimeout()

Returns how many seconds are left in the current elevated user session.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionGetElevatedSessionTimeout ( )

actionGetPasswordResetUrl()

Generates a new verification code for a given user, and returns its URL.

View source

Returns

yii\web\Response

Throws

Signature

public yii\web\Response actionGetPasswordResetUrl ( )

actionGetRemainingSessionTime()

Returns how many seconds are left in the current user session.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionGetRemainingSessionTime ( )

actionImpersonate()

Logs a user in for impersonation. Requires you to be an administrator.

View source

Returns

yii\web\Response, null

Signature

public yii\web\Response, null actionImpersonate ( )

actionLogin()

Displays the login template, and handles login post requests.

View source

Returns

yii\web\Response, null

Signature

public yii\web\Response, null actionLogin ( )

actionLogout()

Signature

public yii\web\Response actionLogout ( )

actionSaveFieldLayout()

Saves the user field layout.

View source

Returns

yii\web\Response, null

Signature

public yii\web\Response, null actionSaveFieldLayout ( )

actionSaveUser()

Provides an endpoint for saving a user account. This action accounts for the following scenarios:

  • An admin registering a new user account.
  • An admin editing an existing user account.
  • A normal user with user-administration permissions registering a new user account.
  • A normal user with user-administration permissions editing an existing user account.
  • A guest registering a new user account ("public registration"). This action behaves the same regardless of whether it was requested from the Control Panel or the front-end site.

View source

Returns

yii\web\Response, null

Throws

Signature

public yii\web\Response, null actionSaveUser ( )

actionSendActivationEmail()

Sends a new activation email to a user.

View source

Returns

yii\web\Response

Throws

Signature

public yii\web\Response actionSendActivationEmail ( )

actionSendPasswordResetEmail()

Sends a password reset email.

View source

Returns

yii\web\Response, null

Throws

Signature

public yii\web\Response, null actionSendPasswordResetEmail ( )

actionSetPassword()

Sets a user's password once they've verified they have access to their email.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionSetPassword ( )

actionStartElevatedSession()

Starts an elevated user session. return Response

View source

Signature

public void actionStartElevatedSession ( )

actionSuspendUser()

Suspends a user.

View source

Returns

yii\web\Response, null

Throws

Signature

public yii\web\Response, null actionSuspendUser ( )

actionUnlockUser()

Unlocks a user, bypassing the cooldown phase.

View source

Returns

yii\web\Response

Throws

Signature

public yii\web\Response actionUnlockUser ( )

actionUnsuspendUser()

Unsuspends a user.

View source

Returns

yii\web\Response, null

Throws

Signature

public yii\web\Response, null actionUnsuspendUser ( )

actionUploadUserPhoto()

Upload a user photo.

View source

Returns

yii\web\Response, null

Throws

Signature

public yii\web\Response, null actionUploadUserPhoto ( )

actionUserContentSummary()

Returns a summary of the content that is owned by a given user ID(s).

View source

Returns

yii\web\Response, null

Signature

public yii\web\Response, null actionUserContentSummary ( )

actionVerifyEmail()

Verifies that a user has access to an email address.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionVerifyEmail ( )

actionVerifyPassword()

Verifies a password for a user.

View source

Returns

yii\web\Response

Signature

public yii\web\Response actionVerifyPassword ( )

Events

EVENT_DEFINE_CONTENT_SUMMARY

Type
craft\events\DefineUserContentSummaryEvent

The event that is triggered when defining a summary of content owned by a user(s), before they are deleted

Example

EVENT_LOGIN_FAILURE

Type
craft\events\LoginFailureEvent

The event that is triggered when a failed login attempt was made

EVENT_REGISTER_USER_ACTIONS

Type
craft\events\RegisterUserActionsEvent

The event that is triggered when a user’s available actions are being registered