Yet another merge from head to WCM-DEV2.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3774 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2006-09-12 18:55:07 +00:00
parent e459c188f6
commit 39a18df7f2
65 changed files with 11344 additions and 8063 deletions

View File

@@ -16,6 +16,9 @@
*/
package org.alfresco.repo.audit;
import java.util.List;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.aopalliance.intercept.MethodInvocation;
@@ -47,6 +50,14 @@ public interface AuditComponent
* an arbitrary list of parameters
*/
public void audit(String source, String description, NodeRef key, Object... args);
/**
* Get the audit trail for a node.
*
* @param nodeRef
* @return
*/
public List<AuditInfo> getAuditTrail(NodeRef nodeRef);
}

View File

@@ -21,12 +21,15 @@ import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.service.Auditable;
import org.alfresco.service.NotAuditable;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -181,7 +184,7 @@ public class AuditComponentImpl implements AuditComponent
*/
public Object auditImpl(MethodInvocation mi) throws Throwable
{
AuditInfo auditInfo = new AuditInfo(auditConfiguration);
AuditState auditInfo = new AuditState(auditConfiguration);
// RecordOptions recordOptions = auditModel.getAuditRecordOptions(mi);
AuditMode auditMode = AuditMode.UNSET;
try
@@ -221,7 +224,7 @@ public class AuditComponentImpl implements AuditComponent
* @param t
* @return
*/
private AuditMode onError(AuditMode auditMode, AuditInfo auditInfo, MethodInvocation mi, Throwable t)
private AuditMode onError(AuditMode auditMode, AuditState auditInfo, MethodInvocation mi, Throwable t)
{
if ((auditMode == AuditMode.ALL) || (auditMode == AuditMode.FAIL))
{
@@ -241,7 +244,7 @@ public class AuditComponentImpl implements AuditComponent
* @param returnObject
* @return
*/
private AuditMode postInvocation(AuditMode auditMode, AuditInfo auditInfo, MethodInvocation mi, Object returnObject)
private AuditMode postInvocation(AuditMode auditMode, AuditState auditInfo, MethodInvocation mi, Object returnObject)
{
if (returnObject == null)
{
@@ -255,6 +258,33 @@ public class AuditComponentImpl implements AuditComponent
{
auditInfo.setReturnObject(returnObject.toString());
}
Auditable auditable = mi.getMethod().getAnnotation(Auditable.class);
if (auditable.key() == Auditable.Key.RETURN)
{
if (returnObject != null)
{
if (returnObject instanceof NodeRef)
{
NodeRef key = (NodeRef) returnObject;
auditInfo.setKeyStore(key.getStoreRef());
auditInfo.setKeyGUID(key.getId());
}
else if (returnObject instanceof StoreRef)
{
auditInfo.setKeyStore((StoreRef)returnObject);
}
}
}
// If the user name is not set, try and set it after the method call.
// This covers authentication when the user is only known after the call.
if (auditInfo.getUserIdentifier() == null)
{
auditInfo.setUserIdentifier(AuthenticationUtil.getCurrentUserName());
}
return auditMode;
}
@@ -266,7 +296,7 @@ public class AuditComponentImpl implements AuditComponent
* @param mi
* @return
*/
private AuditMode beforeInvocation(AuditMode auditMode, AuditInfo auditInfo, MethodInvocation mi)
private AuditMode beforeInvocation(AuditMode auditMode, AuditState auditInfo, MethodInvocation mi)
{
AuditMode effectiveAuditMode = auditModel.beforeExecution(auditMode, mi);
@@ -283,27 +313,83 @@ public class AuditComponentImpl implements AuditComponent
auditInfo.setFail(false);
auditInfo.setFiltered(false);
auditInfo.setHostAddress(auditHost);
auditInfo.setKeyGUID(null);
Auditable auditable = mi.getMethod().getAnnotation(Auditable.class);
Object key = null;
switch (auditable.key())
{
case ARG_0:
key = mi.getArguments()[0];
break;
case ARG_1:
key = mi.getArguments()[1];
break;
case ARG_2:
key = mi.getArguments()[2];
break;
case ARG_3:
key = mi.getArguments()[3];
break;
case ARG_4:
key = mi.getArguments()[4];
break;
case ARG_5:
key = mi.getArguments()[5];
break;
case ARG_6:
key = mi.getArguments()[6];
break;
case ARG_7:
key = mi.getArguments()[7];
break;
case ARG_8:
key = mi.getArguments()[8];
break;
case ARG_9:
key = mi.getArguments()[9];
break;
case NO_KEY:
default:
break;
}
if (key != null)
{
if (key instanceof NodeRef)
{
auditInfo.setKeyStore(((NodeRef) key).getStoreRef());
auditInfo.setKeyGUID(((NodeRef) key).getId());
}
else if (key instanceof StoreRef)
{
auditInfo.setKeyStore((StoreRef) key);
}
}
auditInfo.setKeyPropertiesAfter(null);
auditInfo.setKeyPropertiesBefore(null);
auditInfo.setKeyStore(null);
auditInfo.setMessage(null);
if (mi.getArguments() != null)
{
Serializable[] serArgs = new Serializable[mi.getArguments().length];
for (int i = 0; i < mi.getArguments().length; i++)
{
if (mi.getArguments()[i] == null)
if ((auditable.recordable() == null)
|| (auditable.recordable().length <= i) || auditable.recordable()[i])
{
serArgs[i] = null;
}
else if (mi.getArguments()[i] instanceof Serializable)
{
serArgs[i] = (Serializable) mi.getArguments()[i];
if (mi.getArguments()[i] == null)
{
serArgs[i] = null;
}
else if (mi.getArguments()[i] instanceof Serializable)
{
serArgs[i] = (Serializable) mi.getArguments()[i];
}
else
{
serArgs[i] = mi.getArguments()[i].toString();
}
}
else
{
serArgs[i] = mi.getArguments()[i].toString();
serArgs[i] = "********";
}
}
auditInfo.setMethodArguments(serArgs);
@@ -322,9 +408,9 @@ public class AuditComponentImpl implements AuditComponent
/**
* A simple audit entry Currently we ignore filtering here.
*/
public void audit(String source, String description, NodeRef key, Object... args)
public void audit(String source, String description, NodeRef key, Object... args)
{
AuditInfo auditInfo = new AuditInfo(auditConfiguration);
AuditState auditInfo = new AuditState(auditConfiguration);
// RecordOptions recordOptions = auditModel.getAuditRecordOptions(mi);
AuditMode auditMode = AuditMode.UNSET;
try
@@ -353,18 +439,23 @@ public class AuditComponentImpl implements AuditComponent
}
}
private AuditMode onApplicationAudit(AuditMode auditMode, AuditInfo auditInfo, String source, String description,
public List<AuditInfo> getAuditTrail(NodeRef nodeRef)
{
return auditDAO.getAuditTrail(nodeRef);
}
private AuditMode onApplicationAudit(AuditMode auditMode, AuditState auditInfo, String source, String description,
NodeRef key, Object... args)
{
AuditMode effectiveAuditMode = auditModel.beforeExecution(auditMode, source, description, key, args);
if (auditMode != AuditMode.NONE)
{
if(source.equals(SYSTEM_APPLICATION))
if (source.equals(SYSTEM_APPLICATION))
{
throw new AuditException("Application audit can not use the reserved identifier "+SYSTEM_APPLICATION);
throw new AuditException("Application audit can not use the reserved identifier " + SYSTEM_APPLICATION);
}
auditInfo.setAuditApplication(source);
auditInfo.setAuditConfiguration(auditConfiguration);
auditInfo.setAuditMethod(null);
@@ -374,10 +465,13 @@ public class AuditComponentImpl implements AuditComponent
auditInfo.setFail(false);
auditInfo.setFiltered(false);
auditInfo.setHostAddress(auditHost);
auditInfo.setKeyGUID(null);
if (key != null)
{
auditInfo.setKeyStore(key.getStoreRef());
auditInfo.setKeyGUID(key.getId());
}
auditInfo.setKeyPropertiesAfter(null);
auditInfo.setKeyPropertiesBefore(null);
auditInfo.setKeyStore(null);
auditInfo.setMessage(description);
if (args != null)
{
@@ -409,9 +503,9 @@ public class AuditComponentImpl implements AuditComponent
return effectiveAuditMode;
}
private AuditMode onError(AuditMode auditMode, AuditInfo auditInfo, Throwable t, String source, String description,
NodeRef key, Object... args)
private AuditMode onError(AuditMode auditMode, AuditState auditInfo, Throwable t, String source,
String description, NodeRef key, Object... args)
{
if ((auditMode == AuditMode.ALL) || (auditMode == AuditMode.FAIL))
{

View File

@@ -16,6 +16,11 @@
*/
package org.alfresco.repo.audit;
import java.util.List;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.repository.NodeRef;
/**
* The interface to persist audit information.
*
@@ -28,5 +33,13 @@ public interface AuditDAO
*
* @param auditInfo
*/
public void audit(AuditInfo auditInfo);
public void audit(AuditState auditInfo);
/**
* Get the audit trail for a node.
*
* @param nodeRef
* @return
*/
public List<AuditInfo> getAuditTrail(NodeRef nodeRef);
}

View File

@@ -16,8 +16,12 @@
*/
package org.alfresco.repo.audit;
import java.util.List;
import javax.transaction.UserTransaction;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.audit.AuditService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
@@ -64,24 +68,49 @@ public class AuditServiceImpl implements AuditService
auditComponent.audit(source, description, key, args);
}
public List<AuditInfo> getAuditTrail(NodeRef nodeRef)
{
return auditComponent.getAuditTrail(nodeRef);
}
public static void main(String[] args) throws Exception
{
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
AuditService as = (AuditService) ctx.getBean("AuditService");
TransactionService txs = (TransactionService) ctx.getBean("transactionComponent");
UserTransaction tx = txs.getUserTransaction();
tx.begin();
as.audit("AuditedApp", "First");
as.audit("AuditedApp", "Second", new NodeRef(new StoreRef("test", "audit"), "id"));
as.audit("AuditedApp", "Third", new Object[]{"one", "two", "three"});
as.audit("AuditedApp", "Fourth", new NodeRef(new StoreRef("test", "audit"), "id"), new Object[]{"one", "two", "three"});
as.audit("UnAuditedApp", "First");
as.audit("UnAuditedApp", "Second", new NodeRef(new StoreRef("test", "audit"), "id"));
as.audit("UnAuditedApp", "Third", new Object[]{"one", "two", "three"});
as.audit("UnAuditedApp", "Fourth", new NodeRef(new StoreRef("test", "audit"), "id"), new Object[]{"one", "two", "three"});
AuthenticationUtil.setSystemUserAsCurrentUser();
try
{
NodeRef nodeRef = new NodeRef(new StoreRef("test", "audit"), "id");
as.audit("AuditedApp", "First");
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("AuditedApp", "Second", nodeRef);
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("AuditedApp", "Third", new Object[] { "one", "two", "three" });
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("AuditedApp", "Fourth",nodeRef, new Object[] { "one",
"two", "three" });
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("UnAuditedApp", "First");
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("UnAuditedApp", "Second", nodeRef);
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("UnAuditedApp", "Third", new Object[] { "one", "two", "three" });
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
as.audit("UnAuditedApp", "Fourth", nodeRef, new Object[] { "one",
"two", "three" });
System.out.println("Audit entries for node "+as.getAuditTrail(nodeRef).size());
}
finally
{
AuthenticationUtil.clearCurrentSecurityContext();
}
tx.commit();
}

View File

@@ -0,0 +1,291 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.audit;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.audit.model.AuditEntry;
import org.alfresco.repo.audit.model.TrueFalseUnset;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
import org.alfresco.repo.security.permissions.PermissionServiceSPI;
import org.alfresco.repo.security.permissions.impl.ModelDAO;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.audit.AuditService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.NamespacePrefixResolver;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.BaseSpringTest;
public class AuditServiceTest extends BaseSpringTest
{
private NodeService nodeService;
private DictionaryService dictionaryService;
private PermissionServiceSPI permissionService;
private NamespacePrefixResolver namespacePrefixResolver;
private AuthenticationService authenticationService;
private AuthenticationComponent authenticationComponent;
private ServiceRegistry serviceRegistry;
private ModelDAO permissionModelDAO;
private PersonService personService;
private AuthorityService authorityService;
private MutableAuthenticationDao authenticationDAO;
private NodeRef rootNodeRef;
private NodeRef systemNodeRef;
private AuditService auditService;
private AuditEntry auditEntry;
private NodeRef typesNodeRef;
private QName children;
private QName system;
private QName container;
private QName types;
public AuditServiceTest()
{
super();
}
protected void onSetUpInTransaction() throws Exception
{
nodeService = (NodeService) applicationContext.getBean("nodeService");
dictionaryService = (DictionaryService) applicationContext.getBean(ServiceRegistry.DICTIONARY_SERVICE
.getLocalName());
permissionService = (PermissionServiceSPI) applicationContext.getBean("permissionService");
namespacePrefixResolver = (NamespacePrefixResolver) applicationContext
.getBean(ServiceRegistry.NAMESPACE_SERVICE.getLocalName());
authenticationService = (AuthenticationService) applicationContext.getBean("authenticationService");
authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");
serviceRegistry = (ServiceRegistry) applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
permissionModelDAO = (ModelDAO) applicationContext.getBean("permissionsModelDAO");
personService = (PersonService) applicationContext.getBean("personService");
authorityService = (AuthorityService) applicationContext.getBean("authorityService");
authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
authenticationDAO = (MutableAuthenticationDao) applicationContext.getBean("alfDaoImpl");
auditService = (AuditService) applicationContext.getBean("AuditService");
auditEntry = (AuditEntry) applicationContext.getBean("auditModel");
StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.nanoTime());
rootNodeRef = nodeService.getRootNode(storeRef);
children = ContentModel.ASSOC_CHILDREN;
system = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "system");
container = ContentModel.TYPE_CONTAINER;
types = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "people");
systemNodeRef = nodeService.createNode(rootNodeRef, children, system, container).getChildRef();
typesNodeRef = nodeService.createNode(systemNodeRef, children, types, container).getChildRef();
Map<QName, Serializable> props = createPersonProperties("andy");
nodeService.createNode(typesNodeRef, children, ContentModel.TYPE_PERSON, container, props).getChildRef();
props = createPersonProperties("lemur");
nodeService.createNode(typesNodeRef, children, ContentModel.TYPE_PERSON, container, props).getChildRef();
// create an authentication object e.g. the user
if (authenticationDAO.userExists("andy"))
{
authenticationService.deleteAuthentication("andy");
}
authenticationService.createAuthentication("andy", "andy".toCharArray());
if (authenticationDAO.userExists("lemur"))
{
authenticationService.deleteAuthentication("lemur");
}
authenticationService.createAuthentication("lemur", "lemur".toCharArray());
if (authenticationDAO.userExists("admin"))
{
authenticationService.deleteAuthentication("admin");
}
authenticationService.createAuthentication("admin", "admin".toCharArray());
authenticationComponent.clearCurrentSecurityContext();
}
public void testApplicationAudit()
{
AuthenticationUtil.setSystemUserAsCurrentUser();
try
{
NodeRef nodeRef = new NodeRef(new StoreRef("test", "audit"), "id");
int start = auditService.getAuditTrail(nodeRef).size();
int increment = auditEntry.getEnabled() == TrueFalseUnset.TRUE ? 1 : 0;
auditService.audit("AuditedApp", "First");
assertEquals(start, auditService.getAuditTrail(nodeRef).size());
auditService.audit("AuditedApp", "Second", nodeRef);
assertEquals(start + (1 * increment), auditService.getAuditTrail(nodeRef).size());
auditService.audit("AuditedApp", "Third", new Object[] { "one", "two", "three" });
assertEquals(start + (1 * increment), auditService.getAuditTrail(nodeRef).size());
auditService.audit("AuditedApp", "Fourth", nodeRef, new Object[] { "one", "two", "three" });
assertEquals(start + (2 * increment), auditService.getAuditTrail(nodeRef).size());
auditService.audit("UnAuditedApp", "First");
assertEquals(start + (2 * increment), auditService.getAuditTrail(nodeRef).size());
auditService.audit("UnAuditedApp", "Second", nodeRef);
assertEquals(start + (3 * increment), auditService.getAuditTrail(nodeRef).size());
auditService.audit("UnAuditedApp", "Third", new Object[] { "one", "two", "three" });
assertEquals(start + (3 * increment), auditService.getAuditTrail(nodeRef).size());
auditService.audit("UnAuditedApp", "Fourth", nodeRef, new Object[] { "one", "two", "three" });
assertEquals(start + (4 * increment), auditService.getAuditTrail(nodeRef).size());
}
finally
{
AuthenticationUtil.clearCurrentSecurityContext();
}
}
public void testNodeServiceAudit()
{
AuthenticationUtil.setSystemUserAsCurrentUser();
try
{
int start = auditService.getAuditTrail(typesNodeRef).size();
int increment = auditEntry.getEnabled() == TrueFalseUnset.TRUE ? 1 : 0;
// Create
Map<QName, Serializable> props = createPersonProperties("woof");
NodeRef created = serviceRegistry.getNodeService().createNode(typesNodeRef, children,
ContentModel.TYPE_PERSON, container, props).getChildRef();
assertEquals(start + (1 * increment), auditService.getAuditTrail(typesNodeRef).size());
List<AuditInfo> list = auditService.getAuditTrail(typesNodeRef);
assertEquals((1 * increment), auditService.getAuditTrail(created).size());
// Update
serviceRegistry.getNodeService().setProperty(created, ContentModel.PROP_FIRSTNAME, "New First Name");
assertEquals((2 * increment), auditService.getAuditTrail(created).size());
// Update
serviceRegistry.getNodeService().setProperty(created, ContentModel.PROP_FIRSTNAME, "Next First Name");
assertEquals((3 * increment), auditService.getAuditTrail(created).size());
// Delete
serviceRegistry.getNodeService().deleteNode(created);
assertEquals((4 * increment), auditService.getAuditTrail(created).size());
list = auditService.getAuditTrail(created);
assertNotNull(list);
}
finally
{
AuthenticationUtil.clearCurrentSecurityContext();
}
}
public void xtestCreateStore()
{
AuthenticationUtil.setSystemUserAsCurrentUser();
try
{
serviceRegistry.getNodeService()
.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_Audit_" + System.nanoTime());
// Should have a query to support this - check direct in the DB
}
finally
{
AuthenticationUtil.clearCurrentSecurityContext();
}
setComplete();
}
public void xtestAuthenticartionDoesNotReportPasswords()
{
// Should have a query to support this - check direct in the DB
AuthenticationUtil.setSystemUserAsCurrentUser();
try
{
serviceRegistry.getAuthenticationService().createAuthentication("cabbage", "cabbage".toCharArray());
serviceRegistry.getAuthenticationService().updateAuthentication("cabbage", "cabbage".toCharArray(),
"red".toCharArray());
}
finally
{
AuthenticationUtil.clearCurrentSecurityContext();
}
try
{
serviceRegistry.getAuthenticationService().authenticate("cabbage", "red".toCharArray());
}
finally
{
serviceRegistry.getAuthenticationService().clearCurrentSecurityContext();
}
setComplete();
}
public void xtestAuthenticartionFailure()
{
// Should have a query to support this - check direct in the DB
AuthenticationUtil.setSystemUserAsCurrentUser();
serviceRegistry.getAuthenticationService().createAuthentication("woof", "cabbage".toCharArray());
serviceRegistry.getAuthenticationService().authenticate("woof", "red".toCharArray());
}
public void testThereIsAnAuditService()
{
assertNotNull(serviceRegistry.getAuditService());
}
private Map<QName, Serializable> createPersonProperties(String userName)
{
HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
properties.put(ContentModel.PROP_USERNAME, userName);
return properties;
}
}

View File

@@ -0,0 +1,385 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.audit;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Map;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;
/**
* A class to encapsulate audit information supplied to the DAO layer.
*
* Null entries should be stored.
*
* @author Andy Hind
*/
public class AuditState
{
private static Logger s_logger = Logger.getLogger(AuditState.class);
/**
* The user identifier for the person who caused this audit entry
*/
private String userIdentifier;
/**
* The date for this audit entry
*/
private Date date;
/**
* The transaction id in which this entry was made
*/
private String txId;
/**
* The session is for this action
*/
private String sessionId;
/**
* The store in which the action occured.
*/
private StoreRef keyStore;
/**
* For a node ref, the node for the action.
*/
private String keyGUID;
/**
* The path of the key
*/
private String keyPath;
/**
* The audit application
* Internal uses the "System" key and will only audit method information.
*/
private String auditApplication;
/**
* The service holding the audited method.
*/
private String auditService;
/**
* The name of the audited method.
*/
private String auditMethod;
/**
* Did this entry passa filter?
* If false - all entries were being recorded.
*/
private boolean filtered;
/**
* The audit configuration in use at the time.
*/
private AuditConfiguration auditConfiguration;
/**
* The object returned by the audited method.
*/
private Serializable returnObject;
/**
* The arguments to the audited method.
*/
private Serializable[] methodArguments;
/**
* Any Exception thrown by the audited method.
*/
private Throwable throwable;
/**
* Did the audited method throw an exception?
*/
private boolean fail;
/**
* The host address for where the audit was generated.
*/
private InetAddress hostAddress;
private static InetAddress s_hostAddress;
/**
* The client address causing the audit
*/
private InetAddress clientAddress;
/**
* The properties of the key node before the method execution.
*/
private Map<QName, Serializable> keyPropertiesBefore;
/**
* The properties of the key node after the method execution.
*/
private Map<QName, Serializable> keyPropertiesAfter;
/**
* For general auditing - the audit message.
*/
private String message;
static
{
try
{
s_hostAddress = InetAddress.getLocalHost();
}
catch (UnknownHostException e)
{
s_logger.error(e);
s_hostAddress = null;
}
}
public AuditState(AuditConfiguration auditConfiguration)
{
super();
// Add default information
userIdentifier = AuthenticationUtil.getCurrentUserName();
date = new Date();
txId = AlfrescoTransactionSupport.getTransactionId();
sessionId = "Unavailable";
hostAddress = s_hostAddress;
}
public String getAuditApplication()
{
return auditApplication;
}
public void setAuditApplication(String auditApplication)
{
this.auditApplication = auditApplication;
}
public AuditConfiguration getAuditConfiguration()
{
return auditConfiguration;
}
public void setAuditConfiguration(AuditConfiguration auditConfiguration)
{
this.auditConfiguration = auditConfiguration;
}
public String getAuditMethod()
{
return auditMethod;
}
public void setAuditMethod(String auditMethod)
{
this.auditMethod = auditMethod;
}
public String getAuditService()
{
return auditService;
}
public void setAuditService(String auditService)
{
this.auditService = auditService;
}
public InetAddress getClientAddress()
{
return clientAddress;
}
public void setClientAddress(InetAddress clientAddress)
{
this.clientAddress = clientAddress;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
public boolean isFail()
{
return fail;
}
public void setFail(boolean fail)
{
this.fail = fail;
}
public boolean isFiltered()
{
return filtered;
}
public void setFiltered(boolean filtered)
{
this.filtered = filtered;
}
public InetAddress getHostAddress()
{
return hostAddress;
}
public void setHostAddress(InetAddress hostAddress)
{
this.hostAddress = hostAddress;
}
public String getKeyGUID()
{
return keyGUID;
}
public void setKeyGUID(String keyGUID)
{
this.keyGUID = keyGUID;
}
public Map<QName, Serializable> getKeyPropertiesAfter()
{
return keyPropertiesAfter;
}
public void setKeyPropertiesAfter(Map<QName, Serializable> keyPropertiesAfter)
{
this.keyPropertiesAfter = keyPropertiesAfter;
}
public Map<QName, Serializable> getKeyPropertiesBefore()
{
return keyPropertiesBefore;
}
public void setKeyPropertiesBefore(Map<QName, Serializable> keyPropertiesBefore)
{
this.keyPropertiesBefore = keyPropertiesBefore;
}
public StoreRef getKeyStore()
{
return keyStore;
}
public void setKeyStore(StoreRef keyStore)
{
this.keyStore = keyStore;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public Serializable[] getMethodArguments()
{
return methodArguments;
}
public void setMethodArguments(Serializable[] methodArguments)
{
this.methodArguments = methodArguments;
}
public String getPath()
{
return keyPath;
}
public void setPath(String keyPath)
{
this.keyPath = keyPath;
}
public Serializable getReturnObject()
{
return returnObject;
}
public void setReturnObject(Serializable returnObject)
{
this.returnObject = returnObject;
}
public String getSessionId()
{
return sessionId;
}
public void setSessionId(String sessionId)
{
this.sessionId = sessionId;
}
public Throwable getThrowable()
{
return throwable;
}
public void setThrowable(Throwable throwable)
{
this.throwable = throwable;
}
public String getTxId()
{
return txId;
}
public void setTxId(String txId)
{
this.txId = txId;
}
public String getUserIdentifier()
{
return userIdentifier;
}
public void setUserIdentifier(String userIdentifier)
{
this.userIdentifier = userIdentifier;
}
}

View File

@@ -109,9 +109,9 @@
<!-- The app_source_idx index is used to find the app source -->
<!-- The look up is always the tripple, the service and method or just the method may be null -->
<property name="application" column="application" type="string" length="256" not-null="true" index="app_source_idx"/>
<property name="service" column="service" type="string" length="256" not-null="false" index="app_source_idx"/>
<property name="method" column="method" type="string" length="256" not-null="false" index="app_source_idx"/>
<property name="application" column="application" type="string" length="255" not-null="true" index="app_source_app_idx"/>
<property name="service" column="service" type="string" length="255" not-null="false" index="app_source_ser_idx"/>
<property name="method" column="method" type="string" length="255" not-null="false" index="app_source_met_idx"/>
</class>
@@ -155,4 +155,30 @@
audit_store.method = :method
</query>
<query name="audit.GetAuditTrailForNode">
select
audit_fact
from
org.alfresco.repo.audit.hibernate.AuditFactImpl as audit_fact
where
(audit_fact.storeProtocol = :protocol and
audit_fact.storeId = :store_id and
audit_fact.nodeUUID = :node_id)
or
arg1 like :nodeRef
or
arg2 like :nodeRef
or
arg3 like :nodeRef
or
arg4 like :nodeRef
or
arg5 like :nodeRef
or
returnValue like :nodeRef
order by
audit_fact.date asc
</query>
</hibernate-mapping>

View File

@@ -87,10 +87,10 @@ public class AuditConfigImpl implements AuditConfig, InitializingBean
/**
* Helper method to get the latest audit config
*/
public static AuditConfigImpl getLatestConfig(Session session)
public static AuditConfig getLatestConfig(Session session)
{
Query query = session.getNamedQuery(HibernateAuditDAO.QUERY_LAST_AUDIT_CONFIG);
return (AuditConfigImpl) query.uniqueResult();
return (AuditConfig) query.uniqueResult();
}
@Override

View File

@@ -280,10 +280,10 @@ public class AuditDateImpl implements AuditDate
/**
* Helper method to get the latest audit date
*/
public static AuditDateImpl getLatestDate(Session session)
public static AuditDate getLatestDate(Session session)
{
Query query = session.getNamedQuery(HibernateAuditDAO.QUERY_LAST_AUDIT_DATE);
return (AuditDateImpl) query.uniqueResult();
return (AuditDate) query.uniqueResult();
}
}

View File

@@ -17,6 +17,11 @@
package org.alfresco.repo.audit.hibernate;
import java.util.Date;
import java.util.List;
import org.alfresco.service.cmr.repository.NodeRef;
import org.hibernate.Query;
import org.hibernate.Session;
/**
* An Audit fact Rely on standard equals and hash code as they should all be unique.
@@ -575,4 +580,17 @@ public class AuditFactImpl implements AuditFact
this.userId = userId;
}
/**
* Helper method to get all the audit entries for a node.
*/
@SuppressWarnings("unchecked")
public static List<AuditFact> getAuditTrail(Session session, NodeRef nodeRef)
{
Query query = session.getNamedQuery(HibernateAuditDAO.QUERY_AUDIT_TRAIL);
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_PROTOCOL, nodeRef.getStoreRef().getProtocol());
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_STORE_ID, nodeRef.getStoreRef().getIdentifier());
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_NODE_ID, nodeRef.getId());
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_NODE_REF, "%"+nodeRef.toString()+"%");
return (List<AuditFact>) query.list();
}
}

View File

@@ -0,0 +1,231 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.audit.hibernate;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
public class AuditInfoImpl implements AuditInfo
{
private String auditApplication;
private String auditMethod;
private String auditService;
private String clientAddress;
private Date date;
private boolean fail;
private boolean filtered;
private String hostAddress;
private String keyGUID;
private Map<QName, Serializable> keyPropertiesAfter;
private Map<QName, Serializable> keyPropertiesBefore;
private StoreRef keyStore;
private String message;
private Serializable[] methodArguments;
private String[] methodArgumentsAsStrings;
private String path;
private Serializable returnObject;
private String returnObjectAsString;
private String sessionId;
private Throwable throwable;
private String throwableAsString;
private String txId;
private String userIdentifier;
public AuditInfoImpl(AuditFact auditFact)
{
super();
this.auditApplication = auditFact.getAuditSource().getApplication();
this.auditMethod = auditFact.getAuditSource().getMethod();
this.auditService = auditFact.getAuditSource().getService();
this.clientAddress = auditFact.getClientInetAddress();
this.date = auditFact.getDate();
this.fail = auditFact.isFail();
this.filtered = auditFact.isFiltered();
this.hostAddress= auditFact.getHostInetAddress();
this.keyGUID = auditFact.getNodeUUID();
this.keyPropertiesAfter = null;
this.keyPropertiesBefore = null;
if((auditFact.getStoreProtocol() != null) && (auditFact.getStoreId() != null))
{
this.keyStore = new StoreRef(auditFact.getStoreProtocol(), auditFact.getStoreId());
}
else
{
this.keyStore = null;
}
this.message = auditFact.getMessage();
this.methodArguments = null;
this.methodArgumentsAsStrings = new String[5];
this.methodArgumentsAsStrings[0] = auditFact.getArg1();
this.methodArgumentsAsStrings[1] = auditFact.getArg2();
this.methodArgumentsAsStrings[2] = auditFact.getArg3();
this.methodArgumentsAsStrings[3] = auditFact.getArg4();
this.methodArgumentsAsStrings[4] = auditFact.getArg5();
this.path = auditFact.getPath();
this.returnObject = null;
this.returnObjectAsString = auditFact.getReturnValue();
this.sessionId = auditFact.getSessionId();
this.throwable = null;
this.throwableAsString = auditFact.getException();
this.txId = auditFact.getTransactionId();
this.userIdentifier = auditFact.getUserId();
}
public String getAuditApplication()
{
return auditApplication;
}
public String getAuditMethod()
{
return auditMethod;
}
public String getAuditService()
{
return auditService;
}
public String getClientAddress()
{
return clientAddress;
}
public Date getDate()
{
return date;
}
public boolean isFail()
{
return fail;
}
public boolean isFiltered()
{
return filtered;
}
public String getHostAddress()
{
return hostAddress;
}
public String getKeyGUID()
{
return keyGUID;
}
public Map<QName, Serializable> getKeyPropertiesAfter()
{
return keyPropertiesAfter;
}
public Map<QName, Serializable> getKeyPropertiesBefore()
{
return keyPropertiesBefore;
}
public StoreRef getKeyStore()
{
return keyStore;
}
public String getMessage()
{
return message;
}
public Serializable[] getMethodArguments()
{
return methodArguments;
}
public String[] getMethodArgumentsAsStrings()
{
return methodArgumentsAsStrings;
}
public String getPath()
{
return path;
}
public Serializable getReturnObject()
{
return returnObject;
}
public String getReturnObjectAsString()
{
return returnObjectAsString;
}
public String getSessionId()
{
return sessionId;
}
public Throwable getThrowable()
{
return throwable;
}
public String getThrowableAsString()
{
return throwableAsString;
}
public String getTxId()
{
return txId;
}
public String getUserIdentifier()
{
return userIdentifier;
}
}

View File

@@ -26,4 +26,10 @@ public interface AuditSource
public String getService();
void setApplication(String auditApplication);
void setService(String auditService);
void setMethod(String auditMethod);
}

