Skip to content

Commit

Permalink
initial logout button setup
Browse files Browse the repository at this point in the history
  • Loading branch information
CDaut committed Oct 11, 2024
1 parent de38e97 commit 38c2c97
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 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,23 @@ 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
boolean loggedOut = PluginState.getInstance().logout();
//clear data iff logout was actually carried out
if (loggedOut) {
ArtemisCredentialsProvider.getInstance().setJwt(null);
ArtemisCredentialsProvider.getInstance().setArtemisPassword(null);
ArtemisSettingsState.getInstance().setJwtExpiry(null);
this.apply();
}

});
contentPanel.add(logoutButton, "span 1, growx");

// Login options
contentPanel.add(new TitledSeparator("Login Options"), "span 2, growx");
Expand Down
26 changes: 21 additions & 5 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,6 +29,27 @@ private CefUtils() {
throw new IllegalAccessError("Utility Class");
}

/**
* Reset the CEF Browsers cookies to remove the artemis login token
*/
public static void resetCookies() {
if (browserClient == null) {
browserClient = JBCefApp.getInstance().createClient();
}

// 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!
*
Expand All @@ -47,11 +68,6 @@ public static CompletableFuture<JBCefCookie> jcefBrowserLogin() {
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
import edu.kit.kastel.sdq.intelligrade.utils.ArtemisUtils;
import edu.kit.kastel.sdq.intelligrade.utils.IntellijUtil;
import org.apache.commons.io.FileUtils;
import org.cef.CefApp;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class PluginState {
private static final Logger LOG = Logger.getInstance(PluginState.class);

Expand All @@ -61,6 +64,32 @@ public static PluginState getInstance() {
return pluginState;
}

/**
* Logs out while displaying a warning if an assessment is still running
*
* @return true iff the logout was completed, false otherwise
*/
public boolean logout() {
int answer = JOptionPane.OK_OPTION;
//check if confirmation is necessary because assessment is running
if (isAssessing()) {
answer = JOptionPane.showConfirmDialog(null,
"Logging out while assessing will terminate the current assessment. Continue?",
"Logging out while assessing!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
}
//actually reset state
if (answer == JOptionPane.OK_OPTION) {
this.resetState();

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

return answer == JOptionPane.OK_OPTION;
}

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

Expand Down

0 comments on commit 38c2c97

Please sign in to comment.