You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserLastConnectionDatesUpdaterImpl.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import javax.annotation.Nullable;
  22. import org.sonar.api.utils.System2;
  23. import org.sonar.db.DbClient;
  24. import org.sonar.db.DbSession;
  25. import org.sonar.db.user.UserDto;
  26. import org.sonar.db.user.UserTokenDto;
  27. public class UserLastConnectionDatesUpdaterImpl implements UserLastConnectionDatesUpdater {
  28. private static final long ONE_HOUR_IN_MILLISECONDS = 60 * 60 * 1000L;
  29. private final DbClient dbClient;
  30. private final System2 system2;
  31. public UserLastConnectionDatesUpdaterImpl(DbClient dbClient, System2 system2) {
  32. this.dbClient = dbClient;
  33. this.system2 = system2;
  34. }
  35. @Override
  36. public void updateLastConnectionDateIfNeeded(UserDto user) {
  37. Long lastConnectionDate = user.getLastConnectionDate();
  38. long now = system2.now();
  39. if (doesNotRequireUpdate(lastConnectionDate, now)) {
  40. return;
  41. }
  42. try (DbSession dbSession = dbClient.openSession(false)) {
  43. dbClient.userDao().update(dbSession, user.setLastConnectionDate(now));
  44. dbSession.commit();
  45. }
  46. }
  47. @Override
  48. public void updateLastConnectionDateIfNeeded(UserTokenDto userToken) {
  49. Long lastConnectionDate = userToken.getLastConnectionDate();
  50. long now = system2.now();
  51. if (doesNotRequireUpdate(lastConnectionDate, now)) {
  52. return;
  53. }
  54. try (DbSession dbSession = dbClient.openSession(false)) {
  55. dbClient.userTokenDao().update(dbSession, userToken.setLastConnectionDate(now));
  56. userToken.setLastConnectionDate(now);
  57. dbSession.commit();
  58. }
  59. }
  60. private static boolean doesNotRequireUpdate(@Nullable Long lastConnectionDate, long now) {
  61. // Update date only once per hour in order to decrease pressure on DB
  62. return lastConnectionDate != null && (now - lastConnectionDate) < ONE_HOUR_IN_MILLISECONDS;
  63. }
  64. }