DataStore.java

  1. /*
  2.  * Copyright (C) 2020-2024 by Savoir-faire Linux
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16.  */
  17. package net.jami.datastore.main;

  18. import lombok.Getter;
  19. import lombok.Setter;

  20. import net.jami.datastore.dao.ContactDao;
  21. import net.jami.datastore.dao.ConversationDao;
  22. import net.jami.datastore.dao.ConversationRequestDao;
  23. import net.jami.datastore.dao.DeviceDao;
  24. import net.jami.datastore.dao.GroupDao;
  25. import net.jami.datastore.dao.PolicyDao;
  26. import net.jami.datastore.dao.SystemDao;
  27. import net.jami.datastore.dao.UserDao;
  28. import net.jami.datastore.dao.UserGroupMappingsDao;
  29. import net.jami.datastore.dao.UserProfileDao;
  30. import net.jami.jams.common.authentication.AuthenticationSource;
  31. import net.jami.jams.common.authentication.AuthenticationSourceInfo;
  32. import net.jami.jams.common.authentication.AuthenticationSourceType;
  33. import net.jami.jams.common.dao.connectivity.ConnectionPool;
  34. import net.jami.jams.common.objects.user.User;
  35. import net.jami.jams.common.objects.user.UserProfile;
  36. import net.jami.jams.common.utils.X509Utils;

  37. import org.flywaydb.core.Flyway;

  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import java.util.Optional;

  41. @Getter
  42. @Setter
  43. public class DataStore implements AuthenticationSource {

  44.     public static ConnectionPool connectionPool;
  45.     private UserDao userDao;
  46.     private GroupDao groupDao;
  47.     private PolicyDao policyDao;
  48.     private DeviceDao deviceDao;
  49.     private SystemDao systemDao;
  50.     private ContactDao contactDao;
  51.     private ConversationDao conversationDao;
  52.     private ConversationRequestDao conversationRequestDao;
  53.     private UserProfileDao userProfileDao;
  54.     private UserGroupMappingsDao userGroupMappingsDao;
  55.     public static final Integer RESULTS_PER_PAGE = 24;
  56.     public static Integer NUM_PAGES;

  57.     // Implicitly connect to derby.
  58.     public DataStore(String connectionString) {
  59.         Flyway flyway = Flyway.configure().dataSource(connectionString, "", "").load();
  60.         flyway.migrate();
  61.         connectionPool = new ConnectionPool(connectionString);
  62.         userDao = new UserDao();
  63.         groupDao = new GroupDao();
  64.         policyDao = new PolicyDao();
  65.         deviceDao = new DeviceDao();
  66.         systemDao = new SystemDao();
  67.         contactDao = new ContactDao();
  68.         conversationDao = new ConversationDao();
  69.         conversationRequestDao = new ConversationRequestDao();
  70.         userProfileDao = new UserProfileDao();
  71.         userGroupMappingsDao = new UserGroupMappingsDao();
  72.     }

  73.     @Override
  74.     public boolean createUser(User user) {
  75.         return userDao.storeObject(user);
  76.     }

  77.     @Override
  78.     public List<UserProfile> searchUserProfiles(
  79.             String queryString, String field, Optional<Integer> page) {
  80.         List<UserProfile> userList;

  81.         if (queryString.equals("*")) {
  82.             userList = userProfileDao.getAllUserProfile();
  83.         } else {
  84.             if (field.equals("LOGON_NAME")) {
  85.                 userList = userProfileDao.searchUsername(queryString);
  86.             } else if (field.equals("FULL_TEXT_NAME")) {
  87.                 userList = userProfileDao.searchFullName(queryString);
  88.             } else {
  89.                 throw new IllegalArgumentException("Invalid field");
  90.             }
  91.         }

  92.         if (userList == null) userList = new ArrayList<>();

  93.         NUM_PAGES = (Integer) userList.size() / RESULTS_PER_PAGE;
  94.         if (userList.size() % RESULTS_PER_PAGE != 0) NUM_PAGES++;

  95.         if (page.isPresent() && !userList.isEmpty()) {
  96.             if (userList.size() < RESULTS_PER_PAGE) userList = userList.subList(0, userList.size());
  97.             else if (page.get() * RESULTS_PER_PAGE > userList.size())
  98.                 userList = userList.subList((page.get() - 1) * RESULTS_PER_PAGE, userList.size());
  99.             else
  100.                 userList =
  101.                         userList.subList(
  102.                                 (page.get() - 1) * RESULTS_PER_PAGE,
  103.                                 (page.get() * RESULTS_PER_PAGE));
  104.         }

  105.         return userList;
  106.     }

  107.     @Override
  108.     public UserProfile getUserProfile(String username) {
  109.         return userProfileDao.getByUsername(username).orElse(null);
  110.     }

  111.     @Override
  112.     public boolean setUserProfile(UserProfile userProfile) {
  113.         return userProfileDao.storeObject(userProfile);
  114.     }

  115.     public boolean updateUserProfile(UserProfile userProfile) {
  116.         return userProfileDao.updateUserProfile(userProfile);
  117.     }

  118.     public boolean updateUserCertificate(User user) {
  119.         return userDao.updateUserCertificate(
  120.                 user.getUsername(), X509Utils.getPEMStringFromCertificate(user.getCertificate()));
  121.     }

  122.     @Override
  123.     public boolean authenticate(String username, String password) {
  124.         return userDao.getByUsername(username)
  125.                 .map(user -> user.getPassword())
  126.                 .orElse("")
  127.                 .equals(password);
  128.     }

  129.     @Override
  130.     public AuthenticationSourceInfo getInfo() {
  131.         return new AuthenticationSourceInfo("LOCAL", AuthenticationSourceType.LOCAL);
  132.     }

  133.     @Override
  134.     public boolean test() {
  135.         return true;
  136.     }

  137.     @Override
  138.     public boolean updatePassword(User user, String password) {
  139.         return false;
  140.     }
  141. }