aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-08-02 21:01:06 -0700
committerAnton Tananaev <anton@traccar.org>2022-08-02 21:01:06 -0700
commitbb7bdcfc3389b6822b7680837386e3650962f30a (patch)
tree624eb4d9d8dcb6fff2169db85213387cf91ee26b /src/main
parentbd55a835340547aabc3f401bb97e181a3e70df8f (diff)
downloadtrackermap-server-bb7bdcfc3389b6822b7680837386e3650962f30a.tar.gz
trackermap-server-bb7bdcfc3389b6822b7680837386e3650962f30a.tar.bz2
trackermap-server-bb7bdcfc3389b6822b7680837386e3650962f30a.zip
Notifications unsubscribe option
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/traccar/api/resource/PasswordResource.java2
-rw-r--r--src/main/java/org/traccar/api/signature/TokenManager.java4
-rw-r--r--src/main/java/org/traccar/notification/TextTemplateFormatter.java13
3 files changed, 17 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/api/resource/PasswordResource.java b/src/main/java/org/traccar/api/resource/PasswordResource.java
index 1fb08b02a..625ff4cb1 100644
--- a/src/main/java/org/traccar/api/resource/PasswordResource.java
+++ b/src/main/java/org/traccar/api/resource/PasswordResource.java
@@ -62,7 +62,7 @@ public class PasswordResource extends BaseResource {
new Columns.All(), new Condition.Equals("email", "email", email)));
if (user != null) {
var velocityContext = textTemplateFormatter.prepareContext(permissionsService.getServer(), user);
- velocityContext.put("token", tokenManager.generateToken(user.getId(), null));
+ velocityContext.put("token", tokenManager.generateToken(user.getId()));
var fullMessage = textTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full");
mailManager.sendMessage(user, fullMessage.getSubject(), fullMessage.getBody());
}
diff --git a/src/main/java/org/traccar/api/signature/TokenManager.java b/src/main/java/org/traccar/api/signature/TokenManager.java
index a51234a95..a352ecc10 100644
--- a/src/main/java/org/traccar/api/signature/TokenManager.java
+++ b/src/main/java/org/traccar/api/signature/TokenManager.java
@@ -46,6 +46,10 @@ public class TokenManager {
this.cryptoManager = cryptoManager;
}
+ public String generateToken(long userId) throws IOException, GeneralSecurityException, StorageException {
+ return generateToken(userId, null);
+ }
+
public String generateToken(
long userId, Date expiration) throws IOException, GeneralSecurityException, StorageException {
Data data = new Data();
diff --git a/src/main/java/org/traccar/notification/TextTemplateFormatter.java b/src/main/java/org/traccar/notification/TextTemplateFormatter.java
index bca18f53c..be894af96 100644
--- a/src/main/java/org/traccar/notification/TextTemplateFormatter.java
+++ b/src/main/java/org/traccar/notification/TextTemplateFormatter.java
@@ -23,14 +23,18 @@ import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.NumberTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.traccar.api.signature.TokenManager;
import org.traccar.helper.model.UserUtil;
import org.traccar.model.Server;
import org.traccar.model.User;
+import org.traccar.storage.StorageException;
import javax.inject.Inject;
+import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
+import java.security.GeneralSecurityException;
import java.util.Locale;
public class TextTemplateFormatter {
@@ -38,10 +42,12 @@ public class TextTemplateFormatter {
private static final Logger LOGGER = LoggerFactory.getLogger(TextTemplateFormatter.class);
private final VelocityEngine velocityEngine;
+ private final TokenManager tokenManager;
@Inject
- public TextTemplateFormatter(VelocityEngine velocityEngine) {
+ public TextTemplateFormatter(VelocityEngine velocityEngine, TokenManager tokenManager) {
this.velocityEngine = velocityEngine;
+ this.tokenManager = tokenManager;
}
public VelocityContext prepareContext(Server server, User user) {
@@ -51,6 +57,11 @@ public class TextTemplateFormatter {
if (user != null) {
velocityContext.put("user", user);
velocityContext.put("timezone", UserUtil.getTimezone(server, user));
+ try {
+ velocityContext.put("token", tokenManager.generateToken(user.getId()));
+ } catch (IOException | GeneralSecurityException | StorageException e) {
+ LOGGER.warn("Token generation failed", e);
+ }
}
velocityContext.put("webUrl", velocityEngine.getProperty("web.url"));