All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.craftercms.security.utils.tenant.TenantUtils Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
package org.craftercms.security.utils.tenant;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.craftercms.profile.api.Profile;
import org.craftercms.profile.api.Tenant;
import org.craftercms.profile.api.exceptions.ProfileException;
import org.craftercms.profile.api.services.TenantService;
import org.craftercms.security.utils.SecurityUtils;

/**
 * Tenant related utility methods.
 *
 * @author avasquez
 */
public class TenantUtils {

    private TenantUtils() {
    }

    /**
     * Returns a list with the names of all tenants.
     *
     * @param tenantService the service that retrieves the {@link org.craftercms.profile.api.Tenant}s.
     *
     * @return the list of tenant names
     */
    public static List getTenantNames(TenantService tenantService) throws ProfileException {
        List tenants = tenantService.getAllTenants();
        List tenantNames = new ArrayList<>(tenants.size());

        if (CollectionUtils.isNotEmpty(tenants)) {
            for (Tenant tenant : tenants) {
                tenantNames.add(tenant.getName());
            }
        }

        return tenantNames;
    }

    /**
     * Returns the current tenant name, which is the tenant of the currently authenticated profile.
     *
     * @return the current tenant name.
     */
    public static String getCurrentTenantName() {
        Profile profile = SecurityUtils.getCurrentProfile();
        if (profile != null) {
            return profile.getTenant();
        } else {
            return null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy