vitalidze / traccar-web

Traccar Web UI mod
http://traccar.litvak.su/
152 stars 145 forks source link

allow commands to users only read #1170

Open gechdell opened 6 years ago

gechdell commented 6 years ago

that such, I have a small problem that I do not know how to solve it, I am modifying the frontend so that in the read-only view, users can send commands to the devices, I already have in the read-only view the command button, click opens the dialog window and the commands are visible, but when I try to send one, it gives me the following error

HTTP ERROR 401

Problem accessing /traccar/rest/sendCommand. Reason:

Unauthorized

I was lost to solve it, could help me solve it

sorry my english but I'm using google translator

vitalidze commented 6 years ago

You need to update the DataServiceImpl as well because currently it disallows sending of commands by read-only users. You need to change annotations on sendCommand method.

gechdell commented 6 years ago

Thanks for your answer, but already trying to modify the parameters in the DataServiceImpl but I still get the same error 401, in this section

`public String sendCommand(Command command) throws AccessDeniedException { Device device = getSessionEntityManager().find(Device.class, command.getDeviceId()); if (!getSessionUser().hasAccessTo(device)) { throw new AccessDeniedException(); }

    //if (applicationSettings.get().isAllowCommandsOnlyForAdmins() && !getSessionUser().getAdmin()) {
    if (applicationSettings.get().isAllowCommandsOnlyForAdmins()) {
        throw new AccessDeniedException();
                }`

I am lost, you could orientate me more, since I have been several days and I do not find the solution

vitalidze commented 6 years ago

You need to remove @RequireWrite annotation from method in DataServiceImpl class. All methods annotated in same way will not be allowed to execute by read-only user.

gechdell commented 6 years ago

perfect thank you very much for your help simply commenting the @RequireWrite line is solved and already read users can send commands.

I would also like to add to the mobiles view a command send button, as well as sending it by email or sms, I plan to send the data to the API, any suggestions?

vitalidze commented 6 years ago

I would also like to add to the mobiles view a command send button, as well as sending it by email or sms, I plan to send the data to the API, any suggestions?

You should be able to use sendCommand method via RESTful API. You can find additional information here: http://traccar.litvak.su/features/rest-api.html

gechdell commented 6 years ago

hello vitalidze, I appreciate your great help, I was trying to use the RESTful API as you suggested, but I can not get the expected results, if I try on the page for mobile write this code

` $$.ajax({

                            url: '../traccar/rest/sendCommand',
                            method: 'POST',
                            dataType: 'json',
                            data:JSON.stringify([{"deviceId": deviceId, "type": "engineStop"}]),
                            processData: false,
                            start: function(xhr) {
                                if (options.showIndicator != undefined && options.showIndicator) {
                                    myApp.showIndicator();
                                }
                            },
                            success: options.success,
                            complete: function(xhr) {
                                if (options.showIndicator != undefined && options.showIndicator) {
                                    myApp.hideIndicator();
                                }
                                if (xhr.status != 200 && options.error != undefined) {
                                    options.error(xhr);
                                    options.error = undefined;
                                }
                            },
                            error: function(xhr) {
                                options.error(xhr);
                                options.error = undefined;
                            }
                        })

`

but does not do anything, try to do it as a test by means of curl with the following

curl -v --cookie "JSESSIONID=134u24dd0vagq1h4s2959ehnu2" POST --data "[{\"deviceId\":5,\"type\":\"engineStop\"}]" localhost:8082/traccar/rest/sendCommand

and the console responds

upload completely sent off: 36 out of 36 bytes < HTTP/1.1 200 OK < Date: Wed, 13 Sep 2017 20:42:15 GMT < Content-Type: application/json; charset=UTF-8 < Content-Length: 4 < Server: Jetty(9.2.22.v20170606)

but the device does nothing, enter the server log and I see this error

2017-09-13 15:42:15 WARN: [21DBB7F7] error - NullPointerException (StringProtocolEncoder:33 < *:48 < Tk103ProtocolEncoder:42 < BaseProtocolEncoder:48 < ...) what am I doing wrong, or where is the error ??

vitalidze commented 6 years ago

I think your request is not properly formatted. You need to read documentation carefully.

ср, 13 сент. 2017 г. в 23:58, gechdell notifications@github.com:

hello vitalidze, I appreciate your great help, I was trying to use the RESTful API as you suggested, but I can not get the expected results, if I try on the page for mobile write this code ` $$.ajax({ url: '../traccar/rest/sendCommand', method: 'POST', dataType: 'json', data:JSON.stringify([{"deviceId": deviceId, "type": "engineStop"}]), processData: false, start: function(xhr) { if (options.showIndicator != undefined && options.showIndicator) { myApp.showIndicator(); } }, success: options.success, complete: function(xhr) { if (options.showIndicator != undefined && options.showIndicator) { myApp.hideIndicator(); }

                            if (xhr.status != 200 && options.error != undefined) {
                                options.error(xhr);
                                options.error = undefined;
                            }
                        },
                        error: function(xhr) {
                            options.error(xhr);
                            options.error = undefined;
                        }
                    })

but does not do anything, try to do it as a test by means of curl with the followingcurl -v --cookie "JSESSIONID=134u24dd0vagq1h4s2959ehnu2" POST --data "[{"deviceId":5,"type":"engineStop"}]" localhost:8082/traccar/rest/sendCommand ` and the console responds

upload completely sent off: 36 out of 36 bytes < HTTP/1.1 200 OK < Date: Wed, 13 Sep 2017 20:42:15 GMT < Content-Type: application/json; charset=UTF-8 < Content-Length: 4 < Server: Jetty(9.2.22.v20170606) but the device does nothing, enter the server log and I see this error

`2017-09-13 15:42:15 WARN: [21DBB7F7] error - NullPointerException (StringProtocolEncoder:33 < *:48 < Tk103ProtocolEncoder:42 < BaseProtocolEncoder:48 < ...)

what am I doing wrong, or where is the error ??`

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/vitalidze/traccar-web/issues/1170#issuecomment-329294955, or mute the thread https://github.com/notifications/unsubscribe-auth/ACMCNZKY_QXKl1qqI_ZTqSb7Lzo03OtRks5siEHpgaJpZM4PR2bL .

-- Kind regards, Vitaly Litvak