allow anonymous access

This commit is contained in:
Sebastian Sdorra
2010-12-30 13:44:40 +01:00
parent 422b4cab9e
commit b1a9173cd3
6 changed files with 115 additions and 24 deletions

View File

@@ -94,7 +94,19 @@ public class AuthenticationResource
{ {
securityContext.logout(request, response); 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 ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------

View File

@@ -78,6 +78,7 @@ public class ScmConfiguration
this.pluginUrl = other.pluginUrl; this.pluginUrl = other.pluginUrl;
this.sslPort = other.sslPort; this.sslPort = other.sslPort;
this.enableSSL = other.enableSSL; this.enableSSL = other.enableSSL;
this.anonymousAccessEnabled = other.anonymousAccessEnabled;
} }
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
@@ -115,6 +116,17 @@ public class ScmConfiguration
return sslPort; return sslPort;
} }
/**
* Method description
*
*
* @return
*/
public boolean isAnonymousAccessEnabled()
{
return anonymousAccessEnabled;
}
/** /**
* Method description * Method description
* *
@@ -128,6 +140,17 @@ public class ScmConfiguration
//~--- set methods ---------------------------------------------------------- //~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param anonymousAccessEnabled
*/
public void setAnonymousAccessEnabled(boolean anonymousAccessEnabled)
{
this.anonymousAccessEnabled = anonymousAccessEnabled;
}
/** /**
* Method description * Method description
* *
@@ -186,4 +209,7 @@ public class ScmConfiguration
/** Field description */ /** Field description */
private int sslPort = 8181; private int sslPort = 8181;
/** Field description */
private boolean anonymousAccessEnabled = false;
} }

View File

@@ -41,6 +41,7 @@ import com.google.inject.servlet.SessionScoped;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.user.User; import sonia.scm.user.User;
import sonia.scm.user.UserManager; import sonia.scm.user.UserManager;
@@ -57,6 +58,9 @@ import javax.servlet.http.HttpServletResponse;
public class BasicSecurityContext implements WebSecurityContext public class BasicSecurityContext implements WebSecurityContext
{ {
/** Field description */
public static final String USER_ANONYMOUS = "anonymous";
/** the logger for BasicSecurityContext */ /** the logger for BasicSecurityContext */
private static final Logger logger = private static final Logger logger =
LoggerFactory.getLogger(BasicSecurityContext.class); LoggerFactory.getLogger(BasicSecurityContext.class);
@@ -67,13 +71,17 @@ public class BasicSecurityContext implements WebSecurityContext
* Constructs ... * Constructs ...
* *
* *
*
* @param configuration
* @param authenticator * @param authenticator
* @param userManager * @param userManager
*/ */
@Inject @Inject
public BasicSecurityContext(AuthenticationManager authenticator, public BasicSecurityContext(ScmConfiguration configuration,
AuthenticationManager authenticator,
UserManager userManager) UserManager userManager)
{ {
this.configuration = configuration;
this.authenticator = authenticator; this.authenticator = authenticator;
this.userManager = userManager; this.userManager = userManager;
} }
@@ -155,6 +163,11 @@ public class BasicSecurityContext implements WebSecurityContext
@Override @Override
public User getUser() public User getUser()
{ {
if ((user == null) && configuration.isAnonymousAccessEnabled())
{
user = userManager.get(USER_ANONYMOUS);
}
return user; return user;
} }
@@ -167,7 +180,7 @@ public class BasicSecurityContext implements WebSecurityContext
@Override @Override
public boolean isAuthenticated() public boolean isAuthenticated()
{ {
return user != null; return getUser() != null;
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
@@ -175,6 +188,9 @@ public class BasicSecurityContext implements WebSecurityContext
/** Field description */ /** Field description */
private AuthenticationManager authenticator; private AuthenticationManager authenticator;
/** Field description */
private ScmConfiguration configuration;
/** Field description */ /** Field description */
private User user; private User user;

View File

@@ -104,6 +104,11 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
name: 'plugin-url', name: 'plugin-url',
vtype: 'url', vtype: 'url',
allowBlank: false allowBlank: false
},{
xtype: 'checkbox',
fieldLabel: 'Allow Anonymous Access',
name: 'anonymousAccessEnabled',
inputValue: 'true'
},{ },{
xtype: 'checkbox', xtype: 'checkbox',
fieldLabel: 'Enable SSL', fieldLabel: 'Enable SSL',

View File

@@ -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(){ function logout(){
Ext.Ajax.request({ Ext.Ajax.request({
url: restUrl + 'authentication/logout.json', url: restUrl + 'authentication/logout.json',
method: 'GET', method: 'GET',
success: function(){ success: function(response){
if ( debug ){ if ( debug ){
console.debug('logout success'); console.debug('logout success');
} }
// clear state clearState();
state = null;
// clear repository store
repositoryTypeStore.removeAll();
// remove all tabs
Ext.getCmp('mainTabPanel').removeAll();
// remove navigation items
Ext.getCmp('navigationPanel').removeAll();
// call logout callback functions // call logout callback functions
Ext.each(logoutCallbacks, function(callback){ Ext.each(logoutCallbacks, function(callback){
if ( Ext.isFunction(callback) ){ if ( Ext.isFunction(callback) ){
callback(state); callback(state);
} }
}); });
// show login window
var loginWin = new Sonia.login.Window(); var s = null;
loginWin.show(); 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(){ failure: function(){
if ( debug ){ if ( debug ){

View File

@@ -150,15 +150,27 @@ Ext.onReady(function(){
}] }]
}]); }]);
} }
panel.addSection({ if ( state.user.name == 'anonymous' ){
id: 'navLogout', panel.addSection({
title: 'Log out', id: 'navLogin',
items: [{ title: 'Login',
label: 'Log out', items: [{
fn: logout label: 'Login',
}] fn: login
}); }]
});
} else {
panel.addSection({
id: 'navLogout',
title: 'Log out',
items: [{
label: 'Log out',
fn: logout
}]
});
}
//fix hidden logout button //fix hidden logout button
panel.doLayout(); panel.doLayout();
} }