diff options
author | Simon Rettberg | 2018-06-14 16:18:42 +0200 |
---|---|---|
committer | Simon Rettberg | 2018-06-14 16:18:42 +0200 |
commit | ade450e19527dbf377d945ca7bae7a145cf44196 (patch) | |
tree | aa6098a0a6b907fd39b3ca7591b9fe8670b08567 /dozentenmodulserver/src | |
parent | [server] Don't wipe network shares if client didn't set field (diff) | |
download | tutor-module-ade450e19527dbf377d945ca7bae7a145cf44196.tar.gz tutor-module-ade450e19527dbf377d945ca7bae7a145cf44196.tar.xz tutor-module-ade450e19527dbf377d945ca7bae7a145cf44196.zip |
Add support for LDAP lecture filters
Diffstat (limited to 'dozentenmodulserver/src')
7 files changed, 165 insertions, 4 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java index 2587661b..5f21c22f 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java @@ -11,6 +11,7 @@ public class SupportedFeatures { registerFeature(Feature.NETWORK_SHARES); registerFeature(Feature.MULTIPLE_HYPERVISORS); registerFeature(Feature.SERVER_SIDE_COPY); + registerFeature(Feature.LECTURE_FILTER_LDAP); } public static String getFeatureString() { diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java index 423428c0..003abc05 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java @@ -16,6 +16,7 @@ public class Updater { addLogTable(); fixEmailFieldLength(); addNetworkShares(); + addLectureFilter(); } private static void addLectureLocationMapTable() throws SQLException { @@ -182,4 +183,39 @@ public class Updater { throw e; } } + + private static void addLectureFilter() throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement tablesStmt = connection.prepareStatement("SHOW TABLES"); + ResultSet tables = tablesStmt.executeQuery(); + while (tables.next()) { + if (tables.getString(1).equals("lecturefilter")) { + return; // Table exists, don't do anything + } + } + // Add table + MysqlStatement tableAddStmt = connection.prepareStatement( + "CREATE TABLE `lecturefilter` (" + + " `lectureid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL," + + " `filtertype` varchar(24) CHARACTER SET ascii NOT NULL," + + " `filterkey` varchar(24) COLLATE utf8mb4_unicode_ci NOT NULL," + + " `filtervalue` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL," + + " KEY `lectureid` (`lectureid`,`filtertype`)" + + " KEY `fk_lectureid_1` (`lectureid`)" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"); + tableAddStmt.executeUpdate(); + // Add constraint + MysqlStatement constraintStmt = connection.prepareStatement( + "ALTER TABLE `lecturefilter` ADD " + + " CONSTRAINT `lectureid` FOREIGN KEY (`lectureid`) REFERENCES `lecture` (`lectureid`)" + + " ON DELETE CASCADE ON UPDATE CASCADE"); + constraintStmt.executeUpdate(); + connection.commit(); + LOGGER.info("Updated database: Added lecture filter table"); + } catch (SQLException e) { + LOGGER.error("Query failed in Updater.addLectureFilter()", e); + throw e; + } + } + } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java index 32124899..701d435b 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java @@ -20,6 +20,7 @@ import org.openslx.bwlp.sat.thrift.cache.OperatingSystemList; import org.openslx.bwlp.sat.util.Util; import org.openslx.bwlp.sat.web.VmChooserEntryXml; import org.openslx.bwlp.sat.web.VmChooserListXml; +import org.openslx.bwlp.sat.web.XmlFilterEntry; import org.openslx.bwlp.thrift.iface.LectureRead; import org.openslx.bwlp.thrift.iface.LectureSummary; import org.openslx.bwlp.thrift.iface.LectureWrite; @@ -121,6 +122,9 @@ public class DbLecture { if (lecture.isSetNetworkShares()) { DbNetshare.writeNetworkShares(connection, lectureId, lecture.networkShares); } + if (lecture.isSetLdapFilters()) { + DbLectureFilter.writeLdapFilters(connection, lectureId, lecture.ldapFilters); + } connection.commit(); return lectureId; } catch (SQLException e) { @@ -145,6 +149,9 @@ public class DbLecture { if (lecture.isSetNetworkShares()) { DbNetshare.writeNetworkShares(connection, lectureId, lecture.networkShares); } + if (lecture.isSetLdapFilters()) { + DbLectureFilter.writeLdapFilters(connection, lectureId, lecture.ldapFilters); + } stmt.executeUpdate(); } @@ -332,6 +339,7 @@ public class DbLecture { User.setCombinedUserPermissions(lecture, user); lecture.setLocationIds(DbLocation.getLectureLocations(connection, lectureId)); lecture.setNetworkShares(DbNetshare.getLectureNetshares(connection, lectureId)); + lecture.setLdapFilters(DbLectureFilter.getLectureLdapFilters(connection, lectureId)); return lecture; } catch (SQLException e) { LOGGER.error("Query failed in DbLecture.getLectureDetails()", e); @@ -507,12 +515,15 @@ public class DbLecture { boolean isForThisLocation = rs.getString("loctest") != null; if (!isForThisLocation && rs.getBoolean("islocationprivate")) continue; // Is limited to location, and we're not in one of the required locations + String lectureId = rs.getString("lectureid"); boolean isTemplate = rs.getBoolean("istemplate"); int prio = 100; + // Get ldap filters + List<XmlFilterEntry> ldapFilters = DbLectureFilter.getFiltersXml(connection, lectureId); list.add(new VmChooserEntryXml(rs.getString("filepath"), prio, "-", - rs.getString("lecturename"), rs.getString("description"), rs.getString("lectureid"), + rs.getString("lecturename"), rs.getString("description"), lectureId, rs.getString("virtid"), rs.getString("virtname"), rs.getString("virtoskeyword"), - rs.getString("osname"), "", isForThisLocation, isTemplate)); + rs.getString("osname"), "", isForThisLocation, isTemplate, ldapFilters)); } return list; } catch (SQLException e) { diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLectureFilter.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLectureFilter.java new file mode 100644 index 00000000..81c42444 --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLectureFilter.java @@ -0,0 +1,75 @@ +package org.openslx.bwlp.sat.database.mappers; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.sat.database.MysqlConnection; +import org.openslx.bwlp.sat.database.MysqlStatement; +import org.openslx.bwlp.sat.util.Util; +import org.openslx.bwlp.sat.web.XmlFilterEntry; +import org.openslx.bwlp.thrift.iface.LdapFilter; + +public class DbLectureFilter { + + private static final Logger LOGGER = Logger.getLogger(DbLectureFilter.class); + + public static List<LdapFilter> getLectureLdapFilters(MysqlConnection connection, String lectureId) + throws SQLException { + List<LdapFilter> list = new ArrayList<>(); + MysqlStatement stmt = connection.prepareStatement("SELECT filterkey, filtervalue FROM lecturefilter" + + " WHERE lectureid = :lectureid AND filtertype = 'LDAP'"); + stmt.setString("lectureid", lectureId); + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + String key = rs.getString("filterkey"); + String value = rs.getString("filtervalue"); + list.add(new LdapFilter(key, value)); + } + return list; + } + + public static final List<XmlFilterEntry> getFiltersXml(MysqlConnection connection, String lectureId) + throws SQLException { + List<XmlFilterEntry> list = null; + MysqlStatement stmt = connection.prepareStatement("SELECT filterkey, filtervalue FROM lecturefilter" + + " WHERE lectureid = :lectureid AND filtertype = 'LDAP'"); + stmt.setString("lectureid", lectureId); + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + String key = rs.getString("filterkey"); + String value = rs.getString("filtervalue"); + if (list == null) { + list = new ArrayList<>(); + } + list.add(new XmlFilterEntry("LDAP", key, value)); + } + return list; + } + + public static void writeLdapFilters(MysqlConnection connection, String lectureId, List<LdapFilter> list) + throws SQLException { + if (lectureId == null || lectureId.isEmpty()) { + return; + } + MysqlStatement delStmt = connection.prepareStatement("DELETE FROM lecturefilter WHERE lectureid = :lectureid"); + delStmt.setString("lectureid", lectureId); + delStmt.executeUpdate(); + if (list == null || list.isEmpty()) { + return; + } + MysqlStatement addStmt = connection.prepareStatement("INSERT INTO lecturefilter (lectureid, filtertype, filterkey, filtervalue)" + + " VALUES (:lectureid, 'LDAP', :key, :value)"); + addStmt.setString("lectureid", lectureId); + for (LdapFilter filter : list) { + if (Util.isEmptyString(filter.attribute) || filter.value == null) + continue; + addStmt.setString("key", filter.attribute); + addStmt.setString("value", filter.value); + addStmt.executeUpdate(); + } + } + +} diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java index d383a72a..fd4e0505 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java @@ -1,7 +1,10 @@ package org.openslx.bwlp.sat.web; +import java.util.List; + import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; +import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; @Root(name = "eintrag") @@ -33,10 +36,12 @@ public class VmChooserEntryXml { private VmChooserParamXml for_location; @Element private VmChooserParamXml is_template; + @Element + private VmChooserListXml filters; public VmChooserEntryXml(String imageFilePath, int priority, String creator, String short_description, String long_description, String uuid, String virtId, String virtualizerName, String osVirtName, - String osDisplayName, String icon, boolean isForThisLocation, boolean isTemplate) { + String osDisplayName, String icon, boolean isForThisLocation, boolean isTemplate, List<XmlFilterEntry> ldapFilters) { this.image_name = new VmChooserParamXml(imageFilePath); this.priority = new VmChooserParamXml(priority); this.creator = new VmChooserParamXml(creator); @@ -50,6 +55,7 @@ public class VmChooserEntryXml { this.os_name = new VmChooserParamXml(osDisplayName); this.for_location = new VmChooserParamXml(isForThisLocation); this.is_template = new VmChooserParamXml(isTemplate); + this.filters = new VmChooserListXml(ldapFilters); } private static class VmChooserParamXml { @@ -70,5 +76,16 @@ public class VmChooserEntryXml { } } + + private static class VmChooserListXml { + + @ElementList(required = false, inline = true, entry = "filter") + private List<XmlFilterEntry> list; + + public VmChooserListXml(List<XmlFilterEntry> list) { + this.list = list; + } + + } } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java index beebce1a..47ca0e1e 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java @@ -10,7 +10,7 @@ import org.simpleframework.xml.Root; public class VmChooserListXml { @ElementList(inline = true, name = "eintrag") - public List<VmChooserEntryXml> entries; + private List<VmChooserEntryXml> entries; public VmChooserListXml(boolean createEmptyList) { if (createEmptyList) { diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/XmlFilterEntry.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/XmlFilterEntry.java new file mode 100644 index 00000000..1c733f9f --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/XmlFilterEntry.java @@ -0,0 +1,21 @@ +package org.openslx.bwlp.sat.web; + +import org.simpleframework.xml.Attribute; +import org.simpleframework.xml.Element; + +public class XmlFilterEntry { + + @Attribute(required = false) + private String type; + @Element + private String key; + @Element + private String value; + + public XmlFilterEntry(String type, String key, String value) { + this.type = type; + this.key = key; + this.value = value; + } + +} |