Skip to content

Commit 292a3a2

Browse files
authored
[PM-20115] Delete button visibility based on cipher permissions (#1495)
1 parent 4e3a2cb commit 292a3a2

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

BitwardenShared/UI/Vault/VaultItem/CipherItemState.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,27 @@ struct CipherItemState: Equatable {
136136

137137
/// Whether or not this item can be deleted by the user.
138138
var canBeDeleted: Bool {
139-
// New permission model from PM-18091
140-
if restrictCipherItemDeletionFlagEnabled, let cipherPermissions = cipher.permissions {
141-
return cipherPermissions.delete
139+
// backwards compatibility for old server versions
140+
guard restrictCipherItemDeletionFlagEnabled, let cipherPermissions = cipher.permissions else {
141+
guard !collectionIds.isEmpty else { return true }
142+
return collections.contains { collection in
143+
guard let id = collection.id else { return false }
144+
return collection.manage && collectionIds.contains(id)
145+
}
142146
}
143147

144-
guard !collectionIds.isEmpty else { return true }
145-
return collections.contains { collection in
146-
guard let id = collection.id else { return false }
147-
return collection.manage && collectionIds.contains(id)
148-
}
148+
// New permission model from PM-18091
149+
return cipherPermissions.delete
149150
}
150151

151152
/// Whether or not this item can be restored by the user.
152-
/// New permission model from PM-18091
153-
var canBeRestoredPermission: Bool {
153+
var canBeRestored: Bool {
154154
// backwards compatibility for old server versions
155-
guard let cipherPermissions = cipher.permissions else {
155+
guard restrictCipherItemDeletionFlagEnabled, let cipherPermissions = cipher.permissions else {
156156
return isSoftDeleted
157157
}
158+
159+
// New permission model from PM-18091
158160
return cipherPermissions.restore && isSoftDeleted
159161
}
160162

BitwardenShared/UI/Vault/VaultItem/CipherItemStateTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ class CipherItemStateTests: BitwardenTestCase {
157157
XCTAssertFalse(state.canBeDeleted)
158158
}
159159

160-
/// `canBeRestoredPermission` cipher permissions is nil fallback to isSoftDeleted
161-
func test_canBeRestoredPermission_permissions_nil() throws {
160+
/// `canBeRestored` cipher permissions is nil fallback to isSoftDeleted
161+
func test_canBeRestored_permissions_nil() throws {
162162
var cipher = CipherView.loginFixture(
163163
collectionIds: ["1", "2"],
164164
deletedDate: nil,
@@ -167,7 +167,7 @@ class CipherItemStateTests: BitwardenTestCase {
167167
)
168168
var state = try XCTUnwrap(CipherItemState(existing: cipher, hasPremium: true))
169169
state.restrictCipherItemDeletionFlagEnabled = true
170-
XCTAssertFalse(state.canBeRestoredPermission)
170+
XCTAssertFalse(state.canBeRestored)
171171

172172
cipher = CipherView.loginFixture(
173173
collectionIds: ["1", "2"],
@@ -176,12 +176,12 @@ class CipherItemStateTests: BitwardenTestCase {
176176
permissions: nil
177177
)
178178
state = try XCTUnwrap(CipherItemState(existing: cipher, hasPremium: true))
179-
XCTAssertTrue(state.canBeRestoredPermission)
179+
XCTAssertTrue(state.canBeRestored)
180180
}
181181

182-
/// `canBeRestoredPermission` returns value from cipher permissions if not nil
182+
/// `canBeRestored` returns value from cipher permissions if not nil
183183
/// restore value true
184-
func test_canBeRestoredPermission_true() throws {
184+
func test_canBeRestored_true() throws {
185185
let cipher = CipherView.loginFixture(
186186
deletedDate: Date(),
187187
login: .fixture(),
@@ -192,12 +192,12 @@ class CipherItemStateTests: BitwardenTestCase {
192192
)
193193
var state = try XCTUnwrap(CipherItemState(existing: cipher, hasPremium: true))
194194
state.restrictCipherItemDeletionFlagEnabled = true
195-
XCTAssertTrue(state.canBeRestoredPermission)
195+
XCTAssertTrue(state.canBeRestored)
196196
}
197197

198-
/// `canBeRestoredPermission` returns value from cipher permissions if not nil
198+
/// `canBeRestored` returns value from cipher permissions if not nil
199199
/// restore value false
200-
func test_canBeRestoredPermission_false() throws {
200+
func test_canBeRestored_false() throws {
201201
let cipher = CipherView.loginFixture(
202202
deletedDate: Date(),
203203
login: .fixture(),
@@ -208,7 +208,7 @@ class CipherItemStateTests: BitwardenTestCase {
208208
)
209209
var state = try XCTUnwrap(CipherItemState(existing: cipher, hasPremium: true))
210210
state.restrictCipherItemDeletionFlagEnabled = true
211-
XCTAssertFalse(state.canBeRestoredPermission)
211+
XCTAssertFalse(state.canBeRestored)
212212
}
213213

214214
/// `restrictCipherItemDeletionFlagEnable` default value is false

BitwardenShared/UI/Vault/VaultItem/ViewItem/ViewItemState.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ extension ViewItemState {
7979
hasPremium: Bool,
8080
restrictCipherItemDeletionFlagEnabled: Bool
8181
) {
82-
guard let cipherItemState = CipherItemState(
82+
guard var cipherItemState = CipherItemState(
8383
existing: cipherView,
8484
hasMasterPassword: hasMasterPassword,
8585
hasPremium: hasPremium
8686
) else { return nil }
87+
cipherItemState.restrictCipherItemDeletionFlagEnabled = restrictCipherItemDeletionFlagEnabled
8788
self.init(loadingState: .data(cipherItemState))
8889
self.hasMasterPassword = hasMasterPassword
8990
hasPremiumFeatures = cipherItemState.accountHasPremium

BitwardenShared/UI/Vault/VaultItem/ViewItem/ViewItemView.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ struct ViewItemView: View {
2525

2626
/// Whether the restore option is available.
2727
/// New permission model from PM-18091
28-
var isRestoredPermissionEnabled: Bool {
29-
store.state.loadingState.data?.canBeRestoredPermission ?? false
28+
var isRestoredEnabled: Bool {
29+
store.state.loadingState.data?.canBeRestored ?? false
3030
}
3131

3232
/// Whether to show the move to organization option in the toolbar menu.
@@ -68,10 +68,7 @@ struct ViewItemView: View {
6868

6969
ToolbarItemGroup(placement: .navigationBarTrailing) {
7070
if let state = store.state.loadingState.data {
71-
let restoreButtonVisible = store.state.restrictCipherItemDeletionFlagEnabled
72-
? isRestoredPermissionEnabled
73-
: state.isSoftDeleted
74-
if restoreButtonVisible {
71+
if isRestoredEnabled {
7572
toolbarButton(Localizations.restore) {
7673
await store.perform(.restorePressed)
7774
}

0 commit comments

Comments
 (0)