aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/lib/util.php
diff options
context:
space:
mode:
authorBjoern Schiessle <schiessle@owncloud.com>2014-05-06 19:20:49 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2014-06-05 16:54:04 +0200
commiteb29b2984cf81f25df42d59ce1d5c9a9b307763c (patch)
tree7277d43f58f92aa3fd58bb863bf4bbf344f4a71b /apps/files_encryption/lib/util.php
parent2d83424a29e4dbaeb16856c87378a753b10cdb90 (diff)
downloadnextcloud-server-eb29b2984cf81f25df42d59ce1d5c9a9b307763c.tar.gz
nextcloud-server-eb29b2984cf81f25df42d59ce1d5c9a9b307763c.zip
use oc_preferences instead of oc_encryption to store encyption settings
Diffstat (limited to 'apps/files_encryption/lib/util.php')
-rw-r--r--apps/files_encryption/lib/util.php139
1 files changed, 17 insertions, 122 deletions
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php
index 434d23f4a5a..ea2b1b10d63 100644
--- a/apps/files_encryption/lib/util.php
+++ b/apps/files_encryption/lib/util.php
@@ -194,22 +194,6 @@ class Util {
}
}
- // If there's no record for this user's encryption preferences
- if (false === $this->recoveryEnabledForUser()) {
-
- // create database configuration
- $sql = 'INSERT INTO `*PREFIX*encryption` (`uid`,`mode`,`recovery_enabled`,`migration_status`) VALUES (?,?,?,?)';
- $args = array(
- $this->userId,
- 'server-side',
- 0,
- self::MIGRATION_OPEN
- );
- $query = \OCP\DB::prepare($sql);
- $query->execute($args);
-
- }
-
return true;
}
@@ -230,36 +214,9 @@ class Util {
*/
public function recoveryEnabledForUser() {
- $sql = 'SELECT `recovery_enabled` FROM `*PREFIX*encryption` WHERE `uid` = ?';
-
- $args = array($this->userId);
-
- $query = \OCP\DB::prepare($sql);
-
- $result = $query->execute($args);
-
- $recoveryEnabled = array();
-
- if (\OCP\DB::isError($result)) {
- \OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
- } else {
- $row = $result->fetchRow();
- if ($row && isset($row['recovery_enabled'])) {
- $recoveryEnabled[] = $row['recovery_enabled'];
- }
- }
-
- // If no record is found
- if (empty($recoveryEnabled)) {
+ $recoveryMode = \OC_Preferences::getValue($this->userId, 'files_encryption', 'recovery_enabled', '0');
- return false;
-
- // If a record is found
- } else {
-
- return $recoveryEnabled[0];
-
- }
+ return ($recoveryMode === '1') ? true : false;
}
@@ -270,32 +227,8 @@ class Util {
*/
public function setRecoveryForUser($enabled) {
- $recoveryStatus = $this->recoveryEnabledForUser();
-
- // If a record for this user already exists, update it
- if (false === $recoveryStatus) {
-
- $sql = 'INSERT INTO `*PREFIX*encryption` (`uid`,`mode`,`recovery_enabled`) VALUES (?,?,?)';
-
- $args = array(
- $this->userId,
- 'server-side',
- $enabled
- );
-
- // Create a new record instead
- } else {
-
- $sql = 'UPDATE `*PREFIX*encryption` SET `recovery_enabled` = ? WHERE `uid` = ?';
-
- $args = array(
- $enabled ? '1' : '0',
- $this->userId
- );
-
- }
-
- return is_numeric(\OC_DB::executeAudited($sql, $args));
+ $value = $enabled ? '1' : '0';
+ return \OC_Preferences::setValue($this->userId, 'files_encryption', 'recovery_enabled', $value);
}
@@ -1133,24 +1066,16 @@ class Util {
/**
* set migration status
* @param int $status
+ * @param int $preCondition only update migration status if the previous value equals $preCondition
* @return boolean
*/
- private function setMigrationStatus($status) {
+ private function setMigrationStatus($status, $preCondition = null) {
- $sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ?';
- $args = array($status, $this->userId);
- $query = \OCP\DB::prepare($sql);
- $manipulatedRows = $query->execute($args);
+ // convert to string if preCondition is set
+ $preCondition = ($preCondition === null) ? null : (string)$preCondition;
- if ($manipulatedRows === 1) {
- $result = true;
- \OCP\Util::writeLog('Encryption library', "Migration status set to " . self::MIGRATION_OPEN, \OCP\Util::INFO);
- } else {
- $result = false;
- \OCP\Util::writeLog('Encryption library', "Could not set migration status to " . self::MIGRATION_OPEN, \OCP\Util::WARN);
- }
+ return \OC_Preferences::setValue($this->userId, 'files_encryption', 'migration_status', (string)$status, $preCondition);
- return $result;
}
/**
@@ -1159,7 +1084,7 @@ class Util {
*/
public function beginMigration() {
- $result = $this->setMigrationStatus(self::MIGRATION_IN_PROGRESS);
+ $result = $this->setMigrationStatus(self::MIGRATION_IN_PROGRESS, self::MIGRATION_OPEN);
if ($result) {
\OCP\Util::writeLog('Encryption library', "Start migration to encryption mode for " . $this->userId, \OCP\Util::INFO);
@@ -1199,46 +1124,16 @@ class Util {
*/
public function getMigrationStatus() {
- $sql = 'SELECT `migration_status` FROM `*PREFIX*encryption` WHERE `uid` = ?';
-
- $args = array($this->userId);
- $query = \OCP\DB::prepare($sql);
-
- $result = $query->execute($args);
-
- $migrationStatus = array();
-
- if (\OCP\DB::isError($result)) {
- \OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
- } else {
- $row = $result->fetchRow();
- if ($row && isset($row['migration_status'])) {
- $migrationStatus[] = $row['migration_status'];
+ $migrationStatus = false;
+ if (\OCP\User::userExists($this->userId)) {
+ $migrationStatus = \OC_Preferences::getValue($this->userId, 'files_encryption', 'migration_status');
+ if ($migrationStatus === null) {
+ \OC_Preferences::setValue($this->userId, 'files_encryption', 'migration_status', (string)self::MIGRATION_OPEN);
+ $migrationStatus = self::MIGRATION_OPEN;
}
}
- // If no record is found
- if (empty($migrationStatus)) {
- \OCP\Util::writeLog('Encryption library', "Could not get migration status for " . $this->userId . ", no record found", \OCP\Util::ERROR);
- // insert missing entry in DB with status open if the user exists
- if (\OCP\User::userExists($this->userId)) {
- $sql = 'INSERT INTO `*PREFIX*encryption` (`uid`,`mode`,`recovery_enabled`,`migration_status`) VALUES (?,?,?,?)';
- $args = array(
- $this->userId,
- 'server-side',
- 0,
- self::MIGRATION_OPEN
- );
- $query = \OCP\DB::prepare($sql);
- $query->execute($args);
-
- return self::MIGRATION_OPEN;
- } else {
- return false;
- }
- } else { // If a record is found
- return (int)$migrationStatus[0];
- }
+ return (int)$migrationStatus;
}