-
Notifications
You must be signed in to change notification settings - Fork 952
feat: Add collection management to Android #6717
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
Draft
dasien
wants to merge
6
commits into
main
Choose a base branch
from
android-collections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d3e4dc8
feat: Add collection management (create, edit, delete) to Settings > …
f6435a0
feat: Replace encryptCollection stub with real SDK call
27eab55
fix: Adapt to local SDK API changes for local development
d0809a7
fix: Include access permissions in collection update request
SaintPatrck 5dcaf6e
fix: Grant creating user manage access and fix permission checks
5d5cbdd
fix: Address code review findings and add ViewModel tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/kotlin/com/x8bit/bitwarden/data/vault/manager/CollectionManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.x8bit.bitwarden.data.vault.manager | ||
|
|
||
| import com.bitwarden.collections.CollectionView | ||
| import com.x8bit.bitwarden.data.vault.repository.model.CreateCollectionResult | ||
| import com.x8bit.bitwarden.data.vault.repository.model.DeleteCollectionResult | ||
| import com.x8bit.bitwarden.data.vault.repository.model.UpdateCollectionResult | ||
|
|
||
| /** | ||
| * Manages the creating, updating, and deleting collections. | ||
| */ | ||
| interface CollectionManager { | ||
| /** | ||
| * Attempt to create a collection in the given [organizationId]. | ||
| * The [organizationUserId] is used to grant the creating user manage access. | ||
| */ | ||
| suspend fun createCollection( | ||
| organizationId: String, | ||
| organizationUserId: String?, | ||
| collectionView: CollectionView, | ||
| ): CreateCollectionResult | ||
|
|
||
| /** | ||
| * Attempt to delete a collection. | ||
| */ | ||
| suspend fun deleteCollection( | ||
| organizationId: String, | ||
| collectionId: String, | ||
| ): DeleteCollectionResult | ||
|
|
||
| /** | ||
| * Attempt to update a collection. | ||
| */ | ||
| suspend fun updateCollection( | ||
| organizationId: String, | ||
| collectionId: String, | ||
| collectionView: CollectionView, | ||
| ): UpdateCollectionResult | ||
| } |
163 changes: 163 additions & 0 deletions
163
app/src/main/kotlin/com/x8bit/bitwarden/data/vault/manager/CollectionManagerImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package com.x8bit.bitwarden.data.vault.manager | ||
|
|
||
| import com.bitwarden.collections.CollectionView | ||
| import com.bitwarden.core.data.util.flatMap | ||
| import com.bitwarden.network.model.CollectionAccessSelectionJson | ||
| import com.bitwarden.network.model.CollectionJsonRequest | ||
| import com.bitwarden.network.model.UpdateCollectionResponseJson | ||
| import com.bitwarden.network.service.CollectionService | ||
| import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource | ||
| import com.x8bit.bitwarden.data.platform.error.NoActiveUserException | ||
| import com.x8bit.bitwarden.data.vault.datasource.disk.VaultDiskSource | ||
| import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource | ||
| import com.x8bit.bitwarden.data.vault.repository.model.CreateCollectionResult | ||
| import com.x8bit.bitwarden.data.vault.repository.model.DeleteCollectionResult | ||
| import com.x8bit.bitwarden.data.vault.repository.model.UpdateCollectionResult | ||
| import com.x8bit.bitwarden.data.vault.repository.util.toEncryptedSdkCollection | ||
|
|
||
| /** | ||
| * The default implementation of the [CollectionManager]. | ||
| */ | ||
| class CollectionManagerImpl( | ||
| private val authDiskSource: AuthDiskSource, | ||
| private val collectionService: CollectionService, | ||
| private val vaultDiskSource: VaultDiskSource, | ||
| private val vaultSdkSource: VaultSdkSource, | ||
| ) : CollectionManager { | ||
| private val activeUserId: String? get() = authDiskSource.userState?.activeUserId | ||
|
|
||
| override suspend fun createCollection( | ||
| organizationId: String, | ||
| organizationUserId: String?, | ||
| collectionView: CollectionView, | ||
| ): CreateCollectionResult { | ||
| val userId = activeUserId | ||
| ?: return CreateCollectionResult.Error(error = NoActiveUserException()) | ||
| // Grant the creating user manage access, matching web client behavior. | ||
| val users = organizationUserId?.let { | ||
| listOf( | ||
| CollectionAccessSelectionJson( | ||
| id = it, | ||
| readOnly = false, | ||
| hidePasswords = false, | ||
| manage = true, | ||
| ), | ||
| ) | ||
| } | ||
| return vaultSdkSource | ||
| .encryptCollection(userId = userId, collectionView = collectionView) | ||
| .flatMap { | ||
| collectionService.createCollection( | ||
| organizationId = organizationId, | ||
| body = CollectionJsonRequest( | ||
| name = it.name, | ||
| users = users, | ||
| ), | ||
| ) | ||
| } | ||
| .onSuccess { | ||
| vaultDiskSource.saveCollection(userId = userId, collection = it) | ||
| } | ||
| .flatMap { | ||
| vaultSdkSource.decryptCollection( | ||
| userId = userId, | ||
| collection = it.toEncryptedSdkCollection(), | ||
| ) | ||
| } | ||
| .fold( | ||
| onSuccess = { CreateCollectionResult.Success(collectionView = it) }, | ||
| onFailure = { CreateCollectionResult.Error(error = it) }, | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun deleteCollection( | ||
| organizationId: String, | ||
| collectionId: String, | ||
| ): DeleteCollectionResult { | ||
| val userId = activeUserId | ||
| ?: return DeleteCollectionResult.Error(error = NoActiveUserException()) | ||
| return collectionService | ||
| .deleteCollection( | ||
| organizationId = organizationId, | ||
| collectionId = collectionId, | ||
| ) | ||
| .onSuccess { | ||
| vaultDiskSource.deleteCollection( | ||
| userId = userId, | ||
| collectionId = collectionId, | ||
| ) | ||
| } | ||
| .fold( | ||
| onSuccess = { DeleteCollectionResult.Success }, | ||
| onFailure = { DeleteCollectionResult.Error(error = it) }, | ||
| ) | ||
| } | ||
|
|
||
| @Suppress("LongMethod") | ||
| override suspend fun updateCollection( | ||
| organizationId: String, | ||
| collectionId: String, | ||
| collectionView: CollectionView, | ||
| ): UpdateCollectionResult { | ||
| val userId = activeUserId | ||
| ?: return UpdateCollectionResult.Error(error = NoActiveUserException()) | ||
| return collectionService | ||
| .getCollectionDetails( | ||
| organizationId = organizationId, | ||
| collectionId = collectionId, | ||
| ) | ||
| .flatMap { details -> | ||
| vaultSdkSource | ||
| .encryptCollection( | ||
| userId = userId, | ||
| collectionView = collectionView, | ||
| ) | ||
| .flatMap { collection -> | ||
| collectionService.updateCollection( | ||
| organizationId = organizationId, | ||
| collectionId = collectionId, | ||
| body = CollectionJsonRequest( | ||
| name = collection.name, | ||
| externalId = details.externalId, | ||
| groups = details.groups, | ||
| users = details.users, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| .fold( | ||
| onSuccess = { response -> | ||
| when (response) { | ||
| is UpdateCollectionResponseJson.Success -> { | ||
| vaultDiskSource.saveCollection( | ||
| userId = userId, | ||
| collection = response.collection, | ||
| ) | ||
| vaultSdkSource | ||
| .decryptCollection( | ||
| userId = userId, | ||
| collection = response.collection | ||
| .toEncryptedSdkCollection(), | ||
| ) | ||
| .fold( | ||
| onSuccess = { | ||
| UpdateCollectionResult.Success(it) | ||
| }, | ||
| onFailure = { | ||
| UpdateCollectionResult.Error(error = it) | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| is UpdateCollectionResponseJson.Invalid -> { | ||
| UpdateCollectionResult.Error( | ||
| errorMessage = response.message, | ||
| error = null, | ||
| ) | ||
| } | ||
| } | ||
| }, | ||
| onFailure = { UpdateCollectionResult.Error(error = it) }, | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought: Encrypt-only in SDK was really only meant as a transition phase while API, state were not mature in the SDK, and the expectation is to move state and API to the SDK too. In this case, moving at least API one layer down into the SDK would already reduce this PR by a large margin, and remove redundant work from the corresponding iOS PR and would allow dropping TS clients code.
Given that this is a net new feature, can we consider just moving this functionality into the SDK in the first place instead of duplicating logic? Moving this down later will cause more effort overall compared to doing it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While that is all true, I think introducing SDK managed state and network calls are out of scope for this PR. The Mobile apps have not migrated to using the SDK for state or network calls anywhere, so this would require a non-trivial change in architecture and introduces a second pattern that we have to actively maintain.