Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-13349] Hide edit button unless item is in at least one non-readOnly collection #4430

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.x8bit.bitwarden.ui.vault.feature.item.model.VaultItemStateData
import com.x8bit.bitwarden.ui.vault.feature.item.util.toViewState
import com.x8bit.bitwarden.ui.vault.feature.util.canAssignToCollections
import com.x8bit.bitwarden.ui.vault.feature.util.hasDeletePermissionInAtLeastOneCollection
import com.x8bit.bitwarden.ui.vault.feature.util.hasEditPermissionInAtLeastOneCollection
import com.x8bit.bitwarden.ui.vault.model.VaultCardBrand
import com.x8bit.bitwarden.ui.vault.model.VaultLinkedFieldType
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -116,11 +117,18 @@ class VaultItemViewModel @Inject constructor(
.data
.canAssignToCollections(cipherViewState.data?.collectionIds)

val canEdit = collectionsState
.data
.hasEditPermissionInAtLeastOneCollection(
cipherViewState.data?.collectionIds,
)

VaultItemStateData(
cipher = cipherViewState.data,
totpCodeItemData = totpCodeData,
canDelete = canDelete,
canAssociateToCollections = canAssignToCollections,
canEdit = canEdit,
)
},
)
Expand Down Expand Up @@ -1040,6 +1048,7 @@ class VaultItemViewModel @Inject constructor(
totpCodeItemData = this.data?.totpCodeItemData,
canDelete = this.data?.canDelete == true,
canAssignToCollections = this.data?.canAssociateToCollections == true,
canEdit = this.data?.canEdit == true,
)
}
?: VaultItemState.ViewState.Error(message = errorText)
Expand Down Expand Up @@ -1277,11 +1286,16 @@ data class VaultItemState(
?.currentCipher
?.deletedDate != null

private val isCipherEditable: Boolean
get() = viewState.asContentOrNull()
?.common
?.canEdit == true

/**
* Whether or not the fab is visible.
*/
val isFabVisible: Boolean
get() = viewState is ViewState.Content && !isCipherDeleted
get() = viewState is ViewState.Content && !isCipherDeleted && isCipherEditable

/**
* Whether or not the cipher is in a collection.
Expand Down Expand Up @@ -1377,6 +1391,7 @@ data class VaultItemState(
val attachments: List<AttachmentItem>?,
val canDelete: Boolean,
val canAssignToCollections: Boolean,
val canEdit: Boolean,
) : Parcelable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import com.bitwarden.vault.CipherView
* @property totpCodeItemData The data for the totp code.
* @property canDelete Whether the item can be deleted.
* @property canAssociateToCollections Whether the item can be associated to a collection.
* @property canEdit Whether the item can be edited.
*/
data class VaultItemStateData(
val cipher: CipherView?,
val totpCodeItemData: TotpCodeItemData?,
val canDelete: Boolean,
val canAssociateToCollections: Boolean,
val canEdit: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fun CipherView.toViewState(
clock: Clock = Clock.systemDefaultZone(),
canDelete: Boolean,
canAssignToCollections: Boolean,
canEdit: Boolean,
): VaultItemState.ViewState =
VaultItemState.ViewState.Content(
common = VaultItemState.ViewState.Content.Common(
Expand Down Expand Up @@ -83,6 +84,7 @@ fun CipherView.toViewState(
.orEmpty(),
canDelete = canDelete,
canAssignToCollections = canAssignToCollections,
canEdit = canEdit,
),
type = when (type) {
CipherType.LOGIN -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,20 @@ fun List<CollectionView>?.canAssignToCollections(currentCollectionIds: List<Stri
itemIsInCollection && (!it.manage || it.readOnly)
}
?: true

/**
* Checks if the user has edit permission in at least one collection.
*
* Editing is allowed when the item is in any collection that isn't read-only.
*/
fun List<CollectionView>?.hasEditPermissionInAtLeastOneCollection(
collectionIds: List<String>?,
): Boolean {
if (this.isNullOrEmpty() || collectionIds.isNullOrEmpty()) return true
return this
.any { collectionView ->
collectionIds
.contains(collectionView.id)
.let { isInCollection -> isInCollection && !collectionView.readOnly }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,7 @@ private val DEFAULT_COMMON: VaultItemState.ViewState.Content.Common =
),
canDelete = true,
canAssignToCollections = true,
canEdit = true,
)

private val DEFAULT_PASSKEY = R.string.created_xy.asText(
Expand Down Expand Up @@ -2781,6 +2782,7 @@ private val EMPTY_COMMON: VaultItemState.ViewState.Content.Common =
attachments = emptyList(),
canDelete = true,
canAssignToCollections = true,
canEdit = true,
)

private val EMPTY_LOGIN_TYPE: VaultItemState.ViewState.Content.ItemType.Login =
Expand Down
Loading
Loading