Expiring users #17401
-
I just got this question from a client who works a lot with interns to edit website content. Wouldn't it be nice if you could (just like entries) set an expiration date on users? If the user is expired, they cannot login anymore or reset their password. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You could accomplish this by creating a “User Expirations” section, with a Users relation field. Then create a plugin/module with the following code: use craft\base\Event;
use craft\elements\Entry;
use craft\elements\User;
use craft\events\ModelEvent;
use craft\helpers\ElementHelper;
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (ModelEvent $event) {
/** @var Entry $entry */
$entry = $event->sender;
if (
!ElementHelper::isDraftOrRevision($entry) &&
$entry->getSection()->handle === 'userExpirations' &&
$entry->oldStatus === Entry::STATUS_LIVE &&
$entry->getStatus() === Entry::STATUS_EXPIRED
) {
/** @var User|null $user */
$user = $entry->user->one();
if ($user) {
Craft::$app->users->deactivateUser($user);
}
}
}); Finally, enable Static Statuses for the site, so that Then you can start creating entries in the User Expirations section, with a related user and an Expiry Date set, and the user will get deactivated when the entry expires. |
Beta Was this translation helpful? Give feedback.
You could accomplish this by creating a “User Expirations” section, with a Users relation field.
Then create a plugin/module with the following code: