config = $config; $this->policyConfig = $policyConfig; $this->userManager = $userManager; $this->eventDispatcher = $eventDispatcher; $this->l = $l; } /** * @throws PreConditionNotMetException */ public function update(IUser $user, string $password): void { if (!$this->isLocalUser($user)) { return; } if ($this->policyConfig->getExpiryInDays() === 0) { $this->config->deleteUserValue( $user->getUID(), 'password_policy', 'pwd_last_updated' ); return; } $this->config->setUserValue( $user->getUID(), 'password_policy', 'pwd_last_updated', (string)time() ); } public function entryControl(IUser $user, ?string $password): void { if ($this->policyConfig->getExpiryInDays() !== 0 && $this->isLocalUser($user) && $this->isPasswordExpired($user) ) { $message = 'Password is expired, please use forgot password method to reset'; $message_t = $this->l->t('Password is expired, please use forgot password method to reset'); throw new HintException($message, $message_t); } } protected function isPasswordExpired(IUser $user): bool { $updatedAt = (int)$this->config->getUserValue( $user->getUID(), 'password_policy', 'pwd_last_updated', 0 ); if ($updatedAt === 0) { $this->update($user, ''); return false; } $expiryInDays = $this->policyConfig->getExpiryInDays(); $expiresIn = $updatedAt + $expiryInDays * 24 * 60 * 60; return $expiresIn <= time(); } protected function isLocalUser(IUser $user): bool { $localBackends = ['Database', 'Guests']; return in_array($user->getBackendClassName(), $localBackends); } }