Skip to content

Commit

Permalink
Merge pull request #209 from teldosas/account-remove
Browse files Browse the repository at this point in the history
Move remove account to the Authenticator
  • Loading branch information
emansih authored Mar 12, 2022
2 parents 3965904 + 1d5e49d commit 9048915
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class NewAccountManager (private val accountManager: AccountManager,
get() = accountManager.getUserData(account, "AUTH_METHOD") ?: ""
set(value) = accountManager.setUserData(account, "AUTH_METHOD", value)

fun destroyAccount(){
accountManager.removeAccount(account, null, null, null)
}
fun destroyAccount() = accountManager.removeAccount(account, null, null, null)

var tokenExpiry
get() = accountManager.getUserData(account, "token_expires_in").toLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.work.WorkManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import xyz.hisname.fireflyiii.Constants
Expand Down Expand Up @@ -65,25 +66,24 @@ class FireflyAccountAuthenticator(private val context: Context): AbstractAccount

override fun getAccountRemovalAllowed(response: AccountAuthenticatorResponse, account: Account): Bundle {
runBlocking(Dispatchers.IO){
val fireflyUserDatabase = FireflyUserDatabase.getInstance(context).fireflyUserDao()
fireflyUserDatabase.deleteCurrentUser()
val customCaFile = File(context.applicationInfo.dataDir + "/" + account.name + ".pem")
val customPref = File(context.applicationInfo.dataDir + "/shared_prefs/" + account.name + "-user-preferences.xml")
if(customCaFile.exists()){
customCaFile.delete()
}
customPref.delete()
val fireflyUsers: List<FireflyUsers>
runBlocking(Dispatchers.IO) {
fireflyUsers = fireflyUserDatabase.getAllUser()
}
if(fireflyUsers.isNotEmpty()){
runBlocking(Dispatchers.IO){
fireflyUserDatabase.updateActiveUser(fireflyUsers[0].uniqueHash, true)
context.deleteDatabase(account.name + "-photuris.db")
context.deleteDatabase(account.name + "-temp-" + Constants.DB_NAME + "-photuris.db")
context.deleteFile(account.name + ".pem")
val sharedPrefDir = File(context.applicationInfo.dataDir + "/shared_prefs")
sharedPrefDir.resolve(account.name + "-user-preferences.xml").delete()

val fireflyUserDao = FireflyUserDatabase.getInstance(context).fireflyUserDao()
val userToBeDeleted = fireflyUserDao.getUserByHash(account.name)
if (userToBeDeleted != null) {
fireflyUserDao.deleteUserByPrimaryKey(userToBeDeleted.id)

val fireflyUsers = fireflyUserDao.getAllUser()
if(userToBeDeleted.activeUser && fireflyUsers.isNotEmpty()){
fireflyUserDao.updateActiveUser(fireflyUsers[0].uniqueHash, true)
}
}
File(context.getDatabasePath(account.name + "-temp-" + Constants.DB_NAME).toString()).delete()
File(context.getDatabasePath(account.name + "-photuris.db").toString()).delete()

WorkManager.getInstance(context).cancelAllWorkByTag(account.name)
}
return super.getAccountRemovalAllowed(response, account)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class HomeActivity: BaseActivity(){
authenticator = Authenticator(this, ::handleResult)
authenticator.authenticate()
}
//homeViewModel.deleteUnusedAccount()
homeViewModel.deleteUnusedAccount()
}
}

Expand Down
48 changes: 15 additions & 33 deletions app/src/main/java/xyz/hisname/fireflyiii/ui/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,30 @@ class HomeViewModel(application: Application): BaseViewModel(application) {
fun getFireflyUsers(): LiveData<List<FireflyUsers>>{
val usersLiveData = MutableLiveData<List<FireflyUsers>>()
viewModelScope.launch(Dispatchers.IO){
removeInvalidFireflyAccounts()
usersLiveData.postValue(fireflyUserDatabase.getAllUser())
}
return usersLiveData
}

private suspend fun removeInvalidFireflyAccounts() {
fireflyUserDatabase.getAllUser()
.filter { it.userEmail.isEmpty() }
.forEach { removeFireflyAccount(it.uniqueHash) }
}

fun removeFireflyAccounts(fireflyUsers: List<FireflyUsers>){
viewModelScope.launch(Dispatchers.IO){
var isDefault = false
val allUsers = fireflyUserDatabase.getAllUser()
fireflyUsers.forEach { user ->
val fireflyUser = fireflyUserDatabase.getUserByHash(user.uniqueHash)
isDefault = fireflyUser.activeUser
File(getApplication<Application>().applicationInfo.dataDir + "/shared_prefs/" + fireflyUser.uniqueHash
+ "-user-preferences.xml").delete()
fireflyUserDatabase.deleteUserByPrimaryKey(fireflyUser.id)
File(getApplication<Application>().applicationInfo.dataDir + "/databases/" + fireflyUser.uniqueHash
+ "-photuris.db").delete()
File(getApplication<Application>().applicationInfo.dataDir + fireflyUser.uniqueHash + ".pem").delete()
val accountManager = NewAccountManager(AccountManager.get(getApplication()), fireflyUser.uniqueHash)
accountManager.destroyAccount()
WorkManager.getInstance(getApplication()).cancelAllWorkByTag(user.uniqueHash)
}
// Check if all users are being deleted. can't set a default user if there are no users in DB.
if(isDefault && allUsers.size != fireflyUsers.size){
val uniqueHash = fireflyUserDatabase.getAllUser()[0].uniqueHash
fireflyUserDatabase.updateActiveUser(uniqueHash, true)
}
fireflyUsers.forEach { removeFireflyAccount(it.uniqueHash) }
}
}

private fun removeFireflyAccount(uuid: String) {
val app = getApplication<Application>()
val accountManager = NewAccountManager(AccountManager.get(app), uuid)
accountManager.destroyAccount().result
}

fun migrateFirefly(){
/* Migration routine:
* 1. Check if old database exists(firefly.db)
Expand Down Expand Up @@ -207,19 +201,7 @@ class HomeViewModel(application: Application): BaseViewModel(application) {
userArray.add(fireflyUsers.uniqueHash)
}
val accountToRemove = accountArray.filterNot { userArray.contains(it) }
accountToRemove.forEach { uuid ->
NewAccountManager(AccountManager.get(getApplication()), uuid).destroyAccount()
val customCaFile = File(getApplication<Application>().applicationInfo.dataDir
+ "/" + uuid + ".pem")
val customPref = File(getApplication<Application>().applicationInfo.dataDir
+ "/shared_prefs/" + uuid + "-user-preferences.xml")
if(customCaFile.exists()){
customCaFile.delete()
}
customPref.delete()
File(getApplication<Application>().getDatabasePath("$uuid-temp-" + Constants.DB_NAME).toString()).delete()
File(getApplication<Application>().getDatabasePath("$uuid-photuris.db").toString()).delete()
}
accountToRemove.forEach { removeFireflyAccount(it) }
}
}

Expand Down

0 comments on commit 9048915

Please sign in to comment.