diff options
-rw-r--r-- | schema/changelog-5.7.xml | 25 | ||||
-rw-r--r-- | schema/changelog-master.xml | 1 | ||||
-rw-r--r-- | src/main/java/org/traccar/api/resource/NotificationResource.java | 6 | ||||
-rw-r--r-- | src/main/java/org/traccar/api/security/PermissionsService.java | 20 | ||||
-rw-r--r-- | src/main/java/org/traccar/model/Notification.java | 12 | ||||
-rw-r--r-- | src/main/java/org/traccar/notificators/NotificatorCommand.java | 62 |
6 files changed, 119 insertions, 7 deletions
diff --git a/schema/changelog-5.7.xml b/schema/changelog-5.7.xml new file mode 100644 index 000000000..ad15ac48c --- /dev/null +++ b/schema/changelog-5.7.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<databaseChangeLog + xmlns="http://www.liquibase.org/xml/ns/dbchangelog" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog + http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd" + logicalFilePath="changelog-5.7"> + + <changeSet author="author" id="changelog-5.7"> + + <addColumn tableName="tc_notifications"> + <column name="commandid" type="INT" /> + </addColumn> + + <addForeignKeyConstraint + baseTableName="tc_notifications" + baseColumnNames="commandid" + constraintName="fk_notifications_commandid" + referencedTableName="tc_commands" + referencedColumnNames="id" + onDelete="CASCADE" /> + + </changeSet> + +</databaseChangeLog> diff --git a/schema/changelog-master.xml b/schema/changelog-master.xml index 0835918c9..1587cc789 100644 --- a/schema/changelog-master.xml +++ b/schema/changelog-master.xml @@ -37,5 +37,6 @@ <include file="changelog-5.4.xml" relativeToChangelogFile="true" /> <include file="changelog-5.5.xml" relativeToChangelogFile="true" /> <include file="changelog-5.6.xml" relativeToChangelogFile="true" /> + <include file="changelog-5.7.xml" relativeToChangelogFile="true" /> </databaseChangeLog> diff --git a/src/main/java/org/traccar/api/resource/NotificationResource.java b/src/main/java/org/traccar/api/resource/NotificationResource.java index d58557601..7005fc083 100644 --- a/src/main/java/org/traccar/api/resource/NotificationResource.java +++ b/src/main/java/org/traccar/api/resource/NotificationResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,7 +80,7 @@ public class NotificationResource extends ExtendedObjectResource<Notification> { @POST @Path("test") - public Response testMessage() throws MessageException, InterruptedException, StorageException { + public Response testMessage() throws MessageException, StorageException { User user = permissionsService.getUser(getUserId()); for (Typed method : notificatorManager.getAllNotificatorTypes()) { notificatorManager.getNotificator(method.getType()).send(null, user, new Event("test", 0), null); @@ -91,7 +91,7 @@ public class NotificationResource extends ExtendedObjectResource<Notification> { @POST @Path("test/{notificator}") public Response testMessage(@PathParam("notificator") String notificator) - throws MessageException, InterruptedException, StorageException { + throws MessageException, StorageException { User user = permissionsService.getUser(getUserId()); notificatorManager.getNotificator(notificator).send(null, user, new Event("test", 0), null); return Response.noContent().build(); diff --git a/src/main/java/org/traccar/api/security/PermissionsService.java b/src/main/java/org/traccar/api/security/PermissionsService.java index 4421572d7..18a376601 100644 --- a/src/main/java/org/traccar/api/security/PermissionsService.java +++ b/src/main/java/org/traccar/api/security/PermissionsService.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.GroupedModel; import org.traccar.model.ManagedUser; +import org.traccar.model.Notification; import org.traccar.model.ScheduledModel; import org.traccar.model.Server; import org.traccar.model.User; @@ -129,7 +130,7 @@ public class PermissionsService { GroupedModel before = null; if (!addition) { before = storage.getObject(after.getClass(), new Request( - new Columns.Include("groupId"), new Condition.Equals("id", object.getId()))); + new Columns.Include("groupId"), new Condition.Equals("id", after.getId()))); } if (before == null || before.getGroupId() != after.getGroupId()) { checkPermission(Group.class, userId, after.getGroupId()); @@ -142,13 +143,26 @@ public class PermissionsService { ScheduledModel before = null; if (!addition) { before = storage.getObject(after.getClass(), new Request( - new Columns.Include("calendarId"), new Condition.Equals("id", object.getId()))); + new Columns.Include("calendarId"), new Condition.Equals("id", after.getId()))); } if (before == null || before.getCalendarId() != after.getCalendarId()) { checkPermission(Calendar.class, userId, after.getCalendarId()); } } } + if (object instanceof Notification) { + Notification after = ((Notification) object); + if (after.getCommandId() > 0) { + Notification before = null; + if (!addition) { + before = storage.getObject(after.getClass(), new Request( + new Columns.Include("commandId"), new Condition.Equals("id", after.getId()))); + } + if (before == null || before.getCommandId() != after.getCommandId()) { + checkPermission(Command.class, userId, after.getCommandId()); + } + } + } } } diff --git a/src/main/java/org/traccar/model/Notification.java b/src/main/java/org/traccar/model/Notification.java index 95e446132..b6a6e4cf5 100644 --- a/src/main/java/org/traccar/model/Notification.java +++ b/src/main/java/org/traccar/model/Notification.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,16 @@ public class Notification extends ScheduledModel { this.type = type; } + private long commandId; + + public long getCommandId() { + return commandId; + } + + public void setCommandId(long commandId) { + this.commandId = commandId; + } + private String notificators; public String getNotificators() { diff --git a/src/main/java/org/traccar/notificators/NotificatorCommand.java b/src/main/java/org/traccar/notificators/NotificatorCommand.java new file mode 100644 index 000000000..55cbe0a6a --- /dev/null +++ b/src/main/java/org/traccar/notificators/NotificatorCommand.java @@ -0,0 +1,62 @@ +/* + * Copyright 2023 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.notificators; + +import org.traccar.database.CommandsManager; +import org.traccar.model.Command; +import org.traccar.model.Event; +import org.traccar.model.Notification; +import org.traccar.model.Position; +import org.traccar.model.User; +import org.traccar.notification.MessageException; +import org.traccar.storage.Storage; +import org.traccar.storage.query.Columns; +import org.traccar.storage.query.Condition; +import org.traccar.storage.query.Request; + +import javax.inject.Inject; +import javax.inject.Singleton; + +@Singleton +public class NotificatorCommand implements Notificator { + + private final Storage storage; + private final CommandsManager commandsManager; + + @Inject + public NotificatorCommand(Storage storage, CommandsManager commandsManager) { + this.storage = storage; + this.commandsManager = commandsManager; + } + + @Override + public void send(Notification notification, User user, Event event, Position position) throws MessageException { + + if (notification == null || notification.getCommandId() <= 0) { + throw new MessageException("Saved command not provided"); + } + + try { + Command command = storage.getObject(Command.class, new Request( + new Columns.All(), new Condition.Equals("id", notification.getCommandId()))); + command.setDeviceId(event.getDeviceId()); + commandsManager.sendCommand(command); + } catch (Exception e) { + throw new MessageException(e); + } + } + +} |