public void importFromLDAP(long companyId) throws Exception { if (!LDAPSettingsUtil.isImportEnabled(companyId)) { return; } long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId); if (LockLocalServiceUtil.hasLock( defaultUserId, PortalLDAPImporterUtil.class.getName(), companyId)) { if (_log.isDebugEnabled()) { _log.debug( "Skipping LDAP import for company " + companyId + "because another LDAP import is in process"); } return; } LockLocalServiceUtil.lock( defaultUserId, PortalLDAPImporterUtil.class.getName(), companyId, PortalLDAPImporterImpl.class.getName(), false, PropsValues.LDAP_IMPORT_LOCK_EXPIRATION_TIME); long threadLocalCompanyId = CompanyThreadLocal.getCompanyId(); try { if (threadLocalCompanyId == CompanyConstants.SYSTEM) { CompanyThreadLocal.setCompanyId(companyId); } long[] ldapServerIds = StringUtil.split(PrefsPropsUtil.getString(companyId, "ldap.server.ids"), 0L); for (long ldapServerId : ldapServerIds) { importFromLDAP(ldapServerId, companyId); } for (int ldapServerId = 0; ; ldapServerId++) { String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId); String providerUrl = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_PROVIDER_URL + postfix); if (Validator.isNull(providerUrl)) { break; } importFromLDAP(ldapServerId, companyId); } } finally { LockLocalServiceUtil.unlock(PortalLDAPImporterUtil.class.getName(), companyId); CompanyThreadLocal.setCompanyId(threadLocalCompanyId); } }
private static void _populateContextFromThreadLocals(Map<String, Serializable> context) { if (!context.containsKey("companyId")) { context.put("companyId", CompanyThreadLocal.getCompanyId()); } if (!context.containsKey("defaultLocale")) { context.put("defaultLocale", LocaleThreadLocal.getDefaultLocale()); } if (!context.containsKey("groupId")) { context.put("groupId", GroupThreadLocal.getGroupId()); } if (!context.containsKey("principalName")) { context.put("principalName", PrincipalThreadLocal.getName()); } if (!context.containsKey("principalPassword")) { context.put("principalPassword", PrincipalThreadLocal.getPassword()); } if (!context.containsKey("siteDefaultLocale")) { context.put("siteDefaultLocale", LocaleThreadLocal.getSiteDefaultLocale()); } if (!context.containsKey("themeDisplayLocale")) { context.put("themeDisplayLocale", LocaleThreadLocal.getThemeDisplayLocale()); } }
private static void _populateThreadLocalsFromContext(Map<String, Serializable> context) { long companyId = GetterUtil.getLong(context.get("companyId")); if (companyId > 0) { CompanyThreadLocal.setCompanyId(companyId); } Locale defaultLocale = (Locale) context.get("defaultLocale"); if (defaultLocale != null) { LocaleThreadLocal.setDefaultLocale(defaultLocale); } long groupId = GetterUtil.getLong(context.get("groupId")); if (groupId > 0) { GroupThreadLocal.setGroupId(groupId); } String principalName = GetterUtil.getString(context.get("principalName")); if (Validator.isNotNull(principalName)) { PrincipalThreadLocal.setName(principalName); } PermissionChecker permissionChecker = null; if (Validator.isNotNull(principalName)) { try { User user = UserLocalServiceUtil.fetchUser(PrincipalThreadLocal.getUserId()); permissionChecker = PermissionCheckerFactoryUtil.create(user); } catch (Exception e) { throw new RuntimeException(e); } } if (permissionChecker != null) { PermissionThreadLocal.setPermissionChecker(permissionChecker); } String principalPassword = GetterUtil.getString(context.get("principalPassword")); if (Validator.isNotNull(principalPassword)) { PrincipalThreadLocal.setPassword(principalPassword); } Locale siteDefaultLocale = (Locale) context.get("siteDefaultLocale"); if (siteDefaultLocale != null) { LocaleThreadLocal.setSiteDefaultLocale(siteDefaultLocale); } Locale themeDisplayLocale = (Locale) context.get("themeDisplayLocale"); if (themeDisplayLocale != null) { LocaleThreadLocal.setThemeDisplayLocale(themeDisplayLocale); } }
/** @deprecated {@link #addTable(long, String, String)} */ public ExpandoTable addTable(String className, String name) throws PortalException, SystemException { long companyId = CompanyThreadLocal.getCompanyId(); return addTable(companyId, className, name); }
private static CompanyLocalesBag _getCompanyLocalesBag() { Long companyId = CompanyThreadLocal.getCompanyId(); CompanyLocalesBag companyLocalesBag = _companyLocalesBags.get(companyId); if (companyLocalesBag == null) { companyLocalesBag = new CompanyLocalesBag(companyId); _companyLocalesBags.put(companyId, companyLocalesBag); } return companyLocalesBag; }
@Override public void initContextUser(long userId) throws AuthException { try { User user = UserLocalServiceUtil.getUser(userId); CompanyThreadLocal.setCompanyId(user.getCompanyId()); PrincipalThreadLocal.setName(userId); PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user); PermissionThreadLocal.setPermissionChecker(permissionChecker); AccessControlThreadLocal.setRemoteAccess(false); } catch (Exception e) { throw new AuthException(e.getMessage(), e); } }
public static boolean isOmniadmin(long userId) { if (CompanyThreadLocal.getCompanyId() != PortalInstances.getDefaultCompanyId()) { return false; } if (userId <= 0) { return false; } try { if (PropsValues.OMNIADMIN_USERS.length > 0) { for (int i = 0; i < PropsValues.OMNIADMIN_USERS.length; i++) { if (PropsValues.OMNIADMIN_USERS[i] == userId) { User user = UserLocalServiceUtil.getUserById(userId); if (user.getCompanyId() != PortalInstances.getDefaultCompanyId()) { return false; } return true; } } return false; } else { User user = UserLocalServiceUtil.getUserById(userId); if (user.getCompanyId() != PortalInstances.getDefaultCompanyId()) { return false; } return RoleLocalServiceUtil.hasUserRole( userId, user.getCompanyId(), RoleConstants.ADMINISTRATOR, true); } } catch (Exception e) { _log.error(e); return false; } }
protected void initThreadLocals(User user) throws Exception { CompanyThreadLocal.setCompanyId(user.getCompanyId()); PrincipalThreadLocal.setName(user.getUserId()); if (!_usePermissionChecker) { return; } PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker(); if (permissionChecker != null) { return; } permissionChecker = PermissionCheckerFactoryUtil.create(user); PermissionThreadLocal.setPermissionChecker(permissionChecker); }
protected void initCompanies() throws Exception { ServletContext servletContext = getServletContext(); try { String[] webIds = PortalInstances.getWebIds(); for (String webId : webIds) { PortalInstances.initCompany(servletContext, webId); } } finally { CompanyThreadLocal.setCompanyId(PortalInstances.getDefaultCompanyId()); ShardDataSourceTargetSource shardDataSourceTargetSource = (ShardDataSourceTargetSource) InfrastructureUtil.getShardDataSourceTargetSource(); if (shardDataSourceTargetSource != null) { shardDataSourceTargetSource.resetDataSource(); } } }
private Configuration _getConfiguration() { if (_configurations == null) { return _configuration; } Long companyId = CompanyThreadLocal.getCompanyId(); if (companyId > CompanyConstants.SYSTEM) { Configuration configuration = _configurations.get(companyId); if (configuration == null) { configuration = new ConfigurationImpl(PropsUtil.class.getClassLoader(), PropsFiles.PORTAL, companyId); _configurations.put(companyId, configuration); } return configuration; } else { return _configuration; } }
@AccessControlled(guestAccessEnabled = true) @Override public SyncContext getSyncContext() throws PortalException { try { User user = getGuestOrUser(); SyncContext syncContext = new SyncContext(); String authType = PrefsPropsUtil.getString( CompanyThreadLocal.getCompanyId(), PropsKeys.COMPANY_SECURITY_AUTH_TYPE, PropsUtil.get(PropsKeys.COMPANY_SECURITY_AUTH_TYPE)); syncContext.setAuthType(authType); boolean oAuthEnabled = PrefsPropsUtil.getBoolean( user.getCompanyId(), PortletPropsKeys.SYNC_OAUTH_ENABLED, PortletPropsValues.SYNC_OAUTH_ENABLED); if (oAuthEnabled) { String oAuthConsumerKey = PrefsPropsUtil.getString(user.getCompanyId(), PortletPropsKeys.SYNC_OAUTH_CONSUMER_KEY); syncContext.setOAuthConsumerKey(oAuthConsumerKey); String oAuthConsumerSecret = PrefsPropsUtil.getString( user.getCompanyId(), PortletPropsKeys.SYNC_OAUTH_CONSUMER_SECRET); syncContext.setOAuthConsumerSecret(oAuthConsumerSecret); } syncContext.setOAuthEnabled(oAuthEnabled); PluginPackage syncWebPluginPackage = DeployManagerUtil.getInstalledPluginPackage("sync-web"); syncContext.setPluginVersion(syncWebPluginPackage.getVersion()); if (!user.isDefaultUser()) { syncContext.setPortalBuildNumber(ReleaseInfo.getBuildNumber()); PluginPackage soPortletPluginPackage = DeployManagerUtil.getInstalledPluginPackage("so-portlet"); syncContext.setPortletPreferencesMap(getPortletPreferencesMap()); if (soPortletPluginPackage != null) { syncContext.setSocialOfficeInstalled(true); } else { syncContext.setSocialOfficeInstalled(false); } syncContext.setUser(user); syncContext.setUserSitesGroups(getUserSitesGroups()); } return syncContext; } catch (PortalException pe) { throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe); } }
@Override protected void dispatch(final Set<MessageListener> messageListeners, final Message message) { if (!message.contains("companyId")) { message.put("companyId", CompanyThreadLocal.getCompanyId()); } if (!message.contains("permissionChecker")) { message.put("permissionChecker", PermissionThreadLocal.getPermissionChecker()); } if (!message.contains("principalName")) { message.put("principalName", PrincipalThreadLocal.getName()); } if (!message.contains("principalPassword")) { message.put("principalPassword", PrincipalThreadLocal.getPassword()); } ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor(); Runnable runnable = new MessageRunnable(message) { public void run() { try { long messageCompanyId = message.getLong("companyId"); if (messageCompanyId > 0) { CompanyThreadLocal.setCompanyId(messageCompanyId); } PermissionChecker permissionChecker = (PermissionChecker) message.get("permissionChecker"); if (permissionChecker != null) { PermissionThreadLocal.setPermissionChecker(permissionChecker); } String messagePrincipalName = message.getString("principalName"); if (Validator.isNotNull(messagePrincipalName)) { PrincipalThreadLocal.setName(messagePrincipalName); } String messagePrincipalPassword = message.getString("principalPassword"); if (Validator.isNotNull(messagePrincipalPassword)) { PrincipalThreadLocal.setPassword(messagePrincipalPassword); } Boolean clusterForwardMessage = (Boolean) message.get(ClusterLinkUtil.CLUSTER_FORWARD_MESSAGE); if (clusterForwardMessage != null) { MessageValuesThreadLocal.setValue( ClusterLinkUtil.CLUSTER_FORWARD_MESSAGE, clusterForwardMessage); } for (MessageListener messageListener : messageListeners) { try { messageListener.receive(message); } catch (MessageListenerException mle) { _log.error("Unable to process message " + message, mle); } } } finally { ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST); CentralizedThreadLocal.clearShortLivedThreadLocals(); } } }; threadPoolExecutor.execute(runnable); }
private long _initCompany(ServletContext servletContext, String webId) { // Begin initializing company if (_log.isDebugEnabled()) { _log.debug("Begin initializing company with web id " + webId); } long companyId = 0; try { Company company = CompanyLocalServiceUtil.checkCompany(webId); companyId = company.getCompanyId(); } catch (Exception e) { _log.error(e, e); } CompanyThreadLocal.setCompanyId(companyId); // Lucene LuceneHelperUtil.startup(companyId); // Initialize display if (_log.isDebugEnabled()) { _log.debug("Initialize display"); } try { String xml = HttpUtil.URLtoString(servletContext.getResource("/WEB-INF/liferay-display.xml")); PortletCategory portletCategory = (PortletCategory) WebAppPool.get(companyId, WebKeys.PORTLET_CATEGORY); if (portletCategory == null) { portletCategory = new PortletCategory(); } PortletCategory newPortletCategory = PortletLocalServiceUtil.getEARDisplay(xml); portletCategory.merge(newPortletCategory); for (int i = 0; i < _companyIds.length; i++) { long currentCompanyId = _companyIds[i]; PortletCategory currentPortletCategory = (PortletCategory) WebAppPool.get(currentCompanyId, WebKeys.PORTLET_CATEGORY); if (currentPortletCategory != null) { portletCategory.merge(currentPortletCategory); } } WebAppPool.put(companyId, WebKeys.PORTLET_CATEGORY, portletCategory); } catch (Exception e) { _log.error(e, e); } // Check journal content search if (_log.isDebugEnabled()) { _log.debug("Check journal content search"); } if (GetterUtil.getBoolean(PropsUtil.get(PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) { try { JournalContentSearchLocalServiceUtil.checkContentSearches(companyId); } catch (Exception e) { _log.error(e, e); } } // Process application startup events if (_log.isDebugEnabled()) { _log.debug("Process application startup events"); } try { EventsProcessorUtil.process( PropsKeys.APPLICATION_STARTUP_EVENTS, PropsValues.APPLICATION_STARTUP_EVENTS, new String[] {String.valueOf(companyId)}); } catch (Exception e) { _log.error(e, e); } // End initializing company if (_log.isDebugEnabled()) { _log.debug("End initializing company with web id " + webId + " and company id " + companyId); } addCompanyId(companyId); return companyId; }
private long _getCompanyId(HttpServletRequest request) { if (_log.isDebugEnabled()) { _log.debug("Get company id"); } Long companyIdObj = (Long) request.getAttribute(WebKeys.COMPANY_ID); if (_log.isDebugEnabled()) { _log.debug("Company id from request " + companyIdObj); } if (companyIdObj != null) { return companyIdObj.longValue(); } long companyId = _getCompanyIdByVirtualHosts(request); if (_log.isDebugEnabled()) { _log.debug("Company id from host " + companyId); } if (companyId <= 0) { long cookieCompanyId = GetterUtil.getLong(CookieKeys.getCookie(request, CookieKeys.COMPANY_ID, false)); if (cookieCompanyId > 0) { try { CompanyLocalServiceUtil.getCompanyById(cookieCompanyId); companyId = cookieCompanyId; if (_log.isDebugEnabled()) { _log.debug("Company id from cookie " + companyId); } } catch (NoSuchCompanyException nsce) { if (_log.isWarnEnabled()) { _log.warn("Company id from cookie " + cookieCompanyId + " does not exist"); } } catch (Exception e) { _log.error(e, e); } } } if (companyId <= 0) { companyId = _getDefaultCompanyId(); if (_log.isDebugEnabled()) { _log.debug("Default company id " + companyId); } } if (_log.isDebugEnabled()) { _log.debug("Set company id " + companyId); } request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId)); CompanyThreadLocal.setCompanyId(companyId); if (Validator.isNotNull(PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME) && (request.getAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET) == null)) { try { Group group = GroupLocalServiceUtil.getGroup(companyId, PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME); LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(group.getGroupId(), false); if (Validator.isNull(layoutSet.getVirtualHostname())) { request.setAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet); } } catch (Exception e) { _log.error(e, e); } } return companyId; }
@Override protected void dispatch(Set<MessageListener> messageListeners, final Message message) { if (!message.contains("companyId")) { message.put("companyId", CompanyThreadLocal.getCompanyId()); } if (!message.contains("principalName")) { message.put("principalName", PrincipalThreadLocal.getName()); } if (!message.contains("principalPassword")) { message.put("principalPassword", PrincipalThreadLocal.getPassword()); } ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor(); for (final MessageListener messageListener : messageListeners) { Runnable runnable = new MessageRunnable(message) { public void run() { long companyId = CompanyThreadLocal.getCompanyId(); String principalName = PrincipalThreadLocal.getName(); String principalPassword = PrincipalThreadLocal.getPassword(); try { long messageCompanyId = message.getLong("companyId"); if (messageCompanyId > 0) { CompanyThreadLocal.setCompanyId(messageCompanyId); } String messagePrincipalName = message.getString("principalName"); if (Validator.isNotNull(messagePrincipalName)) { PrincipalThreadLocal.setName(messagePrincipalName); } String messagePrincipalPassword = message.getString("principalPassword"); if (Validator.isNotNull(messagePrincipalPassword)) { PrincipalThreadLocal.setPassword(messagePrincipalPassword); } messageListener.receive(message); } catch (MessageListenerException mle) { _log.error("Unable to process message " + message, mle); } finally { CompanyThreadLocal.setCompanyId(companyId); PrincipalThreadLocal.setName(principalName); PrincipalThreadLocal.setPassword(principalPassword); CentralizedThreadLocal.clearShortLivedThreadLocals(); } } }; threadPoolExecutor.execute(runnable); } }
private long _initCompany(ServletContext servletContext, String webId) { // Begin initializing company if (_log.isDebugEnabled()) { _log.debug("Begin initializing company with web id " + webId); } long companyId = 0; try { Company company = CompanyLocalServiceUtil.checkCompany(webId); companyId = company.getCompanyId(); } catch (Exception e) { _log.error(e, e); } Long currentThreadCompanyId = CompanyThreadLocal.getCompanyId(); String currentThreadPrincipalName = PrincipalThreadLocal.getName(); try { CompanyThreadLocal.setCompanyId(companyId); String principalName = null; try { User user = UserLocalServiceUtil.getUser(PrincipalThreadLocal.getUserId()); if (user.getCompanyId() == companyId) { principalName = currentThreadPrincipalName; } } catch (Exception e) { } PrincipalThreadLocal.setName(principalName); // Initialize display if (_log.isDebugEnabled()) { _log.debug("Initialize display"); } try { String xml = HttpUtil.URLtoString(servletContext.getResource("/WEB-INF/liferay-display.xml")); PortletCategory portletCategory = (PortletCategory) WebAppPool.get(companyId, WebKeys.PORTLET_CATEGORY); if (portletCategory == null) { portletCategory = new PortletCategory(); } PortletCategory newPortletCategory = PortletLocalServiceUtil.getEARDisplay(xml); portletCategory.merge(newPortletCategory); for (int i = 0; i < _companyIds.length; i++) { long currentCompanyId = _companyIds[i]; PortletCategory currentPortletCategory = (PortletCategory) WebAppPool.get(currentCompanyId, WebKeys.PORTLET_CATEGORY); if (currentPortletCategory != null) { portletCategory.merge(currentPortletCategory); } } WebAppPool.put(companyId, WebKeys.PORTLET_CATEGORY, portletCategory); } catch (Exception e) { _log.error(e, e); } // LDAP import try { if (LDAPSettingsUtil.isImportOnStartup(companyId)) { UserImporterUtil.importUsers(companyId); } } catch (Exception e) { _log.error(e, e); } // Process application startup events if (_log.isDebugEnabled()) { _log.debug("Process application startup events"); } try { EventsProcessorUtil.process( PropsKeys.APPLICATION_STARTUP_EVENTS, PropsValues.APPLICATION_STARTUP_EVENTS, new String[] {String.valueOf(companyId)}); } catch (Exception e) { _log.error(e, e); } // End initializing company if (_log.isDebugEnabled()) { _log.debug( "End initializing company with web id " + webId + " and company id " + companyId); } addCompanyId(companyId); } finally { CompanyThreadLocal.setCompanyId(currentThreadCompanyId); PrincipalThreadLocal.setName(currentThreadPrincipalName); } return companyId; }