Search in sources :

Example 1 with Lifetime

use of org.alfresco.repo.lock.mem.Lifetime in project alfresco-repository by Alfresco.

the class LockServiceImplTest method testUnlockNodeWithAdminUserAndAllPermissionsUser.

@SuppressWarnings("deprecation")
public void testUnlockNodeWithAdminUserAndAllPermissionsUser() {
    for (Lifetime lt : new Lifetime[] { Lifetime.EPHEMERAL, Lifetime.PERSISTENT }) {
        TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
        /* create node */
        final NodeRef testNode = this.nodeService.createNode(parentNode, ContentModel.ASSOC_CONTAINS, QName.createQName("{}testNode"), ContentModel.TYPE_CONTAINER).getChildRef();
        // lock it as GOOD user
        this.securedLockService.lock(testNode, LockType.WRITE_LOCK, 2 * 86400, lt, null);
        // check lock state and status as GOOD user
        assertNotNull(this.securedLockService.getLockState(testNode));
        assertNotNull(this.securedLockService.getLockStatus(testNode));
        assertTrue(this.securedLockService.isLocked(testNode));
        assertFalse(this.securedLockService.isLockedAndReadOnly(testNode));
        TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
        // check lock state and status as BAD user
        assertNotNull(this.securedLockService.getLockState(testNode));
        assertNotNull(this.securedLockService.getLockStatus(testNode));
        assertTrue(this.securedLockService.isLocked(testNode));
        assertTrue(this.securedLockService.isLockedAndReadOnly(testNode));
        try {
            // try to unlock as bad user
            this.securedLockService.unlock(testNode);
            fail("BAD user shouldn't be able to unlock " + lt + " lock");
        } catch (AccessDeniedException e) {
        // expected expetion
        }
        TestWithUserUtils.authenticateUser(AuthenticationUtil.getAdminUserName(), "admin", rootNodeRef, this.authenticationService);
        // check lock state and status as ADMIN user
        assertNotNull(this.securedLockService.getLockState(testNode));
        assertNotNull(this.securedLockService.getLockStatus(testNode));
        assertTrue(this.securedLockService.isLocked(testNode));
        assertTrue(this.securedLockService.isLockedAndReadOnly(testNode));
        // try to unlock as ADMIN user
        this.securedLockService.unlock(testNode);
        // test that bad use able to lock/unlock node
        TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
        this.securedLockService.lock(testNode, LockType.WRITE_LOCK, 2 * 86400, lt, null);
        this.securedLockService.unlock(testNode);
        this.securedLockService.lock(testNode, LockType.WRITE_LOCK, 2 * 86400, lt, null);
        // user who has ALL PERMISSIONS is able to unlock another's user lock
        TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
        this.securedLockService.unlock(testNode);
        this.nodeService.deleteNode(testNode);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Lifetime(org.alfresco.repo.lock.mem.Lifetime) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException)

Example 2 with Lifetime

use of org.alfresco.repo.lock.mem.Lifetime in project alfresco-repository by Alfresco.

the class WorkingCopyAspect method onRestoreNode.

/**
 * onRestoreNode policy behaviour
 *
 * @param nodeRef
 *            the node reference that was restored
 */
@SuppressWarnings("unchecked")
@Override
public void onRestoreNode(ChildAssociationRef childAssocRef) {
    NodeRef workingCopyNodeRef = childAssocRef.getChildRef();
    // check that node is working copy
    if (nodeService.hasAspect(workingCopyNodeRef, ContentModel.ASPECT_WORKING_COPY)) {
        try {
            NodeRef checkedOutNodeRef = null;
            policyBehaviourFilter.disableBehaviour(workingCopyNodeRef, ContentModel.ASPECT_AUDITABLE);
            Map<QName, Serializable> workingCopyProperties = nodeService.getProperties(workingCopyNodeRef);
            // get archived lock properties in order to be restored on the original node
            String lockOwner = (String) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_LOCK_OWNER);
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_LOCK_OWNER);
            Date expiryDate = (Date) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_EXPIRY_DATE);
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_EXPIRY_DATE);
            String lockTypeStr = (String) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_LOCK_TYPE);
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_LOCK_TYPE);
            LockType lockType = lockTypeStr != null ? LockType.valueOf(lockTypeStr) : null;
            String lifetimeStr = (String) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_LOCK_LIFETIME);
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_LOCK_LIFETIME);
            Lifetime lifetime = lifetimeStr != null ? Lifetime.valueOf(lifetimeStr) : null;
            String additionalInfo = (String) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_LOCK_ADDITIONAL_INFO);
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_LOCK_ADDITIONAL_INFO);
            List<AssociationRef> targetAssocList = (ArrayList<AssociationRef>) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_TARGET_ASSOCS);
            if (targetAssocList != null && targetAssocList.get(0) != null) {
                AssociationRef targetAssoc = (AssociationRef) targetAssocList.get(0);
                checkedOutNodeRef = targetAssoc.getSourceRef();
                nodeService.createAssociation(targetAssoc.getSourceRef(), targetAssoc.getTargetRef(), ContentModel.ASSOC_ORIGINAL);
            }
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_TARGET_ASSOCS);
            ArrayList<AssociationRef> sourceAssocList = (ArrayList<AssociationRef>) workingCopyProperties.get(ContentModel.PROP_ARCHIVED_SOURCE_ASSOCS);
            if (sourceAssocList != null && sourceAssocList.get(0) != null) {
                AssociationRef sourceAssoc = (AssociationRef) sourceAssocList.get(0);
                checkedOutNodeRef = sourceAssoc.getSourceRef();
                nodeService.createAssociation(sourceAssoc.getSourceRef(), sourceAssoc.getTargetRef(), ContentModel.ASSOC_WORKING_COPY_LINK);
            }
            workingCopyProperties.remove(ContentModel.PROP_ARCHIVED_SOURCE_ASSOCS);
            // clean up the archived aspect and properties for working copy node
            nodeService.removeAspect(workingCopyNodeRef, ContentModel.ASPECT_ARCHIVE_LOCKABLE);
            nodeService.setProperties(workingCopyNodeRef, workingCopyProperties);
            // restore properties on original node
            nodeService.addAspect(checkedOutNodeRef, ContentModel.ASPECT_LOCKABLE, null);
            Map<QName, Serializable> checkedOutNodeProperties = nodeService.getProperties(checkedOutNodeRef);
            checkedOutNodeProperties.put(ContentModel.PROP_LOCK_OWNER, lockOwner);
            checkedOutNodeProperties.put(ContentModel.PROP_LOCK_TYPE, lockType);
            checkedOutNodeProperties.put(ContentModel.PROP_LOCK_LIFETIME, lifetime);
            checkedOutNodeProperties.put(ContentModel.PROP_EXPIRY_DATE, expiryDate);
            checkedOutNodeProperties.put(ContentModel.PROP_LOCK_ADDITIONAL_INFO, additionalInfo);
            nodeService.setProperties(checkedOutNodeRef, checkedOutNodeProperties);
        } finally {
            policyBehaviourFilter.enableBehaviour(workingCopyNodeRef, ContentModel.ASPECT_AUDITABLE);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) Lifetime(org.alfresco.repo.lock.mem.Lifetime) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) LockType(org.alfresco.service.cmr.lock.LockType) Date(java.util.Date) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 3 with Lifetime

use of org.alfresco.repo.lock.mem.Lifetime in project alfresco-repository by Alfresco.

the class WorkingCopyAspect method beforeArchiveNode.

/**
 * beforeArchiveNode policy behaviour
 *
 * @param nodeRef
 *            the node reference about to be archived
 */
@Override
public void beforeArchiveNode(NodeRef workingCopyNodeRef) {
    NodeRef checkedOutNodeRef = checkOutCheckInService.getCheckedOut(workingCopyNodeRef);
    if (checkedOutNodeRef != null) {
        try {
            policyBehaviourFilter.disableBehaviour(workingCopyNodeRef, ContentModel.ASPECT_AUDITABLE);
            if (nodeService.hasAspect(checkedOutNodeRef, ContentModel.ASPECT_LOCKABLE)) {
                Map<QName, Serializable> checkedOutNodeProperties = nodeService.getProperties(checkedOutNodeRef);
                Map<QName, Serializable> workingCopyProperties = nodeService.getProperties(workingCopyNodeRef);
                Long nodeId = nodeDAO.getNodePair(workingCopyNodeRef).getFirst();
                // get lock properties from checked out node and set them on working copy node in order to be available for restore
                String lockOwner = (String) checkedOutNodeProperties.get(ContentModel.PROP_LOCK_OWNER);
                Date expiryDate = (Date) checkedOutNodeProperties.get(ContentModel.PROP_EXPIRY_DATE);
                String lockTypeStr = (String) checkedOutNodeProperties.get(ContentModel.PROP_LOCK_TYPE);
                LockType lockType = lockTypeStr != null ? LockType.valueOf(lockTypeStr) : null;
                String lifetimeStr = (String) checkedOutNodeProperties.get(ContentModel.PROP_LOCK_LIFETIME);
                Lifetime lifetime = lifetimeStr != null ? Lifetime.valueOf(lifetimeStr) : null;
                String additionalInfo = (String) checkedOutNodeProperties.get(ContentModel.PROP_LOCK_ADDITIONAL_INFO);
                nodeService.addAspect(workingCopyNodeRef, ContentModel.ASPECT_ARCHIVE_LOCKABLE, null);
                workingCopyProperties.put(ContentModel.PROP_ARCHIVED_LOCK_OWNER, lockOwner);
                workingCopyProperties.put(ContentModel.PROP_ARCHIVED_LOCK_TYPE, lockType);
                workingCopyProperties.put(ContentModel.PROP_ARCHIVED_LOCK_LIFETIME, lifetime);
                workingCopyProperties.put(ContentModel.PROP_ARCHIVED_EXPIRY_DATE, expiryDate);
                workingCopyProperties.put(ContentModel.PROP_ARCHIVED_LOCK_ADDITIONAL_INFO, additionalInfo);
                // Target associations
                Collection<Pair<Long, AssociationRef>> targetAssocs = nodeDAO.getTargetNodeAssocs(nodeId, null);
                for (Pair<Long, AssociationRef> targetAssocPair : targetAssocs) {
                    if (ContentModel.ASSOC_ORIGINAL.equals(targetAssocPair.getSecond().getTypeQName())) {
                        workingCopyProperties.put(ContentModel.PROP_ARCHIVED_TARGET_ASSOCS, targetAssocPair.getSecond());
                    }
                }
                // Source associations
                Collection<Pair<Long, AssociationRef>> sourceAssocs = nodeDAO.getSourceNodeAssocs(nodeId, null);
                for (Pair<Long, AssociationRef> sourceAssocPair : sourceAssocs) {
                    if (ContentModel.ASSOC_WORKING_COPY_LINK.equals(sourceAssocPair.getSecond().getTypeQName())) {
                        workingCopyProperties.put(ContentModel.PROP_ARCHIVED_SOURCE_ASSOCS, sourceAssocPair.getSecond());
                    }
                }
                // update working copy node properties
                nodeService.setProperties(workingCopyNodeRef, workingCopyProperties);
            }
        } finally {
            policyBehaviourFilter.enableBehaviour(checkedOutNodeRef, ContentModel.ASPECT_AUDITABLE);
        }
    }
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) LockType(org.alfresco.service.cmr.lock.LockType) Date(java.util.Date) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Lifetime(org.alfresco.repo.lock.mem.Lifetime) Pair(org.alfresco.util.Pair)

