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());
}
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();
}
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();
}
Aggregations