diff options
-rw-r--r-- | src/org/traccar/api/BaseResource.java | 30 | ||||
-rw-r--r-- | src/org/traccar/api/resource/DeviceResource.java | 2 | ||||
-rw-r--r-- | src/org/traccar/api/resource/GroupResource.java | 2 | ||||
-rw-r--r-- | src/org/traccar/api/resource/PermissionsResource.java | 29 | ||||
-rw-r--r-- | src/org/traccar/events/GeofenceEventHandler.java | 16 | ||||
-rw-r--r-- | src/org/traccar/model/Permission.java | 57 |
6 files changed, 96 insertions, 40 deletions
diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 920bb3931..502591efe 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -16,15 +16,11 @@ package org.traccar.api; import java.sql.SQLException; -import java.util.Iterator; -import java.util.LinkedHashMap; import javax.ws.rs.core.SecurityContext; import org.traccar.Context; -import org.traccar.database.DataManager; import org.traccar.model.BaseModel; -import org.traccar.model.Device; import org.traccar.model.User; public class BaseResource { @@ -40,32 +36,6 @@ public class BaseResource { return 0; } - protected void checkAndLinkPermission(LinkedHashMap<String, Long> entity, boolean link) - throws SQLException, ClassNotFoundException { - Iterator<String> iterator = entity.keySet().iterator(); - String owner = iterator.next(); - Class<?> ownerClass = DataManager.getClassByName(owner); - String property = iterator.next(); - Class<?> propertyClass = DataManager.getClassByName(property); - - long ownerId = entity.get(owner); - long propertyId = entity.get(property); - - if (!link && ownerClass.equals(User.class) - && propertyClass.equals(Device.class)) { - if (getUserId() != ownerId) { - Context.getPermissionsManager().checkUser(getUserId(), ownerId); - } else { - Context.getPermissionsManager().checkAdmin(getUserId()); - } - } else { - Context.getPermissionsManager().checkPermission(ownerClass, getUserId(), ownerId); - } - Context.getPermissionsManager().checkPermission(propertyClass, getUserId(), propertyId); - - Context.getDataManager().linkObject(ownerClass, ownerId, propertyClass, propertyId, link); - } - protected void linkNewEntity(BaseModel entity) throws SQLException { Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index e1c0fc96d..0d6e4f09c 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -95,7 +95,7 @@ public class DeviceResource extends BaseResource { return Response.ok(entity).build(); } - @Path("{id : \\d+}") + @Path("{id}") @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index 2ecd11fe5..402e687e6 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -77,7 +77,7 @@ public class GroupResource extends BaseResource { return Response.ok(entity).build(); } - @Path("{id : \\d+}") + @Path("{id}") @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); diff --git a/src/org/traccar/api/resource/PermissionsResource.java b/src/org/traccar/api/resource/PermissionsResource.java index 515efbe41..88fd9d092 100644 --- a/src/org/traccar/api/resource/PermissionsResource.java +++ b/src/org/traccar/api/resource/PermissionsResource.java @@ -29,16 +29,38 @@ import javax.ws.rs.core.Response; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.model.Device; +import org.traccar.model.Permission; +import org.traccar.model.User; @Path("permissions") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class PermissionsResource extends BaseResource { + private void checkPermission(Permission permission, boolean link) { + if (!link && permission.getOwnerClass().equals(User.class) + && permission.getPropertyClass().equals(Device.class)) { + if (getUserId() != permission.getOwnerId()) { + Context.getPermissionsManager().checkUser(getUserId(), permission.getOwnerId()); + } else { + Context.getPermissionsManager().checkAdmin(getUserId()); + } + } else { + Context.getPermissionsManager().checkPermission( + permission.getOwnerClass(), getUserId(), permission.getOwnerId()); + } + Context.getPermissionsManager().checkPermission( + permission.getPropertyClass(), getUserId(), permission.getPropertyId()); + } + @POST public Response add(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); - checkAndLinkPermission(entity, true); + Permission permission = new Permission(entity); + checkPermission(permission, true); + Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId(), true); Context.getPermissionsManager().refreshPermissions(entity); return Response.noContent().build(); } @@ -46,7 +68,10 @@ public class PermissionsResource extends BaseResource { @DELETE public Response remove(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); - checkAndLinkPermission(entity, false); + Permission permission = new Permission(entity); + checkPermission(permission, false); + Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId(), false); Context.getPermissionsManager().refreshPermissions(entity); return Response.noContent().build(); } diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index cdac7993f..089d1e0ef 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -60,9 +60,11 @@ public class GeofenceEventHandler extends BaseEventHandler { Collection<Event> events = new ArrayList<>(); for (long geofenceId : newGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); - if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null - || ((Calendar) Context.getCalendarManager().getById(calendarId)) - .checkMoment(position.getFixTime())) { + Calendar calendar = null; + if (calendarId != 0) { + calendar = (Calendar) Context.getCalendarManager().getById(calendarId); + } + if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); @@ -70,9 +72,11 @@ public class GeofenceEventHandler extends BaseEventHandler { } for (long geofenceId : oldGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); - if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null - || ((Calendar) Context.getCalendarManager().getById(calendarId)) - .checkMoment(position.getFixTime())) { + Calendar calendar = null; + if (calendarId != 0) { + calendar = (Calendar) Context.getCalendarManager().getById(calendarId); + } + if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); diff --git a/src/org/traccar/model/Permission.java b/src/org/traccar/model/Permission.java new file mode 100644 index 000000000..1006b1c47 --- /dev/null +++ b/src/org/traccar/model/Permission.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.model; + +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.traccar.database.DataManager; + +public class Permission { + + private Class<?> ownerClass; + private long ownerId; + private Class<?> propertyClass; + private long propertyId; + + public Permission(LinkedHashMap<String, Long> permissionMap) throws ClassNotFoundException { + Iterator<Map.Entry<String, Long>> iterator = permissionMap.entrySet().iterator(); + String owner = iterator.next().getKey(); + ownerClass = DataManager.getClassByName(owner); + String property = iterator.next().getKey(); + propertyClass = DataManager.getClassByName(property); + ownerId = permissionMap.get(owner); + propertyId = permissionMap.get(property); + } + + public Class<?> getOwnerClass() { + return ownerClass; + } + + public long getOwnerId() { + return ownerId; + } + + public Class<?> getPropertyClass() { + return propertyClass; + } + + public long getPropertyId() { + return propertyId; + } +} |