View File

@@ -87,21 +87,21 @@ public class AuditSourceImpl implements AuditSource
this.service = service;
}
public static AuditSourceImpl getApplicationSource(Session session, String application)
public static AuditSource getApplicationSource(Session session, String application)
{
Query query = session.getNamedQuery(HibernateAuditDAO.QUERY_AUDIT_APP_SOURCE);
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_APP_SOURCE_APP, application);
return (AuditSourceImpl) query.uniqueResult();
return (AuditSource) query.uniqueResult();
}
public static AuditSourceImpl getApplicationSource(Session session, String application, String service,
public static AuditSource getApplicationSource(Session session, String application, String service,
String method)
{
Query query = session.getNamedQuery(HibernateAuditDAO.QUERY_AUDIT_METHOD_SOURCE);
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_APP_SOURCE_APP, application);
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_APP_SOURCE_SER, service);
query.setParameter(HibernateAuditDAO.QUERY_AUDIT_APP_SOURCE_MET, method);
return (AuditSourceImpl) query.uniqueResult();
return (AuditSource) query.uniqueResult();
}
@Override

View File

@@ -20,21 +20,28 @@ import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.audit.AuditConfiguration;
import org.alfresco.repo.audit.AuditDAO;
import org.alfresco.repo.audit.AuditInfo;
import org.alfresco.repo.audit.AuditException;
import org.alfresco.repo.audit.AuditState;
import org.alfresco.repo.content.AbstractContentStore;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.TransactionalDao;
import org.alfresco.service.cmr.audit.AuditInfo;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.datatype.Duration;
import org.alfresco.util.EqualsHelper;
import org.alfresco.util.GUID;
@@ -63,6 +70,16 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
public static final String QUERY_AUDIT_APP_SOURCE_MET = "method";
public static final String QUERY_AUDIT_TRAIL = "audit.GetAuditTrailForNode";
public static final String QUERY_AUDIT_PROTOCOL = "protocol";
public static final String QUERY_AUDIT_STORE_ID = "store_id";
public static final String QUERY_AUDIT_NODE_ID = "node_id";
public static final String QUERY_AUDIT_NODE_REF = "nodeRef";
/** a uuid identifying this unique instance */
private String uuid;
@@ -92,16 +109,40 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
this.contentStore = contentStore;
}
public void audit(AuditInfo auditInfo)
public void audit(AuditState auditInfo)
{
if(auditInfo.getUserIdentifier() == null)
{
auditInfo.setUserIdentifier(AuthenticationUtil.getSystemUserName());
}
if(AuthenticationUtil.getCurrentUserName() == null)
{
AuthenticationUtil.setSystemUserAsCurrentUser();
try
{
audit0(auditInfo);
}
finally
{
AuthenticationUtil.clearCurrentSecurityContext();
}
}
else
{
audit0(auditInfo);
}
}
private void audit0(AuditState auditInfo)
{
// Find/Build the configuraton entry
AuditConfigImpl auditConfig = getAuditConfig(auditInfo);
AuditConfig auditConfig = getAuditConfig(auditInfo);
// Find/Build any dates
AuditDateImpl auditDate = getAuditDate(auditInfo);
AuditDate auditDate = getAuditDate(auditInfo);
// Find/Build the source
AuditSourceImpl auditSource = getAuditSource(auditInfo);
AuditSource auditSource = getAuditSource(auditInfo);
// Build the new audit fact information
AuditFactImpl auditFact = new AuditFactImpl();
@@ -170,9 +211,9 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
}
}
private AuditSourceImpl getAuditSource(AuditInfo auditInfo)
private AuditSource getAuditSource(AuditState auditInfo)
{
AuditSourceImpl auditSourceImpl;
AuditSource auditSourceImpl;
SourceKey sourceKey = new SourceKey(auditInfo.getAuditApplication(), auditInfo.getAuditService(), auditInfo.getAuditMethod());
if(sourceIds.get() == null)
@@ -182,7 +223,7 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
Long id = sourceIds.get().get(sourceKey);
if(id != null)
{
auditSourceImpl = (AuditSourceImpl) getSession().get(AuditSourceImpl.class, id.longValue());
auditSourceImpl = (AuditSource) getSession().get(AuditSourceImpl.class, id.longValue());
if(auditSourceImpl != null)
{
return auditSourceImpl;
@@ -218,7 +259,7 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
return auditSourceImpl;
}
private AuditDateImpl getAuditDate(AuditInfo auditInfo)
private AuditDate getAuditDate(AuditState auditInfo)
{
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(auditInfo.getDate());
@@ -228,7 +269,7 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
cal.set(Calendar.HOUR_OF_DAY, 0);
Date required = cal.getTime();
AuditDateImpl auditDate;
AuditDate auditDate;
if (auditDateImplId.get() == null)
{
auditDate = AuditDateImpl.getLatestDate(getSession());
@@ -242,7 +283,7 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
}
else
{
auditDate = (AuditDateImpl) getSession().get(AuditDateImpl.class, auditDateImplId.get().longValue());
auditDate = (AuditDate) getSession().get(AuditDateImpl.class, auditDateImplId.get().longValue());
if ((auditDate == null) || (!required.equals(auditDate.getDate())))
{
auditDate = AuditDateImpl.getLatestDate(getSession());
@@ -265,9 +306,9 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
return auditDate;
}
private AuditConfigImpl getAuditConfig(AuditInfo auditInfo)
private AuditConfig getAuditConfig(AuditState auditInfo)
{
AuditConfigImpl auditConfig;
AuditConfig auditConfig;
if ((auditConfiguration.get() == null) || (auditConfiguration.get() != auditInfo.getAuditConfiguration()))
{
auditConfig = AuditConfigImpl.getLatestConfig(getSession());
@@ -313,7 +354,7 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
}
else
{
auditConfig = (AuditConfigImpl) getSession()
auditConfig = (AuditConfig) getSession()
.get(AuditConfigImpl.class, auditConfigImplId.get().longValue());
if (auditConfig == null)
{
@@ -323,7 +364,7 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
return auditConfig;
}
private AuditConfigImpl createNewAuditConfigImpl(AuditInfo auditInfo)
private AuditConfigImpl createNewAuditConfigImpl(AuditState auditInfo)
{
AuditConfigImpl auditConfig = new AuditConfigImpl();
InputStream is = new BufferedInputStream(auditInfo.getAuditConfiguration().getInputStream());
@@ -436,4 +477,23 @@ public class HibernateAuditDAO extends HibernateDaoSupport implements AuditDAO,
return hash;
}
}
public List<AuditInfo> getAuditTrail(NodeRef nodeRef)
{
if(nodeRef == null)
{
return Collections.<AuditInfo>emptyList();
}
List<? extends AuditFact> internalTrail = AuditFactImpl.getAuditTrail(getSession(), nodeRef);
ArrayList<AuditInfo> answer = new ArrayList<AuditInfo>(internalTrail.size());
for(AuditFact auditFact : internalTrail)
{
AuditInfo info = new AuditInfoImpl(auditFact);
answer.add(info);
}
return answer;
}
}

View File

@@ -117,7 +117,7 @@ public abstract class AbstractAuditEntry
return auditMode;
}
/* package */TrueFalseUnset getEnabled()
public TrueFalseUnset getEnabled()
{
return enabled;
}