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 String _getLogin() {
    String login = PrincipalThreadLocal.getName();

    if (Validator.isNull(login) || _isDefaultUser(login)) {
      return PropsUtil.get(PropsKeys.DL_REPOSITORY_GUEST_USERNAME);
    }

    try {
      String authType = getAuthType();

      if (!authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
        User user = userLocalService.getUser(GetterUtil.getLong(login));

        if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
          login = user.getEmailAddress();
        } else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
          login = user.getScreenName();
        }
      }
    } catch (PortalException | SystemException e) {
      if (_log.isWarnEnabled()) {
        _log.warn("Unable to get login to connect to external repository " + _extRepository, e);
      }

      login = null;
    }

    return login;
  }
  private String _getPassword() {
    String login = PrincipalThreadLocal.getName();

    if (Validator.isNull(login) || _isDefaultUser(login)) {
      return PropsUtil.get(PropsKeys.DL_REPOSITORY_GUEST_PASSWORD);
    }

    return PrincipalThreadLocal.getPassword();
  }
  protected void searchRecentEntries() throws Exception {
    long initialSearchGroupEntriesCount = searchGroupEntriesCount(group.getGroupId(), 0);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    BaseModel<?> parentBaseModel1 = getParentBaseModel(group, serviceContext);
    BaseModel<?> parentBaseModel2 = getParentBaseModel(group, serviceContext);

    String name = PrincipalThreadLocal.getName();

    try {
      User user1 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user1.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);

      User user2 = UserTestUtil.addUser(null, 0);

      PrincipalThreadLocal.setName(user2.getUserId());

      baseModel =
          addBaseModel(parentBaseModel1, true, RandomTestUtil.randomString(), serviceContext);
      baseModel =
          addBaseModel(parentBaseModel2, true, RandomTestUtil.randomString(), serviceContext);
    } finally {
      PrincipalThreadLocal.setName(name);
    }

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));

    moveParentBaseModelToTrash((Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 3, searchGroupEntriesCount(group.getGroupId(), 0));

    TrashHandler parentTrashHandler =
        TrashHandlerRegistryUtil.getTrashHandler(getParentBaseModelClassName());

    parentTrashHandler.restoreTrashEntry(
        TestPropsValues.getUserId(), (Long) parentBaseModel2.getPrimaryKeyObj());

    Assert.assertEquals(
        initialSearchGroupEntriesCount + 5, searchGroupEntriesCount(group.getGroupId(), 0));
  }
  protected File[] exportLayouts(long userGroupId, Map<String, String[]> parameterMap)
      throws PortalException {

    File[] files = new File[2];

    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(userGroupId);

    User user = userLocalService.getUser(GetterUtil.getLong(PrincipalThreadLocal.getName()));

    Group group = userGroup.getGroup();

    if (userGroup.hasPrivateLayouts()) {
      Map<String, Serializable> exportLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildExportLayoutSettingsMap(
              user,
              group.getGroupId(),
              true,
              ExportImportHelperUtil.getAllLayoutIds(group.getGroupId(), true),
              parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
              exportLayoutSettingsMap);

      files[0] = exportImportLocalService.exportLayoutsAsFile(exportImportConfiguration);
    }

    if (userGroup.hasPublicLayouts()) {
      Map<String, Serializable> exportLayoutSettingsMap =
          ExportImportConfigurationSettingsMapFactory.buildExportLayoutSettingsMap(
              user,
              group.getGroupId(),
              false,
              ExportImportHelperUtil.getAllLayoutIds(group.getGroupId(), false),
              parameterMap);

      ExportImportConfiguration exportImportConfiguration =
          exportImportConfigurationLocalService.addDraftExportImportConfiguration(
              user.getUserId(),
              ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
              exportLayoutSettingsMap);

      files[1] = exportImportLocalService.exportLayoutsAsFile(exportImportConfiguration);
    }

    return files;
  }
  /**
   * See {@link com.liferay.portal.service.BaseServiceImpl#getUserId()}
   *
   * @deprecated As of 7.0.0, with no direct replacement
   */
  @Deprecated
  public static long getUserId() throws PrincipalException {
    String name = PrincipalThreadLocal.getName();

    if (Validator.isNull(name)) {
      throw new PrincipalException("Principal is null");
    } else {
      for (int i = 0; i < BaseServiceImpl.ANONYMOUS_NAMES.length; i++) {
        if (StringUtil.equalsIgnoreCase(name, BaseServiceImpl.ANONYMOUS_NAMES[i])) {

          throw new PrincipalException("Principal cannot be " + BaseServiceImpl.ANONYMOUS_NAMES[i]);
        }
      }
    }

    return GetterUtil.getLong(name);
  }
  @Override
  protected void doUpgrade() throws Exception {
    String name = PrincipalThreadLocal.getName();

    try {
      long companyId = PortalUtil.getDefaultCompanyId();

      long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);

      PrincipalThreadLocal.setName(defaultUserId);

      clearData(companyId);
      setupPermissionsDefined(defaultUserId);
    } finally {
      PrincipalThreadLocal.setName(name);
    }
  }
Пример #8
0
  @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);
  }
  @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);
    }
  }
Пример #10
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;
  }