Skip to content

UserGroups

Type
Class
Namespace
craft\services
Inherits
craft\services\UserGroups » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

User Groups service.

An instance of the service is available via Craft::$app->userGroups.

View source

Public Properties

PropertyDescription
allGroupscraft\models\UserGroup[]
assignableGroupscraft\models\UserGroup[]
behaviorsyii\base\Behavior – List of behaviors attached to this component.
teamGroupcraft\models\UserGroup

allGroups

Type
craft\models\UserGroup[]
Default value
null
Access
Read-only

View source

assignableGroups

Type
craft\models\UserGroup[]
Default value
null
Access
Read-only

View source

teamGroup

Type
craft\models\UserGroup
Default value
null
Access
Read-only
Since
5.1.0

View source

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 a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
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.
deleteGroup()Deletes a user group.
deleteGroupById()Deletes a user group by its ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
eagerLoadGroups()Eager-loads user groups onto the given users.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
getAllGroups()Returns all user groups.
getAssignableGroups()Returns the user groups that the current user is allowed to assign to another user.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getGroupByHandle()Gets a user group by its handle.
getGroupById()Gets a user group by its ID.
getGroupByUid()Gets a user group by its UID.
getGroupsByUserId()Gets user groups by a user ID.
getTeamGroup()Returns the Craft Team edition’s user group.
handleChangedUserGroup()Handle any changed user groups.
handleDeletedUserGroup()Handle any deleted user groups.
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 for this component.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
saveGroup()Saves a user group.
trigger()Triggers an event.

deleteGroup()

Since
3.0.12

Deletes a user group.

View source

Arguments

Returns

boolean – Whether the user group was deleted successfully

Throws

deleteGroupById()

Deletes a user group by its ID.

View source

Arguments

  • $groupId (integer) – The user group's ID

Returns

boolean – Whether the user group was deleted successfully

Throws

eagerLoadGroups()

Since
3.6.0

Eager-loads user groups onto the given users.

View source

Arguments

getAllGroups()

Returns all user groups.

View source

Returns

craft\models\UserGroup[]

getAssignableGroups()

Returns the user groups that the current user is allowed to assign to another user.

View source

Arguments

  • $user (craft\elements\User, null) – The recipient of the user groups. If set, their current groups will be included as well.

Returns

craft\models\UserGroup[]

getGroupByHandle()

Gets a user group by its handle.

View source

Arguments

Returns

craft\models\UserGroup, null

getGroupById()

Gets a user group by its ID.

View source

Arguments

Returns

craft\models\UserGroup, null

getGroupByUid()

Gets a user group by its UID.

View source

Arguments

Returns

craft\models\UserGroup, null

getGroupsByUserId()

Gets user groups by a user ID.

View source

Arguments

Returns

craft\models\UserGroup[]

getTeamGroup()

Since
5.1.0

Returns the Craft Team edition’s user group.

View source

Returns

craft\models\UserGroup

handleChangedUserGroup()

Handle any changed user groups.

View source

Arguments

handleDeletedUserGroup()

Handle any deleted user groups.

View source

Arguments

saveGroup()

Saves a user group.

View source

Arguments

Returns

boolean

Throws

Constants

ConstantDescription
TEAM_GROUP_UUIDThe “Team” group’s UUID.

Events

EVENT_AFTER_DELETE_USER_GROUP

Type
craft\events\UserGroupEvent

The event that is triggered after a user group is saved.


EVENT_AFTER_SAVE_USER_GROUP

Type
craft\events\UserGroupEvent

The event that is triggered after a user group is saved.


EVENT_BEFORE_APPLY_GROUP_DELETE

Type
craft\events\UserGroupEvent
Since
3.1.0

The event that is triggered before a user group delete is applied to the database.


EVENT_BEFORE_DELETE_USER_GROUP

Type
craft\events\UserGroupEvent

The event that is triggered before a user group is deleted.


EVENT_BEFORE_SAVE_USER_GROUP

Type
craft\events\UserGroupEvent

The event that is triggered before a user group is saved.