Example 4 with Lifetime

use of org.alfresco.repo.lock.mem.Lifetime in project alfresco-repository by Alfresco.

the class LockServiceImpl method unlock.

/**
 * @see org.alfresco.service.cmr.lock.LockService#unlock(NodeRef, boolean, boolean)
 */
@Override
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
public void unlock(NodeRef nodeRef, boolean unlockChildren, boolean allowCheckedOut) throws UnableToReleaseLockException {
    invokeBeforeUnlock(nodeRef);
    // Unlock the parent
    nodeRef = tenantService.getName(nodeRef);
    LockState lockState = getLockState(nodeRef);
    if (lockState.isLockInfo()) {
        // MNT-231: forbidden to unlock a checked out node
        if (!allowCheckedOut && nodeService.hasAspect(nodeRef, ContentModel.ASPECT_CHECKED_OUT)) {
            throw new UnableToReleaseLockException(nodeRef, CAUSE.CHECKED_OUT);
        }
        // Remove the lock from persistent storage.
        Lifetime lifetime = lockState.getLifetime();
        if (lifetime == Lifetime.PERSISTENT) {
            addToIgnoreSet(nodeRef);
            behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
            lockableAspectInterceptor.disableForThread();
            try {
                // Clear the lock
                if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE)) {
                    nodeService.removeAspect(nodeRef, ContentModel.ASPECT_LOCKABLE);
                }
            } finally {
                behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
                lockableAspectInterceptor.enableForThread();
                removeFromIgnoreSet(nodeRef);
            }
        } else if (lifetime == Lifetime.EPHEMERAL) {
            // Remove the ephemeral lock.
            lockStore.set(nodeRef, LockState.createUnlocked(nodeRef));
        } else {
            throw new IllegalStateException("Unhandled Lifetime value: " + lifetime);
        }
    }
    if (unlockChildren) {
        // Get the children and unlock them
        Collection<ChildAssociationRef> childAssocRefs = this.nodeService.getChildAssocs(nodeRef);
        for (ChildAssociationRef childAssocRef : childAssocRefs) {
            unlock(childAssocRef.getChildRef(), unlockChildren);
        }
    }
}
Also used : Lifetime(org.alfresco.repo.lock.mem.Lifetime) LockState(org.alfresco.repo.lock.mem.LockState) UnableToReleaseLockException(org.alfresco.service.cmr.lock.UnableToReleaseLockException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) Extend(org.alfresco.traitextender.Extend)

