mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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
This commit is contained in:
+1
-1
@@ -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<StepicUser> courseAuthors = course.getAuthors();
|
||||
for (int i = 0; i < courseAuthors.size(); i++) {
|
||||
|
||||
@@ -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());
|
||||
|
||||
+14
-4
@@ -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);
|
||||
|
||||
+1
-1
@@ -100,7 +100,7 @@ public class Course {
|
||||
for (String name : authors) {
|
||||
final List<String> 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, " "));
|
||||
|
||||
+3
-3
@@ -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<Integer> 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<StepicUser> 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);
|
||||
|
||||
+9
-2
@@ -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<StepicWrappers.AdaptiveAttemptWrapper.Attempt> 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, "");
|
||||
|
||||
+66
-81
@@ -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<BasicHeader> 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<NameValuePair> 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;
|
||||
}
|
||||
}
|
||||
|
||||
+26
@@ -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<BasicHeader> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -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> T getFromStepic(String link, final Class<T> 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));
|
||||
|
||||
+1
-1
@@ -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 {
|
||||
|
||||
+14
-7
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+3
-6
@@ -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<StepicUpda
|
||||
public static StepicUpdateSettings getInstance() {
|
||||
return ServiceManager.getService(StepicUpdateSettings.class);
|
||||
}
|
||||
@NotNull
|
||||
|
||||
@Nullable
|
||||
public StepicUser getUser() {
|
||||
if (myUser == null) {
|
||||
myUser = new StepicUser();
|
||||
}
|
||||
return myUser;
|
||||
}
|
||||
|
||||
public void setUser(@NotNull final StepicUser user) {
|
||||
public void setUser(@Nullable final StepicUser user) {
|
||||
myUser = user;
|
||||
}
|
||||
|
||||
|
||||
+21
-3
@@ -14,16 +14,24 @@ public class StepicUser {
|
||||
private String myEmail;
|
||||
private String myAccessToken;
|
||||
private String myRefreshToken;
|
||||
private boolean isGuest;
|
||||
|
||||
public StepicUser() {
|
||||
private StepicUser() {
|
||||
myFirstName = "";
|
||||
myLastName = "";
|
||||
myEmail = "";
|
||||
myAccessToken = "";
|
||||
myRefreshToken = "";
|
||||
}
|
||||
|
||||
public static StepicUser createEmptyUser() {
|
||||
return new StepicUser();
|
||||
}
|
||||
|
||||
public StepicUser(@NotNull final String email, @NotNull final String password) {
|
||||
public StepicUser(@NotNull final String email, @NotNull final String password, @NotNull StepicWrappers.TokenInfo tokenInfo) {
|
||||
this.myEmail = email;
|
||||
setPassword(password);
|
||||
setTokenInfo(tokenInfo);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
@@ -77,6 +85,7 @@ public class StepicUser {
|
||||
return StringUtil.join(new String[]{myFirstName, myLastName}, " ");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getAccessToken() {
|
||||
return myAccessToken;
|
||||
}
|
||||
@@ -85,6 +94,7 @@ public class StepicUser {
|
||||
this.myAccessToken = accessToken;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getRefreshToken() {
|
||||
return myRefreshToken;
|
||||
}
|
||||
@@ -93,8 +103,16 @@ public class StepicUser {
|
||||
this.myRefreshToken = refreshToken;
|
||||
}
|
||||
|
||||
public void setupTokenInfo(@NotNull final StepicWrappers.TokenInfo tokenInfo) {
|
||||
public void setTokenInfo(@NotNull final StepicWrappers.TokenInfo tokenInfo) {
|
||||
myAccessToken = tokenInfo.getAccessToken();
|
||||
myRefreshToken = tokenInfo.getRefreshToken();
|
||||
}
|
||||
|
||||
public boolean isGuest() {
|
||||
return isGuest;
|
||||
}
|
||||
|
||||
public void setGuest(boolean guest) {
|
||||
isGuest = guest;
|
||||
}
|
||||
}
|
||||
|
||||
-8
@@ -423,14 +423,6 @@ public class StepicWrappers {
|
||||
}
|
||||
}
|
||||
|
||||
static class UserWrapper {
|
||||
StepicUser user;
|
||||
|
||||
public UserWrapper(String user, String password) {
|
||||
this.user = new StepicUser(user, password);
|
||||
}
|
||||
}
|
||||
|
||||
static class RecommendationReaction {
|
||||
int reaction;
|
||||
String user;
|
||||
|
||||
@@ -50,8 +50,10 @@ public class LoginPanel {
|
||||
}
|
||||
});
|
||||
final StepicUser user = StepicUpdateSettings.getInstance().getUser();
|
||||
myLoginField.setText(user.getEmail());
|
||||
myPasswordField.setText(user.getPassword());
|
||||
if (user != null) {
|
||||
myLoginField.setText(user.getEmail());
|
||||
myPasswordField.setText(user.getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
public JPanel getContentPanel() {
|
||||
|
||||
+1
-1
@@ -358,7 +358,7 @@ public class StudyNewProjectPanel extends JPanel implements PanelWithAnchor {
|
||||
|
||||
final StepicUser stepicUser = StudyUtils.execCancelable(() -> 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);
|
||||
|
||||
Reference in New Issue
Block a user