Remove access from ALL users a specific Device (API)

GB3 years ago

The example below removes "user 2" access to "device 1":

$.ajax({
  url: 'http://{host}:{port}/api/permissions',
  method: 'DELETE',
  headers: {
    'content-type': 'application/json',
    'authorization': 'Basic ' + btoa(`${USER}:${PASSWORD}`)
  },
  processData: false,
  data: JSON.stringify({userId: 2, deviceId: 1})
})

But suppose "N users" have access to this device and I want to remove access from all these "N users". How should I do?
Can I use some code, like {userId: 0, deviceId: 1} or {userId: -1, deviceId: 1} to indicate ALL users and not a specific one?

Note: I don't know the list of users who have permission. If I knew the list, it would be easy. Just call DELETE several times, one for each user.

Anton Tananaev3 years ago

Probably the easiest option is to iterate through all users.

GB3 years ago

I was afraid to hear this solution (there are approximately 500 users) :-(

Anton Tananaev3 years ago

Why is that an issue? I'm assuming you want to automate it either way.

GB3 years ago

yes... I'm automating. My script read a csv file with some devices and update the information in the database.

10 devices X 500 users = 5,000 server requests

My fear is that the number of devices (or users) starts to grow a lot, but I believe it won't.
It is a solution that solves my problem!
Thank you very much Anton

Anton Tananaev3 years ago

Actually we do have a "bulk" option for permissions:

https://github.com/traccar/traccar/pull/4717

GB3 years ago

Thank you Anton. I will check the source code. This "bulk for permissions" is not documented in https://www.traccar.org/api-reference, correct?

Anton Tananaev3 years ago

Yeah, I don't think it's documented there.

Sanjay Shah3 years ago

Bulk update is good. Can we have any documents for better use case

Anton Tananaev3 years ago

Sure, feel free to send a pull request for the API documentation.

Sanjay Shah3 years ago

not getting this

GB3 years ago

Anton helps me because I know little about Java.
From what I understand from the source code, I must inform an array of [ {userId: A, deviceId: B}, {userId: C, deviceId: D} ], correct?

Example:

$.ajax({
  url: 'http://{host}:{port}/api/permissions/bulk',
  method: 'DELETE',
  headers: {
    'content-type': 'application/json',
    'authorization': 'Basic ' + btoa(`${USER}:${PASSWORD}`)
  },
  processData: false,
  data: JSON.stringify( [ {userId: 1, deviceId: 1}, 
                          {userId: 2, deviceId: 1}, 
                          ...., 
                          ...., 
                          {userId: N, deviceId: 1} ] )
})

src/main/java/org/traccar/api/resource/PermissionsResource.java, line 96:

    @DELETE
    @Path("bulk")
    public Response remove(List<LinkedHashMap<String, Long>> entities) throws StorageException, ClassNotFoundException {
        Context.getPermissionsManager().checkReadonly(getUserId());
        checkPermissionTypes(entities);
        for (LinkedHashMap<String, Long> entity: entities) {
            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());
        }
        if (!entities.isEmpty()) {
            Context.getPermissionsManager().refreshPermissions(new Permission(entities.get(0)));
        }
        return Response.noContent().build();
    }
GB3 years ago

Anton good afternoon! How are you? I imagine you are very busy, but I need your help.

I opened the Java code src/main/java/org/traccar/api/resource/PermissionsResource.java, line 96 (my last post above) but I didn't understand how to make the "permissions/bulk" request using JQuery/Ajax.

Could you guide me on how to tweak my code below or post an example in any language so I can "transcribe" the example to JQuery/Ajax?

$.ajax({
  url: 'http://{host}:{port}/api/permissions/bulk',
  method: 'DELETE',
  headers: {
    'content-type': 'application/json',
    'authorization': 'Basic ' + btoa(`${USER}:${PASSWORD}`)
  },
  processData: false,
  data: JSON.stringify( [ {userId: 1, deviceId: 1}, 
                          {userId: 2, deviceId: 1}, 
                          ...., 
                          {userId: N, deviceId: 1} ] )
})

I'm coming back with this subject, I really need this feature: "remove access from all users of a certain vehicle".

Thanks again for your attention and congratulations on the project!!!

Anton Tananaev3 years ago

What's the API response?

GB3 years ago

Anton, sorry! The example I posted here https://www.traccar.org/forums/topic/remove-access-from-all-users-a-specific-device-api/#post-70135 works yes. It was my mistake while testing the code.

Do you as admin have permission to delete my message from yesterday? If so, please delete it and close the topic as resolved.

Thank you very much and again congratulations on the project!

PS: Do you already have a forecast of when the new version of Traccar will be released?