diff options
Diffstat (limited to 'src/main/java/org/traccar/model/Permission.java')
-rw-r--r-- | src/main/java/org/traccar/model/Permission.java | 95 |
1 files changed, 84 insertions, 11 deletions
diff --git a/src/main/java/org/traccar/model/Permission.java b/src/main/java/org/traccar/model/Permission.java index 6475a4582..ad0176b39 100644 --- a/src/main/java/org/traccar/model/Permission.java +++ b/src/main/java/org/traccar/model/Permission.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2020 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,42 +16,115 @@ */ package org.traccar.model; -import java.util.Iterator; +import java.beans.Introspector; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.LinkedHashMap; import java.util.Map; +import java.util.TreeMap; -import org.traccar.database.DataManager; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.traccar.helper.ClassScanner; +import org.traccar.storage.QueryIgnore; public class Permission { + private static final Map<String, Class<?>> CLASSES = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + + static { + try { + for (Class<?> clazz : ClassScanner.findSubclasses(BaseModel.class)) { + CLASSES.put(clazz.getSimpleName(), clazz); + } + } catch (IOException | ReflectiveOperationException | URISyntaxException e) { + throw new RuntimeException(e); + } + } + + private final LinkedHashMap<String, Long> data; + private final Class<?> ownerClass; private final long ownerId; private final Class<?> propertyClass; private final 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 Permission(LinkedHashMap<String, Long> data) { + this.data = data; + var iterator = data.entrySet().iterator(); + var owner = iterator.next(); + ownerClass = CLASSES.get(owner.getKey().substring(0, owner.getKey().length() - 2)); + ownerId = owner.getValue(); + var property = iterator.next(); + propertyClass = CLASSES.get(property.getKey().substring(0, property.getKey().length() - 2)); + propertyId = property.getValue(); } + public Permission(Class<?> ownerClass, long ownerId, Class<?> propertyClass, long propertyId) { + this.ownerClass = ownerClass; + this.ownerId = ownerId; + this.propertyClass = propertyClass; + this.propertyId = propertyId; + data = new LinkedHashMap<>(); + data.put(getKey(ownerClass), ownerId); + data.put(getKey(propertyClass), propertyId); + } + + private static String getKey(Class<?> clazz) { + return Introspector.decapitalize(clazz.getSimpleName()) + "Id"; + } + + public static String getStorageName(Class<?> ownerClass, Class<?> propertyClass) { + String ownerName = ownerClass.getSimpleName(); + String propertyName = propertyClass.getSimpleName(); + String managedPrefix = "Managed"; + if (propertyName.startsWith(managedPrefix)) { + propertyName = propertyName.substring(managedPrefix.length()); + } + return "tc_" + Introspector.decapitalize(ownerName) + "_" + Introspector.decapitalize(propertyName); + } + + @QueryIgnore + @JsonIgnore + public String getStorageName() { + return getStorageName(ownerClass, propertyClass); + } + + @QueryIgnore + @JsonAnyGetter + public Map<String, Long> get() { + return data; + } + + @QueryIgnore + @JsonAnySetter + public void set(String key, Long value) { + data.put(key, value); + } + + @QueryIgnore + @JsonIgnore public Class<?> getOwnerClass() { return ownerClass; } + @QueryIgnore + @JsonIgnore public long getOwnerId() { return ownerId; } + @QueryIgnore + @JsonIgnore public Class<?> getPropertyClass() { return propertyClass; } + @QueryIgnore + @JsonIgnore public long getPropertyId() { return propertyId; } + } |