summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImagePermissions.java
blob: 76a8ad3fef9d1e393df12288449ef71a73a17c70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package org.openslx.bwlp.sat.database.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.Database;
import org.openslx.bwlp.sat.database.MysqlConnection;
import org.openslx.bwlp.sat.database.MysqlStatement;
import org.openslx.bwlp.sat.util.Sanitizer;
import org.openslx.bwlp.thrift.iface.ImagePermissions;

public class DbImagePermissions {

	private static final Logger LOGGER = Logger.getLogger(DbImagePermissions.class);

	/**
	 * Build an instance of {@link ImagePermissions} by reading the given
	 * columns from the given {@link ResultSet}. If there are no permissions
	 * given in the ResultSet, <code>null</code> is returned.
	 * 
	 * @param rs the {@link ResultSet} to read from
	 * @param canLink Name of the column to read the "can link" permission from
	 * @param canDownload Name of the column to read the "can download"
	 *            permission from
	 * @param canEdit Name of the column to read the "can edit" permission from
	 * @param canAdmin Name of the column to read the "can admin" permission
	 *            from
	 * @return instance of {@link ImagePermissions}, or <code>null</code>
	 * @throws SQLException
	 */
	private static ImagePermissions fromResultSet(ResultSet rs, String canLink, String canDownload,
			String canEdit, String canAdmin) throws SQLException {
		byte link = rs.getByte(canLink);
		if (rs.wasNull())
			return null;
		return new ImagePermissions(link != 0, rs.getByte(canDownload) != 0, rs.getByte(canEdit) != 0,
				rs.getByte(canAdmin) != 0);
	}

	/**
	 * Build an instance of {@link ImagePermissions} by reading the
	 * columns <code>canlink</code>, <code>candownload</code>,
	 * <code>canedit</code>, <code>canadmin</code> from the given
	 * {@link ResultSet}. If there are no permissions
	 * given in the ResultSet, <code>null</code> is returned.
	 * 
	 * @param rs the {@link ResultSet} to read from
	 * @return instance of {@link ImagePermissions}, or <code>null</code>
	 * @throws SQLException
	 */
	public static ImagePermissions fromResultSetUser(ResultSet rs) throws SQLException {
		return fromResultSet(rs, "canlink", "candownload", "canedit", "canadmin");
	}

	/**
	 * Build an instance of {@link ImagePermissions} by reading the
	 * columns <code>canlinkdefault</code>, <code>candownloaddefault</code>,
	 * <code>caneditdefault</code>, <code>canadmindefault</code> from the given
	 * {@link ResultSet}. If there are no permissions
	 * given in the ResultSet, <code>null</code> is returned.
	 * 
	 * @param rs the {@link ResultSet} to read from
	 * @return instance of {@link ImagePermissions}, or <code>null</code>
	 * @throws SQLException
	 */
	public static ImagePermissions fromResultSetDefault(ResultSet rs) throws SQLException {
		return fromResultSet(rs, "canlinkdefault", "candownloaddefault", "caneditdefault", "canadmindefault");
	}

	/**
	 * Get permissions for the given image. IF <code>adminOnly</code> is true,
	 * only users with admin permissions will be returned.
	 * 
	 * @param imageBaseId UUID of image
	 * @param adminOnly Only return users with admin permission
	 * @return
	 * @throws SQLException
	 */
	public static Map<String, ImagePermissions> getForImageBase(String imageBaseId, boolean adminOnly)
			throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("SELECT userid, canlink, candownload, canedit, canadmin"
					+ " FROM imagepermission WHERE imagebaseid = :imagebaseid");
			stmt.setString("imagebaseid", imageBaseId);
			ResultSet rs = stmt.executeQuery();
			Map<String, ImagePermissions> list = new HashMap<>();
			while (rs.next()) {
				boolean admin = rs.getBoolean("canadmin");
				if (adminOnly && !admin)
					continue;
				ImagePermissions perm = new ImagePermissions(rs.getBoolean("canlink"),
						rs.getBoolean("candownload"), rs.getBoolean("canedit"), admin);
				list.put(rs.getString("userid"), perm);
			}
			return list;
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbImagePermissions.getForImageBase()", e);
			throw e;
		}
	}

	public static void writeForImageBase(String imageBaseId, Map<String, ImagePermissions> permissions)
			throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("DELETE FROM imagepermission"
					+ " WHERE imagebaseid = :baseid");
			stmt.setString("baseid", imageBaseId);
			stmt.executeUpdate();
			stmt = connection.prepareStatement("INSERT INTO imagepermission"
					+ " (imagebaseid, userid, canlink, candownload, canedit, canadmin)"
					+ " VALUES (:baseid, :userid, :canlink, :candownload, :canedit, :canadmin)");
			stmt.setString("baseid", imageBaseId);
			for (Map.Entry<String, ImagePermissions> entry : permissions.entrySet()) {
				ImagePermissions perm = entry.getValue();
				perm = Sanitizer.handleImagePermissions(perm);
				stmt.setString("userid", entry.getKey());
				stmt.setBoolean("canlink", perm.link);
				stmt.setBoolean("candownload", perm.download);
				stmt.setBoolean("canedit", perm.edit);
				stmt.setBoolean("canadmin", perm.admin);
				stmt.executeUpdate();
			}
			connection.commit();
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbImagePermissions.writeForImageBase()", e);
			throw e;
		}
	}

	public static void writeForImageBase(String imageBaseId, String userId, ImagePermissions imagePermissions) throws SQLException {
		Map<String, ImagePermissions> map = new HashMap<>();
		map.put(userId, imagePermissions);
		writeForImageBase(imageBaseId, map);
	}

}