Skip to content

Instantly share code, notes, and snippets.

@flox1an
Last active August 29, 2015 13:57
Show Gist options
  • Save flox1an/9470057 to your computer and use it in GitHub Desktop.
Save flox1an/9470057 to your computer and use it in GitHub Desktop.
Use a repository extension to add custom properties to Alfresco Share Document Library Metdata. This example adds the current users' authorities.

Repository extension to add custom DocLib meta data

Using the repository extension from the files UserInfoCustomResponse.java and fme-user-info-context.xml you can add custom doclib metadata as a json object. This data will be pulled into the browser with Share's DocLib XHR request and can be used to configure or extend the document library.

Usage as DocLib action evaluator

To use the custom data in a Share document library action evaluator you can write an evaluator in as shown in the file FmeGroupEvaluator.java

This can be registered as usual...

<bean id="evaluator.doclib.fme.group" class="de.fme.share.evaluator.FmeGroupEvaluator" />

and used in the action definitions as such, e.g. to restrict the users who can use the copy-to action:

<!-- Copy to -->
<action id="document-copy-to" type="javascript" label="actions.document.copy-to">
    <param name="function">onActionCopyTo</param>
    <evaluator negate="true">evaluator.doclib.action.transferred</evaluator>
    <evaluator negate="true">evaluator.doclib.action.isLocked</evaluator>
    <evaluator negate="true">evaluator.doclib.indicator.IsReadOnly</evaluator>
    <evaluator>evaluator.doclib.fme.group</evaluator>
</action>

Usage in custom Share javascript extensions

This adds a custom JSON property to the Document Library that can be used in your Javascript extensions, e.g. in the toolbar Share javascript:

doclistToolbar.modules.docList.doclistMetadata.custom.fme.authorities

--> ["GROUP_ALFRESCO_ADMINISTRATORS", "GROUP_EVERYONE", "GROUP_site_swsdp", 
 "GROUP_site_swsdp_SiteManager", "ROLE_ADMINISTRATOR"]
<bean id="fmeUserInformation" class="de.fme.repo.UserInfoCustomResponse">
<property name="authorityService" ref="authorityService"/>
</bean>
<bean id="fmeCustomResponses" parent="slingshotDocLibCustomResponse">
<property name="customResponses">
<map>
<entry key="fme">
<ref bean="fmeUserInformation"/>
</entry>
</map>
</property>
</bean>
package de.fme.share.evaluator;
import org.alfresco.web.evaluator.BaseEvaluator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/**
* Tests example evaluator tests if a user is in the FME Group and returns true if he is,
* or false otherwise.
*
* This uses data from the custom response extension in the repository that adds the user's
* authorities to the doclib json response.
*
* @author Florian Maul (fme AG)
*/
public class FmeGroupEvaluator extends BaseEvaluator {
@Override
public boolean evaluate(JSONObject nodeJson) {
JSONArray authorities = ((JSONArray) getJSONValue(getMetadata(), "custom.fme.authorities"));
if (authorities != null) {
return authorities.contains("GROUP_FME");
}
return false;
}
}
package de.fme.repo;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.alfresco.repo.jscript.app.CustomResponse;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.security.AuthorityService;
public class UserInfoCustomResponse implements CustomResponse {
private AuthorityService authorityService;
@Override
public Serializable populate() {
Map<String, Serializable> map = new LinkedHashMap<String, Serializable>(2);
String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
Set<String> authoritiesForUser = authorityService.getAuthoritiesForUser(currentUser);
map.put("authorities", (Serializable) authoritiesForUser.toArray(new String[]{}));
return (Serializable)map;
}
public void setAuthorityService(AuthorityService authorityService) {
this.authorityService = authorityService;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment