mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
allow anonymous access
This commit is contained in:
@@ -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 ----------------------------------------------------------
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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 ){
|
||||
|
||||
@@ -151,14 +151,26 @@ 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user