Search in sources :

Example 31 with User

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

the class SessionResource method get.

@PermitAll
@GET
public User get(@QueryParam("token") String token) throws StorageException, UnsupportedEncodingException {
    if (token != null) {
        User user = Context.getUsersManager().getUserByToken(token);
        if (user != null) {
            Context.getPermissionsManager().checkUserEnabled(user.getId());
            request.getSession().setAttribute(USER_ID_KEY, user.getId());
            return user;
        }
    }
    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) {
                Context.getPermissionsManager().checkUserEnabled(user.getId());
                request.getSession().setAttribute(USER_ID_KEY, user.getId());
                return user;
            }
        }
    } else {
        Context.getPermissionsManager().checkUserEnabled(userId);
        return Context.getPermissionsManager().getUser(userId);
    }
    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)

Example 32 with User

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

the class PasswordResource method update.

@Path("update")
@PermitAll
@POST
public Response update(@FormParam("token") String token, @FormParam("password") String password) throws StorageException {
    for (long userId : Context.getUsersManager().getAllItems()) {
        User user = Context.getUsersManager().getById(userId);
        if (token.equals(user.getString(PASSWORD_RESET_TOKEN))) {
            user.getAttributes().remove(PASSWORD_RESET_TOKEN);
            user.setPassword(password);
            Context.getUsersManager().updateItem(user);
            return Response.ok().build();
        }
    }
    return Response.status(Response.Status.NOT_FOUND).build();
}
Also used : User(org.traccar.model.User) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) PermitAll(javax.annotation.security.PermitAll)

Example 33 with User

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

the class PasswordResource method reset.

@Path("reset")
@PermitAll
@POST
public Response reset(@FormParam("email") String email) throws StorageException, MessagingException {
    for (long userId : Context.getUsersManager().getAllItems()) {
        User user = Context.getUsersManager().getById(userId);
        if (email.equals(user.getEmail())) {
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            user.set(PASSWORD_RESET_TOKEN, token);
            Context.getUsersManager().updateItem(user);
            VelocityContext velocityContext = TextTemplateFormatter.prepareContext(null);
            velocityContext.put("token", token);
            NotificationMessage fullMessage = TextTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full");
            Context.getMailManager().sendMessage(userId, fullMessage.getSubject(), fullMessage.getBody());
            break;
        }
    }
    return Response.ok().build();
}
Also used : User(org.traccar.model.User) NotificationMessage(org.traccar.notification.NotificationMessage) VelocityContext(org.apache.velocity.VelocityContext) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) 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