Skip to content

Commit

Permalink
Merge pull request traccar#3685 from Abyss777/log_actions
Browse files Browse the repository at this point in the history
Log user actions
  • Loading branch information
tananaev authored Dec 19, 2017
2 parents 8162c8e + 7858975 commit f9b7863
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/org/traccar/api/BaseObjectResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.traccar.database.ExtendedObjectManager;
import org.traccar.database.ManagableObjects;
import org.traccar.database.SimpleObjectManager;
import org.traccar.helper.LogAction;
import org.traccar.model.BaseModel;
import org.traccar.model.Command;
import org.traccar.model.Device;
Expand Down Expand Up @@ -80,8 +81,10 @@ public Response add(T entity) throws SQLException {

BaseObjectManager<T> manager = Context.getManager(baseClass);
manager.addItem(entity);
LogAction.create(getUserId(), entity);

Context.getDataManager().linkObject(User.class, getUserId(), baseClass, entity.getId(), true);
LogAction.link(getUserId(), User.class, getUserId(), baseClass, entity.getId());

if (manager instanceof SimpleObjectManager) {
((SimpleObjectManager<T>) manager).refreshUserItems();
Expand All @@ -107,6 +110,7 @@ public Response update(T entity) throws SQLException {
Context.getPermissionsManager().checkPermission(baseClass, getUserId(), entity.getId());

Context.getManager(baseClass).updateItem(entity);
LogAction.edit(getUserId(), entity);

if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
Expand All @@ -128,6 +132,7 @@ public Response remove(@PathParam("id") long id) throws SQLException {

BaseObjectManager<T> manager = Context.getManager(baseClass);
manager.removeItem(id);
LogAction.remove(getUserId(), baseClass, id);

if (manager instanceof SimpleObjectManager) {
((SimpleObjectManager<T>) manager).refreshUserItems();
Expand Down
2 changes: 2 additions & 0 deletions src/org/traccar/api/resource/DeviceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.traccar.Context;
import org.traccar.api.BaseObjectResource;
import org.traccar.database.DeviceManager;
import org.traccar.helper.LogAction;
import org.traccar.model.Device;
import org.traccar.model.DeviceTotalDistance;

Expand Down Expand Up @@ -85,6 +86,7 @@ public Collection<Device> get(
public Response updateTotalDistance(DeviceTotalDistance entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDeviceManager().resetTotalDistance(entity);
LogAction.resetTotalDistance(getUserId(), entity.getDeviceId());
return Response.noContent().build();
}

Expand Down
5 changes: 5 additions & 0 deletions src/org/traccar/api/resource/PermissionsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.traccar.Context;
import org.traccar.api.BaseResource;
import org.traccar.helper.LogAction;
import org.traccar.model.Device;
import org.traccar.model.Permission;
import org.traccar.model.User;
Expand Down Expand Up @@ -61,6 +62,8 @@ public Response add(LinkedHashMap<String, Long> entity) throws SQLException, Cla
checkPermission(permission, true);
Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(),
permission.getPropertyClass(), permission.getPropertyId(), true);
LogAction.link(getUserId(), permission.getOwnerClass(), permission.getOwnerId(),
permission.getPropertyClass(), permission.getPropertyId());
Context.getPermissionsManager().refreshPermissions(permission);
return Response.noContent().build();
}
Expand All @@ -72,6 +75,8 @@ public Response remove(LinkedHashMap<String, Long> entity) throws SQLException,
checkPermission(permission, false);
Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(),
permission.getPropertyClass(), permission.getPropertyId(), false);
LogAction.unlink(getUserId(), permission.getOwnerClass(), permission.getOwnerId(),
permission.getPropertyClass(), permission.getPropertyId());
Context.getPermissionsManager().refreshPermissions(permission);
return Response.noContent().build();
}
Expand Down
2 changes: 2 additions & 0 deletions src/org/traccar/api/resource/ServerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.traccar.Context;
import org.traccar.api.BaseResource;
import org.traccar.helper.LogAction;
import org.traccar.model.Server;

import javax.annotation.security.PermitAll;
Expand Down Expand Up @@ -45,6 +46,7 @@ public Server get() throws SQLException {
public Response update(Server entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getPermissionsManager().updateServer(entity);
LogAction.edit(getUserId(), entity);
return Response.ok(entity).build();
}

Expand Down
3 changes: 3 additions & 0 deletions src/org/traccar/api/resource/SessionResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.traccar.Context;
import org.traccar.api.BaseResource;
import org.traccar.helper.LogAction;
import org.traccar.model.User;

import javax.annotation.security.PermitAll;
Expand Down Expand Up @@ -103,6 +104,7 @@ public User add(
User user = Context.getPermissionsManager().login(email, password);
if (user != null) {
request.getSession().setAttribute(USER_ID_KEY, user.getId());
LogAction.login(user.getId());
return user;
} else {
throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).build());
Expand All @@ -111,6 +113,7 @@ public User add(

@DELETE
public Response remove() {
LogAction.logout(getUserId());
request.getSession().removeAttribute(USER_ID_KEY);
return Response.noContent().build();
}
Expand Down
3 changes: 3 additions & 0 deletions src/org/traccar/api/resource/UserResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.traccar.Context;
import org.traccar.api.BaseObjectResource;
import org.traccar.database.UsersManager;
import org.traccar.helper.LogAction;
import org.traccar.model.ManagedUser;
import org.traccar.model.User;

Expand Down Expand Up @@ -81,8 +82,10 @@ public Response add(User entity) throws SQLException {
}
}
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();
Expand Down
96 changes: 96 additions & 0 deletions src/org/traccar/helper/LogAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2017 Anton Tananaev ([email protected])
* Copyright 2017 Andrey Kunitsyn ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.helper;

import java.beans.Introspector;

import org.traccar.model.BaseModel;

public final class LogAction {

private LogAction() {
}

private static final String ACTION_CREATE = "create";
private static final String ACTION_EDIT = "edit";
private static final String ACTION_REMOVE = "remove";

private static final String ACTION_LINK = "link";
private static final String ACTION_UNLINK = "unlink";

private static final String ACTION_LOGIN = "login";
private static final String ACTION_LOGOUT = "logout";

private static final String ACTION_TOTAL_DISTANCE = "resetTotalDistance";

private static final String PATTERN_OBJECT = "user: %d, action: %s, object: %s, id: %d";
private static final String PATTERN_LINK = "user: %d, action: %s, owner: %s, id: %d, property: %s, id: %d";
private static final String PATTERN_LOGIN = "user: %d, action: %s";
private static final String PATTERN_TOTAL_DISTANCE = "user: %d, action: %s, deviceId: %d";

public static void create(long userId, BaseModel object) {
logObjectAction(ACTION_CREATE, userId, object.getClass(), object.getId());
}

public static void edit(long userId, BaseModel object) {
logObjectAction(ACTION_EDIT, userId, object.getClass(), object.getId());
}

public static void remove(long userId, Class<?> clazz, long objectId) {
logObjectAction(ACTION_REMOVE, userId, clazz, objectId);
}

public static void link(long userId, Class<?> owner, long ownerId, Class<?> property, long propertyId) {
logLinkAction(ACTION_LINK, userId, owner, ownerId, property, propertyId);
}

public static void unlink(long userId, Class<?> owner, long ownerId, Class<?> property, long propertyId) {
logLinkAction(ACTION_UNLINK, userId, owner, ownerId, property, propertyId);
}

public static void login(long userId) {
logLoginAction(ACTION_LOGIN, userId);
}

public static void logout(long userId) {
logLoginAction(ACTION_LOGOUT, userId);
}

public static void resetTotalDistance(long userId, long deviceId) {
log(String.format(PATTERN_TOTAL_DISTANCE, userId, ACTION_TOTAL_DISTANCE, deviceId));
}

private static void logObjectAction(String action, long userId, Class<?> clazz, long objectId) {
log(String.format(PATTERN_OBJECT, userId, action, Introspector.decapitalize(clazz.getSimpleName()), objectId));
}

private static void logLinkAction(String action, long userId,
Class<?> owner, long ownerId, Class<?> property, long propertyId) {
log(String.format(PATTERN_LINK, userId, action,
Introspector.decapitalize(owner.getSimpleName()), ownerId,
Introspector.decapitalize(property.getSimpleName()), propertyId));
}

private static void logLoginAction(String action, long userId) {
log(String.format(PATTERN_LOGIN, userId, action));
}

private static void log(String msg) {
Log.info(msg);
}

}

0 comments on commit f9b7863

Please sign in to comment.