From 6a401f73236a70f702b64646d8bdec7c5a90e15d Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Tue, 5 Jul 2022 10:00:38 +0200 Subject: SONAR-16567 Notify the token creator about expiring tokens via email --- .../main/java/org/sonar/db/user/UserTokenDao.java | 12 ++++++++ .../java/org/sonar/db/user/UserTokenMapper.java | 3 ++ .../org/sonar/db/user/UserTokenMapper.xml | 8 ++++++ .../java/org/sonar/db/user/UserTokenDaoTest.java | 33 +++++++++++++++++++++- 4 files changed, 55 insertions(+), 1 deletion(-) (limited to 'server/sonar-db-dao') diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java index e1191ee266c..581db16e863 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java @@ -19,6 +19,8 @@ */ package org.sonar.db.user; +import java.time.LocalDate; +import java.time.ZoneOffset; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -54,6 +56,16 @@ public class UserTokenDao implements Dao { auditPersister.updateUserToken(dbSession, new UserTokenNewValue(userTokenDto, userLogin)); } + public List selectTokensExpiredInDays(DbSession dbSession, long days){ + long timestamp = LocalDate.now().plusDays(days).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); + return mapper(dbSession).selectTokensExpiredOnDate(timestamp); + } + + public List selectExpiredTokens(DbSession dbSession){ + long timestamp = LocalDate.now().atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); + return mapper(dbSession).selectTokensExpiredOnDate(timestamp); + } + public void updateWithoutAudit(DbSession dbSession, UserTokenDto userTokenDto) { mapper(dbSession).update(userTokenDto); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java index 435d445c35d..173085903e9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java @@ -41,4 +41,7 @@ public interface UserTokenMapper { int deleteByProjectKey(@Param("projectKey") String projectKey); List countTokensByUserUuids(@Param("userUuids") List userUuids); + + List selectTokensExpiredOnDate(@Param("timestamp") long timestamp); + } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml index f99bfb6c5c7..3e52e4907e2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml @@ -55,6 +55,14 @@ WHERE t.token_hash=#{tokenHash, jdbcType=VARCHAR} + +