PolicyDao.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.dao;

  18. import lombok.extern.slf4j.Slf4j;

  19. import net.jami.jams.common.objects.user.Policy;

  20. import java.util.List;
  21. import java.util.Optional;

  22. @Slf4j
  23. public class PolicyDao extends AbstractDao<Policy> {

  24.     public PolicyDao() {
  25.         this.setTableName("policies");
  26.         this.setTClass(Policy.class);
  27.     }

  28.     public List<Policy> getAll() {
  29.         return getObjectsFromResultSet("SELECT * FROM policies");
  30.     }

  31.     public Optional<Policy> getByName(String name) {
  32.         String q = "SELECT * FROM policies WHERE name = ?";
  33.         return getFirstObjectFromResultSet(q, List.of(name));
  34.     }

  35.     public Optional<Policy> getByUsername(String username) {
  36.         // TODO this can return multiple policies
  37.         String query =
  38.                 "SELECT * FROM usergroupmappings ugm"
  39.                         + " JOIN groups g ON ugm.groupid = g.id"
  40.                         + " JOIN policies p ON g.blueprint = p.name"
  41.                         + " WHERE ugm.username = ?";
  42.         return getFirstObjectFromResultSet(query, List.of(username));
  43.     }

  44.     @Override
  45.     public boolean storeObject(Policy object) {
  46.         String query = "INSERT INTO policies (name, policyData) VALUES (?, ?)";
  47.         return executeInsert(query, object);
  48.     }

  49.     public boolean updateObject(String name, String policyData) {
  50.         String query = "UPDATE policies SET policyData = ? WHERE name = ?";
  51.         return executeUpdate(query, List.of(policyData, name));
  52.     }

  53.     public boolean deleteObject(String name) {
  54.         String query = "DELETE FROM policies WHERE name = ?";
  55.         return executeUpdate(query, List.of(name));
  56.     }
  57. }