吸引所有租户的人

时间:2018-03-21 08:54:25

标签: java alfresco multi-tenant

我尝试使用Java Web脚本统计所有租户中的人员。此Web脚本应作为默认租户的管理员用户调用,并收集每个租户的人员统计信息。

我努力让人们成为租户。 countPeople()的方法getAllPeople()org.alfresco.service.cmr.security.PersonService始终仅向我提供默认租户中的人员,即使我将此调用包装在AuthenticationUtil.runAs(...)中也是如此。

import org.alfresco.repo.security.authentication.AuthenticationContext;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.tenant.MultiTAdminServiceImpl;
import org.alfresco.repo.tenant.Tenant;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.PersonService;

// ...

protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
    final Map<String, Object> model = new HashMap<>();
    final Map<String, Map<String, Long>> tenantNameToTenantData = new HashMap<>();
    if (tenantAdminService != null) {
        for (final Tenant tenant : tenantAdminService.getAllTenants()) {
            final String tenantDomain = tenant.getTenantDomain();
            final String systemUserName = authenticationContext.getSystemUserName(tenantDomain);

            final Map<String, Long> tenantData = AuthenticationUtil.runAs(() -> {
                final Map<String, Long> td = new HashMap<>();
                final long userCount = (long) personService.countPeople();
                td.put("userCount", userCount);

                long totalUsage = 0L;
                for (NodeRef personNodeRef : personService.getAllPeople()) {
                    totalUsage += (long) serviceRegistry.getNodeService().getProperty(personNodeRef, ContentModel.PROP_SIZE_CURRENT);
                }
                totalUsage = totalUsage / (1_024L * 1_024L);
                td.put("totalUsage", totalUsage);
                return td;
            }, systemUserName);

            tenantNameToTenantData.put(tenantDomain, tenantData);
        }
    }
    model.put("tenants", tenantNameToTenantData);
    return model;
}

authenticationContext.getSystemUserName(tenantDomain)的调用会返回正确的名称(System@tenant1,...)。

如何在不必明确认证为租户管理员的情况下为每个租户获取人员?

1 个答案:

答案 0 :(得分:1)

感谢Gragravarr提示使用TenantUtil.runAsTenant。这是它的工作原理:

final TenantUtil.TenantRunAsWork<Map<String, Long>> runAsWork = () -> {
    final Map<String, String> td = new HashMap<>();
    td.put("userCount", (long) personService.countPeople());

    long totalUsage = 0L;
    for (NodeRef personNodeRef : personService.getAllPeople()) {
        totalUsage += (long) serviceRegistry.getNodeService().getProperty(personNodeRef, ContentModel.PROP_SIZE_CURRENT);
    }
    totalUsage = totalUsage / (1_024L * 1_024L);
    td.put("totalUsage", totalUsage);

    return td;
};
final Map<String, Long> tenantData =
    TenantUtil.runAsTenant(runAsWork, tenantDomain);