raimund@833: package de.intevation.lada.util.auth; raimund@833: raimund@833: import java.util.ArrayList; raimund@833: import java.util.List; raimund@833: raimund@833: import de.intevation.lada.model.land.LProbe; raimund@833: import de.intevation.lada.util.rest.RequestMethod; raimund@833: import de.intevation.lada.util.rest.Response; raimund@833: raimund@833: public class ProbeAuthorizer extends BaseAuthorizer { raimund@833: raimund@833: @Override raimund@833: public boolean isAuthorized( raimund@833: Object data, raimund@833: RequestMethod method, raimund@833: UserInfo userInfo, raimund@833: Class clazz raimund@833: ) { raimund@833: LProbe probe = (LProbe)data; raimund@833: if (method == RequestMethod.POST) { raimund@833: return getAuthorization(userInfo, probe); raimund@833: } raimund@833: else if (method == RequestMethod.PUT || raimund@833: method == RequestMethod.DELETE) { raimund@833: return !isProbeReadOnly(probe.getId()); raimund@833: } raimund@833: return false; raimund@833: } raimund@833: raimund@833: @SuppressWarnings("unchecked") raimund@833: @Override raimund@833: public Response filter( raimund@833: Response data, raimund@833: UserInfo userInfo, raimund@833: Class clazz raimund@833: ) { raimund@833: if (data.getData() instanceof List) { raimund@833: List proben = new ArrayList(); raimund@833: for (LProbe probe :(List)data.getData()) { raimund@833: proben.add(setAuthData(userInfo, probe)); raimund@833: } raimund@833: data.setData(proben); raimund@833: } raimund@833: else if (data.getData() instanceof LProbe) { raimund@833: LProbe probe = (LProbe)data.getData(); raimund@833: data.setData(setAuthData(userInfo, probe)); raimund@833: } raimund@833: return data; raimund@833: } raimund@833: raimund@833: /** raimund@833: * Set authorization data for the current probe object. raimund@833: * raimund@833: * @param userInfo The user information. raimund@833: * @param probe The probe object. raimund@833: * @return The probe. raimund@833: */ raimund@833: private LProbe setAuthData(UserInfo userInfo, LProbe probe) { raimund@833: if (!userInfo.getNetzbetreiber().contains(probe.getNetzbetreiberId())) { raimund@833: probe.setOwner(false); raimund@833: probe.setReadonly(true); raimund@833: return probe; raimund@833: } raimund@833: if (userInfo.getMessstellen().contains(probe.getMstId())) { raimund@833: probe.setOwner(true); raimund@833: } raimund@833: else { raimund@833: probe.setOwner(false); raimund@833: } raimund@833: probe.setReadonly(this.isProbeReadOnly(probe.getId())); raimund@833: return probe; raimund@833: } raimund@833: }