protected void copyResourcePermissions(String sourcePortletId, String targetPortletId) {

    Layout layout = getLayout();

    Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), sourcePortletId);

    String sourcePortletPrimaryKey =
        PortletPermissionUtil.getPrimaryKey(layout.getPlid(), sourcePortletId);

    List<ResourcePermission> resourcePermissions =
        ResourcePermissionLocalServiceUtil.getResourcePermissions(
            portlet.getCompanyId(),
            portlet.getPortletName(),
            PortletKeys.PREFS_OWNER_TYPE_USER,
            sourcePortletPrimaryKey);

    for (ResourcePermission resourcePermission : resourcePermissions) {
      String targetPortletPrimaryKey =
          PortletPermissionUtil.getPrimaryKey(layout.getPlid(), targetPortletId);

      resourcePermission.setResourcePermissionId(CounterLocalServiceUtil.increment());
      resourcePermission.setPrimKey(targetPortletPrimaryKey);

      ResourcePermissionLocalServiceUtil.addResourcePermission(resourcePermission);
    }
  }
  @Override
  public void movePortletId(long userId, String portletId, String columnId, int columnPos) {

    if (!hasPortletId(portletId)) {
      return;
    }

    _enablePortletLayoutListener = false;

    try {
      removePortletId(userId, portletId, false);
      addPortletId(userId, portletId, columnId, columnPos, false, true);
    } finally {
      _enablePortletLayoutListener = true;
    }

    Layout layout = getLayout();

    try {
      Portlet portlet = PortletLocalServiceUtil.getPortletById(layout.getCompanyId(), portletId);

      if (portlet != null) {
        PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();

        if (portletLayoutListener != null) {
          portletLayoutListener.onMoveInLayout(portletId, layout.getPlid());
        }
      }
    } catch (Exception e) {
      _log.error("Unable to fire portlet layout listener event", e);
    }
  }
  @Override
  public void removePortletId(long userId, String portletId, boolean cleanUp) {

    try {
      Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), portletId);

      if (portlet == null) {
        _log.error("Portlet " + portletId + " cannot be removed because it is not registered");

        return;
      }

      PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

      if (!LayoutPermissionUtil.contains(permissionChecker, getLayout(), ActionKeys.UPDATE)
          && !isCustomizable()) {

        return;
      }
    } catch (Exception e) {
      _log.error(e, e);

      return;
    }

    List<String> columns = getColumns();

    for (int i = 0; i < columns.size(); i++) {
      String columnId = columns.get(i);

      if (isCustomizable() && isColumnDisabled(columnId)) {
        continue;
      }

      String columnValue = StringPool.BLANK;

      if (hasUserPreferences()) {
        columnValue = getUserPreference(columnId);
      } else {
        columnValue = getTypeSettingsProperty(columnId);
      }

      columnValue = StringUtil.removeFromList(columnValue, portletId);

      if (hasUserPreferences()) {
        setUserPreference(columnId, columnValue);
      } else {
        setTypeSettingsProperty(columnId, columnValue);
      }
    }

    if (cleanUp) {
      try {
        onRemoveFromLayout(new String[] {portletId});
      } catch (Exception e) {
        _log.error(e, e);
      }
    }
  }
