Ticket #18283: 18283.patch

File 18283.patch, 5.8 KB (added by taylor.smock, 2 years ago)

Don't hardcode PermissionPrefWithDefault values for getPermissionPrefs

  • src/org/openstreetmap/josm/io/remotecontrol/PermissionPrefWithDefault.java

     
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.util.Arrays;
     6import java.util.ArrayList;
     7import java.util.Collections;
    78import java.util.List;
     9import java.util.stream.Collectors;
    810
    911import org.openstreetmap.josm.spi.preferences.Config;
    1012
     
    1618 * @author Bodo Meissner
    1719 */
    1820public class PermissionPrefWithDefault {
     21    private static final List<PermissionPrefWithDefault> PREFS = new ArrayList<>();
    1922
    2023    public static final PermissionPrefWithDefault LOAD_DATA =
    2124            new PermissionPrefWithDefault("remotecontrol.permission.load-data", true, tr("Load data from API"));
     
    4649     */
    4750    public final String preferenceText;
    4851
     52    /**
     53     * Create a new {@code PermissionPrefWithDefault}
     54     *
     55     * @param pref           The preference key for the permission
     56     * @param defaultVal     The default value of the preference
     57     * @param preferenceText The text to show in UI objects
     58     */
    4959    public PermissionPrefWithDefault(String pref, boolean defaultVal, String preferenceText) {
    5060        this.pref = pref;
    5161        this.defaultVal = defaultVal;
     
    5262        this.preferenceText = preferenceText;
    5363    }
    5464
     65    /**
     66     * @return true if the action is allowed
     67     */
    5568    public boolean isAllowed() {
    5669        return Config.getPref().getBoolean(pref, defaultVal);
    5770    }
    5871
     72    /**
     73     * @return A non-modifiable list of permission preferences for Remote Control
     74     */
    5975    public static List<PermissionPrefWithDefault> getPermissionPrefs() {
    60         return Arrays.asList(
    61                 LOAD_DATA, IMPORT_DATA, OPEN_FILES, LOAD_IMAGERY,
    62                 CHANGE_SELECTION, CHANGE_VIEWPORT,
    63                 CREATE_OBJECTS, READ_PROTOCOL_VERSION);
     76        return Collections.unmodifiableList(PREFS);
    6477    }
     78
     79    /**
     80     * @param pref The preference to add to the list returned by
     81     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
     82     */
     83    public static void addPermissionPref(PermissionPrefWithDefault pref) {
     84        if (pref.pref != null && PREFS.parallelStream().noneMatch(tPref -> pref.pref.equals(tPref.pref)))
     85            PREFS.add(pref);
     86    }
     87
     88    /**
     89     * @param pref The preference to remove from the list returned by
     90     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
     91     *
     92     * @return see {@link List#removeAll}
     93     */
     94    public static boolean removePermissionPref(PermissionPrefWithDefault pref) {
     95        List<PermissionPrefWithDefault> toRemove = Collections.emptyList();
     96        if (pref.pref != null)
     97            toRemove = PREFS.parallelStream().filter(tPref -> pref.pref.equals(tPref.pref))
     98                    .collect(Collectors.toList());
     99        return PREFS.removeAll(toRemove);
     100    }
    65101}
  • src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java

     
    1010import java.io.PrintWriter;
    1111import java.io.StringWriter;
    1212import java.io.Writer;
     13import java.lang.reflect.InvocationTargetException;
    1314import java.net.Socket;
    1415import java.nio.charset.Charset;
    1516import java.nio.charset.StandardCharsets;
     
    126127                    command + "\" (handled by " + handler.getSimpleName() + ')');
    127128            }
    128129            handlers.put(commandWithSlash, handler);
     130            try {
     131                PermissionPrefWithDefault pref = handler.getConstructor().newInstance().getPermissionPref();
     132                if (pref != null)
     133                    PermissionPrefWithDefault.addPermissionPref(pref);
     134            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
     135                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
     136                Logging.debug(e);
     137            }
    129138        }
    130139    }
    131140
    132     /** Add default request handlers */
     141    /**
     142     * Add default request handlers and permission preferences (order is important)
     143     */
    133144    static {
    134145        addRequestHandlerClass(LoadAndZoomHandler.command, LoadAndZoomHandler.class, true);
    135146        addRequestHandlerClass(LoadAndZoomHandler.command2, LoadAndZoomHandler.class, true);
     147        addRequestHandlerClass(LoadObjectHandler.command, LoadObjectHandler.class, true);
    136148        addRequestHandlerClass(LoadDataHandler.command, LoadDataHandler.class, true);
     149        addRequestHandlerClass(ImportHandler.command, ImportHandler.class, true);
     150        addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true);
    137151        addRequestHandlerClass(ImageryHandler.command, ImageryHandler.class, true);
     152        PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.CHANGE_SELECTION);
     153        PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.CHANGE_VIEWPORT);
    138154        addRequestHandlerClass(AddNodeHandler.command, AddNodeHandler.class, true);
    139155        addRequestHandlerClass(AddWayHandler.command, AddWayHandler.class, true);
    140         addRequestHandlerClass(ImportHandler.command, ImportHandler.class, true);
    141156        addRequestHandlerClass(VersionHandler.command, VersionHandler.class, true);
    142         addRequestHandlerClass(LoadObjectHandler.command, LoadObjectHandler.class, true);
    143         addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true);
    144157        addRequestHandlerClass(FeaturesHandler.command, FeaturesHandler.class, true);
    145158    }
    146159