Commit b8c4b3e9 authored by Manfred Kutas's avatar Manfred Kutas

ZP-1165 Remove device command honours --days-old parameter.

Released under the Affero GNU General Public License (AGPL) version 3.
parent 1a046a9d
...@@ -28,6 +28,11 @@ class ZPushAdmin { ...@@ -28,6 +28,11 @@ class ZPushAdmin {
* //TODO resync of a foldertype for all users (e.g. Appointment) * //TODO resync of a foldertype for all users (e.g. Appointment)
*/ */
const STATUS_SUCCESS = 0;
const STATUS_DEVICE_SYNCED_AFTER_DAYSOLD = 1;
public static $status = self::STATUS_SUCCESS;
/** /**
* List devices known to Z-Push. * List devices known to Z-Push.
* If no user is given, all devices are listed * If no user is given, all devices are listed
...@@ -219,11 +224,13 @@ class ZPushAdmin { ...@@ -219,11 +224,13 @@ class ZPushAdmin {
* *
* @param string $user (opt) user of the device * @param string $user (opt) user of the device
* @param string $devid (opt) device id which should be removed * @param string $devid (opt) device id which should be removed
* @param int $daysOld (opt) devices which haven't synced for $daysOld days
* @param int $time (opt) unix timestamp to use with $daysOld
* *
* @return boolean * @return boolean
* @access public * @access public
*/ */
static public function RemoveDevice($user = false, $devid = false) { static public function RemoveDevice($user = false, $devid = false, $daysOld = false, $time = false) {
if ($user === false && $devid === false) if ($user === false && $devid === false)
return false; return false;
...@@ -232,7 +239,7 @@ class ZPushAdmin { ...@@ -232,7 +239,7 @@ class ZPushAdmin {
$devicesIds = ZPush::GetStateMachine()->GetAllDevices($user); $devicesIds = ZPush::GetStateMachine()->GetAllDevices($user);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): all '%d' devices for user '%s' found to be removed", count($devicesIds), $user)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): all '%d' devices for user '%s' found to be removed", count($devicesIds), $user));
foreach ($devicesIds as $deviceid) { foreach ($devicesIds as $deviceid) {
if (!self::RemoveDevice($user, $deviceid)) { if (!self::RemoveDevice($user, $deviceid, $daysOld, $time)) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing devices failed for device '%s' of user '%s'. Aborting", $deviceid, $user)); ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing devices failed for device '%s' of user '%s'. Aborting", $deviceid, $user));
return false; return false;
} }
...@@ -243,7 +250,7 @@ class ZPushAdmin { ...@@ -243,7 +250,7 @@ class ZPushAdmin {
$users = self::ListUsers($devid); $users = self::ListUsers($devid);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): device '%d' is used by '%d' users and will be removed", $devid, count($users))); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): device '%d' is used by '%d' users and will be removed", $devid, count($users)));
foreach ($users as $aUser) { foreach ($users as $aUser) {
if (!self::RemoveDevice($aUser, $devid)) { if (!self::RemoveDevice($aUser, $devid, $daysOld, $time)) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing user '%s' from device '%s' failed. Aborting", $aUser, $devid)); ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing user '%s' from device '%s' failed. Aborting", $aUser, $devid));
return false; return false;
} }
...@@ -260,6 +267,21 @@ class ZPushAdmin { ...@@ -260,6 +267,21 @@ class ZPushAdmin {
$device->SetData($devicedata, false); $device->SetData($devicedata, false);
if (!isset($devicedata->devices)) if (!isset($devicedata->devices))
throw new StateInvalidException("No devicedata stored in ASDevice"); throw new StateInvalidException("No devicedata stored in ASDevice");
if ($daysOld) {
if (!$time) {
$time = time();
}
$lastSynced = floor(($time - $device->getLastupdatetime()) / 86400);
if ($daysOld > $lastSynced) {
ZLog::Write(LOGLEVEL_INFO,
sprintf("ZPushAdmin::RemoveDevice(): device '%s' of user '%s' synced %d day(s) ago but only devices which synced more than %d days ago will be removed. Skipping.",
$devid, $user, $lastSynced, $daysOld));
self::$status = self::STATUS_DEVICE_SYNCED_AFTER_DAYSOLD;
return true;
}
}
$devices = $devicedata->devices; $devices = $devicedata->devices;
} }
catch (StateNotFoundException $e) { catch (StateNotFoundException $e) {
......
...@@ -525,20 +525,24 @@ class ZPushAdminCLI { ...@@ -525,20 +525,24 @@ class ZPushAdminCLI {
} }
/** /**
* Command "Remove device" * Command "Remove device".
* Remove a device of that user from the device list * Removes a device of that user from the device list.
* *
* @return * @return
* @access public * @access public
*/ */
static public function CommandRemoveDevice() { static public function CommandRemoveDevice() {
$stat = ZPushAdmin::RemoveDevice(self::$user, self::$device); $stat = ZPushAdmin::RemoveDevice(self::$user, self::$device, self::$daysold, time());
if (self::$user === false) if (self::$user === false)
echo sprintf("State data of device '%s' removed: %s", self::$device, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n"; echo sprintf("State data of device '%s' removed: %s", self::$device, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n";
elseif (self::$device === false) elseif (self::$device === false)
echo sprintf("State data of all devices of user '%s' removed: %s", self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n"; echo sprintf("State data of all devices of user '%s' removed: %s", self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n";
else else
echo sprintf("State data of device '%s' of user '%s' removed: %s", self::$device, self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n"; echo sprintf("State data of device '%s' of user '%s' removed: %s", self::$device, self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n";
if (ZPushAdmin::$status == ZPushAdmin::STATUS_DEVICE_SYNCED_AFTER_DAYSOLD) {
print("Some devices might not have been removed because of --days-old parameter. Check Z-Push log file for more details.\n");
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment