use of org.traccar.model.User in project traccar by traccar.
the class UserResource method add.
@Override
@PermitAll
@POST
public Response add(User entity) throws SQLException {
if (!Context.getPermissionsManager().getUserAdmin(getUserId())) {
Context.getPermissionsManager().checkUserUpdate(getUserId(), new User(), entity);
if (Context.getPermissionsManager().getUserManager(getUserId())) {
Context.getPermissionsManager().checkUserLimit(getUserId());
} else {
Context.getPermissionsManager().checkRegistration(getUserId());
entity.setDeviceLimit(Context.getConfig().getInteger("users.defaultDeviceLimit", -1));
int expirationDays = Context.getConfig().getInteger("users.defaultExpirationDays");
if (expirationDays > 0) {
entity.setExpirationTime(new Date(System.currentTimeMillis() + (long) expirationDays * 24 * 3600 * 1000));
}
}
}
Context.getUsersManager().addItem(entity);
LogAction.create(getUserId(), entity);
if (Context.getPermissionsManager().getUserManager(getUserId())) {
Context.getDataManager().linkObject(User.class, getUserId(), ManagedUser.class, entity.getId(), true);
LogAction.link(getUserId(), User.class, getUserId(), ManagedUser.class, entity.getId());
}
Context.getUsersManager().refreshUserItems();
return Response.ok(entity).build();
}
use of org.traccar.model.User in project traccar by traccar.
the class SecurityRequestFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) {
if (requestContext.getMethod().equals("OPTIONS")) {
return;
}
SecurityContext securityContext = null;
try {
String authHeader = requestContext.getHeaderString(AUTHORIZATION_HEADER);
if (authHeader != null) {
try {
String[] auth = decodeBasicAuth(authHeader);
User user = Context.getPermissionsManager().login(auth[0], auth[1]);
if (user != null) {
Context.getStatisticsManager().registerRequest(user.getId());
securityContext = new UserSecurityContext(new UserPrincipal(user.getId()));
}
} catch (SQLException e) {
throw new WebApplicationException(e);
}
} else if (request.getSession() != null) {
Long userId = (Long) request.getSession().getAttribute(SessionResource.USER_ID_KEY);
if (userId != null) {
Context.getPermissionsManager().checkUserEnabled(userId);
Context.getStatisticsManager().registerRequest(userId);
securityContext = new UserSecurityContext(new UserPrincipal(userId));
}
}
} catch (SecurityException e) {
Log.warning(e);
}
if (securityContext != null) {
requestContext.setSecurityContext(securityContext);
} else {
Method method = resourceInfo.getResourceMethod();
if (!method.isAnnotationPresent(PermitAll.class)) {
Response.ResponseBuilder responseBuilder = Response.status(Response.Status.UNAUTHORIZED);
if (!XML_HTTP_REQUEST.equals(request.getHeader(X_REQUESTED_WITH))) {
responseBuilder.header(WWW_AUTHENTICATE, BASIC_REALM);
}
throw new WebApplicationException(responseBuilder.build());
}
}
}
use of org.traccar.model.User in project traccar by traccar.
the class UsersManager method updateCachedItem.
@Override
protected void updateCachedItem(User user) {
User cachedUser = getById(user.getId());
super.updateCachedItem(user);
putToken(user);
if (cachedUser.getToken() != null && !cachedUser.getToken().equals(user.getToken())) {
usersTokens.remove(cachedUser.getToken());
}
}
use of org.traccar.model.User in project traccar by traccar.
the class NotificationMail method sendMailSync.
public static void sendMailSync(long userId, Event event, Position position) throws MessagingException {
User user = Context.getPermissionsManager().getUser(userId);
Properties properties = null;
if (!Context.getConfig().getBoolean("mail.smtp.ignoreUserConfig")) {
properties = getProperties(new PropertiesProvider(user));
}
if (properties == null || !properties.containsKey("mail.smtp.host")) {
properties = getProperties(new PropertiesProvider(Context.getConfig()));
}
if (!properties.containsKey("mail.smtp.host")) {
Log.warning("No SMTP configuration found");
return;
}
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
String from = properties.getProperty("mail.smtp.from");
if (from != null) {
message.setFrom(new InternetAddress(from));
}
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position);
message.setSubject(mailMessage.getSubject());
message.setSentDate(new Date());
message.setContent(mailMessage.getBody(), "text/html; charset=utf-8");
Transport transport = session.getTransport();
try {
Context.getStatisticsManager().registerMail();
transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.username"), properties.getProperty("mail.smtp.password"));
transport.sendMessage(message, message.getAllRecipients());
} finally {
transport.close();
}
}
use of org.traccar.model.User in project traccar by traccar.
the class NotificationFormatter method prepareContext.
public static VelocityContext prepareContext(long userId, Event event, Position position) {
User user = Context.getPermissionsManager().getUser(userId);
Device device = Context.getIdentityManager().getById(event.getDeviceId());
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("user", user);
velocityContext.put("device", device);
velocityContext.put("event", event);
if (position != null) {
velocityContext.put("position", position);
velocityContext.put("speedUnit", ReportUtils.getSpeedUnit(userId));
velocityContext.put("distanceUnit", ReportUtils.getDistanceUnit(userId));
velocityContext.put("volumeUnit", ReportUtils.getVolumeUnit(userId));
}
if (event.getGeofenceId() != 0) {
velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId()));
}
String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID);
if (driverUniqueId != null) {
velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId));
}
velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url"));
velocityContext.put("dateTool", new DateTool());
velocityContext.put("numberTool", new NumberTool());
velocityContext.put("timezone", ReportUtils.getTimezone(userId));
velocityContext.put("locale", Locale.getDefault());
return velocityContext;
}
Aggregations