aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/api
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-03 17:06:30 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-03 17:06:30 -0700
commit4030d3207c157a3fcee2653c18440898b6b2a2e6 (patch)
treeb7e04a72bd369f75871edf832f7ff58198001a85 /src/main/java/org/traccar/api
parent65b0f9c5398ddcb28018cb1963108534c638b1f4 (diff)
downloadtrackermap-server-4030d3207c157a3fcee2653c18440898b6b2a2e6.tar.gz
trackermap-server-4030d3207c157a3fcee2653c18440898b6b2a2e6.tar.bz2
trackermap-server-4030d3207c157a3fcee2653c18440898b6b2a2e6.zip
Remove attributes manager
Diffstat (limited to 'src/main/java/org/traccar/api')
-rw-r--r--src/main/java/org/traccar/api/BaseObjectResource.java36
-rw-r--r--src/main/java/org/traccar/api/ExtendedObjectResource.java2
-rw-r--r--src/main/java/org/traccar/api/SimpleObjectResource.java2
-rw-r--r--src/main/java/org/traccar/api/resource/AttributeResource.java6
-rw-r--r--src/main/java/org/traccar/api/resource/PermissionsResource.java40
-rw-r--r--src/main/java/org/traccar/api/security/PermissionsService.java19
6 files changed, 52 insertions, 53 deletions
diff --git a/src/main/java/org/traccar/api/BaseObjectResource.java b/src/main/java/org/traccar/api/BaseObjectResource.java
index 07c74449c..d6401dc42 100644
--- a/src/main/java/org/traccar/api/BaseObjectResource.java
+++ b/src/main/java/org/traccar/api/BaseObjectResource.java
@@ -66,7 +66,12 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
permissionsService.checkEdit(getUserId(), entity, true);
BaseObjectManager<T> manager = Context.getManager(baseClass);
- manager.addItem(entity);
+ if (manager != null) {
+ manager.addItem(entity);
+ } else {
+ entity.setId(storage.addObject(entity, new Request(new Columns.Exclude("id"))));
+ }
+
LogAction.create(getUserId(), entity);
storage.addPermission(new Permission(User.class, getUserId(), baseClass, entity.getId()));
@@ -87,7 +92,15 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
permissionsService.checkEdit(getUserId(), entity, false);
permissionsService.checkPermission(baseClass, getUserId(), entity.getId());
- Context.getManager(baseClass).updateItem(entity);
+ BaseObjectManager<T> manager = Context.getManager(baseClass);
+ if (manager != null) {
+ manager.updateItem(entity);
+ } else {
+ storage.updateObject(entity, new Request(
+ new Columns.Exclude("id"),
+ new Condition.Equals("id", "id")));
+ }
+
LogAction.edit(getUserId(), entity);
if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) {
@@ -104,15 +117,20 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
permissionsService.checkPermission(baseClass, getUserId(), id);
BaseObjectManager<T> manager = Context.getManager(baseClass);
- manager.removeItem(id);
- LogAction.remove(getUserId(), baseClass, id);
-
- if (manager instanceof SimpleObjectManager) {
- ((SimpleObjectManager<T>) manager).refreshUserItems();
- if (manager instanceof ExtendedObjectManager) {
- ((ExtendedObjectManager<T>) manager).refreshExtendedPermissions();
+ if (manager != null) {
+ manager.removeItem(id);
+ if (manager instanceof SimpleObjectManager) {
+ ((SimpleObjectManager<T>) manager).refreshUserItems();
+ if (manager instanceof ExtendedObjectManager) {
+ ((ExtendedObjectManager<T>) manager).refreshExtendedPermissions();
+ }
}
+ } else {
+ storage.removeObject(baseClass, new Request(new Condition.Equals("id", "id", id)));
}
+
+ LogAction.remove(getUserId(), baseClass, id);
+
if (baseClass.equals(Group.class) || baseClass.equals(Device.class) || baseClass.equals(User.class)) {
if (baseClass.equals(Group.class)) {
Context.getGroupsManager().refreshItems();
diff --git a/src/main/java/org/traccar/api/ExtendedObjectResource.java b/src/main/java/org/traccar/api/ExtendedObjectResource.java
index 41ed3e9d9..8467b46c6 100644
--- a/src/main/java/org/traccar/api/ExtendedObjectResource.java
+++ b/src/main/java/org/traccar/api/ExtendedObjectResource.java
@@ -44,7 +44,7 @@ public class ExtendedObjectResource<T extends BaseModel> extends BaseObjectResou
var conditions = new LinkedList<Condition>();
if (all) {
- if (!permissionsService.isAdmin(getUserId())) {
+ if (permissionsService.notAdmin(getUserId())) {
conditions.add(new Condition.Permission(User.class, getUserId(), baseClass));
}
} else {
diff --git a/src/main/java/org/traccar/api/SimpleObjectResource.java b/src/main/java/org/traccar/api/SimpleObjectResource.java
index 15a496c5f..4a435ca7d 100644
--- a/src/main/java/org/traccar/api/SimpleObjectResource.java
+++ b/src/main/java/org/traccar/api/SimpleObjectResource.java
@@ -41,7 +41,7 @@ public class SimpleObjectResource<T extends BaseModel> extends BaseObjectResourc
var conditions = new LinkedList<Condition>();
if (all) {
- if (!permissionsService.isAdmin(getUserId())) {
+ if (permissionsService.notAdmin(getUserId())) {
conditions.add(new Condition.Permission(User.class, getUserId(), baseClass));
}
} else {
diff --git a/src/main/java/org/traccar/api/resource/AttributeResource.java b/src/main/java/org/traccar/api/resource/AttributeResource.java
index 478b7acfd..fdd0d4f6f 100644
--- a/src/main/java/org/traccar/api/resource/AttributeResource.java
+++ b/src/main/java/org/traccar/api/resource/AttributeResource.java
@@ -67,10 +67,8 @@ public class AttributeResource extends ExtendedObjectResource<Attribute> {
throw new IllegalArgumentException("Device has no last position");
}
- Object result = new ComputedAttributesHandler(
- Context.getConfig(),
- Context.getIdentityManager(),
- Context.getAttributesManager()).computeAttribute(entity, last);
+ Object result = new ComputedAttributesHandler(Context.getConfig(), Context.getIdentityManager(), null)
+ .computeAttribute(entity, last);
if (result != null) {
switch (entity.getType()) {
case "number":
diff --git a/src/main/java/org/traccar/api/resource/PermissionsResource.java b/src/main/java/org/traccar/api/resource/PermissionsResource.java
index 7def38919..484c61e66 100644
--- a/src/main/java/org/traccar/api/resource/PermissionsResource.java
+++ b/src/main/java/org/traccar/api/resource/PermissionsResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,10 +16,11 @@
*/
package org.traccar.api.resource;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Set;
+import org.traccar.Context;
+import org.traccar.api.BaseResource;
+import org.traccar.helper.LogAction;
+import org.traccar.model.Permission;
+import org.traccar.storage.StorageException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
@@ -29,34 +30,21 @@ import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-
-import org.traccar.Context;
-import org.traccar.api.BaseResource;
-import org.traccar.helper.LogAction;
-import org.traccar.model.Device;
-import org.traccar.model.Permission;
-import org.traccar.model.User;
-import org.traccar.storage.StorageException;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Set;
@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());
+ private void checkPermission(Permission permission, boolean link) throws StorageException {
+ if (permissionsService.notAdmin(getUserId())) {
+ permissionsService.checkPermission(permission.getOwnerClass(), getUserId(), permission.getOwnerId());
+ permissionsService.checkPermission(permission.getOwnerClass(), getUserId(), permission.getOwnerId());
}
- Context.getPermissionsManager().checkPermission(
- permission.getPropertyClass(), getUserId(), permission.getPropertyId());
}
private void checkPermissionTypes(List<LinkedHashMap<String, Long>> entities) {
diff --git a/src/main/java/org/traccar/api/security/PermissionsService.java b/src/main/java/org/traccar/api/security/PermissionsService.java
index ac687fc1c..9daef355e 100644
--- a/src/main/java/org/traccar/api/security/PermissionsService.java
+++ b/src/main/java/org/traccar/api/security/PermissionsService.java
@@ -61,8 +61,8 @@ public class PermissionsService {
return user;
}
- public boolean isAdmin(long userId) throws StorageException {
- return getUser(userId).getAdministrator();
+ public boolean notAdmin(long userId) throws StorageException {
+ return !getUser(userId).getAdministrator();
}
public void checkAdmin(long userId) throws StorageException, SecurityException {
@@ -134,16 +134,11 @@ public class PermissionsService {
if (!getUser(userId).getAdministrator() && !(clazz.equals(User.class) && userId == objectId)) {
var objects = storage.getObjects(clazz, new Request(
new Columns.Include("id"),
- new Condition.Permission(
- User.class, userId, clazz.equals(User.class) ? ManagedUser.class : clazz)));
- boolean found = false;
- for (var object : objects) {
- if (object.getId() == objectId) {
- found = true;
- break;
- }
- }
- if (!found) {
+ new Condition.And(
+ new Condition.Equals("id", "id", objectId),
+ new Condition.Permission(
+ User.class, userId, clazz.equals(User.class) ? ManagedUser.class : clazz))));
+ if (!objects.isEmpty()) {
throw new SecurityException(clazz.getSimpleName() + " access denied");
}
}