1 17 package org.alfresco.repo.security.permissions.dynamic; 18 19 import javax.transaction.UserTransaction ; 20 21 import junit.framework.TestCase; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.security.authentication.AuthenticationComponent; 25 import org.alfresco.repo.security.authentication.MutableAuthenticationDao; 26 import org.alfresco.service.ServiceRegistry; 27 import org.alfresco.service.cmr.lock.LockService; 28 import org.alfresco.service.cmr.lock.LockStatus; 29 import org.alfresco.service.cmr.lock.LockType; 30 import org.alfresco.service.cmr.repository.NodeRef; 31 import org.alfresco.service.cmr.repository.NodeService; 32 import org.alfresco.service.cmr.repository.StoreRef; 33 import org.alfresco.service.cmr.security.AccessStatus; 34 import org.alfresco.service.cmr.security.AuthenticationService; 35 import org.alfresco.service.cmr.security.PermissionService; 36 import org.alfresco.service.transaction.TransactionService; 37 import org.alfresco.util.ApplicationContextHelper; 38 import org.springframework.context.ApplicationContext; 39 40 public class LockOwnerDynamicAuthorityTest extends TestCase 41 { 42 private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); 43 44 private NodeService nodeService; 45 46 private AuthenticationService authenticationService; 47 48 private AuthenticationComponent authenticationComponent; 49 50 private MutableAuthenticationDao authenticationDAO; 51 52 private LockService lockService; 53 54 private NodeRef rootNodeRef; 55 56 private UserTransaction userTransaction; 57 58 private PermissionService permissionService; 59 60 private LockOwnerDynamicAuthority dynamicAuthority; 61 62 public LockOwnerDynamicAuthorityTest() 63 { 64 super(); 65 } 66 67 public LockOwnerDynamicAuthorityTest(String arg0) 68 { 69 super(arg0); 70 } 71 72 public void setUp() throws Exception 73 { 74 nodeService = (NodeService) ctx.getBean("nodeService"); 75 authenticationService = (AuthenticationService) ctx.getBean("authenticationService"); 76 authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent"); 77 lockService = (LockService) ctx.getBean("lockService"); 78 permissionService = (PermissionService) ctx.getBean("permissionService"); 79 authenticationDAO = (MutableAuthenticationDao) ctx.getBean("alfDaoImpl"); 80 81 authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName()); 82 83 TransactionService transactionService = (TransactionService) ctx.getBean(ServiceRegistry.TRANSACTION_SERVICE 84 .getLocalName()); 85 userTransaction = transactionService.getUserTransaction(); 86 userTransaction.begin(); 87 88 StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis()); 89 rootNodeRef = nodeService.getRootNode(storeRef); 90 permissionService.setPermission(rootNodeRef, PermissionService.ALL_AUTHORITIES, PermissionService.ADD_CHILDREN, 91 true); 92 93 if (authenticationDAO.userExists("andy")) 94 { 95 authenticationService.deleteAuthentication("andy"); 96 } 97 authenticationService.createAuthentication("andy", "andy".toCharArray()); 98 if (authenticationDAO.userExists("lemur")) 99 { 100 authenticationService.deleteAuthentication("lemur"); 101 } 102 authenticationService.createAuthentication("lemur", "lemur".toCharArray()); 103 if (authenticationDAO.userExists("frog")) 104 { 105 authenticationService.deleteAuthentication("frog"); 106 } 107 authenticationService.createAuthentication("frog", "frog".toCharArray()); 108 109 dynamicAuthority = new LockOwnerDynamicAuthority(); 110 dynamicAuthority.setLockService(lockService); 111 112 authenticationComponent.clearCurrentSecurityContext(); 113 } 114 115 @Override 116 protected void tearDown() throws Exception 117 { 118 authenticationComponent.clearCurrentSecurityContext(); 119 userTransaction.rollback(); 120 super.tearDown(); 121 } 122 123 public void testSetup() 124 { 125 assertNotNull(nodeService); 126 assertNotNull(authenticationService); 127 assertNotNull(lockService); 128 } 129 130 public void testUnSet() 131 { 132 permissionService.setPermission(rootNodeRef, "andy", PermissionService.ALL_PERMISSIONS, true); 133 authenticationService.authenticate("andy", "andy".toCharArray()); 134 assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(rootNodeRef)); 135 authenticationService.clearCurrentSecurityContext(); 136 } 137 138 public void testPermissionWithNoLockAspect() 139 { 140 authenticationService.authenticate("andy", "andy".toCharArray()); 141 NodeRef testNode = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, ContentModel.TYPE_PERSON, 142 ContentModel.TYPE_CMOBJECT, null).getChildRef(); 143 assertNotNull(testNode); 144 permissionService.setPermission(rootNodeRef, "andy", PermissionService.ALL_PERMISSIONS, true); 145 146 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(rootNodeRef, 147 PermissionService.LOCK)); 148 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(rootNodeRef, 149 PermissionService.UNLOCK)); 150 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(rootNodeRef, PermissionService.CHECK_OUT)); 151 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(rootNodeRef, PermissionService.CHECK_IN)); 152 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(rootNodeRef, PermissionService.CANCEL_CHECK_OUT)); 153 154 } 155 156 public void testPermissionWithLockAspect() 157 { 158 permissionService.setPermission(rootNodeRef, "andy", PermissionService.ALL_PERMISSIONS, true); 159 permissionService.setPermission(rootNodeRef, "lemur", PermissionService.CHECK_OUT, true); 160 permissionService.setPermission(rootNodeRef, "lemur", PermissionService.WRITE, true); 161 permissionService.setPermission(rootNodeRef, "lemur", PermissionService.READ, true); 162 permissionService.setPermission(rootNodeRef, "frog", PermissionService.CHECK_OUT, true); 163 permissionService.setPermission(rootNodeRef, "frog", PermissionService.WRITE, true); 164 permissionService.setPermission(rootNodeRef, "frog", PermissionService.READ, true); 165 authenticationService.authenticate("andy", "andy".toCharArray()); 166 NodeRef testNode = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, ContentModel.TYPE_PERSON, 167 ContentModel.TYPE_CMOBJECT, null).getChildRef(); 168 lockService.lock(testNode, LockType.READ_ONLY_LOCK); 169 170 171 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, 172 PermissionService.LOCK)); 173 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, 174 PermissionService.UNLOCK)); 175 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CHECK_OUT)); 176 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CHECK_IN)); 177 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CANCEL_CHECK_OUT)); 178 179 authenticationService.authenticate("lemur", "lemur".toCharArray()); 180 181 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, 182 PermissionService.LOCK)); 183 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(testNode, 184 PermissionService.UNLOCK)); 185 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CHECK_OUT)); 186 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(testNode, PermissionService.CHECK_IN)); 187 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(testNode, PermissionService.CANCEL_CHECK_OUT)); 188 189 authenticationService.authenticate("andy", "andy".toCharArray()); 190 lockService.unlock(testNode); 191 authenticationService.authenticate("lemur", "lemur".toCharArray()); 192 lockService.lock(testNode, LockType.READ_ONLY_LOCK); 193 194 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, 195 PermissionService.LOCK)); 196 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, 197 PermissionService.UNLOCK)); 198 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CHECK_OUT)); 199 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CHECK_IN)); 200 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CANCEL_CHECK_OUT)); 201 202 203 authenticationService.authenticate("frog", "frog".toCharArray()); 204 205 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, 206 PermissionService.LOCK)); 207 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(testNode, 208 PermissionService.UNLOCK)); 209 assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(testNode, PermissionService.CHECK_OUT)); 210 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(testNode, PermissionService.CHECK_IN)); 211 assertEquals(AccessStatus.DENIED, permissionService.hasPermission(testNode, PermissionService.CANCEL_CHECK_OUT)); 212 213 } 214 215 216 } 217 | Popular Tags |