1

I am currently building an intranet for a client and one of their requirements is that staff must update their passwords every 90 days.

I assume I would need to build a plugin for this, but what would be best to way to approach this? I was hoping it might work whereby this was set 90 days after the last password change, rather than a fixed date for every user.

Thanks

2
$date90DaysAgo = (new \DateTime())->modify('90 days ago')->format('Y-m-d H:i:s');
foreach(\craft\elements\User::find()->lastPasswordChangeDate('<='.$date90DaysAgo)->all() as $user){
    Craft::$app->getUsers()->sendPasswordResetEmail($user);
    $user->newPassword = // generate some temp password, so the user can't login with his/her anymore
    $user->setScenario(User::SCENARIO_PASSWORD);
    Craft::$app->getElements()->saveElement($user);
}

get the date 90 days ago, find all users where lastPasswordChangeDate is smaller than this date, send them a new password reset email and change their password to something else, so they can't login anymore.

  • Thanks for this Robin. Excuse my nativity but aside from creating this in a plugin how/what do I need to do to get this to run? – Terry Upton Jun 23 '18 at 17:01
  • What do you mean? – Robin Schambach Jun 23 '18 at 17:03
  • I am not sure how this code would run? Or how to make it run? I assume I would need to create it as a plugin but then when/how does it go through and reset the passwords for those that have expired? – Terry Upton Jun 23 '18 at 17:05
  • Same question again, I have no idea what you mean... You need to create a module or a plugin and copy the code there. You could use a very easy and nasty solution and just copy it into your init function so it's executed in every request or you create a route with a controller to access it via cronjob. Do you want to know what it does exactly or how to create a controller or route or what? – Robin Schambach Jun 23 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.