public static void updatePermissionFields(String name, String primKey) {
    if (isIndexReadOnly() || !PermissionThreadLocal.isFlushEnabled()) {
      return;
    }

    _searchPermissionChecker.updatePermissionFields(name, primKey);
  }
  protected void addModelResources(
      long companyId,
      long groupId,
      long userId,
      String name,
      String primKey,
      String[] groupPermissions,
      String[] guestPermissions,
      PermissionedModel permissionedModel)
      throws PortalException, SystemException {

    if (!PermissionThreadLocal.isAddResource()) {
      return;
    }

    validate(name, false);

    if (primKey == null) {
      return;
    }

    // Individual

    Resource resource = getResource(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);

    // Permissions

    boolean flushEnabled = PermissionThreadLocal.isFlushEnabled();

    PermissionThreadLocal.setIndexEnabled(false);

    try {
      addModelResources(
          companyId,
          groupId,
          userId,
          resource,
          groupPermissions,
          guestPermissions,
          permissionedModel);
    } finally {
      PermissionThreadLocal.setIndexEnabled(flushEnabled);

      PermissionCacheUtil.clearCache();

      SearchEngineUtil.updatePermissionFields(name, primKey);
    }
  }
  protected void addResources(
      long companyId,
      long groupId,
      long userId,
      String name,
      String primKey,
      boolean portletActions,
      boolean addGroupPermissions,
      boolean addGuestPermissions,
      PermissionedModel permissionedModel)
      throws PortalException, SystemException {

    if (!PermissionThreadLocal.isAddResource()) {
      return;
    }

    validate(name, portletActions);

    if (primKey == null) {
      return;
    }

    // Individual

    Resource resource = getResource(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);

    // Permissions

    boolean flushEnabled = PermissionThreadLocal.isFlushEnabled();

    PermissionThreadLocal.setIndexEnabled(false);

    List<ResourcePermission> resourcePermissions =
        resourcePermissionPersistence.findByC_N_S_P(
            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);

    ResourcePermissionsThreadLocal.setResourcePermissions(resourcePermissions);

    try {
      addResources(companyId, groupId, userId, resource, portletActions, permissionedModel);

      // Group permissions

      if ((groupId > 0) && addGroupPermissions) {
        addGroupPermissions(
            companyId, groupId, userId, name, resource, portletActions, permissionedModel);
      }

      // Guest permissions

      if (addGuestPermissions) {

        // Don't add guest permissions when you've already added group
        // permissions and the given group is the guest group.

        addGuestPermissions(
            companyId, groupId, userId, name, resource, portletActions, permissionedModel);
      }
    } finally {
      ResourcePermissionsThreadLocal.setResourcePermissions(null);

      PermissionThreadLocal.setIndexEnabled(flushEnabled);

      PermissionCacheUtil.clearCache();

      SearchEngineUtil.updatePermissionFields(name, primKey);
    }
  }