Skip to content

Commit

Permalink
Mobileapps 1076 (#56)
Browse files Browse the repository at this point in the history
* checking if session is available or not (#78)

* Mobileapps 1054 (#79)

Login user if session is expired

Co-authored-by: AnniNygren <[email protected]>
Co-authored-by: Gloria Camino <[email protected]>
Co-authored-by: Lovekesh Saxena <[email protected]>
  • Loading branch information
4 people authored Feb 17, 2022
1 parent c4a9765 commit 8c56dd5
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import com.airbnb.mvrx.MavericksView
import com.airbnb.mvrx.withState
import com.alfresco.auth.activity.LoginViewModel
import com.alfresco.content.activityViewModel
import com.alfresco.content.app.R
import com.alfresco.content.app.widget.ActionBarController
import com.alfresco.content.session.SessionManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.lang.ref.WeakReference

/**
* Marked as ExtensionActivity class
Expand All @@ -20,6 +25,7 @@ class ExtensionActivity : AppCompatActivity(), MavericksView {
private val viewModel: MainActivityViewModel by activityViewModel()
private val navController by lazy { findNavController(R.id.nav_host_fragment) }
private lateinit var actionBarController: ActionBarController
private var signedOutDialog = WeakReference<AlertDialog>(null)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -64,6 +70,48 @@ class ExtensionActivity : AppCompatActivity(), MavericksView {
}
}

override fun invalidate() = withState(viewModel) {
override fun invalidate() = withState(viewModel) { state ->

if (state.requiresReLogin) {
if (state.isOnline) {
showSignedOutPrompt()
}
} else {
// Only when logged in otherwise triggers re-login prompts
actionBarController.setProfileIcon(viewModel.profileIcon)
}

actionBarController.setOnline(state.isOnline)
}

private fun showSignedOutPrompt() {
val oldDialog = signedOutDialog.get()
if (oldDialog != null && oldDialog.isShowing) return
val dialog = MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.auth_signed_out_title))
.setMessage(resources.getString(R.string.auth_signed_out_subtitle))
.setNegativeButton(resources.getString(R.string.sign_out_confirmation_negative), null)
.setPositiveButton(resources.getString(R.string.auth_basic_sign_in_button)) { _, _ ->
navigateToReLogin()
}
.show()
signedOutDialog = WeakReference(dialog)
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
println("ExtensionActivity.onNewIntent")
}

private fun navigateToReLogin() {
val i = Intent(this, LoginActivity::class.java)
val acc = SessionManager.requireSession.account
i.putExtra(LoginViewModel.EXTRA_IS_EXTENSION, true)
i.putExtra(LoginViewModel.EXTRA_ENDPOINT, acc.serverUrl)
i.putExtra(LoginViewModel.EXTRA_AUTH_TYPE, acc.authType)
i.putExtra(LoginViewModel.EXTRA_AUTH_CONFIG, acc.authConfig)
i.putExtra(LoginViewModel.EXTRA_AUTH_STATE, acc.authState)
startActivity(i)
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlinx.coroutines.launch

class LoginActivity : com.alfresco.auth.activity.LoginActivity() {

override fun onCredentials(credentials: Credentials, endpoint: String, authConfig: AuthConfig) {
override fun onCredentials(credentials: Credentials, endpoint: String, authConfig: AuthConfig, isExtension: Boolean) {
val account = Account(credentials.username, credentials.authState, credentials.authType, authConfig.jsonSerialize(), endpoint)
val context = applicationContext

Expand All @@ -26,7 +26,9 @@ class LoginActivity : com.alfresco.auth.activity.LoginActivity() {
val person = PeopleRepository(session).me()
val myFiles = BrowseRepository(session).myFilesNodeId()
processAccountInformation(person, myFiles, credentials, authConfig, endpoint)
navigateToMain()
if (isExtension)
navigateToExtension()
else navigateToMain()
} catch (ex: Exception) {
onError(R.string.auth_error_wrong_credentials)
}
Expand Down Expand Up @@ -71,4 +73,11 @@ class LoginActivity : com.alfresco.auth.activity.LoginActivity() {
startActivity(i)
finish()
}

private fun navigateToExtension() {
val i = Intent(this, ExtensionActivity::class.java)
i.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(i)
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class MainActivity : AppCompatActivity(), MavericksView {
private fun navigateToReLogin() {
val i = Intent(this, LoginActivity::class.java)
val acc = SessionManager.requireSession.account
i.putExtra(LoginViewModel.EXTRA_IS_EXTENSION, false)
i.putExtra(LoginViewModel.EXTRA_ENDPOINT, acc.serverUrl)
i.putExtra(LoginViewModel.EXTRA_AUTH_TYPE, acc.authType)
i.putExtra(LoginViewModel.EXTRA_AUTH_CONFIG, acc.authConfig)
Expand Down
12 changes: 7 additions & 5 deletions auth/src/main/kotlin/com/alfresco/auth/activity/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,22 @@ abstract class LoginActivity : AuthenticationActivity<LoginViewModel>() {
}
}

abstract fun onCredentials(credentials: Credentials, endpoint: String, authConfig: AuthConfig)
abstract fun onCredentials(credentials: Credentials, endpoint: String, authConfig: AuthConfig, isExtension: Boolean)

override fun onCredentials(credentials: Credentials) {
onCredentials(credentials, viewModel.canonicalApplicationUrl, viewModel.authConfig)
onCredentials(credentials, viewModel.canonicalApplicationUrl, viewModel.authConfig, viewModel.isExtension)
}

override fun onError(error: String) {
// Hide progress view on error
viewModel.isLoading.value = false

val parentLayout: View = findViewById(android.R.id.content)
Snackbar.make(parentLayout,
error,
Snackbar.LENGTH_LONG).show()
Snackbar.make(
parentLayout,
error,
Snackbar.LENGTH_LONG
).show()

// Hacky way to set error state to match design
updateAll<TextInputLayout> {
Expand Down
42 changes: 29 additions & 13 deletions auth/src/main/kotlin/com/alfresco/auth/activity/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginViewModel(private val applicationContext: Context, authType: AuthType?, authState: String?, authConfig: AuthConfig?, endpoint: String?) : AuthenticationViewModel() {
class LoginViewModel(
private val applicationContext: Context,
authType: AuthType?,
authState: String?,
authConfig: AuthConfig?,
endpoint: String?,
val isExtension: Boolean
) : AuthenticationViewModel() {

lateinit var authConfig: AuthConfig
override var context = applicationContext
Expand Down Expand Up @@ -51,7 +58,7 @@ class LoginViewModel(private val applicationContext: Context, authType: AuthType
val canonicalApplicationUrl: String
get() {
return previousAppEndpoint
?: discoveryService.contentServiceUrl(applicationUrl.value!!).toString()
?: discoveryService.contentServiceUrl(applicationUrl.value!!).toString()
}

// Used for display purposes
Expand Down Expand Up @@ -190,16 +197,19 @@ class LoginViewModel(private val applicationContext: Context, authType: AuthType
this.isLoading.value = false

when (step) {
Step.InputIdentityServer -> { }
Step.InputIdentityServer -> {
}
Step.InputAppServer -> {
applicationUrl.value = ""
}
Step.EnterBasicCredentials -> {
// Assume application url is the same as identity for basic auth
applicationUrl.value = identityUrl.value
}
Step.EnterPkceCredentials -> { }
Step.Cancelled -> { }
Step.EnterPkceCredentials -> {
}
Step.Cancelled -> {
}
}

_step.value = step
Expand All @@ -214,6 +224,7 @@ class LoginViewModel(private val applicationContext: Context, authType: AuthType
}

val basicAuth = BasicAuth()

inner class BasicAuth {
private val _enabled = MediatorLiveData<Boolean>()

Expand Down Expand Up @@ -241,6 +252,7 @@ class LoginViewModel(private val applicationContext: Context, authType: AuthType
const val SHARED_PREFS_CONFIG_KEY = "config"
const val DISTRIBUTION_VERSION = "DISTRIBUTION_VERSION"

const val EXTRA_IS_EXTENSION = "is_extension"
const val EXTRA_ENDPOINT = "endpoint"
const val EXTRA_AUTH_TYPE = "authType"
const val EXTRA_AUTH_STATE = "authState"
Expand All @@ -251,19 +263,23 @@ class LoginViewModel(private val applicationContext: Context, authType: AuthType
var stateString: String? = null
var authType: AuthType? = null
var endpoint: String? = null
var isExtension = false

val extras = intent.extras
if (extras != null) {
config = try {
AuthConfig.jsonDeserialize(extras.getString(EXTRA_AUTH_CONFIG)!!)
} catch (ex: Exception) { null }
} catch (ex: Exception) {
null
}

stateString = extras.getString(EXTRA_AUTH_STATE)
endpoint = extras.getString(EXTRA_ENDPOINT)
isExtension = extras.getBoolean(EXTRA_IS_EXTENSION)
authType = extras.getString(EXTRA_AUTH_TYPE)?.let { AuthType.fromValue(it) }
}

return LoginViewModel(context, authType, stateString, config, endpoint)
return LoginViewModel(context, authType, stateString, config, endpoint, isExtension)
}
}

Expand Down Expand Up @@ -334,12 +350,12 @@ class LoginViewModel(private val applicationContext: Context, authType: AuthType

fun get(): AuthConfig {
return AuthConfig(
https = https.value ?: false,
port = port.value ?: "",
contentServicePath = contentServicePath.value ?: "",
realm = realm.value ?: "",
clientId = clientId.value ?: "",
redirectUrl = redirectUrl
https = https.value ?: false,
port = port.value ?: "",
contentServicePath = contentServicePath.value ?: "",
realm = realm.value ?: "",
clientId = clientId.value ?: "",
redirectUrl = redirectUrl
)
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ coil = "1.2.2"
coroutines = "1.5.0"
epoxy = "4.6.1"
exoplayer = "2.14.0"
kotlin = "1.5.0"
kotlin = "1.5.30"
lifecycle = "2.3.1"
navigation = "2.3.5"
okhttp = "4.9.1"
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ include ':viewer-pdf'
include ':viewer-image'
include ':viewer-text'
include ':viewer-media'
include ':shareextension'
include ':app'
//include ':content'
//include ':content-ktx'
//include ':authalfresco'

// Enable Gradle's version catalog support
// Ref: https://docs.gradle.org/current/userguide/platforms.html
enableFeaturePreview("VERSION_CATALOGS")
include ':shareextension'

0 comments on commit 8c56dd5

Please sign in to comment.