diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-12-19 20:41:09 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-19 20:41:09 +1300 |
commit | f9b786398acee9f73e8591f7b0967be280524045 (patch) | |
tree | 10d544c07400ba89741a275bc1a26182689bad07 | |
parent | 8162c8ea169a39d247747e1731fe713102e6f3cc (diff) | |
parent | 7858975ecb7d78106ad6dec829b838073d0db098 (diff) | |
download | trackermap-server-f9b786398acee9f73e8591f7b0967be280524045.tar.gz trackermap-server-f9b786398acee9f73e8591f7b0967be280524045.tar.bz2 trackermap-server-f9b786398acee9f73e8591f7b0967be280524045.zip |
Merge pull request #3685 from Abyss777/log_actions
Log user actions
-rw-r--r-- | src/org/traccar/api/BaseObjectResource.java | 5 | ||||
-rw-r--r-- | src/org/traccar/api/resource/DeviceResource.java | 2 | ||||
-rw-r--r-- | src/org/traccar/api/resource/PermissionsResource.java | 5 | ||||
-rw-r--r-- | src/org/traccar/api/resource/ServerResource.java | 2 | ||||
-rw-r--r-- | src/org/traccar/api/resource/SessionResource.java | 3 | ||||
-rw-r--r-- | src/org/traccar/api/resource/UserResource.java | 3 | ||||
-rw-r--r-- | src/org/traccar/helper/LogAction.java | 96 |
7 files changed, 116 insertions, 0 deletions
diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java index 634957a49..806c78624 100644 --- a/src/org/traccar/api/BaseObjectResource.java +++ b/src/org/traccar/api/BaseObjectResource.java @@ -31,6 +31,7 @@ import org.traccar.database.BaseObjectManager; 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; @@ -80,8 +81,10 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour 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(); @@ -107,6 +110,7 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour 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(); @@ -128,6 +132,7 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour BaseObjectManager<T> manager = Context.getManager(baseClass); manager.removeItem(id); + LogAction.remove(getUserId(), baseClass, id); if (manager instanceof SimpleObjectManager) { ((SimpleObjectManager<T>) manager).refreshUserItems(); diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 1fae92dc7..fda96a09d 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; 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; @@ -85,6 +86,7 @@ public class DeviceResource extends BaseObjectResource<Device> { public Response updateTotalDistance(DeviceTotalDistance entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDeviceManager().resetTotalDistance(entity); + LogAction.resetTotalDistance(getUserId(), entity.getDeviceId()); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/PermissionsResource.java b/src/org/traccar/api/resource/PermissionsResource.java index 9b9f65ad1..b89d9d376 100644 --- a/src/org/traccar/api/resource/PermissionsResource.java +++ b/src/org/traccar/api/resource/PermissionsResource.java @@ -29,6 +29,7 @@ import javax.ws.rs.core.Response; 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; @@ -61,6 +62,8 @@ public class PermissionsResource extends BaseResource { 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(); } @@ -72,6 +75,8 @@ public class PermissionsResource extends BaseResource { 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(); } diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java index c0914995b..61d3221f0 100644 --- a/src/org/traccar/api/resource/ServerResource.java +++ b/src/org/traccar/api/resource/ServerResource.java @@ -17,6 +17,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.helper.LogAction; import org.traccar.model.Server; import javax.annotation.security.PermitAll; @@ -45,6 +46,7 @@ public class ServerResource extends BaseResource { public Response update(Server entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getPermissionsManager().updateServer(entity); + LogAction.edit(getUserId(), entity); return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java index fa2a14c6f..3f7842626 100644 --- a/src/org/traccar/api/resource/SessionResource.java +++ b/src/org/traccar/api/resource/SessionResource.java @@ -17,6 +17,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.helper.LogAction; import org.traccar.model.User; import javax.annotation.security.PermitAll; @@ -103,6 +104,7 @@ public class SessionResource extends BaseResource { 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()); @@ -111,6 +113,7 @@ public class SessionResource extends BaseResource { @DELETE public Response remove() { + LogAction.logout(getUserId()); request.getSession().removeAttribute(USER_ID_KEY); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 0f6f6edba..0b42d8d92 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; 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; @@ -81,8 +82,10 @@ public class UserResource extends BaseObjectResource<User> { } } 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(); diff --git a/src/org/traccar/helper/LogAction.java b/src/org/traccar/helper/LogAction.java new file mode 100644 index 000000000..d2c7ef5c1 --- /dev/null +++ b/src/org/traccar/helper/LogAction.java @@ -0,0 +1,96 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * 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); + } + +} |