Пример #4
0
  /** @see com.liferay.portal.model.impl.LayoutTypePortletImpl#getStaticPortlets( String) */
  protected static Portlet getPortlet(long companyId, String portletId) throws Exception {

    Portlet portlet = PortletLocalServiceUtil.getPortletById(companyId, portletId);

    // See LayoutTypePortletImpl#getStaticPortlets for why we only clone
    // non-instanceable portlets

    if (!portlet.isInstanceable()) {
      portlet = (Portlet) portlet.clone();
    }

    portlet.setStatic(true);

    return portlet;
  }
  @Override
  public List<Portlet> getPortlets() {
    List<String> portletIds = getPortletIds();

    List<Portlet> portlets = new ArrayList<>(portletIds.size());

    for (int i = 0; i < portletIds.size(); i++) {
      String portletId = portletIds.get(i);

      Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), portletId);

      if (portlet != null) {
        portlets.add(portlet);
      }
    }

    return portlets;
  }
  protected List<Portlet> getStaticPortlets(String position) {
    String[] portletIds = getStaticPortletIds(position);

    List<Portlet> portlets = new ArrayList<>();

    for (String portletId : portletIds) {
      if (Validator.isNull(portletId) || hasNonstaticPortletId(portletId)) {

        continue;
      }

      Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), portletId);

      if (portlet != null) {
        Portlet staticPortlet = portlet;

        if (portlet.isInstanceable()) {

          // Instanceable portlets do not need to be cloned because
          // they are already cloned. See the method getPortletById in
          // the class PortletLocalServiceImpl and how it references
          // the method getClonedInstance in the class PortletImpl.

        } else {
          staticPortlet = (Portlet) staticPortlet.clone();
        }

        staticPortlet.setStatic(true);

        if (position.startsWith("layout.static.portlets.start")) {
          staticPortlet.setStaticStart(true);
        }

        portlets.add(staticPortlet);
      }
    }

    return portlets;
  }
  @Override
  public List<Portlet> getAllPortlets(String columnId) {
    String columnValue = getColumnValue(columnId);

    String[] portletIds = StringUtil.split(columnValue);

    List<Portlet> portlets = new ArrayList<>(portletIds.length);

    for (String portletId : portletIds) {
      Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), portletId);

      if (portlet != null) {
        portlets.add(portlet);
      }
    }

    List<Portlet> startPortlets =
        getStaticPortlets(PropsKeys.LAYOUT_STATIC_PORTLETS_START + columnId);

    List<Portlet> endPortlets = getStaticPortlets(PropsKeys.LAYOUT_STATIC_PORTLETS_END + columnId);

    return addStaticPortlets(portlets, startPortlets, endPortlets);
  }
  protected void onRemoveFromLayout(String[] portletIds) {
    Set<String> portletIdList = new HashSet<>();

    for (String portletId : portletIds) {
      removeModesPortletId(portletId);
      removeStatesPortletId(portletId);

      portletIdList.add(portletId);

      String rootPortletId = PortletConstants.getRootPortletId(portletId);

      if (rootPortletId.equals(PortletKeys.NESTED_PORTLETS)) {
        String portletNamespace = PortalUtil.getPortletNamespace(portletId);

        UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();

        for (Map.Entry<String, String> entry : typeSettingsProperties.entrySet()) {

          String key = entry.getKey();

          if (!key.startsWith(portletNamespace)) {
            continue;
          }

          String nestedPortletIds = entry.getValue();

          for (String nestedPortletId : StringUtil.split(nestedPortletIds)) {

            removeModesPortletId(nestedPortletId);
            removeStatesPortletId(nestedPortletId);

            portletIdList.add(nestedPortletId);
          }
        }

        removeNestedColumns(portletNamespace);
      }

      Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);

      if (portlet == null) {
        continue;
      }

      PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();

      if (portletLayoutListener == null) {
        continue;
      }

      portletLayoutListener.updatePropertiesOnRemoveFromLayout(
          portletId, getTypeSettingsProperties());
    }

    try {
      PortletLocalServiceUtil.deletePortlets(
          getCompanyId(), portletIdList.toArray(new String[portletIdList.size()]), getPlid());
    } catch (PortalException pe) {
      _log.error(pe, pe);
    }
  }
  protected String getUserPreference(String key) {
    String value = StringPool.BLANK;

    if (!hasUserPreferences()) {
      return value;
    }

    value =
        _portalPreferences.getValue(CustomizedPages.namespacePlid(getPlid()), key, StringPool.NULL);

    if (!value.equals(StringPool.NULL)) {
      return value;
    }

    value = getTypeSettingsProperty(key);

    if (Validator.isNull(value)) {
      return value;
    }

    List<String> newPortletIds = new ArrayList<>();

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    String[] portletIds = StringUtil.split(value);

    for (String portletId : portletIds) {
      try {
        if (!PortletPermissionUtil.contains(
            permissionChecker, getLayout(), portletId, ActionKeys.VIEW, true)) {

          continue;
        }

        String rootPortletId = PortletConstants.getRootPortletId(portletId);

        if (!PortletPermissionUtil.contains(
            permissionChecker, rootPortletId, ActionKeys.ADD_TO_PAGE)) {

          continue;
        }
      } catch (Exception e) {
        _log.error(e, e);
      }

      String newPortletId = null;

      boolean preferencesUniquePerLayout = false;

      try {
        Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), portletId);

        preferencesUniquePerLayout = portlet.isPreferencesUniquePerLayout();
      } catch (SystemException se) {
        _log.error(se, se);
      }

      if (PortletConstants.hasInstanceId(portletId) || preferencesUniquePerLayout) {

        String instanceId = null;

        if (PortletConstants.hasInstanceId(portletId)) {
          instanceId = PortletConstants.generateInstanceId();
        }

        newPortletId =
            PortletConstants.assemblePortletId(
                portletId, _portalPreferences.getUserId(), instanceId);

        copyPreferences(_portalPreferences.getUserId(), portletId, newPortletId);

        copyResourcePermissions(portletId, newPortletId);
      } else {
        newPortletId = portletId;
      }

      newPortletIds.add(newPortletId);
    }

    value = StringUtil.merge(newPortletIds);

    setUserPreference(key, value);

    return value;
  }
  protected String addPortletId(
      long userId,
      String portletId,
      String columnId,
      int columnPos,
      boolean checkPermission,
      boolean strictHasPortlet) {

    portletId = JS.getSafeName(portletId);

    Layout layout = getLayout();

    Portlet portlet = null;

    try {
      portlet = PortletLocalServiceUtil.getPortletById(layout.getCompanyId(), portletId);

      if (portlet == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Portlet " + portletId + " cannot be added because it is not registered");
        }

        return null;
      }

      PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

      if (checkPermission
          && !PortletPermissionUtil.contains(
              permissionChecker, layout, portlet, ActionKeys.ADD_TO_PAGE)) {

        return null;
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    if (portlet.isSystem()) {
      return null;
    }

    if (portlet.isInstanceable() && !PortletConstants.hasInstanceId(portletId)) {

      portletId =
          PortletConstants.assemblePortletId(portletId, PortletConstants.generateInstanceId());
    }

    if (hasPortletId(portletId, strictHasPortlet)) {
      return null;
    }

    if (columnId == null) {
      LayoutTemplate layoutTemplate = getLayoutTemplate();

      List<String> columns = layoutTemplate.getColumns();

      if (!columns.isEmpty()) {
        columnId = columns.get(0);
      }
    }

    if (columnId == null) {
      return null;
    }

    if (isCustomizable()) {
      if (isColumnDisabled(columnId)) {
        return null;
      }

      if ((PortletConstants.hasInstanceId(portletId) || portlet.isPreferencesUniquePerLayout())
          && hasUserPreferences()) {

        portletId = PortletConstants.assemblePortletId(portletId, userId);
      }
    }

    String columnValue = StringPool.BLANK;

    if (hasUserPreferences()) {
      columnValue = getUserPreference(columnId);
    } else {
      columnValue = getTypeSettingsProperty(columnId);
    }

    if ((columnValue == null) && columnId.startsWith(_NESTED_PORTLETS_NAMESPACE)) {

      addNestedColumn(columnId);
    }

    if (columnPos >= 0) {
      List<String> portletIds = ListUtil.fromArray(StringUtil.split(columnValue));

      if (columnPos <= portletIds.size()) {
        portletIds.add(columnPos, portletId);
      } else {
        portletIds.add(portletId);
      }

      columnValue = StringUtil.merge(portletIds);
    } else {
      columnValue = StringUtil.add(columnValue, portletId);
    }

    if (hasUserPreferences()) {
      setUserPreference(columnId, columnValue);
    } else {
      setTypeSettingsProperty(columnId, columnValue);
    }

    try {
      if (_enablePortletLayoutListener && !portlet.isUndeployedPortlet()) {

        PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();

        if (portletLayoutListener != null) {
          portletLayoutListener.onAddToLayout(portletId, layout.getPlid());
        }
      }
    } catch (Exception e) {
      _log.error("Unable to fire portlet layout listener event", e);
    }

    return portletId;
  }