Fix move and copy entry in database V1 root

This commit is contained in:
J-Jamet
2019-08-19 01:03:09 +02:00
parent 75b028daf3
commit f707fd7649
10 changed files with 46 additions and 12 deletions

View File

@@ -523,10 +523,10 @@ class GroupActivity : LockingActivity(),
MoveGroupRunnable(
this,
Database.getInstance(),
groupToMove,
newParent,
AfterAddNodeRunnable(),
!readOnly)
groupToMove,
newParent,
AfterAddNodeRunnable(),
!readOnly)
}.start()
}

View File

@@ -1,6 +1,7 @@
package com.kunzisoft.keepass.database.action.node
import android.support.v4.app.FragmentActivity
import android.util.Log
import com.kunzisoft.keepass.database.action.SaveDatabaseRunnable
import com.kunzisoft.keepass.database.element.Database
@@ -23,6 +24,7 @@ abstract class ActionNodeDatabaseRunnable(
super.run()
finishRun(true)
} catch (e: Exception) {
Log.e("ActionNodeDBRunnable", e.message)
finishRun(false, e.message)
}
}

View File

@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.database.action.node
import android.support.v4.app.FragmentActivity
import android.util.Log
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.EntryVersioned
import com.kunzisoft.keepass.database.element.GroupVersioned
@@ -37,9 +38,18 @@ class CopyEntryRunnable constructor(
private var mEntryCopied: EntryVersioned? = null
override fun nodeAction() {
// Update entry with new values
mNewParent.touch(modified = false, touchParents = true)
mEntryCopied = database.copyEntryTo(mEntryToCopy, mNewParent)
// Condition
var conditionAccepted = true
if(mNewParent == database.rootGroup && !database.rootCanContainsEntry())
conditionAccepted = false
if (conditionAccepted) {
// Update entry with new values
mNewParent.touch(modified = false, touchParents = true)
mEntryCopied = database.copyEntryTo(mEntryToCopy, mNewParent)
} else {
// Only finish thread
throw Exception(context.getString(R.string.error_copy_entry_here))
}
mEntryCopied?.apply {
touch(modified = true, touchParents = true)
@@ -56,7 +66,6 @@ class CopyEntryRunnable constructor(
} catch (e: Exception) {
Log.i(TAG, "Unable to delete the copied entry")
}
}
return ActionNodeValues(result, mEntryToCopy, mEntryCopied)
}

View File

@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.database.action.node
import android.support.v4.app.FragmentActivity
import android.util.Log
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.EntryVersioned
import com.kunzisoft.keepass.database.element.GroupVersioned
@@ -40,9 +41,17 @@ class MoveEntryRunnable constructor(
// Move entry in new parent
mEntryToMove?.let {
mOldParent = it.parent
// Condition
var conditionAccepted = true
if(mNewParent == database.rootGroup && !database.rootCanContainsEntry())
conditionAccepted = false
// Move only if the parent change
if (mOldParent != mNewParent) {
if (mOldParent != mNewParent && conditionAccepted) {
database.moveEntryTo(it, mNewParent)
} else {
// Only finish thread
throw Exception(context.getString(R.string.error_move_entry_here))
}
it.touch(modified = true, touchParents = true)
} ?: Log.e(TAG, "Unable to create a copy of the entry")

View File

@@ -46,9 +46,7 @@ class MoveGroupRunnable constructor(
finishRun(true)
} else {
// Only finish thread
val message = context.getString(R.string.error_move_folder_in_itself)
Log.e(TAG, message)
finishRun(false, message)
throw Exception(context.getString(R.string.error_move_folder_in_itself))
}
} ?: Log.e(TAG, "Unable to create a copy of the group")
}

View File

@@ -484,6 +484,10 @@ class Database {
pwDatabaseV4?.retrieveMasterKey(key, keyInputStream)
}
fun rootCanContainsEntry(): Boolean {
return pwDatabaseV3?.rootCanContainsEntry() ?: pwDatabaseV4?.rootCanContainsEntry() ?: false
}
fun createEntry(): EntryVersioned? {
pwDatabaseV3?.let { database ->
return EntryVersioned(database.createEntry()).apply {

View File

@@ -296,6 +296,8 @@ abstract class PwDatabase<Group : PwGroup<*, Group, Entry>, Entry : PwEntry<Grou
* -------------------------------------
*/
abstract fun rootCanContainsEntry(): Boolean
fun addGroupTo(newGroup: Group, parent: Group?) {
// Add tree to parent tree
parent?.addChildGroup(newGroup)

View File

@@ -150,6 +150,10 @@ class PwDatabaseV3 : PwDatabase<PwGroupV3, PwEntryV3>() {
return PwEntryV3()
}
override fun rootCanContainsEntry(): Boolean {
return false
}
override fun isBackup(group: PwGroupV3): Boolean {
var currentGroup: PwGroupV3? = group
while (currentGroup != null) {

View File

@@ -317,6 +317,10 @@ class PwDatabaseV4 : PwDatabase<PwGroupV4, PwEntryV4> {
return PwEntryV4()
}
override fun rootCanContainsEntry(): Boolean {
return true
}
override fun isBackup(group: PwGroupV4): Boolean {
if (recycleBin == null)
return false

View File

@@ -107,6 +107,8 @@
<string name="error_wrong_length">Enter a positive whole number in the \"Length\" field.</string>
<string name="error_autofill_enable_service">Could not enable autofill service.</string>
<string name="error_move_folder_in_itself">You can not move a group into itself.</string>
<string name="error_move_entry_here">You can not move an entry here.</string>
<string name="error_copy_entry_here">You can not copy an entry here.</string>
<string name="field_name">Field name</string>
<string name="field_value">Field value</string>
<string name="file_not_found">Could not find file.</string>