Skip to content

Commit

Permalink
chore: remove more interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
teleivo committed Jan 17, 2025
1 parent ea91da7 commit b65a3df
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 501 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,101 @@
package org.hisp.dhis.tracker.export.trackedentity.aggregates;

import java.util.List;
import org.hisp.dhis.common.collection.CollectionUtils;
import org.hisp.dhis.hibernate.jsonb.type.JsonbFunctions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
* @author Luciano Fiandesio
*/
public interface AclStore {
List<Long> getAccessibleTrackedEntityTypes(String userUID, List<String> userGroupUIDs);
@Repository("org.hisp.dhis.tracker.trackedentity.aggregates.AclStore")
class AclStore {
private final NamedParameterJdbcTemplate jdbcTemplate;

List<Long> getAccessiblePrograms(String userUID, List<String> userGroupUIDs);
private static final String USER_SQL_PARAM_NAME = "userId";

List<Long> getAccessibleProgramStages(String userUID, List<String> userGroupUIDs);
private static final String USER_GROUP_SQL_PARAM_NAME = "userGroupUIDs";

List<Long> getAccessibleRelationshipTypes(String userUID, List<String> userGroupUIDs);
private static final String PUBLIC_ACCESS_CONDITION =
"sharing->>'public' LIKE '__r%' OR sharing->>'public' IS NULL";

private static final String USERACCESS_CONDITION =
"sharing->'users'->:" + USER_SQL_PARAM_NAME + "->>'access' LIKE '__r%'";

private static final String USERGROUPACCESS_CONDITION =
JsonbFunctions.HAS_USER_GROUP_IDS
+ "( sharing, :"
+ USER_GROUP_SQL_PARAM_NAME
+ ") = true "
+ "and "
+ JsonbFunctions.CHECK_USER_GROUPS_ACCESS
+ "(sharing, '__r%', :"
+ USER_GROUP_SQL_PARAM_NAME
+ ") = true";

private static final String GET_TE_TYPE_ACL =
"SELECT trackedentitytypeid FROM trackedentitytype "
+ " WHERE "
+ PUBLIC_ACCESS_CONDITION
+ " OR "
+ USERACCESS_CONDITION;

static final String GET_PROGRAM_ACL =
"SELECT p.programid FROM program p "
+ " WHERE "
+ PUBLIC_ACCESS_CONDITION
+ " OR "
+ USERACCESS_CONDITION;

static final String GET_PROGRAMSTAGE_ACL =
"SELECT ps.programstageid FROM programstage ps "
+ " WHERE "
+ PUBLIC_ACCESS_CONDITION
+ " OR "
+ USERACCESS_CONDITION;

private static final String GET_RELATIONSHIPTYPE_ACL =
"SELECT rs.relationshiptypeid "
+ "FROM relationshiptype rs"
+ " WHERE "
+ PUBLIC_ACCESS_CONDITION
+ " OR "
+ USERACCESS_CONDITION;

AclStore(@Qualifier("readOnlyJdbcTemplate") JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
}

List<Long> getAccessibleTrackedEntityTypes(String userUID, List<String> userGroupUIDs) {
return executeAclQuery(userUID, userGroupUIDs, GET_TE_TYPE_ACL, "trackedentitytypeid");
}

List<Long> getAccessiblePrograms(String userUID, List<String> userGroupUIDs) {
return executeAclQuery(userUID, userGroupUIDs, GET_PROGRAM_ACL, "programid");
}

List<Long> getAccessibleProgramStages(String userUID, List<String> userGroupUIDs) {
return executeAclQuery(userUID, userGroupUIDs, GET_PROGRAMSTAGE_ACL, "programstageid");
}

List<Long> getAccessibleRelationshipTypes(String userUID, List<String> userGroupUIDs) {
return executeAclQuery(userUID, userGroupUIDs, GET_RELATIONSHIPTYPE_ACL, "relationshiptypeid");
}

private List<Long> executeAclQuery(
String userUID, List<String> userGroupUIDs, String sql, String primaryKey) {
MapSqlParameterSource parameterMap = new MapSqlParameterSource();
parameterMap.addValue(USER_SQL_PARAM_NAME, userUID);

if (!CollectionUtils.isEmpty(userGroupUIDs)) {
sql += " OR " + USERGROUPACCESS_CONDITION;
parameterMap.addValue(USER_GROUP_SQL_PARAM_NAME, "{" + String.join(",", userGroupUIDs) + "}");
}

return jdbcTemplate.query(sql, parameterMap, (rs, i) -> rs.getLong(primaryKey));
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit b65a3df

Please sign in to comment.