aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-11-17 22:48:33 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2016-11-17 22:48:33 +1300
commitaf6c734879aa483efcc8150d5fdb704f40fb59cd (patch)
tree3d8e4c832f87954513b1dbd614f7a1f757a4fa99
parent512b95fd6975005c6460ddad16714502510cb16e (diff)
downloadtrackermap-server-af6c734879aa483efcc8150d5fdb704f40fb59cd.tar.gz
trackermap-server-af6c734879aa483efcc8150d5fdb704f40fb59cd.tar.bz2
trackermap-server-af6c734879aa483efcc8150d5fdb704f40fb59cd.zip
Add user expiration and device limit
-rw-r--r--schema/changelog-3.8.xml10
-rw-r--r--setup/default.xml8
-rw-r--r--src/org/traccar/api/SecurityRequestFilter.java2
-rw-r--r--src/org/traccar/api/resource/DeviceResource.java7
-rw-r--r--src/org/traccar/api/resource/SessionResource.java2
-rw-r--r--src/org/traccar/api/resource/UserResource.java11
-rw-r--r--src/org/traccar/database/PermissionsManager.java6
-rw-r--r--src/org/traccar/model/User.java40
8 files changed, 75 insertions, 11 deletions
diff --git a/schema/changelog-3.8.xml b/schema/changelog-3.8.xml
index 780a2d029..ba591f192 100644
--- a/schema/changelog-3.8.xml
+++ b/schema/changelog-3.8.xml
@@ -107,5 +107,15 @@
<column name="category" type="VARCHAR(128)" />
</addColumn>
+ <addColumn tableName="users">
+ <column name="disabled" type="BOOLEAN" defaultValueBoolean="false" />
+ </addColumn>
+ <addColumn tableName="users">
+ <column name="expirationtime" type="TIMESTAMP" />
+ </addColumn>
+ <addColumn tableName="users">
+ <column name="devicelimit" type="INT" defaultValueNumeric="0" />
+ </addColumn>
+
</changeSet>
</databaseChangeLog>
diff --git a/setup/default.xml b/setup/default.xml
index f50f3904f..a9d7684d4 100644
--- a/setup/default.xml
+++ b/setup/default.xml
@@ -62,8 +62,8 @@
</entry>
<entry key='database.insertUser'>
- INSERT INTO users (name, email, hashedPassword, salt, admin, map, distanceUnit, speedUnit, latitude, longitude, zoom, twelveHourFormat, attributes)
- VALUES (:name, :email, :hashedPassword, :salt, :admin, :map, :distanceUnit, :speedUnit, :latitude, :longitude, :zoom, :twelveHourFormat, :attributes)
+ INSERT INTO users (name, email, hashedPassword, salt, admin, map, distanceUnit, speedUnit, latitude, longitude, zoom, twelveHourFormat, coordinateFormat, disabled, expirationTime, deviceLimit, attributes)
+ VALUES (:name, :email, :hashedPassword, :salt, :admin, :map, :distanceUnit, :speedUnit, :latitude, :longitude, :zoom, :twelveHourFormat, :coordinateFormat, :disabled, :expirationTime, :deviceLimit, :attributes)
</entry>
<entry key='database.updateUser'>
@@ -78,6 +78,10 @@
longitude = :longitude,
zoom = :zoom,
twelveHourFormat = :twelveHourFormat,
+ coordinateFormat = :coordinateFormat,
+ disabled = :disabled,
+ expirationTime = :expirationTime,
+ deviceLimit = :deviceLimit,
attributes = :attributes
WHERE id = :id
</entry>
diff --git a/src/org/traccar/api/SecurityRequestFilter.java b/src/org/traccar/api/SecurityRequestFilter.java
index fd4c77525..598e4566d 100644
--- a/src/org/traccar/api/SecurityRequestFilter.java
+++ b/src/org/traccar/api/SecurityRequestFilter.java
@@ -67,7 +67,7 @@ public class SecurityRequestFilter implements ContainerRequestFilter {
try {
String[] auth = decodeBasicAuth(authHeader);
- User user = Context.getDataManager().login(auth[0], auth[1]);
+ User user = Context.getPermissionsManager().login(auth[0], auth[1]);
if (user != null) {
Context.getStatisticsManager().registerRequest(user.getId());
securityContext = new UserSecurityContext(new UserPrincipal(user.getId()));
diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java
index f20ed86ad..0b389ede1 100644
--- a/src/org/traccar/api/resource/DeviceResource.java
+++ b/src/org/traccar/api/resource/DeviceResource.java
@@ -58,6 +58,13 @@ public class DeviceResource extends BaseResource {
@POST
public Response add(Device entity) throws SQLException {
Context.getPermissionsManager().checkReadonly(getUserId());
+ int deviceLimit = Context.getPermissionsManager().getUser(getUserId()).getDeviceLimit();
+ if (deviceLimit != 0) {
+ int deviceCount = Context.getPermissionsManager().getDevicePermissions(getUserId()).size();
+ if (deviceCount >= deviceLimit) {
+ throw new SecurityException("User device limit reached");
+ }
+ }
Context.getDeviceManager().addDevice(entity);
Context.getDataManager().linkDevice(getUserId(), entity.getId());
Context.getPermissionsManager().refreshPermissions();
diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java
index 2847e41c1..a459186fe 100644
--- a/src/org/traccar/api/resource/SessionResource.java
+++ b/src/org/traccar/api/resource/SessionResource.java
@@ -64,7 +64,7 @@ public class SessionResource extends BaseResource {
}
}
if (email != null && password != null) {
- User user = Context.getDataManager().login(email, password);
+ User user = Context.getPermissionsManager().login(email, password);
if (user != null) {
userId = user.getId();
request.getSession().setAttribute(USER_ID_KEY, userId);
diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java
index f0a8597cc..c6e84c918 100644
--- a/src/org/traccar/api/resource/UserResource.java
+++ b/src/org/traccar/api/resource/UserResource.java
@@ -60,13 +60,16 @@ public class UserResource extends BaseResource {
@Path("{id}")
@PUT
public Response update(@PathParam("id") long id, User entity) throws SQLException {
- if (entity.getAdmin()) {
+ User old = Context.getPermissionsManager().getUser(entity.getId());
+ if (old.getExpirationTime() == null && entity.getExpirationTime() != null
+ || old.getExpirationTime() != null && !old.getExpirationTime().equals(entity.getExpirationTime())
+ || old.getAdmin() != entity.getAdmin()
+ || old.getReadonly() != entity.getReadonly()
+ || old.getDisabled() != entity.getDisabled()
+ || old.getDeviceLimit() != entity.getDeviceLimit()) {
Context.getPermissionsManager().checkAdmin(getUserId());
} else {
Context.getPermissionsManager().checkUser(getUserId(), entity.getId());
- if (!entity.getReadonly()) {
- Context.getPermissionsManager().checkReadonly(entity.getId());
- }
}
Context.getPermissionsManager().updateUser(entity);
if (Context.getNotificationManager() != null) {
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index b0ae504d0..e620d5334 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -217,11 +217,11 @@ public class PermissionsManager {
public User login(String email, String password) throws SQLException {
User user = dataManager.login(email, password);
- if (user != null && users.get(user.getId()) != null) {
+ if (user != null && !user.getDisabled() && (user.getExpirationTime() == null
+ || user.getExpirationTime().getTime() > System.currentTimeMillis())) {
return users.get(user.getId());
- } else {
- return null;
}
+ return null;
}
}
diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java
index aa0b33713..a36fa0e31 100644
--- a/src/org/traccar/model/User.java
+++ b/src/org/traccar/model/User.java
@@ -18,6 +18,8 @@ package org.traccar.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.traccar.helper.Hashing;
+import java.util.Date;
+
public class User extends Extensible {
private String name;
@@ -140,6 +142,44 @@ public class User extends Extensible {
this.coordinateFormat = coordinateFormat;
}
+ private boolean disabled;
+
+ public boolean getDisabled() {
+ return disabled;
+ }
+
+ public void setDisabled(boolean disabled) {
+ this.disabled = disabled;
+ }
+
+ private Date expirationTime;
+
+ public Date getExpirationTime() {
+ if (expirationTime != null) {
+ return new Date(expirationTime.getTime());
+ } else {
+ return null;
+ }
+ }
+
+ public void setExpirationTime(Date expirationTime) {
+ if (expirationTime != null) {
+ this.expirationTime = new Date(expirationTime.getTime());
+ } else {
+ this.expirationTime = null;
+ }
+ }
+
+ private int deviceLimit;
+
+ public int getDeviceLimit() {
+ return deviceLimit;
+ }
+
+ public void setDeviceLimit(int deviceLimit) {
+ this.deviceLimit = deviceLimit;
+ }
+
public String getPassword() {
return null;
}