From 2e769ee25bf7d134e4625cc5814661588adf7794 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 15 Feb 2022 23:17:31 -0800 Subject: Implement memory permissions --- src/main/java/org/traccar/model/Pair.java | 44 ++++++++++++++++++++++ .../java/org/traccar/storage/MemoryStorage.java | 36 +++++++++++++----- 2 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/traccar/model/Pair.java (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/model/Pair.java b/src/main/java/org/traccar/model/Pair.java new file mode 100644 index 000000000..fa856db96 --- /dev/null +++ b/src/main/java/org/traccar/model/Pair.java @@ -0,0 +1,44 @@ +package org.traccar.model; + +import java.util.Objects; + +public class Pair { + + private final K first; + private final V second; + + public Pair(K first, V second) { + this.first = first; + this.second = second; + } + + public K getFirst() { + return first; + } + + public V getSecond() { + return second; + } + + @SuppressWarnings("rawtypes") + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Pair pair = (Pair) o; + + if (!Objects.equals(first, pair.first)) return false; + if (!Objects.equals(second, pair.second)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = first != null ? first.hashCode() : 0; + result = 31 * result + (second != null ? second.hashCode() : 0); + return result; + } + +} diff --git a/src/main/java/org/traccar/storage/MemoryStorage.java b/src/main/java/org/traccar/storage/MemoryStorage.java index 0a2e5a81f..9cfe30a2b 100644 --- a/src/main/java/org/traccar/storage/MemoryStorage.java +++ b/src/main/java/org/traccar/storage/MemoryStorage.java @@ -1,43 +1,59 @@ package org.traccar.storage; +import org.traccar.model.Pair; import org.traccar.model.Permission; import org.traccar.storage.query.Request; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; public class MemoryStorage extends Storage { + private final Map, Class>, Set>> permissions = new HashMap<>(); + @Override - public List getObjects(Class clazz, Request request) throws StorageException { + public List getObjects(Class clazz, Request request) { return null; } @Override - public long addObject(T entity, Request request) throws StorageException { + public long addObject(T entity, Request request) { return 0; } @Override - public void updateObject(T entity, Request request) throws StorageException { + public void updateObject(T entity, Request request) { } @Override - public void removeObject(Class clazz, Request request) throws StorageException { + public void removeObject(Class clazz, Request request) { } - @Override - public List getPermissions(Class ownerClass, Class propertyClass) throws StorageException { - return null; + private Set> getPermissionsSet(Class ownerClass, Class propertyClass) { + return permissions.computeIfAbsent(new Pair<>(ownerClass, propertyClass), k -> new HashSet<>()); } @Override - public void addPermission(Permission permission) throws StorageException { - + public List getPermissions(Class ownerClass, Class propertyClass) { + return getPermissionsSet(ownerClass, propertyClass).stream() + .map(pair -> new Permission(ownerClass, pair.getFirst(), propertyClass, pair.getSecond())) + .collect(Collectors.toList()); } @Override - public void removePermission(Permission permission) throws StorageException { + public void addPermission(Permission permission) { + getPermissionsSet(permission.getOwnerClass(), permission.getPropertyClass()) + .add(new Pair<>(permission.getOwnerId(), permission.getPropertyId())); + } + @Override + public void removePermission(Permission permission) { + getPermissionsSet(permission.getOwnerClass(), permission.getPropertyClass()) + .remove(new Pair<>(permission.getOwnerId(), permission.getPropertyId())); } } -- cgit v1.2.3