From 6a77d7e673b1f13107d21408e75e5d9feb3614c3 Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 01:17:48 +0100 Subject: bulk permission update --- .../traccar/api/resource/PermissionsResource.java | 65 ++++++++++++++++------ 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index b89d9d376..1dbbfa190 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -17,7 +17,9 @@ package org.traccar.api.resource; import java.sql.SQLException; -import java.util.LinkedHashMap; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -55,30 +57,61 @@ public class PermissionsResource extends BaseResource { permission.getPropertyClass(), getUserId(), permission.getPropertyId()); } + private void refreshPermissions(List> entities) { + Set ownerProperty = new HashSet<>(); + entities.stream() + .map(e -> { + try { + return new Permission(e); + } catch (ClassNotFoundException classNotFoundException) { + classNotFoundException.printStackTrace(); + return null; + } + }) + .filter(Objects::nonNull) + .filter(p -> ownerProperty.add(p.getOwnerClass().getName() + p.getPropertyClass().getName())) + .forEach(p -> Context.getPermissionsManager().refreshPermissions(p)); + } + @POST public Response add(LinkedHashMap entity) throws SQLException, ClassNotFoundException { + return add(Stream.of(entity).collect(Collectors.toList())); + } + + @Path("bulk") + @POST + public Response add(List> entities) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); - Permission permission = new Permission(entity); - checkPermission(permission, true); - Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), - permission.getPropertyClass(), permission.getPropertyId(), true); - LogAction.link(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), - permission.getPropertyClass(), permission.getPropertyId()); - Context.getPermissionsManager().refreshPermissions(permission); + for (LinkedHashMap entity: entities) { + Permission permission = new Permission(entity); + checkPermission(permission, true); + Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId(), true); + LogAction.link(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId()); + } + refreshPermissions(entities); return Response.noContent().build(); } @DELETE public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { + return remove(Stream.of(entity).collect(Collectors.toList())); + } + + @DELETE + @Path("bulk") + public Response remove(List> entities) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); - Permission permission = new Permission(entity); - checkPermission(permission, false); - Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), - permission.getPropertyClass(), permission.getPropertyId(), false); - LogAction.unlink(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), - permission.getPropertyClass(), permission.getPropertyId()); - Context.getPermissionsManager().refreshPermissions(permission); + for (LinkedHashMap entity: entities) { + Permission permission = new Permission(entity); + checkPermission(permission, false); + Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId(), false); + LogAction.unlink(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId()); + } + refreshPermissions(entities); return Response.noContent().build(); } - } -- cgit v1.2.3 From c56851eac17d29b858f1ecd4ec4d5815e1a01b16 Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 01:19:04 +0100 Subject: single class imports --- src/main/java/org/traccar/api/resource/PermissionsResource.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index 1dbbfa190..d6048d81b 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -17,7 +17,11 @@ package org.traccar.api.resource; import java.sql.SQLException; -import java.util.*; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -- cgit v1.2.3 From 88dfb5313003eb9ba246cab3cfa700ad5c0137de Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 02:15:00 +0100 Subject: remove stream api assume permissions are of same type --- .../traccar/api/resource/PermissionsResource.java | 37 +++++++--------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index d6048d81b..ef1e16e29 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -17,13 +17,9 @@ package org.traccar.api.resource; import java.sql.SQLException; -import java.util.HashSet; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -61,25 +57,11 @@ public class PermissionsResource extends BaseResource { permission.getPropertyClass(), getUserId(), permission.getPropertyId()); } - private void refreshPermissions(List> entities) { - Set ownerProperty = new HashSet<>(); - entities.stream() - .map(e -> { - try { - return new Permission(e); - } catch (ClassNotFoundException classNotFoundException) { - classNotFoundException.printStackTrace(); - return null; - } - }) - .filter(Objects::nonNull) - .filter(p -> ownerProperty.add(p.getOwnerClass().getName() + p.getPropertyClass().getName())) - .forEach(p -> Context.getPermissionsManager().refreshPermissions(p)); - } - @POST public Response add(LinkedHashMap entity) throws SQLException, ClassNotFoundException { - return add(Stream.of(entity).collect(Collectors.toList())); + List> list = new ArrayList<>(); + list.add(entity); + return add(list); } @Path("bulk") @@ -94,13 +76,17 @@ public class PermissionsResource extends BaseResource { LogAction.link(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId()); } - refreshPermissions(entities); + // we assume all permissions are of same type so we use the first one for refreshing + if (!entities.isEmpty()) + Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0))); return Response.noContent().build(); } @DELETE public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { - return remove(Stream.of(entity).collect(Collectors.toList())); + List> list = new ArrayList<>(); + list.add(entity); + return remove(list); } @DELETE @@ -115,7 +101,8 @@ public class PermissionsResource extends BaseResource { LogAction.unlink(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId()); } - refreshPermissions(entities); + if (!entities.isEmpty()) + Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0))); return Response.noContent().build(); } } -- cgit v1.2.3 From 410c29d25c459a825f4349be7b12342105bcbd3e Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 02:18:42 +0100 Subject: fix last line --- src/main/java/org/traccar/api/resource/PermissionsResource.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index ef1e16e29..32cf4faff 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -105,4 +105,5 @@ public class PermissionsResource extends BaseResource { Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0))); return Response.noContent().build(); } + } -- cgit v1.2.3 From 44e9726886ad14c1b61e4dedb0acc97e7d76b46f Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 02:25:31 +0100 Subject: fix style --- src/main/java/org/traccar/api/resource/PermissionsResource.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index 32cf4faff..7502e9134 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -77,8 +77,9 @@ public class PermissionsResource extends BaseResource { permission.getPropertyClass(), permission.getPropertyId()); } // we assume all permissions are of same type so we use the first one for refreshing - if (!entities.isEmpty()) + if (!entities.isEmpty()) { Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0))); + } return Response.noContent().build(); } @@ -101,8 +102,9 @@ public class PermissionsResource extends BaseResource { LogAction.unlink(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId()); } - if (!entities.isEmpty()) + if (!entities.isEmpty()) { Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0))); + } return Response.noContent().build(); } -- cgit v1.2.3 From b1506079ac9ed971d9c658043f83fa5886564f3a Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 03:04:17 +0100 Subject: inline list --- src/main/java/org/traccar/api/resource/PermissionsResource.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index 7502e9134..0acca5273 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -59,9 +60,7 @@ public class PermissionsResource extends BaseResource { @POST public Response add(LinkedHashMap entity) throws SQLException, ClassNotFoundException { - List> list = new ArrayList<>(); - list.add(entity); - return add(list); + return add(Collections.singletonList(entity)); } @Path("bulk") @@ -85,9 +84,7 @@ public class PermissionsResource extends BaseResource { @DELETE public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { - List> list = new ArrayList<>(); - list.add(entity); - return remove(list); + return remove(Collections.singletonList(entity)); } @DELETE -- cgit v1.2.3 From ba3aa1c1367cab26aa3644f640f6706f553ee9b1 Mon Sep 17 00:00:00 2001 From: jcardus Date: Thu, 8 Jul 2021 03:06:47 +0100 Subject: fix style --- src/main/java/org/traccar/api/resource/PermissionsResource.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index 0acca5273..15c298094 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -17,7 +17,6 @@ package org.traccar.api.resource; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; -- cgit v1.2.3 From 9562e31ffebaa9621b28bce453d4383e9eed78b2 Mon Sep 17 00:00:00 2001 From: jcardus Date: Sat, 10 Jul 2021 21:45:11 +0100 Subject: check permission types --- .../org/traccar/api/resource/PermissionsResource.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index 15c298094..db16bf941 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -26,6 +26,7 @@ import javax.ws.rs.DELETE; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -66,6 +67,7 @@ public class PermissionsResource extends BaseResource { @POST public Response add(List> entities) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); + checkPermissionTypes(entities); for (LinkedHashMap entity: entities) { Permission permission = new Permission(entity); checkPermission(permission, true); @@ -74,13 +76,25 @@ public class PermissionsResource extends BaseResource { LogAction.link(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId()); } - // we assume all permissions are of same type so we use the first one for refreshing if (!entities.isEmpty()) { Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0))); } return Response.noContent().build(); } + private void checkPermissionTypes(List> entities) throws ClassNotFoundException { + if (!entities.isEmpty()) { + Permission first = new Permission(entities.get(0)); + for (LinkedHashMap entity: entities) { + Permission permission = new Permission(entity); + if (!first.getOwnerClass().equals(permission.getOwnerClass()) + || !first.getPropertyClass().equals(permission.getPropertyClass())) { + throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build()); + } + } + } + } + @DELETE public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { return remove(Collections.singletonList(entity)); @@ -90,6 +104,7 @@ public class PermissionsResource extends BaseResource { @Path("bulk") public Response remove(List> entities) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); + checkPermissionTypes(entities); for (LinkedHashMap entity: entities) { Permission permission = new Permission(entity); checkPermission(permission, false); -- cgit v1.2.3 From ce3a16ef9d235390e839e6c01bc2ccc96049ead6 Mon Sep 17 00:00:00 2001 From: jcardus Date: Sun, 11 Jul 2021 02:05:50 +0100 Subject: check permission types --- .../org/traccar/api/resource/PermissionsResource.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index db16bf941..50ae11aaf 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -20,6 +20,7 @@ import java.sql.SQLException; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; +import java.util.Set; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -82,16 +83,13 @@ public class PermissionsResource extends BaseResource { return Response.noContent().build(); } - private void checkPermissionTypes(List> entities) throws ClassNotFoundException { - if (!entities.isEmpty()) { - Permission first = new Permission(entities.get(0)); - for (LinkedHashMap entity: entities) { - Permission permission = new Permission(entity); - if (!first.getOwnerClass().equals(permission.getOwnerClass()) - || !first.getPropertyClass().equals(permission.getPropertyClass())) { - throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build()); - } + private void checkPermissionTypes(List> entities) { + Set keys = null; + for (LinkedHashMap entity: entities) { + if (keys != null & !entity.keySet().equals(keys)) { + throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build()); } + keys = entity.keySet(); } } -- cgit v1.2.3 From 445a7242568e282fc79c777bd0416ebdcccdb367 Mon Sep 17 00:00:00 2001 From: jcardus Date: Tue, 13 Jul 2021 01:25:20 +0100 Subject: order methods --- .../traccar/api/resource/PermissionsResource.java | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java index 50ae11aaf..54d3964b6 100644 --- a/src/main/java/org/traccar/api/resource/PermissionsResource.java +++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java @@ -59,9 +59,14 @@ public class PermissionsResource extends BaseResource { permission.getPropertyClass(), getUserId(), permission.getPropertyId()); } - @POST - public Response add(LinkedHashMap entity) throws SQLException, ClassNotFoundException { - return add(Collections.singletonList(entity)); + private void checkPermissionTypes(List> entities) { + Set keys = null; + for (LinkedHashMap entity: entities) { + if (keys != null & !entity.keySet().equals(keys)) { + throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build()); + } + keys = entity.keySet(); + } } @Path("bulk") @@ -83,19 +88,9 @@ public class PermissionsResource extends BaseResource { return Response.noContent().build(); } - private void checkPermissionTypes(List> entities) { - Set keys = null; - for (LinkedHashMap entity: entities) { - if (keys != null & !entity.keySet().equals(keys)) { - throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build()); - } - keys = entity.keySet(); - } - } - - @DELETE - public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { - return remove(Collections.singletonList(entity)); + @POST + public Response add(LinkedHashMap entity) throws SQLException, ClassNotFoundException { + return add(Collections.singletonList(entity)); } @DELETE @@ -117,4 +112,9 @@ public class PermissionsResource extends BaseResource { return Response.noContent().build(); } + @DELETE + public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { + return remove(Collections.singletonList(entity)); + } + } -- cgit v1.2.3