Search in sources :

Example 1 with Permission

use of org.traccar.model.Permission in project traccar by tananaev.

the class PermissionsManager method refreshDeviceAndGroupPermissions.

public final void refreshDeviceAndGroupPermissions() {
    groupPermissions.clear();
    devicePermissions.clear();
    try {
        GroupTree groupTree = new GroupTree(Context.getGroupsManager().getItems(Context.getGroupsManager().getAllItems()), Context.getDeviceManager().getAllDevices());
        for (Permission groupPermission : dataManager.getPermissions(User.class, Group.class)) {
            Set<Long> userGroupPermissions = getGroupPermissions(groupPermission.getOwnerId());
            Set<Long> userDevicePermissions = getDevicePermissions(groupPermission.getOwnerId());
            userGroupPermissions.add(groupPermission.getPropertyId());
            for (Group group : groupTree.getGroups(groupPermission.getPropertyId())) {
                userGroupPermissions.add(group.getId());
            }
            for (Device device : groupTree.getDevices(groupPermission.getPropertyId())) {
                userDevicePermissions.add(device.getId());
            }
        }
        for (Permission devicePermission : dataManager.getPermissions(User.class, Device.class)) {
            getDevicePermissions(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
        }
        groupDevices.clear();
        for (long groupId : Context.getGroupsManager().getAllItems()) {
            for (Device device : groupTree.getDevices(groupId)) {
                getGroupDevices(groupId).add(device.getId());
            }
        }
    } catch (SQLException | ClassNotFoundException error) {
        Log.warning(error);
    }
    deviceUsers.clear();
    for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
        for (long deviceId : entry.getValue()) {
            getAllDeviceUsers(deviceId).add(entry.getKey());
        }
    }
}
Also used : Group(org.traccar.model.Group) Set(java.util.Set) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Permission

use of org.traccar.model.Permission in project traccar by tananaev.

the class ExtendedObjectManager method refreshExtendedPermissions.

public void refreshExtendedPermissions() {
    if (getDataManager() != null) {
        try {
            Collection<Permission> databaseGroupPermissions = getDataManager().getPermissions(Group.class, getBaseClass());
            groupItems.clear();
            for (Permission groupPermission : databaseGroupPermissions) {
                getGroupItems(groupPermission.getOwnerId()).add(groupPermission.getPropertyId());
            }
            Collection<Permission> databaseDevicePermissions = getDataManager().getPermissions(Device.class, getBaseClass());
            deviceItems.clear();
            deviceItemsWithGroups.clear();
            for (Permission devicePermission : databaseDevicePermissions) {
                getDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
                getAllDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
            }
            for (Device device : Context.getDeviceManager().getAllDevices()) {
                long groupId = device.getGroupId();
                while (groupId != 0) {
                    getAllDeviceItems(device.getId()).addAll(getGroupItems(groupId));
                    Group group = Context.getGroupsManager().getById(groupId);
                    if (group != null) {
                        groupId = group.getGroupId();
                    } else {
                        groupId = 0;
                    }
                }
            }
        } catch (SQLException | ClassNotFoundException error) {
            Log.warning(error);
        }
    }
}
Also used : Group(org.traccar.model.Group) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission)

Example 3 with Permission

use of org.traccar.model.Permission in project traccar by tananaev.

the class QueryBuilder method executePermissionsQuery.

public Collection<Permission> executePermissionsQuery() throws SQLException, ClassNotFoundException {
    List<Permission> result = new LinkedList<>();
    if (query != null) {
        try {
            try (ResultSet resultSet = statement.executeQuery()) {
                ResultSetMetaData resultMetaData = resultSet.getMetaData();
                while (resultSet.next()) {
                    LinkedHashMap<String, Long> map = new LinkedHashMap<>();
                    for (int i = 1; i <= resultMetaData.getColumnCount(); i++) {
                        String label = resultMetaData.getColumnLabel(i);
                        map.put(label, resultSet.getLong(label));
                    }
                    result.add(new Permission(map));
                }
            }
        } finally {
            statement.close();
            connection.close();
        }
    }
    return result;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Permission(org.traccar.model.Permission) ResultSet(java.sql.ResultSet) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Permission

use of org.traccar.model.Permission in project traccar by tananaev.

the class PermissionsResource method add.

@POST
public Response add(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException {
    Context.getPermissionsManager().checkReadonly(getUserId());
    Permission permission = new Permission(entity);
    checkPermission(permission, true);
    Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId(), true);
    LogAction.link(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId());
    Context.getPermissionsManager().refreshPermissions(permission);
    return Response.noContent().build();
}
Also used : Permission(org.traccar.model.Permission) POST(javax.ws.rs.POST)

Example 5 with Permission

use of org.traccar.model.Permission in project traccar by tananaev.

the class PermissionsResource method remove.

@DELETE
public Response remove(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException {
    Context.getPermissionsManager().checkReadonly(getUserId());
    Permission permission = new Permission(entity);
    checkPermission(permission, false);
    Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId(), false);
    LogAction.unlink(getUserId(), permission.getOwnerClass(), permission.getOwnerId(), permission.getPropertyClass(), permission.getPropertyId());
    Context.getPermissionsManager().refreshPermissions(permission);
    return Response.noContent().build();
}
Also used : Permission(org.traccar.model.Permission) DELETE(javax.ws.rs.DELETE)

Aggregations

Permission (org.traccar.model.Permission)5 SQLException (java.sql.SQLException)2 Device (org.traccar.model.Device)2 Group (org.traccar.model.Group)2 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1