From 8cd4f224572e55cd3c87119db24b8adcf1f0351a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 8 Mar 2016 22:41:32 +1300 Subject: Start refactoring permissions manager --- src/org/traccar/database/PermissionsManager.java | 32 ++++++++++++++++-------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 29228ceeb..854732fa2 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,13 +34,21 @@ public class PermissionsManager { private final Map users = new HashMap<>(); - private final Map> permissions = new HashMap<>(); + private final Map> groupPermissions = new HashMap<>(); + private final Map> devicePermissions = new HashMap<>(); - private Set getPermissions(long userId) { - if (!permissions.containsKey(userId)) { - permissions.put(userId, new HashSet()); + private Set getGroupPermissions(long userId) { + if (!groupPermissions.containsKey(userId)) { + groupPermissions.put(userId, new HashSet()); } - return permissions.get(userId); + return groupPermissions.get(userId); + } + + private Set getDevicePermissions(long userId) { + if (!devicePermissions.containsKey(userId)) { + devicePermissions.put(userId, new HashSet()); + } + return devicePermissions.get(userId); } public PermissionsManager(DataManager dataManager) { @@ -50,14 +58,14 @@ public class PermissionsManager { public final void refresh() { users.clear(); - permissions.clear(); + devicePermissions.clear(); try { server = dataManager.getServer(); for (User user : dataManager.getUsers()) { users.put(user.getId(), user); } for (DevicePermission permission : dataManager.getPermissions()) { - getPermissions(permission.getUserId()).add(permission.getDeviceId()); + getDevicePermissions(permission.getUserId()).add(permission.getDeviceId()); } } catch (SQLException error) { Log.warning(error); @@ -80,12 +88,16 @@ public class PermissionsManager { } } + public Collection allowedGroups(long userId) { + return getGroupPermissions(userId); + } + public Collection allowedDevices(long userId) { - return getPermissions(userId); + return getDevicePermissions(userId); } public void checkDevice(long userId, long deviceId) throws SecurityException { - if (!getPermissions(userId).contains(deviceId)) { + if (!getDevicePermissions(userId).contains(deviceId)) { throw new SecurityException("Device access denied"); } } -- cgit v1.2.3