05-30-2012 12:16 PM
05-30-2012 07:23 PM
05-31-2012 05:20 AM
I don't think you'll be able to do this without authenticating as the user.
An exception would be if you called a custom web service (Java that is deployed to the Alfresco WAR) that used "runAs" to run as the user specified (specifically, org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork). That would do it. But letting arbitrary web service *clients* set the modified by property would be a security problem.
The ownable aspect has a property called cmwner, which is used to set the owner of a document, but that has nothing to do with the "modified by" and "created by" properties.
Jeff
05-31-2012 11:42 AM
package com.someco.scripts;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.apache.log4j.Logger;
import com.someco.model.SomeCoModel;
import com.someco.service.RatingService;
/**
* This is the controller for the rating.post web script.
*
* @author jpotts
*
*/
public class PostRating extends org.springframework.extensions.webscripts.DeclarativeWebScript {
Logger logger = Logger.getLogger(PostRating.class);
private NodeService nodeService;
private RatingService ratingService;
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
int ratingValue = -1;
String id = req.getParameter("id");
String rating = req.getParameter("rating");
String user = req.getParameter("user");
try {
ratingValue = Integer.parseInt(rating);
} catch (NumberFormatException nfe) {
}
if (id == null || rating == null || user == null) {
logger.debug("ID, rating, or user not set");
status.setCode(Status.STATUS_BAD_REQUEST, "Required data has not been provided");
} else if ((ratingValue < 1) || (ratingValue > 5)) {
logger.debug("Rating out of range");
status.setCode(Status.STATUS_BAD_REQUEST, "Rating value must be between 1 and 5 inclusive");
} else {
logger.debug("Getting current node");
NodeRef curNode = new NodeRef("workspace://SpacesStore/" + id);
if (!nodeService.exists(curNode)) {
logger.debug("Node not found");
status.setCode(Status.STATUS_NOT_FOUND, "No node found for id:" + id);
} else {
// Refactored to use the Rating Service
//create(curNode, Integer.parseInt(rating), user);
ratingService.rate(curNode, Integer.parseInt(rating), user);
}
}
logger.debug("Setting model");
Map<String, Object> model = new HashMap<String, Object>();
model.put("node", id);
model.put("rating", rating);
model.put("user", user);
return model;
}
/**
*
* @param nodeRef
* @param rating
* @param user
* @deprecated Use the Rating Service instead
*/
protected void create(final NodeRef nodeRef, final int rating, final String user) {
AuthenticationUtil.runAs(new RunAsWork<String>() {
@SuppressWarnings("synthetic-access")
public String doWork() throws Exception {
// add the aspect to this document if it needs it
if (nodeService.hasAspect(nodeRef, SomeCoModel.ASPECT_SC_RATEABLE)) {
logger.debug("Document already has aspect");
} else {
logger.debug("Adding rateable aspect");
nodeService.addAspect(nodeRef, SomeCoModel.ASPECT_SC_RATEABLE, null);
}
Map<QName, Serializable> props = new HashMap<QName, Serializable>();
props.put(SomeCoModel.PROP_RATING, rating);
props.put(SomeCoModel.PROP_RATER, user);
nodeService.createNode(
nodeRef,
SomeCoModel.ASSN_SC_RATINGS,
QName.createQName(SomeCoModel.NAMESPACE_SOMECO_CONTENT_MODEL, SomeCoModel.PROP_RATING.getLocalName() + new Date().getTime()),
SomeCoModel.TYPE_SC_RATING,
props);
logger.debug("Created node");
return "";
}
},
"admin");
}
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
public void setRatingService(RatingService ratingService) {
this.ratingService = ratingService;
}
}
The create method needs to be able to create new objects even when the person calling this web script might only have read privileges. So the method wraps its work in a "runAs" and tells Alfresco to run the code as "admin". But you could put any user there.05-31-2012 12:46 PM
05-31-2012 05:40 PM
06-01-2012 10:45 AM
06-01-2012 10:55 AM
06-05-2012 05:57 AM
private static String url =
"http://localhost:8080/alfresco/service/fileUpload";
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
//upload file…
06-07-2012 03:35 AM
for each (field in formdata.fields) {
if (field.name == "name") {
model.name = field.value;
}
if (field.name == "file" && field.isFile) {
filename = field.filename;
content = field.content;
mimetype = field.mimetype;
}
}
var results = search.luceneSearch("+PATH:\"app:company_home/*\" +TYPE:\"cm:folder\" +@cm\\:name:\"Someco\"");
var targetFolder = results[0];
var newDoc = targetFolder.createFile(filename);
newDoc.properties.content.write(content);
newDoc.properties.content.mimetype = mimetype;
newDoc.save();
The code iterates over the fields in the posted form and looks for the "name" and "file" fields. When it finds them, it saves them into variables. The search is used to find a folder to store the file in. The folder's "createFile" method is called to create a new document in Alfresco with the same name as the file that was uploaded. The next line writes the content, which stores the file's bytes in the document's "content" property. Then the mimetype is set and the document is saved.Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.