Skip to content

Instantly share code, notes, and snippets.

@juban
Last active February 23, 2021 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juban/4ad2067a805fd80383a116eb31bf7edd to your computer and use it in GitHub Desktop.
Save juban/4ad2067a805fd80383a116eb31bf7edd to your computer and use it in GitHub Desktop.
Craft CMS 3 - Set activation and reset password emails urls according to user group or site
<?php
Event::on(
\craft\records\User::class,
\craft\records\User::EVENT_AFTER_UPDATE,
static function (\yii\db\AfterSaveEvent $event) {
$userRecord = $event->sender;
$user = Craft::$app->getUsers()->getUserById($userRecord->id);
$sites = Craft::$app->getSites();
if ($user->isInGroup('user')) {
$sites->setCurrentSite($sites->getSiteByHandle('sitehandle'));
}
}
);
Event::on(
UsersController::class,
UsersController::EVENT_AFTER_ASSIGN_GROUPS_AND_PERMISSIONS,
static function (UserEvent $event) {
$user = $event->user;
$sites = Craft::$app->getSites();
if ($user->isInGroup('user')) {
$sites->setCurrentSite($sites->getSiteByHandle('sitehandle'));
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment