annotate src/main/java/de/intevation/lada/util/auth/ProbeIdAuthorizer.java @ 1028:1c41c7b8f7c2 schema-update

Updated server application to new database model. THIS IS STILL WIP!!!
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 08 Jul 2016 15:32:36 +0200
parents f5b4784d9173
children
rev   line source
849
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
2 * Software engineering by Intevation GmbH
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
3 *
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
4 * This file is Free Software under the GNU GPL (v>=3)
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
6 * the documentation coming with IMIS-Labordaten-Application for details.
d0a591b3eade Added missing file header.
Raimund Renkert <raimund.renkert@intevation.de>
parents: 833
diff changeset
7 */
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
8 package de.intevation.lada.util.auth;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
9
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
10 import java.lang.reflect.InvocationTargetException;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
11 import java.lang.reflect.Method;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
12 import java.util.ArrayList;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
13 import java.util.List;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
14
1028
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
15 import de.intevation.lada.model.land.Probe;
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
16 import de.intevation.lada.model.stammdaten.MessStelle;
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
17 import de.intevation.lada.util.rest.RequestMethod;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
18 import de.intevation.lada.util.rest.Response;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
19
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
20 public class ProbeIdAuthorizer extends BaseAuthorizer {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
21
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
22 @Override
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
23 public <T> boolean isAuthorized(
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
24 Object data,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
25 RequestMethod method,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
26 UserInfo userInfo,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
27 Class<T> clazz
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
28 ) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
29 Method m;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
30 try {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
31 m = clazz.getMethod("getProbeId");
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
32 } catch (NoSuchMethodException | SecurityException e1) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
33 return false;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
34 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
35 Integer id;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
36 try {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
37 id = (Integer) m.invoke(data);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
38 } catch (IllegalAccessException |
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
39 IllegalArgumentException |
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
40 InvocationTargetException e
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
41 ) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
42 return false;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
43 }
1028
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
44 Probe probe =
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
45 repository.getByIdPlain(Probe.class, id, "land");
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
46 return !isProbeReadOnly(id) && getAuthorization(userInfo, probe);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
47 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
48
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
49 @SuppressWarnings("unchecked")
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
50 @Override
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
51 public <T> Response filter(
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
52 Response data,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
53 UserInfo userInfo,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
54 Class<T> clazz
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
55 ) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
56 if (data.getData() instanceof List<?>) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
57 List<Object> objects = new ArrayList<Object>();
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
58 for (Object object :(List<Object>)data.getData()) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
59 objects.add(setAuthData(userInfo, object, clazz));
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
60 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
61 data.setData(objects);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
62 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
63 else {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
64 Object object = data.getData();
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
65 data.setData(setAuthData(userInfo, object, clazz));
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
66 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
67 return data;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
68 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
69 /**
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
70 * Authorize a single data object that has a probeId Attribute.
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
71 *
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
72 * @param userInfo The user information.
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
73 * @param data The Response object containing the data.
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
74 * @param clazz The data object class.
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
75 * @return A Response object containing the data.
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
76 */
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
77 private <T> Object setAuthData(
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
78 UserInfo userInfo,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
79 Object data,
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
80 Class<T> clazz
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
81 ) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
82 try {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
83 Method getProbeId = clazz.getMethod("getProbeId");
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
84 Integer id = null;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
85 if (getProbeId != null) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
86 id = (Integer) getProbeId.invoke(data);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
87 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
88 else {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
89 return null;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
90 }
1028
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
91 Probe probe =
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
92 (Probe)repository.getById(Probe.class, id, "land").getData();
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
93
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
94 boolean readOnly = true;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
95 boolean owner = false;
1028
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
96 MessStelle mst = repository.getByIdPlain(MessStelle.class, probe.getMstId(), "stamm");
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
97 if (!userInfo.getNetzbetreiber().contains(
1028
1c41c7b8f7c2 Updated server application to new database model. THIS IS STILL WIP!!!
Raimund Renkert <raimund.renkert@intevation.de>
parents: 894
diff changeset
98 mst.getNetzbetreiberId())) {
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
99 owner = false;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
100 readOnly = true;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
101 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
102 else {
894
f5b4784d9173 Load probe attribute 'laborMstId' from db and use this field to authorize
Raimund Renkert <raimund.renkert@intevation.de>
parents: 849
diff changeset
103 if (userInfo.belongsTo(probe.getMstId(), probe.getLaborMstId())) {
833
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
104 owner = true;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
105 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
106 else {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
107 owner = false;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
108 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
109 readOnly = this.isProbeReadOnly(id);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
110 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
111
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
112 Method setOwner = clazz.getMethod("setOwner", boolean.class);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
113 Method setReadonly = clazz.getMethod("setReadonly", boolean.class);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
114 setOwner.invoke(data, owner);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
115 setReadonly.invoke(data, readOnly);
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
116 } catch (NoSuchMethodException | SecurityException
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
117 | IllegalAccessException | IllegalArgumentException
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
118 | InvocationTargetException e) {
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
119 return null;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
120 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
121 return data;
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
122 }
fa922101a462 Refactored Authorization.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff changeset
123 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)