Example usage for com.liferay.portal.kernel.trash TrashHandler hasTrashPermission

List of usage examples for com.liferay.portal.kernel.trash TrashHandler hasTrashPermission

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.trash TrashHandler hasTrashPermission.

Prototype

public boolean hasTrashPermission(PermissionChecker permissionChecker, long groupId, long classPK,
        String trashActionId) throws PortalException;

Source Link

Document

Returns true if the user has the required permission to perform the trash action on the model entity with the primary key.

Usage

From source file:com.liferay.trash.internal.search.TrashIndexer.java

License:Open Source License

@Override
public boolean hasPermission(PermissionChecker permissionChecker, String entryClassName, long entryClassPK,
        String actionId) throws Exception {

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entryClassName);

    return trashHandler.hasTrashPermission(permissionChecker, 0, entryClassPK, actionId);
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

/**
 * Deletes the trash entries with the matching group ID considering
 * permissions./*from  w w w  . j  a  v  a2  s  . co  m*/
 *
 * @param groupId the primary key of the group
 */
@Override
@Transactional(noRollbackFor = { TrashPermissionException.class })
public void deleteEntries(long groupId) throws PortalException {
    boolean throwTrashPermissionException = false;

    List<TrashEntry> entries = trashEntryPersistence.findByGroupId(groupId);

    PermissionChecker permissionChecker = getPermissionChecker();

    for (TrashEntry entry : entries) {
        entry = trashEntryPersistence.fetchByPrimaryKey(entry.getEntryId());

        if (entry == null) {
            continue;
        }

        try {
            TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

            if (!trashHandler.hasTrashPermission(permissionChecker, 0, entry.getClassPK(), ActionKeys.VIEW)) {

                continue;
            }

            deleteEntry(entry);
        } catch (TrashPermissionException tpe) {

            // LPS-52675

            if (_log.isDebugEnabled()) {
                _log.debug(tpe, tpe);
            }

            throwTrashPermissionException = true;
        } catch (Exception e) {
            _log.error(e, e);
        }
    }

    if (throwTrashPermissionException) {
        throw new TrashPermissionException(TrashPermissionException.EMPTY_TRASH);
    }
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

/**
 * Moves the trash entry with the entity class name and primary key,
 * restoring it to a new location identified by the destination container
 * model ID.//from   w w  w  .  j  a v  a2  s. c o  m
 *
 * <p>
 * This method throws a {@link TrashPermissionException} if the user did not
 * have the permission to perform one of the necessary operations. The
 * exception is created with a type specific to the operation:
 * </p>
 *
 * <ul>
 * <li>
 * {@link TrashPermissionException#MOVE} - if the user did not have
 * permission to move the trash entry to the new
 * destination
 * </li>
 * <li>
 * {@link TrashPermissionException#RESTORE} - if the user did not have
 * permission to restore the trash entry
 * </li>
 * </ul>
 *
 * @param className the class name of the entity
 * @param classPK the primary key of the entity
 * @param destinationContainerModelId the primary key of the new location
 * @param serviceContext the service context to be applied (optionally
 *        <code>null</code>)
 */
@Override
public void moveEntry(String className, long classPK, long destinationContainerModelId,
        ServiceContext serviceContext) throws PortalException {

    PermissionChecker permissionChecker = getPermissionChecker();

    long scopeGroupId = 0;

    if (serviceContext != null) {
        scopeGroupId = serviceContext.getScopeGroupId();
    }

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    destinationContainerModelId = trashHandler.getDestinationContainerModelId(classPK,
            destinationContainerModelId);

    if (!trashHandler.hasTrashPermission(permissionChecker, scopeGroupId, destinationContainerModelId,
            TrashActionKeys.MOVE)) {

        throw new TrashPermissionException(TrashPermissionException.MOVE);
    }

    if (trashHandler.isInTrash(classPK)
            && !trashHandler.hasTrashPermission(permissionChecker, 0, classPK, TrashActionKeys.RESTORE)) {

        throw new TrashPermissionException(TrashPermissionException.RESTORE);
    }

    TrashEntry trashEntry = ModelAdapterUtil.adapt(TrashEntry.class, trashHandler.getTrashEntry(classPK));

    if (trashEntry.isTrashEntry(className, classPK)) {
        trashHandler.checkRestorableEntry(
                ModelAdapterUtil.adapt(com.liferay.trash.kernel.model.TrashEntry.class, trashEntry),
                destinationContainerModelId, StringPool.BLANK);
    } else {
        trashHandler.checkRestorableEntry(classPK, destinationContainerModelId, StringPool.BLANK);
    }

    trashHandler.moveTrashEntry(getUserId(), classPK, destinationContainerModelId, serviceContext);
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

/**
 * Restores the trash entry to its original location. In order to handle a
 * duplicate trash entry already existing at the original location, either
 * pass in the primary key of the existing trash entry's entity to overwrite
 * or pass in a new name to give to the trash entry being restored.
 *
 * <p>/*  w  w  w. j av  a  2 s  .com*/
 * This method throws a {@link TrashPermissionException} if the user did not
 * have the permission to perform one of the necessary operations. The
 * exception is created with a type specific to the operation:
 * </p>
 *
 * <ul>
 * <li>
 * {@link TrashPermissionException#RESTORE} - if the user did not have
 * permission to restore the trash entry
 * </li>
 * <li>
 * {@link TrashPermissionException#RESTORE_OVERWRITE} - if the user did not
 * have permission to delete the existing trash entry
 * </li>
 * <li>
 * {@link TrashPermissionException#RESTORE_RENAME} - if the user did not
 * have permission to rename the trash entry
 * </li>
 * </ul>
 *
 * @param  entryId the primary key of the trash entry to restore
 * @param  overrideClassPK the primary key of the entity to overwrite
 *         (optionally <code>0</code>)
 * @param  name a new name to give to the trash entry being restored
 *         (optionally <code>null</code>)
 * @return the restored trash entry
 */
@Override
public TrashEntry restoreEntry(long entryId, long overrideClassPK, String name) throws PortalException {

    PermissionChecker permissionChecker = getPermissionChecker();

    TrashEntry entry = trashEntryPersistence.findByPrimaryKey(entryId);

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

    if (!trashHandler.hasTrashPermission(permissionChecker, 0, entry.getClassPK(), TrashActionKeys.RESTORE)) {

        throw new TrashPermissionException(TrashPermissionException.RESTORE);
    }

    if (overrideClassPK > 0) {
        if (!trashHandler.hasTrashPermission(permissionChecker, 0, overrideClassPK,
                TrashActionKeys.OVERWRITE)) {

            throw new TrashPermissionException(TrashPermissionException.RESTORE_OVERWRITE);
        }

        trashHandler.deleteTrashEntry(overrideClassPK);

        trashHandler.checkRestorableEntry(
                ModelAdapterUtil.adapt(com.liferay.trash.kernel.model.TrashEntry.class, entry),
                TrashEntryConstants.DEFAULT_CONTAINER_ID, null);
    } else if (name != null) {
        if (!trashHandler.hasTrashPermission(permissionChecker, 0, entry.getClassPK(),
                TrashActionKeys.RENAME)) {

            throw new TrashPermissionException(TrashPermissionException.RESTORE_RENAME);
        }

        trashHandler.checkRestorableEntry(
                ModelAdapterUtil.adapt(com.liferay.trash.kernel.model.TrashEntry.class, entry),
                TrashEntryConstants.DEFAULT_CONTAINER_ID, name);

        trashHandler.updateTitle(entry.getClassPK(), name);
    }

    trashHandler.restoreTrashEntry(getUserId(), entry.getClassPK());

    return entry;
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

protected void deleteEntry(TrashEntry entry) throws PortalException {
    PermissionChecker permissionChecker = getPermissionChecker();

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(entry.getClassName());

    if (!trashHandler.hasTrashPermission(permissionChecker, 0, entry.getClassPK(), ActionKeys.DELETE)) {

        throw new TrashPermissionException(TrashPermissionException.DELETE);
    }//from   w w  w. ja v  a2 s.c  o m

    trashHandler.deleteTrashEntry(entry.getClassPK());
}

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

protected List<TrashEntry> filterEntries(List<TrashEntry> entries) throws PrincipalException {

    List<TrashEntry> filteredEntries = new ArrayList<>();

    PermissionChecker permissionChecker = getPermissionChecker();

    for (TrashEntry entry : entries) {
        String className = entry.getClassName();
        long classPK = entry.getClassPK();

        try {//from ww w .j a  va  2 s  . c  o m
            TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

            if (trashHandler.hasTrashPermission(permissionChecker, 0, classPK, ActionKeys.VIEW)) {

                filteredEntries.add(entry);
            }
        } catch (Exception e) {
            _log.error(e, e);
        }
    }

    return filteredEntries;
}