use of org.alfresco.service.cmr.audit.AuditQueryParameters in project records-management by Alfresco.
the class RecordsManagementAuditServiceImpl method getAuditTrailImpl.
/**
* Get the audit trail, optionally dumping the results the the given writer dumping to a list.
*
* @param params the search parameters
* @param results the list to which individual results will be dumped
* @param writer Writer to write the audit trail
* @param reportFormat Format to write the audit trail in, ignored if writer is <code>null</code>
*/
protected void getAuditTrailImpl(final RecordsManagementAuditQueryParameters params, final List<RecordsManagementAuditEntry> results, final Writer writer, final ReportFormat reportFormat) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Retrieving audit trail in '" + reportFormat + "' format using parameters: " + params);
}
// define the callback
AuditQueryCallback callback = new AuditQueryCallback() {
private boolean firstEntry = true;
@Override
public boolean valuesRequired() {
return true;
}
/**
* Just log the error, but continue
*/
@Override
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
logger.warn(errorMsg, error);
return true;
}
@Override
@SuppressWarnings("unchecked")
public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values) {
// Check for context shutdown
if (shutdown) {
return false;
}
Date timestamp = new Date(time);
String eventName = null;
String fullName = null;
String userRoles = null;
NodeRef nodeRef = null;
String nodeName = null;
String nodeType = null;
String nodeIdentifier = null;
String namePath = null;
Map<QName, Serializable> beforeProperties = null;
Map<QName, Serializable> afterProperties = null;
if (values.containsKey(RM_AUDIT_DATA_EVENT_NAME)) {
// This data is /RM/event/...
eventName = (String) values.get(RM_AUDIT_DATA_EVENT_NAME);
fullName = (String) values.get(RM_AUDIT_DATA_PERSON_FULLNAME);
userRoles = (String) values.get(RM_AUDIT_DATA_PERSON_ROLES);
nodeRef = (NodeRef) values.get(RM_AUDIT_DATA_NODE_NODEREF);
nodeName = (String) values.get(RM_AUDIT_DATA_NODE_NAME);
QName nodeTypeQname = (QName) values.get(RM_AUDIT_DATA_NODE_TYPE);
nodeIdentifier = (String) values.get(RM_AUDIT_DATA_NODE_IDENTIFIER);
namePath = (String) values.get(RM_AUDIT_DATA_NODE_NAMEPATH);
beforeProperties = (Map<QName, Serializable>) values.get(RM_AUDIT_DATA_NODE_CHANGES_BEFORE);
afterProperties = (Map<QName, Serializable>) values.get(RM_AUDIT_DATA_NODE_CHANGES_AFTER);
// Convert some of the values to recognizable forms
nodeType = null;
if (nodeTypeQname != null) {
TypeDefinition typeDef = dictionaryService.getType(nodeTypeQname);
nodeType = (typeDef != null) ? typeDef.getTitle(dictionaryService) : null;
}
} else if (values.containsKey(DOD5015_AUDIT_DATA_EVENT_NAME)) {
// This data is /RM/event/...
eventName = (String) values.get(DOD5015_AUDIT_DATA_EVENT_NAME);
fullName = (String) values.get(DOD5015_AUDIT_DATA_PERSON_FULLNAME);
userRoles = (String) values.get(DOD5015_AUDIT_DATA_PERSON_ROLES);
nodeRef = (NodeRef) values.get(DOD5015_AUDIT_DATA_NODE_NODEREF);
nodeName = (String) values.get(DOD5015_AUDIT_DATA_NODE_NAME);
QName nodeTypeQname = (QName) values.get(DOD5015_AUDIT_DATA_NODE_TYPE);
nodeIdentifier = (String) values.get(DOD5015_AUDIT_DATA_NODE_IDENTIFIER);
namePath = (String) values.get(DOD5015_AUDIT_DATA_NODE_NAMEPATH);
beforeProperties = (Map<QName, Serializable>) values.get(DOD5015_AUDIT_DATA_NODE_CHANGES_BEFORE);
afterProperties = (Map<QName, Serializable>) values.get(DOD5015_AUDIT_DATA_NODE_CHANGES_AFTER);
// Convert some of the values to recognizable forms
nodeType = null;
if (nodeTypeQname != null) {
TypeDefinition typeDef = dictionaryService.getType(nodeTypeQname);
nodeType = (typeDef != null) ? typeDef.getTitle(dictionaryService) : null;
}
} else if (values.containsKey(RM_AUDIT_DATA_LOGIN_USERNAME)) {
user = (String) values.get(RM_AUDIT_DATA_LOGIN_USERNAME);
if (values.containsKey(RM_AUDIT_DATA_LOGIN_ERROR)) {
eventName = RM_AUDIT_EVENT_LOGIN_FAILURE;
// The user didn't log in
fullName = user;
} else {
eventName = RM_AUDIT_EVENT_LOGIN_SUCCESS;
fullName = (String) values.get(RM_AUDIT_DATA_LOGIN_FULLNAME);
}
} else if (values.containsKey(DOD5015_AUDIT_DATA_LOGIN_USERNAME)) {
user = (String) values.get(DOD5015_AUDIT_DATA_LOGIN_USERNAME);
if (values.containsKey(DOD5015_AUDIT_DATA_LOGIN_ERROR)) {
eventName = RM_AUDIT_EVENT_LOGIN_FAILURE;
// The user didn't log in
fullName = user;
} else {
eventName = RM_AUDIT_EVENT_LOGIN_SUCCESS;
fullName = (String) values.get(DOD5015_AUDIT_DATA_LOGIN_FULLNAME);
}
} else {
// This is not recognisable data
logger.warn("Unable to process audit entry for RM. Unexpected data: \n" + " Entry: " + entryId + "\n" + " Data: " + values);
// Skip it
return true;
}
if (nodeRef != null && nodeService.exists(nodeRef) && !AccessStatus.ALLOWED.equals(capabilityService.getCapabilityAccessState(nodeRef, ACCESS_AUDIT_CAPABILITY))) {
return true;
}
// TODO: Refactor this to use the builder pattern
RecordsManagementAuditEntry entry = new RecordsManagementAuditEntry(timestamp, user, fullName, // A concatenated string of roles
userRoles, nodeRef, nodeName, nodeType, eventName, nodeIdentifier, namePath, beforeProperties, afterProperties);
// write out the entry to the file in requested format
writeEntryToFile(entry);
if (results != null) {
results.add(entry);
}
if (logger.isDebugEnabled()) {
logger.debug(" " + entry);
}
// Keep going
return true;
}
private void writeEntryToFile(RecordsManagementAuditEntry entry) {
if (writer == null) {
return;
}
try {
if (!firstEntry) {
if (reportFormat == ReportFormat.HTML) {
writer.write("\n");
} else {
writer.write(",");
}
} else {
firstEntry = false;
}
// write the entry to the file
if (reportFormat == ReportFormat.JSON) {
writer.write("\n\t\t");
}
writeAuditTrailEntry(writer, entry, reportFormat);
} catch (IOException ioe) {
throw new AlfrescoRuntimeException(MSG_TRAIL_FILE_FAIL, ioe);
}
}
};
String user = params.getUser();
Long fromTime = getFromDateTime(params.getDateFrom());
Long toTime = getToDateTime(params.getDateTo());
NodeRef nodeRef = params.getNodeRef();
int maxEntries = params.getMaxEntries();
// Reverse order if the results are limited
boolean forward = maxEntries > 0 ? false : true;
// start the audit trail report
writeAuditTrailHeader(writer, params, reportFormat);
if (logger.isDebugEnabled()) {
logger.debug("RM Audit: Issuing query: " + params);
}
// Build audit query parameters
AuditQueryParameters dod5015AuditQueryParams = new AuditQueryParameters();
dod5015AuditQueryParams.setForward(forward);
dod5015AuditQueryParams.setApplicationName(DOD5015_AUDIT_APPLICATION_NAME);
dod5015AuditQueryParams.setUser(user);
dod5015AuditQueryParams.setFromTime(fromTime);
dod5015AuditQueryParams.setToTime(toTime);
if (nodeRef != null) {
dod5015AuditQueryParams.addSearchKey(DOD5015_AUDIT_DATA_NODE_NODEREF, nodeRef);
}
//
AuditQueryParameters auditQueryParams = new AuditQueryParameters();
auditQueryParams.setForward(forward);
auditQueryParams.setApplicationName(RM_AUDIT_APPLICATION_NAME);
auditQueryParams.setUser(user);
auditQueryParams.setFromTime(fromTime);
auditQueryParams.setToTime(toTime);
if (nodeRef != null) {
auditQueryParams.addSearchKey(RM_AUDIT_DATA_NODE_NODEREF, nodeRef);
} else if (params.getEvent() != null) {
auditQueryParams.addSearchKey(RM_AUDIT_DATA_EVENT_NAME, params.getEvent());
}
// Get audit entries
SiteInfo siteInfo = siteService.getSite(DEFAULT_SITE_NAME);
if (siteInfo != null) {
QName siteType = nodeService.getType(siteInfo.getNodeRef());
if (siteType.equals(TYPE_DOD_5015_SITE)) {
auditService.auditQuery(callback, dod5015AuditQueryParams, maxEntries);
}
}
// We always need to make the standard query - regardless of the type of RM site (to get events like RM site created).
auditService.auditQuery(callback, auditQueryParams, maxEntries);
// finish off the audit trail report
writeAuditTrailFooter(writer, reportFormat);
// audit that the audit has been view'ed
if (nodeRef == null) {
// grab the default file plan, but don't fail if it can't be found!
nodeRef = filePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID);
}
auditEvent(nodeRef, AUDIT_EVENT_VIEW, null, null, true);
}
use of org.alfresco.service.cmr.audit.AuditQueryParameters in project alfresco-remote-api by Alfresco.
the class GetMethodRegressionTest method getLastAuditRecordTime.
private Long getLastAuditRecordTime() {
final Holder<Long> lastTime = new Holder<Long>();
AuditQueryParameters parameters = new AuditQueryParameters();
parameters.setForward(false);
auditService.auditQuery(new AuditQueryCallback() {
@Override
public boolean valuesRequired() {
return false;
}
@Override
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
return false;
}
@Override
public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values) {
lastTime.setValue(time);
return false;
}
}, parameters, 1);
return lastTime.getValue();
}
use of org.alfresco.service.cmr.audit.AuditQueryParameters in project alfresco-remote-api by Alfresco.
the class AuditImpl method getQueryResultAuditEntries.
/**
* @param auditAppId
* @param propertyWalker
* @param includeParams
* @param maxItem
* @param forward
* @return
*/
public List<AuditEntry> getQueryResultAuditEntries(AuditService.AuditApplication auditApplication, AuditEntryQueryWalker propertyWalker, List<String> includeParam, int maxItem, Boolean forward) {
final List<AuditEntry> results = new ArrayList<>();
final String auditAppId = auditApplication.getKey().substring(1);
String auditApplicationName = auditApplication.getName();
// Execute the query
AuditQueryParameters params = new AuditQueryParameters();
// used to orderBY by field createdAt
params.setForward(forward);
params.setApplicationName(auditApplicationName);
params.setUser(propertyWalker.getCreatedByUser());
Long fromId = propertyWalker.getFromId();
Long toId = propertyWalker.getToId();
validateWhereBetween(auditAppId, fromId, toId);
Long fromTime = propertyWalker.getFromTime();
Long toTime = propertyWalker.getToTime();
validateWhereBetween(auditAppId, fromTime, toTime);
params.setFromTime(fromTime);
params.setToTime(toTime);
params.setFromId(fromId);
params.setToId(toId);
if (propertyWalker.getValuesKey() != null && propertyWalker.getValuesValue() != null) {
params.addSearchKey(propertyWalker.getValuesKey(), propertyWalker.getValuesValue());
}
final Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
// create the callback for auditQuery method
final AuditQueryCallback callback = new AuditQueryCallback() {
public boolean valuesRequired() {
return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
}
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
throw new AlfrescoRuntimeException("Failed to retrieve audit data.", error);
}
public boolean handleAuditEntry(Long entryId, String applicationName, String userName, long time, Map<String, Serializable> values) {
UserInfo userInfo = Node.lookupUserInfo(userName, mapUserInfo, personService);
AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
results.add(auditEntry);
return true;
}
};
auditService.auditQuery(callback, params, maxItem);
return results;
}
use of org.alfresco.service.cmr.audit.AuditQueryParameters in project alfresco-remote-api by Alfresco.
the class AuditImpl method getQueryResultAuditEntriesByNodeRef.
private List<AuditEntry> getQueryResultAuditEntriesByNodeRef(NodeRef nodeRef, AuditEntriesByNodeIdQueryWalker propertyWalker, List<String> includeParam, boolean forward, int limit) {
final List<AuditEntry> results = new ArrayList<>();
String auditAppId = "alfresco-access";
String auditApplicationName = AuthenticationUtil.runAs(new RunAsWork<String>() {
public String doWork() throws Exception {
return findAuditAppByIdOr404(auditAppId).getName();
}
}, AuthenticationUtil.getSystemUserName());
// create the callback for auditQuery method
final AuditQueryCallback callback = new AuditQueryCallback() {
public boolean valuesRequired() {
return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
}
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
throw new AlfrescoRuntimeException("Failed to retrieve audit data.", error);
}
public boolean handleAuditEntry(Long entryId, String applicationName, String userName, long time, Map<String, Serializable> values) {
UserInfo userInfo = Node.lookupUserInfo(userName, new HashMap<>(0), personService);
AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
results.add(auditEntry);
return true;
}
};
// resolve the path of the node - note: this will also check read permission for current user
final String nodePath = ISO9075.decode(nodeService.getPath(nodeRef).toPrefixString(namespaceService));
Long fromTime = propertyWalker.getFromTime();
Long toTime = propertyWalker.getToTime();
validateWhereBetween(nodeRef.getId(), fromTime, toTime);
AuthenticationUtil.runAs(new RunAsWork<Object>() {
public Object doWork() throws Exception {
// QueryParameters
AuditQueryParameters pathParams = new AuditQueryParameters();
// used to orderBY by field createdAt
pathParams.setForward(forward);
pathParams.setUser(propertyWalker.getCreatedByUser());
pathParams.setFromTime(fromTime);
pathParams.setToTime(toTime);
pathParams.setApplicationName(auditApplicationName);
pathParams.addSearchKey("/" + auditAppId + "/transaction/path", nodePath);
auditService.auditQuery(callback, pathParams, limit);
AuditQueryParameters copyFromPathParams = new AuditQueryParameters();
// used to orderBY by field createdAt
copyFromPathParams.setForward(forward);
copyFromPathParams.setUser(propertyWalker.getCreatedByUser());
copyFromPathParams.setFromTime(fromTime);
copyFromPathParams.setToTime(toTime);
copyFromPathParams.setApplicationName(auditApplicationName);
copyFromPathParams.addSearchKey("/" + auditAppId + "/transaction/copy/from/path", nodePath);
auditService.auditQuery(callback, copyFromPathParams, limit);
AuditQueryParameters moveFromPathParams = new AuditQueryParameters();
// used to orderBY by field createdAt
moveFromPathParams.setForward(forward);
moveFromPathParams.setUser(propertyWalker.getCreatedByUser());
moveFromPathParams.setFromTime(fromTime);
moveFromPathParams.setToTime(toTime);
moveFromPathParams.setApplicationName(auditApplicationName);
moveFromPathParams.addSearchKey("/" + auditAppId + "/transaction/move/from/path", nodePath);
auditService.auditQuery(callback, moveFromPathParams, limit);
return null;
}
}, AuthenticationUtil.getSystemUserName());
return results;
}
use of org.alfresco.service.cmr.audit.AuditQueryParameters in project alfresco-remote-api by Alfresco.
the class AuditQueryGet method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
final Map<String, Object> model = new HashMap<String, Object>(7);
String appName = getParamAppName(req);
String path = getParamPath(req);
Serializable value = getParamValue(req);
String valueType = getParamValueType(req);
Long fromTime = getParamFromTime(req);
Long toTime = getParamToTime(req);
Long fromId = getParamFromId(req);
Long toId = getParamToId(req);
String user = getParamUser(req);
boolean forward = getParamForward(req);
int limit = getParamLimit(req);
final boolean verbose = getParamVerbose(req);
if (appName == null) {
Map<String, AuditApplication> appsByName = auditService.getAuditApplications();
AuditApplication app = appsByName.get(appName);
if (app == null) {
throw new WebScriptException(Status.STATUS_NOT_FOUND, "audit.err.app.notFound", appName);
}
}
// Transform the value to the correct type
if (value != null && valueType != null) {
try {
@SuppressWarnings("unchecked") Class<? extends Serializable> clazz = (Class<? extends Serializable>) Class.forName(valueType);
value = DefaultTypeConverter.INSTANCE.convert(clazz, value);
} catch (ClassNotFoundException e) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.classNotFound", valueType);
} catch (Throwable e) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.convertFailed", value, valueType);
}
}
// Execute the query
AuditQueryParameters params = new AuditQueryParameters();
params.setApplicationName(appName);
params.setFromTime(fromTime);
params.setToTime(toTime);
params.setFromId(fromId);
params.setToId(toId);
params.setUser(user);
params.setForward(forward);
if (path != null || value != null) {
params.addSearchKey(path, value);
}
final List<Map<String, Object>> entries = new ArrayList<Map<String, Object>>(limit);
AuditQueryCallback callback = new AuditQueryCallback() {
@Override
public boolean valuesRequired() {
return verbose;
}
@Override
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
return true;
}
@Override
public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values) {
Map<String, Object> entry = new HashMap<String, Object>(11);
entry.put(JSON_KEY_ENTRY_ID, entryId);
entry.put(JSON_KEY_ENTRY_APPLICATION, applicationName);
if (user != null) {
entry.put(JSON_KEY_ENTRY_USER, user);
}
entry.put(JSON_KEY_ENTRY_TIME, new Date(time));
if (values != null) {
// Convert values to Strings
Map<String, String> valueStrings = new HashMap<String, String>(values.size() * 2);
for (Map.Entry<String, Serializable> mapEntry : values.entrySet()) {
String key = mapEntry.getKey();
Serializable value = mapEntry.getValue();
try {
String valueString = DefaultTypeConverter.INSTANCE.convert(String.class, value);
valueStrings.put(key, valueString);
} catch (ClassCastException e) {
// a ClassCastException.
if (!(value instanceof MLText)) {
// Rethrow if the exception was not caused by the expected MLText conversion.
throw e;
}
valueStrings.put(key, value.toString());
} catch (TypeConversionException e) {
// Use the toString()
valueStrings.put(key, value.toString());
}
}
entry.put(JSON_KEY_ENTRY_VALUES, valueStrings);
}
entries.add(entry);
return true;
}
};
auditService.auditQuery(callback, params, limit);
model.put(JSON_KEY_ENTRY_COUNT, entries.size());
model.put(JSON_KEY_ENTRIES, entries);
// Done
if (logger.isDebugEnabled()) {
logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
}
return model;
}
Aggregations