diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java index 7fc717cd9d..4bc0b5c186 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/AuthenticationResource.java @@ -94,7 +94,19 @@ public class AuthenticationResource { securityContext.logout(request, response); - return Response.ok().build(); + Response resp = null; + User user = securityContext.getUser(); + + if (user != null) + { + resp = Response.ok(getState(user)).build(); + } + else + { + resp = Response.ok().build(); + } + + return resp; } //~--- get methods ---------------------------------------------------------- diff --git a/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java b/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java index 8efe5d71f7..072100271e 100644 --- a/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java +++ b/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java @@ -78,6 +78,7 @@ public class ScmConfiguration this.pluginUrl = other.pluginUrl; this.sslPort = other.sslPort; this.enableSSL = other.enableSSL; + this.anonymousAccessEnabled = other.anonymousAccessEnabled; } //~--- get methods ---------------------------------------------------------- @@ -115,6 +116,17 @@ public class ScmConfiguration return sslPort; } + /** + * Method description + * + * + * @return + */ + public boolean isAnonymousAccessEnabled() + { + return anonymousAccessEnabled; + } + /** * Method description * @@ -128,6 +140,17 @@ public class ScmConfiguration //~--- set methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param anonymousAccessEnabled + */ + public void setAnonymousAccessEnabled(boolean anonymousAccessEnabled) + { + this.anonymousAccessEnabled = anonymousAccessEnabled; + } + /** * Method description * @@ -186,4 +209,7 @@ public class ScmConfiguration /** Field description */ private int sslPort = 8181; + + /** Field description */ + private boolean anonymousAccessEnabled = false; } diff --git a/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java b/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java index b0da84b804..b8c7ba5336 100644 --- a/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java +++ b/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java @@ -41,6 +41,7 @@ import com.google.inject.servlet.SessionScoped; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.config.ScmConfiguration; import sonia.scm.user.User; import sonia.scm.user.UserManager; @@ -57,6 +58,9 @@ import javax.servlet.http.HttpServletResponse; public class BasicSecurityContext implements WebSecurityContext { + /** Field description */ + public static final String USER_ANONYMOUS = "anonymous"; + /** the logger for BasicSecurityContext */ private static final Logger logger = LoggerFactory.getLogger(BasicSecurityContext.class); @@ -67,13 +71,17 @@ public class BasicSecurityContext implements WebSecurityContext * Constructs ... * * + * + * @param configuration * @param authenticator * @param userManager */ @Inject - public BasicSecurityContext(AuthenticationManager authenticator, + public BasicSecurityContext(ScmConfiguration configuration, + AuthenticationManager authenticator, UserManager userManager) { + this.configuration = configuration; this.authenticator = authenticator; this.userManager = userManager; } @@ -155,6 +163,11 @@ public class BasicSecurityContext implements WebSecurityContext @Override public User getUser() { + if ((user == null) && configuration.isAnonymousAccessEnabled()) + { + user = userManager.get(USER_ANONYMOUS); + } + return user; } @@ -167,7 +180,7 @@ public class BasicSecurityContext implements WebSecurityContext @Override public boolean isAuthenticated() { - return user != null; + return getUser() != null; } //~--- fields --------------------------------------------------------------- @@ -175,6 +188,9 @@ public class BasicSecurityContext implements WebSecurityContext /** Field description */ private AuthenticationManager authenticator; + /** Field description */ + private ScmConfiguration configuration; + /** Field description */ private User user; diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.config.js b/scm-webapp/src/main/webapp/resources/js/sonia.config.js index 8af3521ab4..0602c0486f 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.config.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.config.js @@ -104,6 +104,11 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ name: 'plugin-url', vtype: 'url', allowBlank: false + },{ + xtype: 'checkbox', + fieldLabel: 'Allow Anonymous Access', + name: 'anonymousAccessEnabled', + inputValue: 'true' },{ xtype: 'checkbox', fieldLabel: 'Enable SSL', diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.global.js b/scm-webapp/src/main/webapp/resources/js/sonia.global.js index 8157dab7af..f2dd7034da 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.global.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.global.js @@ -60,31 +60,51 @@ function loadState(s){ }); } +function clearState(){ + // clear state + state = null; + // clear repository store + repositoryTypeStore.removeAll(); + // remove all tabs + Ext.getCmp('mainTabPanel').removeAll(); + // remove navigation items + Ext.getCmp('navigationPanel').removeAll(); +} + +function login(){ + clearState(); + var loginWin = new Sonia.login.Window(); + loginWin.show(); +} + function logout(){ Ext.Ajax.request({ url: restUrl + 'authentication/logout.json', method: 'GET', - success: function(){ + success: function(response){ if ( debug ){ console.debug('logout success'); } - // clear state - state = null; - // clear repository store - repositoryTypeStore.removeAll(); - // remove all tabs - Ext.getCmp('mainTabPanel').removeAll(); - // remove navigation items - Ext.getCmp('navigationPanel').removeAll(); + clearState(); // call logout callback functions Ext.each(logoutCallbacks, function(callback){ if ( Ext.isFunction(callback) ){ callback(state); } }); - // show login window - var loginWin = new Sonia.login.Window(); - loginWin.show(); + + var s = null; + var text = response.responseText; + if ( text != null && text.length > 0 ){ + s = Ext.decode( text ); + } + if ( s != null && s.success ){ + loadState(s); + } else { + // show login window + var loginWin = new Sonia.login.Window(); + loginWin.show(); + } }, failure: function(){ if ( debug ){ diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.scm.js b/scm-webapp/src/main/webapp/resources/js/sonia.scm.js index 032def4ea2..f90482fb02 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.scm.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.scm.js @@ -150,15 +150,27 @@ Ext.onReady(function(){ }] }]); } - - panel.addSection({ - id: 'navLogout', - title: 'Log out', - items: [{ - label: 'Log out', - fn: logout - }] - }); + + if ( state.user.name == 'anonymous' ){ + panel.addSection({ + id: 'navLogin', + title: 'Login', + items: [{ + label: 'Login', + fn: login + }] + }); + } else { + panel.addSection({ + id: 'navLogout', + title: 'Log out', + items: [{ + label: 'Log out', + fn: logout + }] + }); + } + //fix hidden logout button panel.doLayout(); }