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

<p>get the date 90 days ago, find all users where <code>lastPasswordChangeDate</code> 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.</p>

CC BY-SA 4.0
2018-06-23T17:01:38.723Z
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
CC BY-SA 4.0
2018-06-23T17:03:15.403Z
What do you mean?
Robin Schambach
CC BY-SA 4.0
2018-06-23T17:05:48.293Z
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
CC BY-SA 4.0
2018-06-23T17:10:25.660Z
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
CC BY-SA 4.0
2018-06-10T18:56:16.950Z
2018-06-10T18:56:16.950Z
Robin Schambach
<p>I am currently building an intranet for a client and one of their requirements is that staff must update their passwords every 90 days. </p>

<p>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. </p>

<p>Thanks</p>

<password>
Enforce password change every 90 days (3 months)
# Enforce password change every 90 days (3 months)

(Asked by: username_0 on 2018-06-10)

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

**Tags:** password


---

## 1 answers

### Answer by username_1 on 2018-06-10. Score: 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.

> **Comments:**
> **username_0**: 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?  
> **username_1**: What do you mean?  
> **username_0**: 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?  
> **username_1**: 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?  

---

