merge with branch 1.x

This commit is contained in:
Sebastian Sdorra
2014-04-28 13:48:11 +02:00
17 changed files with 338 additions and 59 deletions

View File

@@ -196,9 +196,10 @@ public class ScmContextListener extends GuiceServletContextListener
moduleList.addAll(pluginLoader.getInjectionModules());
moduleList.addAll(overrides.getModules());
SCMContextProvider ctx = SCMContext.getContext();
return Guice.createInjector(ctx.getStage().getInjectionStage(), moduleList);
// TODO: fix cyclic dependencies in production environment
// SCMContextProvider ctx = SCMContext.getContext();
// return Guice.createInjector(ctx.getStage().getInjectionStage(), moduleList);
return Guice.createInjector(moduleList);
}
/**

View File

@@ -202,7 +202,7 @@ public class ScmServletModule extends JerseyServletModule
PATTERN_STYLESHEET, "*.json", "*.xml", "*.txt" };
/** Field description */
private static Logger logger =
private static final Logger logger =
LoggerFactory.getLogger(ScmServletModule.class);
//~--- constructors ---------------------------------------------------------
@@ -356,7 +356,7 @@ public class ScmServletModule extends JerseyServletModule
bind(TemplateEngine.class).annotatedWith(DefaultEngine.class).to(
MustacheTemplateEngine.class);
bind(TemplateEngineFactory.class);
// jersey
Map<String, String> params = new HashMap<String, String>();

View File

@@ -0,0 +1,190 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.EagerSingleton;
import sonia.scm.plugin.Extension;
import sonia.scm.web.security.AdministrationContext;
import sonia.scm.web.security.PrivilegedAction;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
/**
*
* @author Sebastian Sdorra
* @since 1.37
*/
@Extension
@EagerSingleton
public final class LastModifiedUpdateListener
{
/**
* the logger for LastModifiedUpdateListener
*/
private static final Logger logger =
LoggerFactory.getLogger(LastModifiedUpdateListener.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param adminContext
* @param repositoryManager
*/
@Inject
public LastModifiedUpdateListener(AdministrationContext adminContext,
RepositoryManager repositoryManager)
{
this.adminContext = adminContext;
this.repositoryManager = repositoryManager;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param event
*/
@Subscribe
public void onPostReceive(PostReceiveRepositoryHookEvent event)
{
final Repository repository = event.getRepository();
if (repository != null)
{
//J-
adminContext.runAsAdmin(
new LastModifiedPrivilegedAction(repositoryManager, repository)
);
//J+
}
else
{
logger.warn("recevied hook without repository");
}
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 14/04/20
* @author Enter your name here...
*/
static class LastModifiedPrivilegedAction implements PrivilegedAction
{
/**
* Constructs ...
*
*
* @param repositoryManager
* @param repository
*/
public LastModifiedPrivilegedAction(RepositoryManager repositoryManager,
Repository repository)
{
this.repositoryManager = repositoryManager;
this.repository = repository;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*/
@Override
public void run()
{
Repository dbr = repositoryManager.get(repository.getId());
if (dbr != null)
{
logger.info("update last modified date of repository {}", dbr.getId());
dbr.setLastModified(System.currentTimeMillis());
try
{
repositoryManager.modify(dbr);
}
catch (RepositoryException ex)
{
logger.error("could not modify repository", ex);
}
catch (IOException ex)
{
logger.error("could not modify repository", ex);
}
}
else
{
logger.error("could not find repository with id {}",
repository.getId());
}
}
//~--- fields -------------------------------------------------------------
/** Field description */
private final Repository repository;
/** Field description */
private final RepositoryManager repositoryManager;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final AdministrationContext adminContext;
/** Field description */
private final RepositoryManager repositoryManager;
}

View File

@@ -86,8 +86,6 @@ public class DefaultAdministrationContext implements AdministrationContext
*
*
* @param injector
* @param userSessionProvider
* @param contextHolder
* @param securityManager
*/
@Inject
@@ -178,6 +176,22 @@ public class DefaultAdministrationContext implements AdministrationContext
return collection;
}
/**
* Method description
*
*
* @return
*/
private Subject createAdminSubject()
{
//J-
return new Subject.Builder(securityManager)
.authenticated(true)
.principals(principalCollection)
.buildSubject();
//J+
}
/**
* Method description
*
@@ -195,12 +209,7 @@ public class DefaultAdministrationContext implements AdministrationContext
{
SecurityUtils.setSecurityManager(securityManager);
//J-
Subject subject = new Subject.Builder(securityManager)
.authenticated(true)
.principals(principalCollection)
.buildSubject();
//J+
Subject subject = createAdminSubject();
ThreadState state = new SubjectThreadState(subject);
state.bind();
@@ -240,7 +249,7 @@ public class DefaultAdministrationContext implements AdministrationContext
if (logger.isInfoEnabled())
{
String username = null;
String username;
if (subject.hasRole(Role.USER))
{
@@ -255,7 +264,12 @@ public class DefaultAdministrationContext implements AdministrationContext
action.getClass().getName());
}
subject.runAs(principalCollection);
Subject adminSubject = createAdminSubject();
// do not use runas, because we want only execute this action in this
// thread as administrator. Runas could affect other threads
ThreadContext.bind(adminSubject);
try
{
@@ -263,32 +277,20 @@ public class DefaultAdministrationContext implements AdministrationContext
}
finally
{
PrincipalCollection collection = subject.releaseRunAs();
if (logger.isDebugEnabled())
{
logger.debug("release runas for user {}/{}",
principal, collection.getPrimaryPrincipal());
}
if (!subject.getPrincipal().equals(principal))
{
logger.error("release runas failed, {} is not equal with {}, logout.",
subject.getPrincipal(), principal);
subject.logout();
}
logger.debug("release administration context for user {}/{}", principal,
subject.getPrincipal());
ThreadContext.bind(subject);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Injector injector;
private final Injector injector;
/** Field description */
private final org.apache.shiro.mgt.SecurityManager securityManager;
/** Field description */
private PrincipalCollection principalCollection;
/** Field description */
private org.apache.shiro.mgt.SecurityManager securityManager;
}