Example 5 with Lifetime

use of org.alfresco.repo.lock.mem.Lifetime in project alfresco-repository by Alfresco.

the class LockServiceImpl method getLockState.

@Override
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
public LockState getLockState(NodeRef nodeRef) {
    // Check in-memory for ephemeral locks first.
    nodeRef = tenantService.getName(nodeRef);
    LockState lockState = lockStore.get(nodeRef);
    if (lockState != null) {
        String lockOwner = lockState.getOwner();
        Date expiryDate = lockState.getExpires();
        LockStatus status = LockUtils.lockStatus(lockOwner, lockOwner, expiryDate);
        // in-memory ephemeral lock which is expired is irrelevant
        if (status.equals(LockStatus.LOCK_EXPIRED)) {
            lockState = null;
        }
    }
    // whether it represents a real lock
    if (lockState == null || !lockState.isLockInfo()) {
        // No in-memory state, so get from the DB.
        if (nodeService.exists(nodeRef) && nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE)) {
            String lockOwner = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_OWNER);
            Date expiryDate = (Date) nodeService.getProperty(nodeRef, ContentModel.PROP_EXPIRY_DATE);
            String lockTypeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_TYPE);
            LockType lockType = lockTypeStr != null ? LockType.valueOf(lockTypeStr) : null;
            String lifetimeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_LIFETIME);
            Lifetime lifetime = lifetimeStr != null ? Lifetime.valueOf(lifetimeStr) : null;
            String additionalInfo = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_ADDITIONAL_INFO);
            // Mark lockstate as PERSISTENT as it was in the persistent storage!
            lockState = LockState.createLock(nodeRef, lockType, lockOwner, expiryDate, lifetime, additionalInfo);
        } else {
            // There is no lock information
            lockState = LockState.createUnlocked(nodeRef);
        }
    }
    // Never return a null LockState
    Assert.notNull(lockState);
    return lockState;
}
Also used : Lifetime(org.alfresco.repo.lock.mem.Lifetime) LockStatus(org.alfresco.service.cmr.lock.LockStatus) LockState(org.alfresco.repo.lock.mem.LockState) LockType(org.alfresco.service.cmr.lock.LockType) Date(java.util.Date) Extend(org.alfresco.traitextender.Extend)

Aggregations

Lifetime (org.alfresco.repo.lock.mem.Lifetime)5 Date (java.util.Date)3 LockType (org.alfresco.service.cmr.lock.LockType)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 Serializable (java.io.Serializable)2 LockState (org.alfresco.repo.lock.mem.LockState)2 AssociationRef (org.alfresco.service.cmr.repository.AssociationRef)2 QName (org.alfresco.service.namespace.QName)2 Extend (org.alfresco.traitextender.Extend)2 ArrayList (java.util.ArrayList)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 LockStatus (org.alfresco.service.cmr.lock.LockStatus)1 UnableToReleaseLockException (org.alfresco.service.cmr.lock.UnableToReleaseLockException)1 Pair (org.alfresco.util.Pair)1