Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout button #65

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ public class ArtemisSettings implements Configurable {
this.apply();
PluginState.getInstance().connect();
});
contentPanel.add(loginButton, "span 2, growx");
contentPanel.add(loginButton, "span 1, growx");

// Button to log out of artemis
var logoutButton = new JButton("Logout");
logoutButton.addActionListener(a -> {
// request a logout
PluginState.getInstance().logout();
this.apply();
});
contentPanel.add(logoutButton, "span 1, growx");

// Login options
contentPanel.add(new TitledSeparator("Login Options"), "span 2, growx");
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/edu/kit/kastel/sdq/intelligrade/login/CefUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,38 @@ private CefUtils() {
throw new IllegalAccessError("Utility Class");
}

/**
* Reset the CEF Browsers cookies to remove the artemis login token
*/
public static void resetCookies() {

// offscreen rendering is problematic on Linux
JBCefBrowser browser = JBCefBrowser.createBuilder()
.setClient(browserClient)
.setOffScreenRendering(false)
.build();

// clear cookies
CefApp.getInstance().onInitialization(state -> {
browser.getJBCefCookieManager().deleteCookies(null, null);
});
browser.dispose();
}

/**
* Create and display a Window containing a JBCef Window to request login. Call this on the EDT!
*
* @return A future on the JWT Cookie to log in. Don't await it on the EDT.
*/
public static CompletableFuture<JBCefCookie> jcefBrowserLogin() {

if (browserClient == null) {
browserClient = JBCefApp.getInstance().createClient();
}

// offscreen rendering is problematic on Linux
JBCefBrowser browser = JBCefBrowser.createBuilder()
.setClient(browserClient)
.setOffScreenRendering(false)
.setUrl(ArtemisSettingsState.getInstance().getArtemisInstanceUrl())
.build();

// TODO the following code deletes the jwt cookie, which is needed for a "fresh" login
// TODO add this somewhere where it is useful
// CefApp.getInstance().onInitialization(state -> {
// browser.getJBCefCookieManager().deleteCookies(null, null);
// });

// set focus handler because it gets invoked sometimes and causes NullPE otherwise
CefFocusHandler focusHandler = new CefWindowFocusHandler();
browserClient.addFocusHandler(focusHandler, browser.getCefBrowser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

import javax.swing.*;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.ui.jcef.JBCefApp;
import edu.kit.kastel.sdq.artemis4j.ArtemisClientException;
import edu.kit.kastel.sdq.artemis4j.ArtemisNetworkException;
Expand Down Expand Up @@ -61,6 +64,35 @@ public static PluginState getInstance() {
return pluginState;
}

/**
* Logs out while displaying a warning if an assessment is still running
*/
public void logout() {
boolean answer = true;
// check if confirmation is necessary because assessment is running
if (isAssessing()) {
answer = MessageDialogBuilder.okCancel(
"Logging out while assessing!",
"Logging out while assessing will discard current changes. Continue?")
.guessWindowAndAsk();
}
// actually reset state
if (answer) {
this.resetState();

// reset state in settings
ArtemisCredentialsProvider.getInstance().setJwt(null);
ArtemisCredentialsProvider.getInstance().setArtemisPassword(null);
ArtemisSettingsState.getInstance().setJwtExpiry(null);

// reset JBCef cookies iff available
if (JBCefApp.isSupported()) {
CefUtils.resetCookies();
}
}
this.notifyConnectedListeners();
}

public void connect() {
this.resetState();

Expand Down