Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
fix: NPE on accessing ProfileModel
Browse files Browse the repository at this point in the history
- check user session before making API calls
- logout in case user session is not available
LEARNER-9223
  • Loading branch information
omerhabib26 committed Feb 13, 2023
1 parent f908a2e commit 265fc86
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public String getUsername() {

@Nullable
public String getUserEmail() {
return getCurrentUserProfile().email;
return isUserLoggedIn() ? getCurrentUserProfile().email : null;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ object CalendarUtils {
*/
@JvmStatic
fun getUserAccountForSync(environment: IEdxEnvironment): String {
return environment.loginPrefs.currentUserProfile.email ?: LOCAL_USER
return environment.loginPrefs.userEmail ?: LOCAL_USER
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.edx.mobile.base.BaseFragmentActivity;
import org.edx.mobile.course.CourseAPI;
import org.edx.mobile.databinding.ActivityCourseBaseBinding;
import org.edx.mobile.event.LogoutEvent;
import org.edx.mobile.http.notifications.FullScreenErrorNotification;
import org.edx.mobile.interfaces.RefreshListener;
import org.edx.mobile.model.api.CourseUpgradeResponse;
Expand All @@ -19,6 +20,7 @@
import org.edx.mobile.services.CourseManager;
import org.edx.mobile.view.common.MessageType;
import org.edx.mobile.view.common.TaskProcessCallback;
import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

Expand Down Expand Up @@ -110,6 +112,10 @@ protected void restore(Bundle savedInstanceState) {
* Method to force update the course structure from server.
*/
protected void updateCourseStructure(String courseId, String componentId) {
if (!environment.getLoginPrefs().isUserLoggedIn()) {
EventBus.getDefault().post(new LogoutEvent());
return;
}
getHierarchyCall = courseApi.getCourseStructureWithoutStale(courseId);
getHierarchyCall.enqueue(new CourseAPI.GetCourseStructureCallback(this, courseId,
new ProgressViewController(binding.loadingIndicator.loadingIndicator), errorNotification,
Expand Down Expand Up @@ -143,7 +149,7 @@ protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// If the data is available then trigger the callback
// after basic initialization
if (courseComponentId != null) {
if (courseComponentId != null && environment.getLoginPrefs().isUserLoggedIn()) {
onLoadData();
}
}
Expand Down Expand Up @@ -216,6 +222,10 @@ protected boolean isOnCourseOutline() {
@Override
public void onRefresh() {
errorNotification.hideError();
if (!environment.getLoginPrefs().isUserLoggedIn()) {
EventBus.getDefault().post(new LogoutEvent());
return;
}
if (isOnCourseOutline()) {
if (getIntent() != null) {
restore(getIntent().getBundleExtra(Router.EXTRA_BUNDLE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.edx.mobile.deeplink.ScreenDef;
import org.edx.mobile.event.CourseDashboardRefreshEvent;
import org.edx.mobile.event.CourseUpgradedEvent;
import org.edx.mobile.event.LogoutEvent;
import org.edx.mobile.event.MainDashboardRefreshEvent;
import org.edx.mobile.event.MediaStatusChangeEvent;
import org.edx.mobile.event.NetworkConnectivityChangeEvent;
Expand Down Expand Up @@ -412,7 +413,11 @@ private void fetchCourseComponent() {
// Check if course data is available in persistable cache
loadingIndicator.setVisibility(View.VISIBLE);
// Prepare the loader. Either re-connect with an existing one or start a new one.
getLoaderManager().initLoader(0, null, this);
if (environment.getLoginPrefs().isUserLoggedIn()) {
getLoaderManager().initLoader(0, null, this);
} else {
EventBus.getDefault().post(new LogoutEvent());
}
}

private void trackAATestCourseOutline() {
Expand Down Expand Up @@ -461,6 +466,10 @@ public void onLoaderReset(Loader<AsyncTaskResult<CourseComponent>> loader) {
}

public void getCourseComponentFromServer(boolean showProgress, boolean forceRefresh) {
if (!environment.getLoginPrefs().isUserLoggedIn()) {
EventBus.getDefault().post(new LogoutEvent());
return;
}
if (loadingIndicator.getVisibility() == View.VISIBLE) {
showProgress = true;
}
Expand All @@ -486,6 +495,9 @@ protected void onResponse(@NonNull final CourseComponent courseComponent) {
@Override
protected void onFailure(@NonNull Throwable error) {
super.onFailure(error);
if (!isAdded()) {
return;
}
if (error instanceof CourseContentNotValidException) {
errorNotification.showError(getContext(), error);
logger.error(error, true);
Expand Down Expand Up @@ -992,7 +1004,7 @@ public void onRevisit() {
}

public void fetchLastAccessed() {
if (isOnCourseOutline && !isVideoMode) {
if (isOnCourseOutline && !isVideoMode && environment.getLoginPrefs().isUserLoggedIn()) {
courseApi.getCourseStatusInfo(courseData.getCourseId()).enqueue(
new ErrorHandlingCallback<CourseComponentStatusResponse>(
getContextOrThrow()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ class MyCoursesListFragment : OfflineSupportBaseFragment(), RefreshListener {
viewLifecycleOwner,
NonNullObserver { enrolledCourses ->
populateCourseData(data = enrolledCourses)
if (environment.appFeaturesPrefs.isIAPEnabled(environment.loginPrefs.isOddUserId)) {
// Checking if the user is logged-in as we need userId to enable IAP
if (environment.loginPrefs.isUserLoggedIn &&
environment.appFeaturesPrefs.isIAPEnabled(environment.loginPrefs.isOddUserId)
) {
initInAppPurchaseSetup()
resetPurchase()
iapViewModel.detectUnfulfilledPurchase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.edx.mobile.core.IEdxEnvironment
import org.edx.mobile.http.HttpStatus
import org.edx.mobile.http.HttpStatusException
import org.edx.mobile.http.model.NetworkResponseCallback
import org.edx.mobile.http.model.Result
import org.edx.mobile.logger.Logger
Expand Down Expand Up @@ -37,6 +39,10 @@ class CourseViewModel @Inject constructor(
type: CoursesRequestType,
showProgress: Boolean = true
) {
if (environment.loginPrefs.isUserLoggedIn.not()) {
_handleError.value = HttpStatusException(HttpStatus.UNAUTHORIZED, "")
return
}
_showProgress.postValue(showProgress)
courseRepository.fetchEnrolledCourses(
type = type,
Expand Down

0 comments on commit 265fc86

Please sign in to comment.