Use vault views for entry and recycle-bin state
This commit is contained in:
@@ -1260,14 +1260,10 @@ func (u *ui) recentVaultGroup(path string) []string {
|
||||
}
|
||||
|
||||
func (u *ui) hiddenVaultRoot() string {
|
||||
if u.state.Section != appstate.SectionEntries {
|
||||
return ""
|
||||
if u.state.Section == appstate.SectionEntries {
|
||||
return "Root"
|
||||
}
|
||||
model, err := u.state.Session.Current()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return vaultview.HiddenRoot(model)
|
||||
return ""
|
||||
}
|
||||
|
||||
func (u *ui) enterHiddenVaultRoot() {
|
||||
@@ -1294,7 +1290,7 @@ func (u *ui) restoreRecentVaultGroup(path string) {
|
||||
u.setCurrentPath(saved)
|
||||
return
|
||||
}
|
||||
if len(model.EntriesInPath(saved)) > 0 || len(model.ChildGroups(saved)) > 0 || hasExactGroup(model, saved) {
|
||||
if pathExistsInModel(model, saved) {
|
||||
u.setCurrentPath(saved)
|
||||
return
|
||||
}
|
||||
@@ -1317,7 +1313,7 @@ func (u *ui) restoreRecentRemoteGroup(baseURL, path string) {
|
||||
u.setCurrentPath(saved)
|
||||
return
|
||||
}
|
||||
if len(model.EntriesInPath(saved)) > 0 || len(model.ChildGroups(saved)) > 0 || hasExactGroup(model, saved) {
|
||||
if pathExistsInModel(model, saved) {
|
||||
u.setCurrentPath(saved)
|
||||
return
|
||||
}
|
||||
@@ -1339,7 +1335,7 @@ func (u *ui) restoreEntriesPath(path []string) {
|
||||
u.setCurrentPath(path)
|
||||
return
|
||||
}
|
||||
if len(model.EntriesInPath(path)) > 0 || len(model.ChildGroups(path)) > 0 || hasExactGroup(model, path) {
|
||||
if pathExistsInModel(model, path) {
|
||||
u.setCurrentPath(path)
|
||||
return
|
||||
}
|
||||
@@ -1415,6 +1411,22 @@ func pathHasPrefix(path, prefix []string) bool {
|
||||
return slices.Equal(path[:len(prefix)], prefix)
|
||||
}
|
||||
|
||||
func entriesViewPathForModel(model vault.Model, path []string) []string {
|
||||
if len(path) == 0 {
|
||||
return nil
|
||||
}
|
||||
switch {
|
||||
case usesPhysicalEntriesRoot(model) && path[0] == "Root":
|
||||
return append([]string(nil), path[1:]...)
|
||||
case usesLogicalEntriesRoot(model):
|
||||
return append([]string(nil), path...)
|
||||
case path[0] == "Root":
|
||||
return append([]string(nil), path[1:]...)
|
||||
default:
|
||||
return append([]string(nil), path...)
|
||||
}
|
||||
}
|
||||
|
||||
func hasExactGroup(model vault.Model, path []string) bool {
|
||||
for _, group := range model.Groups {
|
||||
if slices.Equal(group, path) {
|
||||
@@ -1433,12 +1445,14 @@ func (u *ui) currentGroupDeletionState() (bool, string) {
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
path := append([]string(nil), u.currentPath...)
|
||||
if len(model.ChildGroups(path)) > 0 {
|
||||
view := vaultview.VaultRoot(model)
|
||||
path := entriesViewPathForModel(model, u.currentPath)
|
||||
physicalPath := view.ToPhysicalPath(path)
|
||||
if len(model.ChildGroups(physicalPath)) > 0 {
|
||||
return false, "This group contains child groups. Move or delete them before removing the group."
|
||||
}
|
||||
for _, item := range model.Entries {
|
||||
if slices.Equal(item.Path, path) || pathHasPrefix(item.Path, path) {
|
||||
if slices.Equal(item.Path, physicalPath) || pathHasPrefix(item.Path, physicalPath) {
|
||||
return false, "This group contains entries. Move or delete them before removing the group."
|
||||
}
|
||||
}
|
||||
@@ -1450,6 +1464,47 @@ func (u *ui) currentGroupDeletionState() (bool, string) {
|
||||
return true, "Deleting this empty group will not remove any entries."
|
||||
}
|
||||
|
||||
func usesPhysicalEntriesRoot(model vault.Model) bool {
|
||||
if len(model.Entries) == 0 && len(model.Groups) == 0 && len(model.RecycleBin) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, group := range model.Groups {
|
||||
if len(group) > 0 && group[0] == vaultview.KeepassRoot {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, entry := range model.Entries {
|
||||
if len(entry.Path) > 0 && entry.Path[0] == vaultview.KeepassRoot {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, entry := range model.RecycleBin {
|
||||
if len(entry.Path) > 0 && entry.Path[0] == vaultview.KeepassRoot {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func usesLogicalEntriesRoot(model vault.Model) bool {
|
||||
for _, group := range model.Groups {
|
||||
if len(group) > 0 && group[0] == "Root" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, entry := range model.Entries {
|
||||
if len(entry.Path) > 0 && entry.Path[0] == "Root" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, entry := range model.RecycleBin {
|
||||
if len(entry.Path) > 0 && entry.Path[0] == "Root" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (u *ui) deleteGroupPendingConfirmation() bool {
|
||||
return len(u.deleteGroupPath) > 0 && slices.Equal(u.deleteGroupPath, u.currentPath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user