fix initial creation of tenant when no current tenant and caching fix

Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com>
This commit is contained in:
Michael Hirsch
2016-07-18 12:48:50 +02:00
parent 015f88a54f
commit d2cd13996a
5 changed files with 85 additions and 29 deletions

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.security;
import java.util.Collection;
import java.util.Collections;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.tenancy.TenantAware;
@@ -80,32 +81,37 @@ public class SecurityContextTenantAware implements TenantAware {
@Override
public boolean equals(final Object another) {
return delegate.equals(another);
if (delegate != null) {
return delegate.equals(another);
} else if (another == null) {
return true;
}
return false;
}
@Override
public String toString() {
return delegate.toString();
return (delegate != null) ? delegate.toString() : null;
}
@Override
public int hashCode() {
return delegate.hashCode();
return (delegate != null) ? delegate.hashCode() : null;
}
@Override
public String getName() {
return delegate.getName();
return (delegate != null) ? delegate.getName() : null;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return delegate.getAuthorities();
return (delegate != null) ? delegate.getAuthorities() : Collections.emptyList();
}
@Override
public Object getCredentials() {
return delegate.getCredentials();
return (delegate != null) ? delegate.getCredentials() : null;
}
@Override
@@ -115,16 +121,19 @@ public class SecurityContextTenantAware implements TenantAware {
@Override
public Object getPrincipal() {
return delegate.getPrincipal();
return (delegate != null) ? delegate.getPrincipal() : null;
}
@Override
public boolean isAuthenticated() {
return delegate.isAuthenticated();
return (delegate != null) ? delegate.isAuthenticated() : null;
}
@Override
public void setAuthenticated(final boolean isAuthenticated) throws IllegalArgumentException {
public void setAuthenticated(final boolean isAuthenticated) {
if (delegate == null) {
return;
}
delegate.setAuthenticated(isAuthenticated);
}
}

View File

@@ -67,12 +67,16 @@ public class SystemSecurityContext {
// Exception squid:S2221 - Callable declares Exception
@SuppressWarnings("squid:S2221")
public <T> T runAsSystem(final Callable<T> callable) {
return runAsSystemAsTenant(callable, tenantAware.getCurrentTenant());
}
public <T> T runAsSystemAsTenant(final Callable<T> callable, final String tenant) {
final SecurityContext oldContext = SecurityContextHolder.getContext();
try {
logger.debug("entering system code execution");
return tenantAware.runAsTenant(tenantAware.getCurrentTenant(), () -> {
return tenantAware.runAsTenant(tenant, () -> {
try {
setSystemContext(oldContext);
setSystemContext(SecurityContextHolder.getContext());
return callable.call();
} catch (final Exception e) {
throw Throwables.propagate(e);