DictionaryAuthorization implements IAuthorization { @Override public boolean chkAuthorization(String userName, Object accessObject, String[] userRoles) throws QuadrigaStorageException, QuadrigaAccessException { IDictionary dictionary; if (accessObject instanceof String) { String dictionaryId = (String) accessObject; dictionary = dictonaryManager.getDictionaryDetails(dictionaryId); } else { try { dictionary = (IDictionary) accessObject; } catch (ClassCastException cce) { throw new IllegalObjectException(cce); } } if (dictionary == null) { throw new QuadrigaAccessException(); } String dictionaryOwner = dictionary.getOwner().getUserName(); if (userName.equals(dictionaryOwner)) { return true; } if (userRoles.length == 0) return false; List<String> roles = Arrays.asList(userRoles); List<IDictionaryCollaborator> dictCollaboratorList = dictonaryManager .showCollaboratingUsers(dictionary.getDictionaryId()); if (dictCollaboratorList == null || dictCollaboratorList.isEmpty()) return false; for (IDictionaryCollaborator dictCollaborator : dictCollaboratorList) { String collaboratorName = dictCollaborator.getCollaborator().getUserObj().getUserName(); if (userName != null && userName.equals(collaboratorName)) { List<IQuadrigaRole> collaboratorRoles = dictCollaborator.getCollaborator().getCollaboratorRoles(); if (collaboratorRoles != null) { for (IQuadrigaRole collabRole : collaboratorRoles) { String collaboratorRoleId = collabRole.getId(); if (roles != null) { if (roles.contains(collaboratorRoleId)) { return true; } } } } } } return false; }  @Override boolean chkAuthorization(String userName, Object accessObject, String[] userRoles); @Override boolean chkAuthorizationByRole(String userName, String[] userRoles); @Override boolean chkAuthorization(String userName, List<String> accessObjectId, String[] userRoles);  }
@Test public void testChkAuthorizationHasValidRole() throws QuadrigaStorageException, QuadrigaAccessException { boolean authorized = authorization.chkAuthorization("collabuser", "dictionaryId", new String[] { "role1" }); assertTrue(authorized); }
@Test public void testChkAuthorizationHasNoValidRole() throws QuadrigaStorageException, QuadrigaAccessException { boolean authorized = authorization.chkAuthorization("collabuser", "dictionaryId", new String[] { "role2" }); assertFalse(authorized); }
@Test public void testChkAuthorizationIsNotCollaborator() throws QuadrigaStorageException, QuadrigaAccessException { boolean authorized = authorization.chkAuthorization("testuser2", "dictionaryId", new String[] { "role1" }); assertFalse(authorized); }
@Test public void testChkAuthorizationIsOwner() throws QuadrigaStorageException, QuadrigaAccessException { boolean authorized = authorization.chkAuthorization("testuser", "dictionaryId", new String[] { "" }); assertTrue(authorized); }