Search in sources :

Example 16 with User

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

the class NotificatorFirebase method sendSync.

@Override
public void sendSync(long userId, Event event, Position position) {
    final User user = Context.getPermissionsManager().getUser(userId);
    if (user.getAttributes().containsKey("notificationTokens")) {
        NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
        Notification notification = new Notification();
        notification.title = shortMessage.getSubject();
        notification.body = shortMessage.getBody();
        notification.sound = "default";
        Message message = new Message();
        message.tokens = user.getString("notificationTokens").split("[, ]");
        message.notification = notification;
        Context.getClient().target(url).request().header("Authorization", "key=" + key).async().post(Entity.json(message), new InvocationCallback<Object>() {

            @Override
            public void completed(Object o) {
            }

            @Override
            public void failed(Throwable throwable) {
                LOGGER.warn("Firebase notification error", throwable);
            }
        });
    }
}
Also used : User(org.traccar.model.User) NotificationMessage(org.traccar.notification.NotificationMessage) NotificationMessage(org.traccar.notification.NotificationMessage)

Example 17 with User

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

the class NotificatorSms method sendAsync.

@Override
public void sendAsync(long userId, Event event, Position position) {
    final User user = Context.getPermissionsManager().getUser(userId);
    if (user.getPhone() != null) {
        NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
        Main.getInjector().getInstance(StatisticsManager.class).registerSms();
        Context.getSmsManager().sendMessageAsync(user.getPhone(), shortMessage.getBody(), false);
    }
}
Also used : User(org.traccar.model.User) StatisticsManager(org.traccar.database.StatisticsManager) NotificationMessage(org.traccar.notification.NotificationMessage)

Example 18 with User

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

the class NotificatorPushover method sendSync.

@Override
public void sendSync(long userId, Event event, Position position) {
    final User user = Context.getPermissionsManager().getUser(userId);
    String device = "";
    if (user.getAttributes().containsKey("notificator.pushover.device")) {
        device = user.getString("notificator.pushover.device").replaceAll(" *, *", ",");
    }
    if (token == null) {
        LOGGER.warn("Pushover token not found");
        return;
    }
    if (this.user == null) {
        LOGGER.warn("Pushover user not found");
        return;
    }
    NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
    Message message = new Message();
    message.token = token;
    message.user = this.user;
    message.device = device;
    message.title = shortMessage.getSubject();
    message.message = shortMessage.getBody();
    Context.getClient().target(url).request().async().post(Entity.json(message), new InvocationCallback<Object>() {

        @Override
        public void completed(Object o) {
        }

        @Override
        public void failed(Throwable throwable) {
            LOGGER.warn("Pushover API error", throwable);
        }
    });
}
Also used : User(org.traccar.model.User) NotificationMessage(org.traccar.notification.NotificationMessage) NotificationMessage(org.traccar.notification.NotificationMessage)

Example 19 with User

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

the class NotificationSms method sendSmsSync.

public static void sendSmsSync(long userId, Event event, Position position) throws RecoverablePduException, UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException {
    User user = Context.getPermissionsManager().getUser(userId);
    if (Context.getSmppManager() != null && user.getPhone() != null) {
        Context.getStatisticsManager().registerSms();
        Context.getSmppManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false);
    }
}
Also used : User(org.traccar.model.User)

Example 20 with User

use of org.traccar.model.User in project traccar by traccar.

the class SessionResource method get.

@PermitAll
@GET
public User get(@QueryParam("token") String token) throws SQLException, UnsupportedEncodingException {
    Long userId = (Long) request.getSession().getAttribute(USER_ID_KEY);
    if (userId == null) {
        Cookie[] cookies = request.getCookies();
        String email = null, password = null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(USER_COOKIE_KEY)) {
                    byte[] emailBytes = DataConverter.parseBase64(URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name()));
                    email = new String(emailBytes, StandardCharsets.UTF_8);
                } else if (cookie.getName().equals(PASS_COOKIE_KEY)) {
                    byte[] passwordBytes = DataConverter.parseBase64(URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name()));
                    password = new String(passwordBytes, StandardCharsets.UTF_8);
                }
            }
        }
        if (email != null && password != null) {
            User user = Context.getPermissionsManager().login(email, password);
            if (user != null) {
                userId = user.getId();
                request.getSession().setAttribute(USER_ID_KEY, userId);
            }
        } else if (token != null) {
            User user = Context.getUsersManager().getUserByToken(token);
            if (user != null) {
                userId = user.getId();
                request.getSession().setAttribute(USER_ID_KEY, userId);
            }
        }
    }
    if (userId != null) {
        Context.getPermissionsManager().checkUserEnabled(userId);
        return Context.getPermissionsManager().getUser(userId);
    } else {
        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
    }
}
Also used : Cookie(javax.servlet.http.Cookie) User(org.traccar.model.User) WebApplicationException(javax.ws.rs.WebApplicationException) GET(javax.ws.rs.GET) PermitAll(javax.annotation.security.PermitAll)

Aggregations

User (org.traccar.model.User)33 PermitAll (javax.annotation.security.PermitAll)11 WebApplicationException (javax.ws.rs.WebApplicationException)7 POST (javax.ws.rs.POST)6 NotificationMessage (org.traccar.notification.NotificationMessage)6 Date (java.util.Date)5 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 Session (javax.mail.Session)3 Transport (javax.mail.Transport)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeMessage (javax.mail.internet.MimeMessage)3 Response (javax.ws.rs.core.Response)3 SecurityContext (javax.ws.rs.core.SecurityContext)3 VelocityContext (org.apache.velocity.VelocityContext)3 StatisticsManager (org.traccar.database.StatisticsManager)3 ManagedUser (org.traccar.model.ManagedUser)3 SQLException (java.sql.SQLException)2 NamingException (javax.naming.NamingException)2 Attribute (javax.naming.directory.Attribute)2