/** * Returns <code>true</code> if this is an exclusive (or temporary exclusive) lock, * and the given user is the owner of this lock.<p> * * @param user the user to compare to the owner of this lock * * @return <code>true</code> if this is an exclusive (or temporary exclusive) lock, * and the given user is the owner of this lock */ public boolean isExclusiveOwnedBy(CmsUser user) { return isExclusive() && isOwnedBy(user); }
/** * Returns <code>true</code> if this is an exclusive (or temporary exclusive) lock, * and the given user is the owner of this lock.<p> * * @param user the user to compare to the owner of this lock * * @return <code>true</code> if this is an exclusive (or temporary exclusive) lock, * and the given user is the owner of this lock */ public boolean isExclusiveOwnedBy(CmsUser user) { return isExclusive() && isOwnedBy(user); }
/** * Returns <code>true</code> if the given user is the owner of this lock, * and this lock belongs to the given project.<p> * * @param user the user to compare to the owner of this lock * @param project the project to compare to the project of this lock * * @return <code>true</code> if the given user is the owner of this lock, * and this lock belongs to the given project */ public boolean isOwnedInProjectBy(CmsUser user, CmsProject project) { return isOwnedBy(user) && isInProject(project); }
/** * Returns <code>true</code> if the given user is the owner of this lock, * and this lock belongs to the given project.<p> * * @param user the user to compare to the owner of this lock * @param project the project to compare to the project of this lock * * @return <code>true</code> if the given user is the owner of this lock, * and this lock belongs to the given project */ public boolean isOwnedInProjectBy(CmsUser user, CmsProject project) { return isOwnedBy(user) && isInProject(project); }
/** * Returns <code>true</code> if this is an exclusive, temporary exclusive, or * directly inherited lock, and the given user is the owner of this lock.<p> * * @param user the user to compare to the owner of this lock * * @return <code>true</code> if this is an exclusive, temporary exclusive, or * directly inherited lock, and the given user is the owner of this lock */ public boolean isDirectlyOwnedBy(CmsUser user) { return (isExclusive() || isDirectlyInherited()) && isOwnedBy(user); }
/** * Returns a list of resources that are locked by another user as the current user.<p> * * @param resourceList the list of all (mixed) resources * * @return a list of resources that are locked by another user as the current user * @throws CmsException if the getLock operation fails */ private List getResourcesLockedByOtherUser(List resourceList) throws CmsException { List lockedResourcesByOtherUser = new ArrayList(); Iterator i = resourceList.iterator(); while (i.hasNext()) { CmsResource resource = (CmsResource)i.next(); // get the lock state for the resource CmsLock lock = getCms().getLock(resource); // add this resource to the list if this is locked by another user if (!lock.isUnlocked() && !lock.isOwnedBy(getCms().getRequestContext().getCurrentUser())) { lockedResourcesByOtherUser.add(resource); } } return lockedResourcesByOtherUser; } }
/** * Tries to lock a resource and throws an exception if it can't be locked.<p> * * Returns true only if the resource wasn't already locked before.<p> * * @param cms the CMS context * @param resource the resource to lock * @return true if the resource wasn't already locked * * @throws CmsException if something goes wrong */ public static boolean ensureLock(CmsObject cms, CmsResource resource) throws CmsException { CmsLock lock = cms.getLock(resource); if (lock.isOwnedBy(cms.getRequestContext().getCurrentUser())) { return false; } cms.lockResourceTemporary(resource); return true; }
/** * Tries to to touch a resource by setting its last modification date, but only if its state is 'unchanged'.<p> * * @param cms the current CMS context * @param resource the resource which should be 'touched'. */ private void touch(CmsObject cms, CmsResource resource) { if (resource.getState().isUnchanged()) { try { CmsLock lock = cms.getLock(resource); if (lock.isUnlocked() || !lock.isOwnedBy(cms.getRequestContext().getCurrentUser())) { cms.lockResourceTemporary(resource); long now = System.currentTimeMillis(); resource.setDateLastModified(now); cms.writeResource(resource); if (lock.isUnlocked()) { cms.unlockResource(resource); } } } catch (CmsException e) { LOG.warn("Could not touch resource after alias modification: " + resource.getRootPath(), e); } } }
/** * Checks if a resource can be locked by a user.<p> * * The resource is not lockable if it already has a lock of type {@link CmsLockType#PUBLISH}.<p> * * The resource is lockable either * - if it is currently unlocked * - if it has a lock of another type set and the user is the lock owner * * @param user the user to test lockeability for * * @return <code>true</code> if this lock blocks any operation on the locked resource until it is unlocked */ public boolean isLockableBy(CmsUser user) { if (getSystemLock().isPublish()) { return false; } if (getEditionLock().isUnlocked() && getSystemLock().isUnlocked()) { return true; } return getEditionLock().isOwnedBy(user); }
/** * Checks if a resource can be locked by a user.<p> * * The resource is not lockable if it already has a lock of type {@link CmsLockType#PUBLISH}.<p> * * The resource is lockable either * - if it is currently unlocked * - if it has a lock of another type set and the user is the lock owner * * @param user the user to test lockeability for * * @return <code>true</code> if this lock blocks any operation on the locked resource until it is unlocked */ public boolean isLockableBy(CmsUser user) { if (getSystemLock().isPublish()) { return false; } if (getEditionLock().isUnlocked() && getSystemLock().isUnlocked()) { return true; } return getEditionLock().isOwnedBy(user); }
/** * Locks the given resource.<p> * * @param cms the cms context * @param resource the resource to lock * * @throws CmsException in case locking fails */ private static void ensureLock(CmsObject cms, CmsResource resource) throws CmsException { CmsUser user = cms.getRequestContext().getCurrentUser(); CmsLock lock = cms.getLock(resource); if (!lock.isOwnedBy(user)) { cms.lockResourceTemporary(resource); } else if (!lock.isOwnedInProjectBy(user, cms.getRequestContext().getCurrentProject())) { cms.changeLock(resource); } }
/** * Enumeration class for the mode parameter in the * {@link CmsDriverManager#readChangedResourcesInsideProject(CmsDbContext, CmsUUID, CmsReadChangedProjectResourceMode)} * method.<p> */ private static class CmsReadChangedProjectResourceMode { /** * Default constructor.<p> */ protected CmsReadChangedProjectResourceMode() { // noop } }
/** * Locks the given resource temporarily.<p> * * @param resource the resource to lock * * @throws CmsException if locking fails */ private void lockTemporary(CmsResource resource) throws CmsException { CmsObject cms = getCms(); CmsUser user = cms.getRequestContext().getCurrentUser(); CmsLock lock = cms.getLock(resource); if (!lock.isOwnedBy(user)) { cms.lockResourceTemporary(resource); } else if (!lock.isOwnedInProjectBy(user, cms.getRequestContext().getCurrentProject())) { cms.changeLock(resource); } } }
/** * Returns whether the current user has write permissions, the resource is lockable or already locked by the current user and is in the current project.<p> * * @param cms the cms context * @param resource the resource * * @return <code>true</code> if the resource is writable * * @throws CmsException in case checking the permissions fails */ protected boolean isWritable(CmsObject cms, CmsResource resource) throws CmsException { boolean writable = cms.hasPermissions( resource, CmsPermissionSet.ACCESS_WRITE, false, CmsResourceFilter.IGNORE_EXPIRATION); if (writable) { CmsLock lock = cms.getLock(resource); writable = lock.isUnlocked() || lock.isOwnedBy(cms.getRequestContext().getCurrentUser()); if (writable) { CmsResourceUtil resUtil = new CmsResourceUtil(cms, resource); writable = resUtil.isInsideProject() && !resUtil.getProjectState().isLockedForPublishing(); } } return writable; }
/** * Writes a sitemap configuration back to the VFS.<p> * * @param content the content to write * @param sitemapConfigFile the file to which the sitemap config should be written * * @throws CmsXmlException if an XML processing error occurs * @throws CmsException if something goes wrong */ private void writeSitemapConfig(CmsXmlContent content, CmsFile sitemapConfigFile) throws CmsXmlException, CmsException { content.correctXmlStructure(m_cms); byte[] contentBytes = content.marshal(); sitemapConfigFile.setContents(contentBytes); try { CmsLock lock = m_cms.getLock(sitemapConfigFile); if (lock.isUnlocked() || !lock.isOwnedBy(m_cms.getRequestContext().getCurrentUser())) { m_cms.lockResourceTemporary(sitemapConfigFile); } m_cms.writeFile(sitemapConfigFile); } finally { m_cms.unlockResource(sitemapConfigFile); } }
/** * Collects the allowable actions for a relation.<p> * * @param cms the current CMS context * @param file the source of the relation * @param relation the relation object * * @return the allowable actions for the given resource */ protected AllowableActions collectAllowableActions(CmsObject cms, CmsResource file, CmsRelation relation) { try { Set<Action> aas = new LinkedHashSet<Action>(); AllowableActionsImpl result = new AllowableActionsImpl(); CmsLock lock = cms.getLock(file); CmsUser user = cms.getRequestContext().getCurrentUser(); boolean canWrite = !cms.getRequestContext().getCurrentProject().isOnlineProject() && (lock.isOwnedBy(user) || lock.isLockableBy(user)) && cms.hasPermissions(file, CmsPermissionSet.ACCESS_WRITE, false, CmsResourceFilter.DEFAULT); addAction(aas, Action.CAN_GET_PROPERTIES, true); addAction(aas, Action.CAN_DELETE_OBJECT, canWrite && !relation.getType().isDefinedInContent()); result.setAllowableActions(aas); return result; } catch (CmsException e) { handleCmsException(e); return null; } }
/** * Tries to remove a lock on an ancestor of a given path owned by the current user.<p> * * @param cms the CMS context * @param folderPath the path for which the lock should be removed * * @throws CmsException if something goes wrong */ protected void tryToUnlock(CmsObject cms, String folderPath) throws CmsException { // Get path of first ancestor that actually exists while (!cms.existsResource(folderPath)) { folderPath = CmsResource.getParentFolder(folderPath); } CmsResource resource = cms.readResource(folderPath); CmsLock lock = cms.getLock(resource); // we are only interested in locks we can safely unlock, i.e. locks by the current user if (lock.isOwnedBy(cms.getRequestContext().getCurrentUser())) { // walk up the tree until we get to the location from which the lock is inherited while (lock.isInherited()) { folderPath = CmsResource.getParentFolder(folderPath); resource = cms.readResource(folderPath); lock = cms.getLock(resource); } cms.unlockResource(folderPath); } }
/** * Helper method for locking a resource which returns some information on whether the locking * failed, and why.<p> * * @param sitepath the site path of the resource to lock * @return the locking information * * @throws CmsException if something went wrong */ protected CmsLockInfo getLock(String sitepath) throws CmsException { CmsObject cms = getCmsObject(); CmsUser user = cms.getRequestContext().getCurrentUser(); CmsLock lock = cms.getLock(sitepath); if (lock.isOwnedBy(user)) { return CmsLockInfo.forSuccess(); } if (lock.getUserId().isNullUUID()) { cms.lockResourceTemporary(sitepath); return CmsLockInfo.forSuccess(); } CmsUser owner = cms.readUser(lock.getUserId()); return CmsLockInfo.forLockedResource(owner.getName()); }
/** * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible() */ @Override public boolean isVisible() { if (getResourceName() != null) { try { // if resource type if editable if (OpenCms.getWorkplaceManager().getEditorHandler().getEditorUri(getResourceName(), getWp().getJsp()) != null) { // check lock state CmsLock lock = getResourceUtil().getLock(); if (lock.isNullLock() || lock.isOwnedBy((getWp().getCms().getRequestContext().currentUser()))) { return isEnabled(); } } } catch (Throwable e) { // ignore } } return !isEnabled(); }
/** * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible() */ @Override public boolean isVisible() { if (getResourceName() != null) { try { // if resource type if editable if (OpenCms.getWorkplaceManager().getEditorHandler().getEditorUri( getResourceName(), getWp().getJsp()) != null) { // check lock state CmsLock lock = getResourceUtil().getLock(); if (lock.isNullLock() || lock.isOwnedBy((getWp().getCms().getRequestContext().getCurrentUser()))) { return isEnabled(); } } } catch (Throwable e) { // ignore } } return !isEnabled(); }