Skip to content

Commit

Permalink
Logout button (#65)
Browse files Browse the repository at this point in the history
* initial logout button setup

* spotless

* notify appropriate listeners upon logout

* removed unnecessary code as per review

* moved JOptionPane to IJ dialog thingy

* moved code from settings to PluginState

* spotless
  • Loading branch information
CDaut authored Oct 11, 2024
1 parent de38e97 commit 7f46f3b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
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

0 comments on commit 7f46f3b

Please sign in to comment.