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 _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);
    }
  }
  @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);
    }
  }
Пример #4
0
  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);
  }
Пример #5
0
  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();
      }
    }
  }
Пример #6
0
  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;
  }
Пример #7
0
  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;
  }
Пример #8
0
  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;
  }