Skip to content

Commit

Permalink
Fixed adding duplicate accounts.| #716
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 26, 2020
1 parent b636360 commit 6085fdf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import java.util.*
*/
class OAuth2AuthCredentialsViewModel(application: Application) : BaseAndroidViewModel(application) {
private val apiRepository: ApiRepository = FlowcryptApiRepository()
val microsoftOAuth2TokenLiveData = MutableLiveData<Result<AuthCredentials>>()
val microsoftOAuth2TokenLiveData = MutableLiveData<Result<AuthCredentials?>>()
val authorizationRequestLiveData = MutableLiveData<Result<AuthorizationRequest>>()

fun getAuthorizationRequestForProvider(requestCode: Long = 0L, provider: OAuth2Helper.Provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,6 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour,
}
}

private fun storeAccountInfoToAccountManager(accountEntity: AccountEntity, authCreds: AuthCredentials) {
val accountManager = AccountManager.get(requireContext())
val account = Account(accountEntity.email.toLowerCase(Locale.US), FlowcryptAccountAuthenticator.ACCOUNT_TYPE)
accountManager.addAccountExplicitly(account, null, Bundle().apply {
with(authCreds.authTokenInfo) {
putString(FlowcryptAccountAuthenticator.KEY_ACCOUNT_EMAIL, this?.email)
putString(FlowcryptAccountAuthenticator.KEY_REFRESH_TOKEN, this?.refreshToken)
putString(FlowcryptAccountAuthenticator.KEY_EXPIRES_AT, this?.expiresAt?.toString())
}
})
}

fun handleOAuth2Intent(intent: Intent?) {
intent?.let {
val schema = intent.data?.scheme
Expand Down Expand Up @@ -629,16 +617,32 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour,
}

Result.Status.SUCCESS -> {
oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.removeObservers(viewLifecycleOwner)
it.data?.let { authCredentials ->
authCreds = authCredentials
val account = AccountEntity(authCredentials).copy(password = authCredentials
.peekPassword(), smtpPassword = authCredentials.peekSmtpPassword())
val nextFrag = AuthorizeAndSearchBackupsFragment.newInstance(account)
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.fragmentContainerView, nextFrag, AuthorizeAndSearchBackupsFragment::class.java.simpleName)
?.addToBackStack(null)
?.commit()

val existedAccount = existedAccounts.firstOrNull { account ->
account.email.equals(authCredentials.email, ignoreCase = true)
}

if (existedAccount == null) {
oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.removeObservers(viewLifecycleOwner)
val account = AccountEntity(authCredentials).copy(
password = authCredentials.peekPassword(),
smtpPassword = authCredentials.peekSmtpPassword()
)

val nextFrag = AuthorizeAndSearchBackupsFragment.newInstance(account)
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.fragmentContainerView, nextFrag, AuthorizeAndSearchBackupsFragment::class.java.simpleName)
?.addToBackStack(null)
?.commit()

return@let
} else {
oAuth2AuthCredentialsViewModel.microsoftOAuth2TokenLiveData.value = Result.success(null)
showContent()
showInfoSnackbar(msgText = getString(R.string.template_email_alredy_added, existedAccount.email), duration = Snackbar.LENGTH_LONG)
}
}
}

Expand Down Expand Up @@ -923,6 +927,18 @@ class AddOtherAccountFragment : BaseSingInFragment(), ProgressBehaviour,
}
}

private fun storeAccountInfoToAccountManager(accountEntity: AccountEntity, authCreds: AuthCredentials) {
val accountManager = AccountManager.get(requireContext())
val account = Account(accountEntity.email.toLowerCase(Locale.US), FlowcryptAccountAuthenticator.ACCOUNT_TYPE)
accountManager.addAccountExplicitly(account, null, Bundle().apply {
with(authCreds.authTokenInfo) {
putString(FlowcryptAccountAuthenticator.KEY_ACCOUNT_EMAIL, this?.email)
putString(FlowcryptAccountAuthenticator.KEY_REFRESH_TOKEN, this?.refreshToken)
putString(FlowcryptAccountAuthenticator.KEY_EXPIRES_AT, this?.expiresAt?.toString())
}
})
}

companion object {
private val KEY_AUTH_REQUEST =
GeneralUtil.generateUniqueExtraKey("KEY_UUID_FOR_OAUTH", AddOtherAccountFragment::class.java)
Expand Down

0 comments on commit 6085fdf

Please sign in to comment.