11-23-2020 01:43 AM
Hi Team,
I have requirement to restrict from creation of exe file.
User is creating plain text file from share UI and entering name like test.exe.
I have created rule that checks if file name ends with exe, it throws exception. But rule is not executing somehow.
Then i have created behaviour that executes on node creation, I am getting parentref using below code.
public void init() {
policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeCreateNodePolicy.QNAME,
ContentModel.TYPE_CONTENT,
new JavaBehaviour(this, "beforeCreateNode", Behaviour.NotificationFrequency.EVERY_EVENT));
}
@Override
public void beforeCreateNode(NodeRef parentRef, QName assocTypeQName, QName assocQName, QName nodeTypeQName) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Started executing FileCreationRestrictionBehaviour ..");
LOGGER.debug("parentref --> "+parentRef);
LOGGER.debug("parentref NAME --> "+nodeService.getProperty(parentRef, ContentModel.PROP_NAME));
System.out.println("parentref --> "+parentRef +
"parentref NAME --> "+nodeService.getProperty(parentRef, ContentModel.PROP_NAME));
}
}So,my requirement is to restrict file creation, if users enters .exe in file name, how can i get the noderef/filename of that file, so, i can throw exception.
Thanks,
Hardik
11-23-2020 03:51 PM
Try : org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy
You can do something like this:
public void onCreateNode(final ChildAssociationRef childAssocRef) {
final NodeRef nodeRef = childAssocRef.getChildRef();//node which is being created.
if (nodeService.exists(nodeRef)) {
final String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
final String mimetype = mimetypeService.guessMimetype(fileName); //find mimetype by extension of the file.
if (restrictedMimeList.contains(mimetype)) {// check whether the result is one of the restricted mime types from the list?
throw new AlfrescoRuntimeException("Mimetype " + mimetype + " is unsupported");
}
}
}
11-23-2020 03:51 PM
Try : org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy
You can do something like this:
public void onCreateNode(final ChildAssociationRef childAssocRef) {
final NodeRef nodeRef = childAssocRef.getChildRef();//node which is being created.
if (nodeService.exists(nodeRef)) {
final String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
final String mimetype = mimetypeService.guessMimetype(fileName); //find mimetype by extension of the file.
if (restrictedMimeList.contains(mimetype)) {// check whether the result is one of the restricted mime types from the list?
throw new AlfrescoRuntimeException("Mimetype " + mimetype + " is unsupported");
}
}
}
11-25-2020 10:41 AM
@hardik_thakkar you can also try this add-on which covers several other scenarios while blocking blacklisted mimetypes.
https://github.com/abhinavmishra14/alfresco-mimetype-blocker/releases
originally forked from https://github.com/keensoft/alfresco-mimetype-blocker
11-27-2020 06:32 AM
I have configured that add-on and that works perfectly during file upload part.
Written behaviour for file creation part as per your suggestion and that's working fine.
Thanks,
Hardik
11-27-2020 09:55 AM
Good to hear that everything works fine for you. Good luck @hardik_thakkar
Explore our Alfresco products with the links below. Use labels to filter content by product module.