From f8cd9e94c00699b7dbc8cc5477a5b7b54549567a Mon Sep 17 00:00:00 2001 From: Valentina Kiryushkina Date: Mon, 20 Mar 2017 21:58:58 +0300 Subject: [PATCH] Refactor connectors 1) Remove unnecessary access token emptiness check. We have only two states of user in connectors: null and authorized (with id, email and tokens). 2) Simplify authorized client creation based on two states of a user: login if there's no user, update tokens if needed and finally initialize client with up-to-date access token. 3) Move token up-to-date check to Stepik client and create separate client for the request, because it simplifies logic of client authorization --- .../stepik/CCStepicConnector.java | 2 +- .../jetbrains/edu/learning/StudyUtils.java | 6 +- .../edu/learning/checker/StudyCheckTask.java | 18 ++- .../edu/learning/courseFormat/Course.java | 2 +- .../StudyProjectGenerator.java | 6 +- .../stepic/EduAdaptiveStepicConnector.java | 11 +- .../stepic/EduStepicAuthorizedClient.java | 147 ++++++++---------- .../edu/learning/stepic/EduStepicClient.java | 26 ++++ .../learning/stepic/EduStepicConnector.java | 10 +- .../edu/learning/stepic/LoginDialog.java | 2 +- .../learning/stepic/StepicStudyOptions.java | 21 ++- .../learning/stepic/StepicUpdateSettings.java | 9 +- .../edu/learning/stepic/StepicUser.java | 24 ++- .../edu/learning/stepic/StepicWrappers.java | 8 - .../jetbrains/edu/learning/ui/LoginPanel.java | 6 +- .../edu/learning/ui/StudyNewProjectPanel.java | 2 +- 16 files changed, 174 insertions(+), 126 deletions(-) diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/stepik/CCStepicConnector.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/stepik/CCStepicConnector.java index 09c98c5ff5b0..aa72044f372d 100644 --- a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/stepik/CCStepicConnector.java +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/stepik/CCStepicConnector.java @@ -73,7 +73,7 @@ public class CCStepicConnector { LOG.warn("Http client is null"); return; } - final StepicUser currentUser = EduStepicAuthorizedClient.getCurrentUser(client); + final StepicUser currentUser = EduStepicAuthorizedClient.getCurrentUser(); if (currentUser != null) { final List courseAuthors = course.getAuthors(); for (int i = 0; i < courseAuthors.size(); i++) { diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java index 605ef028d2a0..7266ee194759 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java @@ -72,6 +72,7 @@ import com.jetbrains.edu.learning.courseFormat.tasks.TheoryTask; import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator; import com.jetbrains.edu.learning.editor.StudyEditor; import com.jetbrains.edu.learning.stepic.StepicUpdateSettings; +import com.jetbrains.edu.learning.stepic.StepicUser; import com.jetbrains.edu.learning.ui.StudyToolWindow; import com.jetbrains.edu.learning.ui.StudyToolWindowFactory; import com.petebevin.markdown.MarkdownProcessor; @@ -612,9 +613,12 @@ public class StudyUtils { public static File getCourseDirectory(Course course) { final File courseDirectory; if (course.isAdaptive()) { + StepicUser user = StepicUpdateSettings.getInstance().getUser(); + String email = user == null ? "" : user.getEmail(); + courseDirectory = new File(StudyProjectGenerator.OUR_COURSES_DIR, StudyProjectGenerator.ADAPTIVE_COURSE_PREFIX + course.getName() - + "_" + StepicUpdateSettings.getInstance().getUser().getEmail()); + + "_" + email); } else { courseDirectory = new File(StudyProjectGenerator.OUR_COURSES_DIR, course.getName()); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java b/python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java index fb6011c80550..c0db2263bc5c 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java @@ -109,7 +109,8 @@ public class StudyCheckTask extends com.intellij.openapi.progress.Task.Backgroun runAfterTaskCheckedActions(); final Course course = StudyTaskManager.getInstance(myProject).getCourse(); if (course != null && EduNames.STUDY.equals(course.getCourseMode())) { - if (StepicUpdateSettings.getInstance().getUser().getAccessToken() != null) { + StepicUser user = StepicUpdateSettings.getInstance().getUser(); + if (user != null) { EduStepicConnector.postSolution(myTask, testsOutput.isSuccess(), myProject); } } @@ -155,9 +156,18 @@ public class StudyCheckTask extends com.intellij.openapi.progress.Task.Backgroun else if (myTask instanceof TheoryTask) { final int lessonId = myTask.getLesson().getId(); final StepicUser user = StepicUpdateSettings.getInstance().getUser(); - final boolean reactionPosted = EduAdaptiveStepicConnector.postRecommendationReaction(String.valueOf(lessonId), - String.valueOf(user.getId()), - EduAdaptiveStepicConnector.NEXT_RECOMMENDATION_REACTION); + + final boolean reactionPosted; + if (user == null) { + LOG.warn("User is null"); + reactionPosted = false; + } + else { + reactionPosted = EduAdaptiveStepicConnector.postRecommendationReaction(String.valueOf(lessonId), + String.valueOf(user.getId()), + EduAdaptiveStepicConnector.NEXT_RECOMMENDATION_REACTION); + } + if (reactionPosted) { if (myStatusBeforeCheck != StudyStatus.Solved) { myTask.setStatus(StudyStatus.Solved); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/Course.java b/python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/Course.java index f731d9b4b116..392ce6b4c6b2 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/Course.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/Course.java @@ -100,7 +100,7 @@ public class Course { for (String name : authors) { final List firstLast = StringUtil.split(name, " "); if (!firstLast.isEmpty()) { - final StepicUser stepicUser = new StepicUser(); + final StepicUser stepicUser = StepicUser.createEmptyUser(); stepicUser.setFirstName(firstLast.remove(0)); if (firstLast.size() > 0) { stepicUser.setLastName(StringUtil.join(firstLast, " ")); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java b/python/educational-core/student/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java index 67876c51f9ee..fa240e513afb 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java @@ -66,7 +66,7 @@ public class StudyProjectGenerator { public boolean isLoggedIn() { final StepicUser user = StepicUpdateSettings.getInstance().getUser(); - return user.getAccessToken() != null; + return user != null; } public void setEnrolledCoursesIds(@NotNull final List coursesIds) { @@ -122,7 +122,7 @@ public class StudyProjectGenerator { } else { final StepicUser user = StepicUpdateSettings.getInstance().getUser(); - if (user.getAccessToken() != null) { + if (user != null) { final File adaptiveCourseFile = new File(new File(OUR_COURSES_DIR, ADAPTIVE_COURSE_PREFIX + mySelectedCourseInfo.getName() + "_" + user.getEmail()), EduNames.COURSE_META_FILE); @@ -617,7 +617,7 @@ public class StudyProjectGenerator { final ArrayList authors = new ArrayList<>(); for (JsonElement author : courseAuthors) { final JsonObject authorAsJsonObject = author.getAsJsonObject(); - final StepicUser stepicUser = new StepicUser(); + final StepicUser stepicUser = StepicUser.createEmptyUser(); stepicUser.setFirstName(authorAsJsonObject.get("first_name").getAsString()); stepicUser.setLastName(authorAsJsonObject.get("last_name").getAsString()); authors.add(stepicUser); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduAdaptiveStepicConnector.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduAdaptiveStepicConnector.java index 77394456f465..9e74f49dfb4e 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduAdaptiveStepicConnector.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduAdaptiveStepicConnector.java @@ -207,6 +207,7 @@ public class EduAdaptiveStepicConnector { @Nullable private static StepicWrappers.AdaptiveAttemptWrapper.Attempt getAttemptForStep(int id) { final StepicUser user = StepicUpdateSettings.getInstance().getUser(); + if (user == null) return null; try { final List attempts = getAttempts(user, id); if (attempts != null && attempts.size() > 0) { @@ -296,7 +297,10 @@ public class EduAdaptiveStepicConnector { if (course != null && editor != null && editor.getTaskFile() != null) { indicator.checkCanceled(); final StepicUser user = StepicUpdateSettings.getInstance().getUser(); - + if (user == null) { + LOG.warn("Can't get next recommendation: user is null"); + return; + } final boolean recommendationReaction = postRecommendationReaction(String.valueOf(editor.getTaskFile().getTask().getLesson().getId()), String.valueOf(user.getId()), reaction); if (recommendationReaction) { @@ -553,7 +557,7 @@ public class EduAdaptiveStepicConnector { if (client != null) { final StepicUser user = StepicUpdateSettings.getInstance().getUser(); StepicWrappers.ResultSubmissionWrapper wrapper = postResultsForCheck(client, submission); - if (wrapper != null) { + if (wrapper != null && user != null) { wrapper = getCheckResults(client, wrapper, attemptId, user.getId()); if (wrapper.submissions.length > 0) { final String status = wrapper.submissions[0].status; @@ -565,6 +569,9 @@ public class EduAdaptiveStepicConnector { LOG.warn("Got a submission wrapper with incorrect submissions number: " + wrapper.submissions.length); } } + else { + LOG.warn("Can't do adaptive check: " + (wrapper == null ? "wrapper is null" : "user is null")); + } } return Pair.create(false, ""); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicAuthorizedClient.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicAuthorizedClient.java index cf1fce7d435e..ab2bd98e4fd5 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicAuthorizedClient.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicAuthorizedClient.java @@ -5,7 +5,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.text.StringUtil; import org.apache.http.*; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; @@ -36,27 +35,37 @@ public class EduStepicAuthorizedClient { if (ourClient != null) { return ourClient; } - else { - final StepicUser stepicUser = StepicUpdateSettings.getInstance().getUser(); - ourClient = initializeClient(stepicUser); - if (ourClient == null || !isTokenUpToDate(ourClient, stepicUser.getId())) { - ourClient = loginAndInitializeClient(stepicUser); + + StepicUpdateSettings stepicUpdateSettings = StepicUpdateSettings.getInstance(); + + + if (stepicUpdateSettings.getUser() == null) { + final StepicUser user = showLoginDialog(); + if (user != null) { + stepicUpdateSettings.setUser(user); + } + else { + LOG.warn("Unable to login"); + return null; } } - return ourClient; - } - @Nullable - private static CloseableHttpClient loginAndInitializeClient(@NotNull StepicUser stepicUser) { - final StepicUser user = login(stepicUser); - if (user != null) { - StepicUpdateSettings.getInstance().setUser(user); - return initializeClient(stepicUser); - } - else { - LOG.warn("Couldn't initialize client: user is null"); - return null; + StepicUser stepicUser = stepicUpdateSettings.getUser(); + assert stepicUser != null; + + if (!EduStepicClient.isTokenUpToDate(stepicUser.getAccessToken())) { + StepicWrappers.TokenInfo tokens = getUpdatedTokens(stepicUser.getRefreshToken()); + if (tokens != null) { + stepicUser.setTokenInfo(tokens); + } + else { + return null; + } } + + ourClient = createInitializedClient(stepicUser.getAccessToken()); + + return ourClient; } @Nullable @@ -74,17 +83,19 @@ public class EduStepicAuthorizedClient { if (ourClient != null) { return ourClient; } - ourClient = initializeClient(stepicUser); - if (ourClient == null || !isTokenUpToDate(ourClient, stepicUser.getId())) { - final StepicUser user = login(stepicUser); - if (user != null) { - StepicUpdateSettings.getInstance().setUser(user); - ourClient = initializeClient(stepicUser); + + if (!EduStepicClient.isTokenUpToDate(stepicUser.getAccessToken())) { + StepicWrappers.TokenInfo tokenInfo = getUpdatedTokens(stepicUser.getRefreshToken()); + if (tokenInfo != null) { + stepicUser.setTokenInfo(tokenInfo); + } + else { + return EduStepicClient.getHttpClient(); } } - if (ourClient == null) { - ourClient = EduStepicClient.getHttpClient(); - } + + ourClient = createInitializedClient(stepicUser.getAccessToken()); + return ourClient; } @@ -96,36 +107,12 @@ public class EduStepicAuthorizedClient { return EduStepicClient.getFromStepic(link, container, getHttpClient(stepicUser)); } - @Nullable - private static CloseableHttpClient initializeClient(@NotNull final StepicUser stepicUser) { + @NotNull + private static CloseableHttpClient createInitializedClient(@NotNull String accessToken) { final List headers = new ArrayList<>(); - final String accessToken = stepicUser.getAccessToken(); - if (accessToken != null && !accessToken.isEmpty()) { - headers.add(new BasicHeader("Authorization", "Bearer " + accessToken)); - headers.add(new BasicHeader("Content-type", EduStepicNames.CONTENT_TYPE_APP_JSON)); - return getBuilder().setDefaultHeaders(headers).build(); - } - return null; - } - - private static StepicUser login(@NotNull final StepicUser user) { - final String login = user.getEmail(); - final String refreshToken = user.getRefreshToken(); - if (StringUtil.isEmptyOrSpaces(login)) { - return showLoginDialog(); - } - else if (StringUtil.isNotEmpty(refreshToken)) { - final StepicWrappers.TokenInfo tokenInfo = login(refreshToken); - if (tokenInfo != null) { - user.setupTokenInfo(tokenInfo); - final StepicUser currentUser = getCurrentUser(getHttpClient()); - if (currentUser != null) { - user.setId(currentUser.getId()); - } - return user; - } - } - return null; + headers.add(new BasicHeader("Authorization", "Bearer " + accessToken)); + headers.add(new BasicHeader("Content-type", EduStepicNames.CONTENT_TYPE_APP_JSON)); + return getBuilder().setDefaultHeaders(headers).build(); } private static StepicUser showLoginDialog() { @@ -148,28 +135,28 @@ public class EduStepicAuthorizedClient { parameters.add(new BasicNameValuePair("username", email)); parameters.add(new BasicNameValuePair("password", password)); - final StepicUser user = new StepicUser(email, password); final StepicWrappers.TokenInfo tokenInfo = getTokens(parameters); if (tokenInfo != null) { - user.setupTokenInfo(tokenInfo); - final CloseableHttpClient client = getHttpClient(user); - final StepicUser currentUser = getCurrentUser(client); + final StepicUser user = new StepicUser(email, password, tokenInfo); + final StepicUser currentUser = getCurrentUser(); if (currentUser != null) { user.setId(currentUser.getId()); } + ourClient = createInitializedClient(user.getAccessToken()); + + return user; } - ourClient = initializeClient(user); - return user; + + return null; } public static void invalidateClient() { ourClient = null; } - private static StepicWrappers.TokenInfo login(@NotNull final String refreshToken) { + @Nullable + private static StepicWrappers.TokenInfo getUpdatedTokens(@NotNull final String refreshToken) { final List parameters = new ArrayList<>(); - - if (refreshToken.isEmpty()) return null; parameters.add(new BasicNameValuePair("client_id", ourClientId)); parameters.add(new BasicNameValuePair("content-type", "application/json")); parameters.add(new BasicNameValuePair("grant_type", "refresh_token")); @@ -179,17 +166,20 @@ public class EduStepicAuthorizedClient { } @Nullable - public static StepicUser getCurrentUser(CloseableHttpClient client) { - try { - final StepicWrappers.AuthorWrapper wrapper = EduStepicClient.getFromStepic(EduStepicNames.CURRENT_USER, - StepicWrappers.AuthorWrapper.class, - client); - if (wrapper != null && !wrapper.users.isEmpty()) { - return wrapper.users.get(0); + public static StepicUser getCurrentUser() { + CloseableHttpClient client = getHttpClient(); + if (client != null) { + try { + final StepicWrappers.AuthorWrapper wrapper = EduStepicClient.getFromStepic(EduStepicNames.CURRENT_USER, + StepicWrappers.AuthorWrapper.class, + client); + if (wrapper != null && !wrapper.users.isEmpty()) { + return wrapper.users.get(0); + } + } + catch (IOException e) { + LOG.warn("Couldn't get a current user"); } - } - catch (IOException e) { - LOG.warn("Couldn't get author info"); } return null; } @@ -212,7 +202,7 @@ public class EduStepicAuthorizedClient { return gson.fromJson(responseString, StepicWrappers.TokenInfo.class); } else { - LOG.warn("Failed to Login: " + statusLine.getStatusCode() + statusLine.getReasonPhrase()); + LOG.warn("Failed to get tokens: " + statusLine.getStatusCode() + statusLine.getReasonPhrase()); } } catch (IOException e) { @@ -220,9 +210,4 @@ public class EduStepicAuthorizedClient { } return null; } - - private static boolean isTokenUpToDate(@NotNull CloseableHttpClient client, int userId) { - final StepicUser user = getCurrentUser(client); - return user != null && userId == user.getId() && userId != -1; - } } diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicClient.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicClient.java index 83a19bfd3476..d81b6f8bd53c 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicClient.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicClient.java @@ -18,6 +18,7 @@ import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.jetbrains.annotations.NotNull; @@ -30,6 +31,7 @@ import java.net.URI; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.List; public class EduStepicClient { @@ -102,4 +104,28 @@ public class EduStepicClient { } return builder; } + + static boolean isTokenUpToDate(@NotNull String token) { + if (token.isEmpty()) return false; + + final List headers = new ArrayList<>(); + headers.add(new BasicHeader("Authorization", "Bearer " + token)); + headers.add(new BasicHeader("Content-type", EduStepicNames.CONTENT_TYPE_APP_JSON)); + CloseableHttpClient httpClient = getBuilder().setDefaultHeaders(headers).build(); + + try { + final StepicWrappers.AuthorWrapper wrapper = getFromStepic(EduStepicNames.CURRENT_USER, StepicWrappers.AuthorWrapper.class, httpClient); + if (wrapper != null && !wrapper.users.isEmpty()) { + StepicUser user = wrapper.users.get(0); + return user != null && !user.isGuest(); + } + else { + throw new IOException(wrapper == null ? "Got a null current user" : "Got an empty wrapper"); + } + } + catch (IOException e) { + LOG.warn(e.getMessage()); + return false; + } + } } diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicConnector.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicConnector.java index af284c60e58f..c9d046918286 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicConnector.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicConnector.java @@ -42,8 +42,8 @@ public class EduStepicConnector { private EduStepicConnector() { } - public static boolean enrollToCourse(final int courseId, final StepicUser stepicUser) { - if (stepicUser == null || stepicUser.getAccessToken() == null) return false; + public static boolean enrollToCourse(final int courseId, @Nullable final StepicUser stepicUser) { + if (stepicUser == null) return false; HttpPost post = new HttpPost(EduStepicNames.STEPIC_API_URL + EduStepicNames.ENROLLMENTS); try { final StepicWrappers.EnrollmentWrapper enrollment = new StepicWrappers.EnrollmentWrapper(String.valueOf(courseId)); @@ -131,7 +131,7 @@ public class EduStepicConnector { return false; } final StepicWrappers.CoursesContainer coursesContainer; - if (user != null && user.getAccessToken() != null) { + if (user != null) { coursesContainer = EduStepicAuthorizedClient.getFromStepic(url.toString(), StepicWrappers.CoursesContainer.class, user); } else { @@ -256,7 +256,7 @@ public class EduStepicConnector { private static T getFromStepic(String link, final Class container) throws IOException{ final StepicUser user = StepicUpdateSettings.getInstance().getUser(); - final boolean isAuthorized = user.getAccessToken() != null; + final boolean isAuthorized = user != null; if (isAuthorized) { return EduStepicAuthorizedClient.getFromStepic(link, container, user); } @@ -351,7 +351,7 @@ public class EduStepicConnector { public static String postAttempt(int id) throws IOException { final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient(); - if (client == null || StepicUpdateSettings.getInstance().getUser().getAccessToken() == null) return ""; + if (client == null || StepicUpdateSettings.getInstance().getUser() == null) return ""; final HttpPost attemptRequest = new HttpPost(EduStepicNames.STEPIC_API_URL + EduStepicNames.ATTEMPTS); String attemptRequestBody = new Gson().toJson(new StepicWrappers.AttemptWrapper(id)); attemptRequest.setEntity(new StringEntity(attemptRequestBody, ContentType.APPLICATION_JSON)); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginDialog.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginDialog.java index 86b97c09d8c3..5eda5a5c0be1 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginDialog.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/LoginDialog.java @@ -43,7 +43,7 @@ public class LoginDialog extends DialogWrapper { protected void doOKAction() { if (!validateLoginAndPasswordFields()) return; myStepicUser = EduStepicAuthorizedClient.login(myLoginPanel.getLogin(), myLoginPanel.getPassword()); - if (myStepicUser != null && myStepicUser.getId() > 0 && myStepicUser.getAccessToken() != null) { + if (myStepicUser != null && myStepicUser.getId() > 0) { doJustOkAction(); } else { diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicStudyOptions.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicStudyOptions.java index 34b2e472feac..14ee91102936 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicStudyOptions.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicStudyOptions.java @@ -82,8 +82,10 @@ public class StepicStudyOptions implements StudyOptionsProvider { final StepicUpdateSettings stepikSettings = StepicUpdateSettings.getInstance(); myEnableTestingFromSamples.setSelected(stepikSettings.isEnableTestingFromSamples()); final StepicUser user = stepikSettings.getUser(); - setLogin(user.getEmail()); - setPassword(user.getPassword()); + if (user != null) { + setLogin(user.getEmail()); + setPassword(user.getPassword()); + } } @Override @@ -99,7 +101,9 @@ public class StepicStudyOptions implements StudyOptionsProvider { } final StepicUser user = stepikSettings.getUser(); - final boolean isCredentialsModified = !getLogin().equals(user.getEmail()) || !getPassword().equals(user.getPassword()); + String savedEmail = user == null ? "" : user.getEmail(); + String savedPassword = user == null ? "" : user.getPassword(); + final boolean isCredentialsModified = !getLogin().equals(savedEmail) || !getPassword().equals(savedPassword); if (isCredentialsModified) { final String login = getLogin(); final String password = getPassword(); @@ -113,7 +117,7 @@ public class StepicStudyOptions implements StudyOptionsProvider { }, "Logging In", true, null); - if (stepicUser[0] != null && stepicUser[0].getAccessToken() != null) { + if (stepicUser[0] != null) { stepikSettings.setUser(stepicUser[0]); } else { @@ -127,7 +131,7 @@ public class StepicStudyOptions implements StudyOptionsProvider { } private static void removeCredentials() { - StepicUpdateSettings.getInstance().setUser(new StepicUser()); + StepicUpdateSettings.getInstance().setUser(null); EduStepicAuthorizedClient.invalidateClient(); } @@ -142,8 +146,11 @@ public class StepicStudyOptions implements StudyOptionsProvider { boolean isTestOptionModified = !isTestingFromSamplesEnabled() == stepikSettings.isEnableTestingFromSamples(); final StepicUser user = stepikSettings.getUser(); - return !getLogin().equals(user.getEmail()) - || !getPassword().equals(user.getPassword()) + String email = user == null ? "" : user.getEmail(); + String password = user == null ? "" : user.getPassword(); + + return !getLogin().equals(email) + || !getPassword().equals(password) || isTestOptionModified; } diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicUpdateSettings.java b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicUpdateSettings.java index 56b26f0377bf..94b2d9b6c817 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicUpdateSettings.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StepicUpdateSettings.java @@ -5,7 +5,6 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.util.xmlb.XmlSerializerUtil; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @State(name = "StepicUpdateSettings", storages = @Storage("other.xml")) @@ -39,15 +38,13 @@ public class StepicUpdateSettings implements PersistentStateComponent EduStepicAuthorizedClient.login(myLoginPanel.getLogin(), myLoginPanel.getPassword())); - if (stepicUser != null && stepicUser.getAccessToken() != null) { + if (stepicUser != null) { stepicUser.setEmail(myLoginPanel.getLogin()); stepicUser.setPassword(myLoginPanel.getPassword()); StepicUpdateSettings.getInstance().setUser(stepicUser);