Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 3a0a141

Browse files
author
Irene
committed
Fix migrating from old settings
Use TMCServerAddressNormalizer to detect organization, only prompt the user for organization if not defined. Prompt for permissions.
1 parent 904c34d commit 3a0a141

35 files changed

+176
-110
lines changed

src/main/java/fi/helsinki/cs/tmc/cli/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private boolean runCommand(String name, String[] args) {
103103
private Optional<Thread> sendAnalytics(AbstractCommand command) {
104104
Optional<Thread> thread = Optional.empty();
105105
if (command instanceof SubmitCommand || timeTracker.anHourHasPassedSinceLastSubmit()) {
106-
this.context.loadUserInformation();
106+
this.context.loadUserInformation(true);
107107
// get course info returns null
108108
CourseInfo courseInfo = this.context.getCourseInfo();
109109
if (courseInfo == null) {

src/main/java/fi/helsinki/cs/tmc/cli/backend/Account.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,25 @@ public Account() {
3131
public Account(String username, String password) {
3232
this.serverAddress = DEFAULT_SERVER;
3333
this.username = username == null ? null : username.trim();
34-
this.password = password == null ? null : password.trim();
34+
this.password = password == null || password.trim().isEmpty() ? null : password.trim();
35+
}
36+
37+
public Account(String username) {
38+
this.serverAddress = DEFAULT_SERVER;
39+
this.username = username == null ? null : username.trim();
3540
}
3641

3742

3843
public Account(String username, String password, Organization organization) {
3944
this.serverAddress = DEFAULT_SERVER;
4045
this.username = username == null ? null : username.trim();
41-
this.password = password == null ? null : password.trim();
46+
this.password = password == null || password.trim().isEmpty() ? null : password.trim();
47+
this.organization = organization;
48+
}
49+
50+
public Account(String username, Organization organization) {
51+
this.serverAddress = DEFAULT_SERVER;
52+
this.username = username == null ? null : username.trim();
4253
this.organization = organization;
4354
}
4455

@@ -138,6 +149,10 @@ public void setSendDetailedAnalytics(boolean sendDetailedAnalytics) {
138149
this.sendDetailedAnalytics = false;
139150
}
140151

152+
public void setServerAddressToDefault() {
153+
this.serverAddress = DEFAULT_SERVER;
154+
}
155+
141156
@Override
142157
public String toString() {
143158
return "Account{" +

src/main/java/fi/helsinki/cs/tmc/cli/backend/AccountList.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ public Account getAccount() {
2424
return null;
2525
}
2626

27-
public Account getAccount(String username, String server) {
28-
if (username == null || server == null) {
27+
public Account getAccount(String username) {
28+
if (username == null) {
2929
return getAccount();
3030
}
3131
for (Account account : this.accountArray) {
3232
if (!account.getUsername().isPresent()) {
3333
continue;
3434
}
35-
if (account.getUsername().get().equals(username)
36-
&& account.getServerAddress().equals(server)) {
35+
if (account.getUsername().get().equals(username)) {
3736
// Move account to index 0 so we can always use the last used account by default
3837
this.accountArray.remove(account);
3938
this.accountArray.add(0, account);
@@ -45,8 +44,7 @@ public Account getAccount(String username, String server) {
4544

4645
public void addAccount(Account newSettings) {
4746
for (Account account : this.accountArray) {
48-
if (account.getUsername().equals(newSettings.getUsername())
49-
&& account.getServerAddress().equals(newSettings.getServerAddress())) {
47+
if (account.getUsername().equals(newSettings.getUsername())) {
5048
// Replace old account if username and server match
5149
this.accountArray.remove(account);
5250
break;
@@ -55,11 +53,13 @@ public void addAccount(Account newSettings) {
5553
this.accountArray.add(0, newSettings);
5654
}
5755

58-
public void deleteAccount(String username, String server) {
56+
public void deleteAccount(String username) {
5957
Account remove = null;
6058
for (Account account : this.accountArray) {
61-
if (account.getUsername().equals(username)
62-
&& account.getServerAddress().equals(server)) {
59+
if (!account.getUsername().isPresent()) {
60+
continue;
61+
}
62+
if (account.getUsername().get().equals(username)) {
6363
remove = account;
6464
break;
6565
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/Settings.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package fi.helsinki.cs.tmc.cli.backend;
22

3+
import fi.helsinki.cs.tmc.cli.command.LoginCommand;
4+
import fi.helsinki.cs.tmc.cli.command.OrganizationCommand;
5+
import fi.helsinki.cs.tmc.cli.core.CliContext;
36
import fi.helsinki.cs.tmc.cli.io.EnvironmentUtil;
7+
import fi.helsinki.cs.tmc.cli.io.Io;
48
import fi.helsinki.cs.tmc.cli.io.WorkDir;
59

6-
import fi.helsinki.cs.tmc.core.communication.oauth2.Oauth;
710
import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
811
import fi.helsinki.cs.tmc.core.domain.Course;
912

1013
import com.google.common.base.Optional;
1114
import fi.helsinki.cs.tmc.core.domain.OauthCredentials;
1215
import fi.helsinki.cs.tmc.core.domain.Organization;
16+
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
17+
import fi.helsinki.cs.tmc.core.utilities.TmcServerAddressNormalizer;
1318
import fi.helsinki.cs.tmc.spyware.SpywareSettings;
1419
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
15-
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
16-
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
1720
import org.slf4j.Logger;
1821
import org.slf4j.LoggerFactory;
1922

2023
import java.nio.file.Path;
2124
import java.util.Locale;
25+
import java.util.concurrent.Callable;
2226

2327
public class Settings implements TmcSettings, SpywareSettings {
2428

@@ -38,21 +42,56 @@ public Settings(String username, String password, Organization organization) {
3842
* This method is used for changing the main settings object.
3943
* @param account account that has the login info
4044
*/
41-
public void setAccount(Account account) {
45+
public void setAccount(CliContext context, Account account) {
4246
if (account == null) {
4347
account = new Account();
4448
}
49+
Account prevAccount = this.account;
50+
this.account = account;
4551
try {
4652
Optional<String> password = account.getPassword();
4753
if(password.isPresent()) {
48-
Oauth.getInstance().fetchNewToken(password.get());
49-
account.setPassword(Optional.absent());
54+
if (!migrateAccount(context, account)) {
55+
return;
56+
}
5057
SettingsIo.saveCurrentSettingsToAccountList(this);
5158
}
52-
} catch (OAuthProblemException | OAuthSystemException var1) {
59+
} catch (Exception e) {
5360
logger.warn("Settings migration failed.");
61+
this.account = prevAccount;
5462
}
55-
this.account = account;
63+
}
64+
65+
private boolean migrateAccount(CliContext context, Account account) throws Exception {
66+
Io io = context.getIo();
67+
io.println("TMC cli has been updated. Please provide the following information.");
68+
TmcServerAddressNormalizer normalizer = new TmcServerAddressNormalizer();
69+
normalizer.normalize();
70+
normalizer.selectOrganizationAndCourse();
71+
if (!context.getSettings().getOrganization().isPresent()) {
72+
OrganizationCommand organizationCommand = new OrganizationCommand();
73+
Optional<Organization> organization = organizationCommand.chooseOrganization(context, null);
74+
if (!organization.isPresent()) {
75+
return false;
76+
}
77+
account.setOrganization(organization);
78+
}
79+
Callable<Void> callable = context.getTmcCore().authenticate(ProgressObserver.NULL_OBSERVER, account.getPassword().get());
80+
callable.call();
81+
LoginCommand loginCommand = new LoginCommand();
82+
boolean sendDiagnostics = loginCommand.getAnswerFromUser(null, account.getServerAddress(),
83+
"Do you want to send crash reports for client development?",
84+
false,
85+
io);
86+
account.setSendDiagnostics(sendDiagnostics);
87+
boolean sendAnalytics = loginCommand.getAnswerFromUser(null, account.getServerAddress(),
88+
"Do you want to send analytics data for research?",
89+
false,
90+
io);
91+
account.setSendAnalytics(sendAnalytics);
92+
io.println("");
93+
account.setPassword(Optional.absent());
94+
return true;
5695
}
5796

5897
public Account getAccount() {
@@ -80,7 +119,7 @@ public Optional<String> getPassword() {
80119

81120
@Override
82121
public void setPassword(Optional<String> password) {
83-
account.setPassword(password);
122+
throw new RuntimeException("Using password is deprecated");
84123
}
85124

86125
@Override

src/main/java/fi/helsinki/cs/tmc/cli/backend/SettingsIo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void saveCurrentSettingsToAccountList(Settings settings) {
8282
deletables.add(account);
8383
}
8484
});
85-
deletables.stream().forEach(d -> list.deleteAccount(d.getUsername().get(), d.getServerAddress()));
85+
deletables.stream().forEach(d -> list.deleteAccount(d.getUsername().get()));
8686
list.addAccount(settings.getAccount());
8787
saveAccountList(list);
8888
}

src/main/java/fi/helsinki/cs/tmc/cli/command/ConfigCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void run(CliContext context, CommandLine args) {
192192
arguments = Arrays.stream(arguments).filter(o -> !o.trim().isEmpty()).toArray(String[]::new);
193193
this.properties = context.getProperties();
194194

195-
if (!this.context.checkIsLoggedIn(false)) {
195+
if (!this.context.checkIsLoggedIn(false, true)) {
196196
return;
197197
}
198198

src/main/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void run(CliContext context, CommandLine args) {
5959
ctx = context;
6060
showAll = args.hasOption("a");
6161

62-
if (!ctx.checkIsLoggedIn(false)) {
62+
if (!ctx.checkIsLoggedIn(false, true)) {
6363
return;
6464
}
6565

src/main/java/fi/helsinki/cs/tmc/cli/command/HelpCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void run(CliContext context, CommandLine args) {
3434
this.context = context;
3535
this.io = context.getIo();
3636

37-
if (this.context.checkIsLoggedIn(true)) {
37+
if (this.context.checkIsLoggedIn(true, true)) {
3838
this.context.getAnalyticsFacade().saveAnalytics("help");
3939
}
4040

src/main/java/fi/helsinki/cs/tmc/cli/command/InfoCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void run(CliContext context, CommandLine args) {
6565
} else {
6666
printLocalInfo(args.getArgs());
6767
}
68-
if (this.ctx.checkIsLoggedIn(true)) {
68+
if (this.ctx.checkIsLoggedIn(true, true)) {
6969
this.ctx.getAnalyticsFacade().saveAnalytics(courseName, "info");
7070
}
7171
}

src/main/java/fi/helsinki/cs/tmc/cli/command/ListCoursesCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public void run(CliContext context, CommandLine args) {
3535
this.ctx = context;
3636
this.io = ctx.getIo();
3737

38-
this.ctx.loadUserInformation();
3938

40-
if (this.ctx.checkIsLoggedIn(true)) {
41-
this.ctx.getAnalyticsFacade().saveAnalytics("list_courses");
39+
if (!this.ctx.checkIsLoggedIn(false, true)) {
40+
return;
4241
}
4342

43+
this.ctx.getAnalyticsFacade().saveAnalytics("list_courses");
4444
if (!TmcUtil.hasConnection(ctx)) {
4545
io.errorln("You don't have internet connection currently.");
4646
io.errorln("Check the tmc-cli logs if you disagree.");

0 commit comments

Comments
 (0)