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-11304] Ownership Not Defaulting To Org and Collection #4254

Merged
merged 16 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ fun NavGraphBuilder.vaultUnlockedGraph(
vaultItemListingDestinationAsRoot(
onNavigateBack = { navController.popBackStack() },
onNavigateToVaultItemScreen = { navController.navigateToVaultItem(vaultItemId = it) },
onNavigateToVaultAddItemScreen = { cipherType, selectedFolderId ->
onNavigateToVaultAddItemScreen = { cipherType, selectedFolderId, collectionId ->
navController.navigateToVaultAddEdit(
VaultAddEditType.AddItem(cipherType),
selectedFolderId,
collectionId,
)
},
onNavigateToSearchVault = { navController.navigateToSearch(searchType = it) },
Expand All @@ -89,10 +90,11 @@ fun NavGraphBuilder.vaultUnlockedGraph(
vaultUnlockedNavBarDestination(
onNavigateToExportVault = { navController.navigateToExportVault() },
onNavigateToFolders = { navController.navigateToFolders() },
onNavigateToVaultAddItem = { cipherType, selectedFolderId ->
onNavigateToVaultAddItem = { cipherType, selectedFolderId, collectionId ->
navController.navigateToVaultAddEdit(
VaultAddEditType.AddItem(cipherType),
selectedFolderId,
collectionId,
)
},
onNavigateToVaultItem = { navController.navigateToVaultItem(it) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun NavController.navigateToVaultUnlockedNavBar(navOptions: NavOptions? = null)
*/
@Suppress("LongParameterList")
fun NavGraphBuilder.vaultUnlockedNavBarDestination(
onNavigateToVaultAddItem: (VaultItemCipherType, String?) -> Unit,
onNavigateToVaultAddItem: (VaultItemCipherType, String?, String?) -> Unit,
onNavigateToVaultItem: (vaultItemId: String) -> Unit,
onNavigateToVaultEditItem: (vaultItemId: String) -> Unit,
onNavigateToSearchSend: (searchType: SearchType.Sends) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import com.x8bit.bitwarden.ui.vault.model.VaultItemCipherType
fun VaultUnlockedNavBarScreen(
viewModel: VaultUnlockedNavBarViewModel = hiltViewModel(),
navController: NavHostController = rememberNavController(),
onNavigateToVaultAddItem: (VaultItemCipherType, String?) -> Unit,
onNavigateToVaultAddItem: (VaultItemCipherType, String?, String?) -> Unit,
onNavigateToVaultItem: (vaultItemId: String) -> Unit,
onNavigateToVaultEditItem: (vaultItemId: String) -> Unit,
onNavigateToSearchSend: (searchType: SearchType.Sends) -> Unit,
Expand Down Expand Up @@ -158,7 +158,7 @@ private fun VaultUnlockedNavBarScaffold(
sendTabClickedAction: () -> Unit,
generatorTabClickedAction: () -> Unit,
settingsTabClickedAction: () -> Unit,
navigateToVaultAddItem: (VaultItemCipherType, String?) -> Unit,
navigateToVaultAddItem: (VaultItemCipherType, String?, String?) -> Unit,
onNavigateToVaultItem: (vaultItemId: String) -> Unit,
onNavigateToVaultEditItem: (vaultItemId: String) -> Unit,
onNavigateToSearchSend: (searchType: SearchType.Sends) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ private const val ADD_ITEM_TYPE: String = "vault_add_item_type"
private const val ADD_EDIT_ITEM_PREFIX: String = "vault_add_edit_item"
private const val ADD_EDIT_ITEM_TYPE: String = "vault_add_edit_type"
private const val ADD_SELECTED_FOLDER_ID: String = "vault_add_selected_folder_id"
private const val ADD_SELECTED_COLLECTION_ID: String = "vault_add_selected_collection_id"

private const val ADD_EDIT_ITEM_ROUTE: String =
ADD_EDIT_ITEM_PREFIX +
"/{$ADD_EDIT_ITEM_TYPE}" +
"?$EDIT_ITEM_ID={$EDIT_ITEM_ID}" +
"?$ADD_ITEM_TYPE={$ADD_ITEM_TYPE}" +
"?$ADD_SELECTED_FOLDER_ID={$ADD_SELECTED_FOLDER_ID}"
"?$ADD_SELECTED_FOLDER_ID={$ADD_SELECTED_FOLDER_ID}" +
"?$ADD_SELECTED_COLLECTION_ID={$ADD_SELECTED_COLLECTION_ID}"

/**
* Class to retrieve vault add & edit arguments from the [SavedStateHandle].
Expand All @@ -42,6 +44,7 @@ private const val ADD_EDIT_ITEM_ROUTE: String =
data class VaultAddEditArgs(
val vaultAddEditType: VaultAddEditType,
val selectedFolderId: String? = null,
val selectedCollectionId: String? = null,
) {
constructor(savedStateHandle: SavedStateHandle) : this(
vaultAddEditType = when (requireNotNull(savedStateHandle[ADD_EDIT_ITEM_TYPE])) {
Expand All @@ -57,6 +60,7 @@ data class VaultAddEditArgs(
else -> throw IllegalStateException("Unknown VaultAddEditType.")
},
selectedFolderId = savedStateHandle[ADD_SELECTED_FOLDER_ID],
selectedCollectionId = savedStateHandle[ADD_SELECTED_COLLECTION_ID],
)
}

Expand All @@ -80,6 +84,10 @@ fun NavGraphBuilder.vaultAddEditDestination(
type = NavType.StringType
nullable = true
},
navArgument(ADD_SELECTED_COLLECTION_ID) {
type = NavType.StringType
nullable = true
},
),
) {
VaultAddEditScreen(
Expand All @@ -99,13 +107,15 @@ fun NavGraphBuilder.vaultAddEditDestination(
fun NavController.navigateToVaultAddEdit(
vaultAddEditType: VaultAddEditType,
selectedFolderId: String? = null,
selectedCollectionId: String? = null,
navOptions: NavOptions? = null,
) {
navigate(
route = "$ADD_EDIT_ITEM_PREFIX/${vaultAddEditType.toTypeString()}" +
"?$EDIT_ITEM_ID=${vaultAddEditType.toIdOrNull()}" +
"?$ADD_ITEM_TYPE=${vaultAddEditType.toVaultItemCipherTypeOrNull()}" +
"?$ADD_SELECTED_FOLDER_ID=$selectedFolderId",
"?$ADD_SELECTED_FOLDER_ID=$selectedFolderId" +
"?$ADD_SELECTED_COLLECTION_ID=$selectedCollectionId",
navOptions = navOptions,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import com.x8bit.bitwarden.data.autofill.fido2.model.Fido2RegisterCredentialResult
import com.x8bit.bitwarden.data.autofill.fido2.model.UserVerificationRequirement
import com.x8bit.bitwarden.data.autofill.util.isActiveWithFido2Credentials
import com.x8bit.bitwarden.data.platform.manager.FeatureFlagManager
import com.x8bit.bitwarden.data.platform.manager.PolicyManager
import com.x8bit.bitwarden.data.platform.manager.SpecialCircumstanceManager
import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardManager
import com.x8bit.bitwarden.data.platform.manager.event.OrganizationEventManager
import com.x8bit.bitwarden.data.platform.manager.model.FlagKey
import com.x8bit.bitwarden.data.platform.manager.model.OrganizationEvent
import com.x8bit.bitwarden.data.platform.manager.util.toAutofillSaveItemOrNull
import com.x8bit.bitwarden.data.platform.manager.util.toAutofillSelectionDataOrNull
Expand Down Expand Up @@ -101,12 +103,14 @@
private val resourceManager: ResourceManager,
private val clock: Clock,
private val organizationEventManager: OrganizationEventManager,
private val featureFlagManager: FeatureFlagManager,
) : BaseViewModel<VaultAddEditState, VaultAddEditEvent, VaultAddEditAction>(
// We load the state from the savedStateHandle for testing purposes.
initialState = savedStateHandle[KEY_STATE]
?: run {
val vaultAddEditType = VaultAddEditArgs(savedStateHandle).vaultAddEditType
val selectedFolderId = VaultAddEditArgs(savedStateHandle).selectedFolderId
val selectedCollectionId = VaultAddEditArgs(savedStateHandle).selectedCollectionId
val isIndividualVaultDisabled = policyManager
.getActivePolicies(type = PolicyTypeJson.PERSONAL_OWNERSHIP)
.any()
Expand Down Expand Up @@ -147,12 +151,11 @@
attestationOptions = fido2AttestationOptions,
isIndividualVaultDisabled = isIndividualVaultDisabled,
)
?: totpData?.toDefaultAddTypeContent(
isIndividualVaultDisabled = isIndividualVaultDisabled,
)
?: totpData?.toDefaultAddTypeContent(isIndividualVaultDisabled)
?: VaultAddEditState.ViewState.Content(
common = VaultAddEditState.ViewState.Content.Common(
selectedFolderId = selectedFolderId,
selectedCollectionId = selectedCollectionId,
),
isIndividualVaultDisabled = isIndividualVaultDisabled,
type = vaultAddEditType.vaultItemCipherType.toItemType(),
Expand All @@ -167,7 +170,11 @@
// Set special conditions for autofill and fido2 save
shouldShowCloseButton = autofillSaveItem == null && fido2AttestationOptions == null,
shouldExitOnSave = shouldExitOnSave,
supportedItemTypes = getSupportedItemTypeOptions(),
supportedItemTypes = getSupportedItemTypeOptions(
isSshKeyVaultItemSupported = featureFlagManager.getFeatureFlag(
key = FlagKey.SshKeyCipherItems,
),
),
)
},
) {
Expand Down Expand Up @@ -209,6 +216,12 @@
}
.onEach(::sendAction)
.launchIn(viewModelScope)

featureFlagManager
.getFeatureFlagFlow(FlagKey.SshKeyCipherItems)
.map { VaultAddEditAction.Internal.SshKeyCipherItemsFeatureFlagReceive(it) }
.onEach(::sendAction)
.launchIn(viewModelScope)
}

override fun handleAction(action: VaultAddEditAction) {
Expand Down Expand Up @@ -1082,7 +1095,9 @@
updateLoginContent { loginType ->
loginType.copy(
uriList = loginType.uriList + UriItem(
id = UUID.randomUUID().toString(),
id = UUID
.randomUUID()
.toString(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to make this change?

Copy link
Contributor Author

@andrebispo5 andrebispo5 Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image It was automatically formatted by the IDE after the file format.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that should not happen as part of the formatter. To be clear, there is nothing wrong with this change but the regular auto-formatter does not normally do this. ๐Ÿค”

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-11-12 at 11 32 52โ€ฏAM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated to match yours, thanks

uri = "",
match = null,
checksum = null,
Expand Down Expand Up @@ -1435,6 +1450,10 @@
is VaultAddEditAction.Internal.ValidateFido2PinResultReceive -> {
handleValidateFido2PinResultReceive(action)
}

is VaultAddEditAction.Internal.SshKeyCipherItemsFeatureFlagReceive -> {
handleSshKeyCipherItemsFeatureFlagReceive(action)

Check warning on line 1455 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt#L1454-L1455

Added lines #L1454 - L1455 were not covered by tests
}
}
}

Expand Down Expand Up @@ -1769,6 +1788,19 @@

getRequestAndRegisterCredential()
}

private fun handleSshKeyCipherItemsFeatureFlagReceive(
action: VaultAddEditAction.Internal.SshKeyCipherItemsFeatureFlagReceive,
) {
mutableStateFlow.update {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for this action

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was actually a merge mistake, I've already reverted it. Thanks for catching that!

it.copy(
supportedItemTypes = getSupportedItemTypeOptions(
isSshKeyVaultItemSupported = action.enabled,

Check warning on line 1798 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt#L1795-L1798

Added lines #L1795 - L1798 were not covered by tests
),
)

Check warning on line 1800 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt#L1800

Added line #L1800 was not covered by tests
}
}

//endregion Internal Type Handlers

//region Utility Functions
Expand Down Expand Up @@ -2113,6 +2145,7 @@
val favorite: Boolean = false,
val customFieldData: List<Custom> = emptyList(),
val notes: String = "",
val selectedCollectionId: String? = null,
val selectedFolderId: String? = null,
val availableFolders: List<Folder> = emptyList(),
val selectedOwnerId: String? = null,
Expand Down Expand Up @@ -2171,7 +2204,9 @@
val canEditItem: Boolean = true,
val uriList: List<UriItem> = listOf(
UriItem(
id = UUID.randomUUID().toString(),
id = UUID
.randomUUID()
.toString(),
uri = "",
match = null,
checksum = null,
Expand Down Expand Up @@ -3083,6 +3118,13 @@
val generatorResult: GeneratorResult,
) : Internal()

/**
* Indicates that the the SSH key vault item feature flag state has been received.
*/
data class SshKeyCipherItemsFeatureFlagReceive(
val enabled: Boolean,
) : Internal()

Check warning on line 3126 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt#L3125-L3126

Added lines #L3125 - L3126 were not covered by tests

/**
* Indicates that the vault item data has been received.
*/
Expand Down Expand Up @@ -3137,10 +3179,7 @@
}
}

/**
* Returns a list of item type options that can be selected during item creation.
*
* TODO: [PM-10413] Allow SSH key creation when the SDK supports it.
*/
private fun getSupportedItemTypeOptions() = VaultAddEditState.ItemTypeOption.entries
.filter { it != VaultAddEditState.ItemTypeOption.SSH_KEYS }
private fun getSupportedItemTypeOptions(
isSshKeyVaultItemSupported: Boolean,
) = VaultAddEditState.ItemTypeOption.entries
.filter { isSshKeyVaultItemSupported || it != VaultAddEditState.ItemTypeOption.SSH_KEYS }
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@
selectedFolderId = folderViewList.toSelectedFolderId(
cipherView = currentContentState.common.originalCipher,
)
?: currentContentState.common.selectedFolderId,
?: currentContentState.common.selectedFolderId,
availableFolders = folderViewList.toAvailableFolders(
resourceManager = resourceManager,
),
selectedOwnerId = activeAccount.toSelectedOwnerId(
cipherView = currentContentState.common.originalCipher,
),
selectedOwnerId = activeAccount
.toSelectedOwnerId(cipherView = currentContentState.common.originalCipher)
?: collectionViewList
.firstOrNull { it.id == currentContentState.common.selectedCollectionId }
?.organizationId,
availableOwners = activeAccount.toAvailableOwners(
collectionViewList = collectionViewList,
cipherView = currentContentState.common.originalCipher,
isIndividualVaultDisabled = isIndividualVaultDisabled,
selectedCollectionId = currentContentState.common.selectedCollectionId,
),
isUnlockWithPasswordEnabled = activeAccount.hasMasterPassword,
hasOrganizations = activeAccount.organizations.isNotEmpty(),
Expand Down Expand Up @@ -197,13 +200,15 @@
collectionViewList: List<CollectionView>,
cipherView: CipherView?,
isIndividualVaultDisabled: Boolean,
selectedCollectionId: String? = null,

Check warning on line 203 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/util/CipherViewExtensions.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/util/CipherViewExtensions.kt#L203

Added line #L203 was not covered by tests
): List<VaultAddEditState.Owner> =
listOfNotNull(
VaultAddEditState.Owner(
name = email,
id = null,
collections = emptyList(),
)
VaultAddEditState
.Owner(
name = email,
id = null,
collections = emptyList(),
)
.takeUnless { isIndividualVaultDisabled },
*organizations
.map {
Expand All @@ -219,9 +224,11 @@
VaultCollection(
id = collection.id.orEmpty(),
name = collection.name,
isSelected = cipherView
isSelected = (cipherView
?.collectionIds
?.contains(collection.id) == true,
?.contains(collection.id))
?: (selectedCollectionId != null &&
collection.id == selectedCollectionId),

Check warning on line 231 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/util/CipherViewExtensions.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/util/CipherViewExtensions.kt#L231

Added line #L231 was not covered by tests
)
},
)
Expand All @@ -232,25 +239,33 @@
private fun FieldView.toCustomField() =
when (this.type) {
FieldType.TEXT -> VaultAddEditState.Custom.TextField(
itemId = UUID.randomUUID().toString(),
itemId = UUID
.randomUUID()
.toString(),
name = this.name.orEmpty(),
value = this.value.orEmpty(),
)

FieldType.HIDDEN -> VaultAddEditState.Custom.HiddenField(
itemId = UUID.randomUUID().toString(),
itemId = UUID
.randomUUID()
.toString(),
name = this.name.orEmpty(),
value = this.value.orEmpty(),
)

FieldType.BOOLEAN -> VaultAddEditState.Custom.BooleanField(
itemId = UUID.randomUUID().toString(),
itemId = UUID
.randomUUID()
.toString(),
name = this.name.orEmpty(),
value = this.value.toBoolean(),
)

FieldType.LINKED -> VaultAddEditState.Custom.LinkedField(
itemId = UUID.randomUUID().toString(),
itemId = UUID
.randomUUID()
.toString(),
name = this.name.orEmpty(),
vaultLinkedFieldType = fromId(requireNotNull(this.linkedId)),
)
Expand Down Expand Up @@ -287,7 +302,9 @@
if (this.isNullOrEmpty()) {
listOf(
UriItem(
id = UUID.randomUUID().toString(),
id = UUID
.randomUUID()
.toString(),

Check warning on line 307 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/util/CipherViewExtensions.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/util/CipherViewExtensions.kt#L306-L307

Added lines #L306 - L307 were not covered by tests
uri = "",
match = null,
checksum = null,
Expand All @@ -296,7 +313,9 @@
} else {
this.map { loginUriView ->
UriItem(
id = UUID.randomUUID().toString(),
id = UUID
.randomUUID()
.toString(),
uri = loginUriView.uri,
match = loginUriView.match,
checksum = loginUriView.uriChecksum,
Expand Down
Loading