Files
alfresco-community-repo/source/java/org/alfresco/repo/tenant/RunAsTenantInterceptor.java
Raluca Munteanu 86dc6f3402 Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.1.N/root@125603 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-20 10:21:07 +00:00

62 lines
1.8 KiB
Java

package org.alfresco.repo.tenant;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @since 4.2
*/
public class RunAsTenantInterceptor implements MethodInterceptor
{
public enum TENANT_TYPE
{
Default,
RealUser
}
private TENANT_TYPE tenantType;
public RunAsTenantInterceptor(TENANT_TYPE tenantType)
{
this.tenantType = tenantType;
}
@Override
public Object invoke(final MethodInvocation mi) throws Throwable
{
TenantRunAsWork<Object> runAs = new TenantRunAsWork<Object>()
{
public Object doWork() throws Exception
{
try
{
return mi.proceed();
}
catch(Throwable e)
{
e.printStackTrace();
// Re-throw the exception
if (e instanceof RuntimeException)
{
throw (RuntimeException) e;
}
throw new RuntimeException("Failed to execute in RunAsTenant context", e);
}
}
};
if (tenantType == TENANT_TYPE.Default)
{
return TenantUtil.runAsDefaultTenant(runAs);
}
else
{
// run as tenant using current tenant context (if no tenant context then it is implied as the primary tenant, based on username)
return TenantUtil.runAsTenant(runAs, AuthenticationUtil.getUserTenant(AuthenticationUtil.getFullyAuthenticatedUser()).getSecond());
}
}
}