Subversion Repositories linid-dm

Compare Revisions

Rev 427 → Rev 428

/trunk/src/main/java/org/linid/dm/wui/t5lib/pages/DnAutocompleteBlock.java New file
0,0 → 1,93
/* *********************************************************
* This file is a part of InterLDAP.
* This software is a computer program whose purpose is
* to manage electronic identity thanks to LDAP directories.
*
* ==LICENSE NOTICE==
* InterLDAP is a free software subjected to the
* ** GNU Affero Public License ** as published by the
* Free Software Foundation, ** version 3 ** of the license.
*
* By application to section 7 in the GNU Affero GPLv3,
* dynamic and static links do not extend license to other
* softwares.
*
* You can redistribute and/or modify since you respect
* the term of the license.
*
* NOTICE : THIS LICENSE IS FREE OF CHARGE AND THE SOFTWARE
* IS DISTRIBUTED WITHOUT ANY WARRANTIES OF ANY KIND
*
* ==LICENSE NOTICE==
*
* (c) 2008-2013 Groupe Linagora - http://linagora.com
********************************************************* */
 
package org.linid.dm.wui.t5lib.pages;
 
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
 
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.services.PropertyEditContext;
import org.linid.dm.wui.model.AttributeEditContext;
import org.linid.dm.wui.model.LdapSelectParameters;
import org.linid.dm.wui.t5lib.components.DnAutocompleter;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
 
/**
* Create an autocomplete textfield from an LDAP request
*
* @author Raph
*/
public class DnAutocompleteBlock {
@Environmental
private PropertyEditContext context;
@InjectService("ApplicationContext")
private ApplicationContext applicationContext;
 
public PropertyEditContext getContext() {
return context;
}
 
@SuppressWarnings("unused")
@Component(parameters = {
"value=prop:context.propertyValue",
"label=prop:context.label",
"clientId=prop:context.propertyId",
"principalDn=prop:principalDn",
"typeConfigurationBean=prop:typeConfigurationBean" })
private DnAutocompleter dnAutocompleter;
public LdapSelectParameters getParameters() {
String bean = ((AttributeEditContext) context).getTypeConfigurationBean();
return (LdapSelectParameters) applicationContext.getBean(bean);
}
// Can get it from environment because context is not available within Ajax requests
public LdapName getPrincipalDn() throws InvalidNameException {
LdapUserDetails tapestryPrincipal = null;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
 
if (authentication != null) {
Object acegiPrincipal = authentication.getPrincipal();
if (acegiPrincipal != null && acegiPrincipal instanceof LdapUserDetails) {
tapestryPrincipal = (LdapUserDetails) acegiPrincipal;
return new LdapName(tapestryPrincipal.getDn());
}
}
return null;
}
public String getTypeConfigurationBean() {
return ((AttributeEditContext) context).getTypeConfigurationBean();
}
 
}
trunk/src/main/java/org/linid/dm/wui/t5lib/pages/DnAutocompleteBlock.java Property changes : Added: svn:mime-type + text/plain
/trunk/src/main/java/org/linid/dm/wui/t5lib/components/DnAutocompleter.java New file
0,0 → 1,169
package org.linid.dm.wui.t5lib.components;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.naming.InvalidNameException;
import javax.naming.directory.SearchControls;
import javax.naming.ldap.LdapName;
 
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Mixins;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Hidden;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.json.JSONObject;
import org.linid.dm.authorization.AuthorizationException;
import org.linid.dm.ldap.SizeLimitExceededExceptionWithPartialResults;
import org.linid.dm.ldap.objects.IELdapEntry;
import org.linid.dm.ldap.repository.LdapEntryRepository;
import org.linid.dm.wui.model.LdapSelectParameters;
import org.linid.dm.wui.t5lib.mixins.DnAutocomplete;
import org.springframework.context.ApplicationContext;
import org.springframework.ldap.NameNotFoundException;
 
public class DnAutocompleter {
@Parameter
private String value;
@SuppressWarnings("unused")
@Parameter
@Property(write=false)
private String label;
@SuppressWarnings("unused")
@Parameter
@Property(write=false)
private String clientId;
@Parameter
private LdapName principalDn;
@Parameter
private String typeConfigurationBean;
@InjectService("ApplicationContext")
private ApplicationContext applicationContext;
 
@Inject
LdapEntryRepository repository;
 
@Component(parameters = { "value=autocompleteDn",
"label=prop:label" })
private Hidden autocompleteDn;
@SuppressWarnings("unused")
@Component(parameters = { "value=autocompleteDescription",
"hiddenDnClientId=prop:autocompleteDnId",
"extra=prop:extra",
"label=prop:label",
"clientId=prop:clientId" })
@Mixins("wui-commons/DnAutocomplete")
private TextField autocompleteDescription;
public String getAutocompleteDnId() {
return autocompleteDn.getClientId();
}
public String getAutocompleteDn() {
return value;
}
 
public void setAutocompleteDn(String dn) {
value = dn;
}
public String getAutocompleteDescription() {
return getLdapAutocompleteDescription();
}
public void setAutocompleteDescription(String autocomplete) {
//Empty
}
 
public JSONObject getExtra() {
return new JSONObject("typeConfigurationBean", typeConfigurationBean);
}
public List<DnAutocomplete.DnValuePair> onProvideCompletionsFromAutocompleteDescription(final String partial, final JSONObject extra) throws InvalidNameException {
String typeConfigurationBean = extra.getString("typeConfigurationBean");
LdapSelectParameters parameters = getParameters(typeConfigurationBean);
String[] searchAttrs = buildAttributes(parameters);
SearchControls scs = new SearchControls(SearchControls.SUBTREE_SCOPE, parameters.getSizeLimit(), 0,
searchAttrs, false, false);
LdapName base = new LdapName(parameters.getSearchBase());
String filter = parameters.getSearchFilter().replace("%s", partial);
List<IELdapEntry> entries;
try {
entries = repository.search(principalDn, base, filter, scs);
} catch (SizeLimitExceededExceptionWithPartialResults e) {
entries = e.getPartialResults();
}
List<DnAutocomplete.DnValuePair> autocompletes = new ArrayList<DnAutocomplete.DnValuePair>(entries.size());
for (int i=0; i<entries.size(); i++) {
IELdapEntry entry = entries.get(i);
String firstAttributeValue = getValueOrDn(entry, parameters.getSearchKeyAttr());
String secondAttributeValue = getValueOrDn(entry, parameters.getSearchValueAttr());
DnAutocomplete.DnValuePair pair = new DnAutocomplete.DnValuePair(firstAttributeValue, secondAttributeValue);
autocompletes.add(i, pair);
}
return autocompletes;
}
 
private String getValueOrDn(IELdapEntry entry, String attributeName) {
if (isDn(attributeName)) {
return entry.getDn().toString();
} else {
return entry.getAttribute(attributeName).getValue().stringValue();
}
}
private LdapSelectParameters getParameters(String typeConfigurationBean) {
return (LdapSelectParameters) applicationContext.getBean(typeConfigurationBean);
}
private LdapSelectParameters getParameters() {
return (LdapSelectParameters) applicationContext.getBean(typeConfigurationBean);
}
private String[] buildAttributes(LdapSelectParameters parameters) {
List<String> attrList = new ArrayList<String>();
attrList.add("objectClass");
if (!isDn(parameters.getSearchKeyAttr())) {
attrList.add(parameters.getSearchKeyAttr());
}
if (!isDn(parameters.getSearchValueAttr())) {
attrList.add(parameters.getSearchValueAttr());
}
return (String[]) attrList.toArray(new String[0]);
}
 
private boolean isDn(String attribute) {
return attribute.equals("dn");
}
 
private String getLdapAutocompleteDescription() {
String autocomplete = getAutocompleteDn();
LdapSelectParameters parameters = getParameters();
if (autocomplete == null || autocomplete.isEmpty()) {
return null;
}
IELdapEntry entry = null;
String description = autocomplete;
try {
LdapName autocompleteDn = new LdapName(autocomplete);
entry = repository.get(principalDn, autocompleteDn, buildAttributes(parameters));
} catch (InvalidNameException e) {
} catch (AuthorizationException e) {
} catch (NameNotFoundException e) {
}
if (entry != null) {
description = getValueOrDn(entry, parameters.getSearchValueAttr());
}
return description;
}
}
trunk/src/main/java/org/linid/dm/wui/t5lib/components/DnAutocompleter.java Property changes : Added: svn:mime-type + text/plain
/trunk/src/main/java/org/linid/dm/wui/t5lib/components/DnDisplayer.java
30,6 → 30,8
private static final int LABEL = 1;
private static final int ATTRIBUTE = 2;
private static final int VISIBILITY = 3;
private static final long NO_DEPTH = -1;
/*
* Parameters
*/
120,8 → 122,10
}
// add default values
switch(param.size()) {
case 0 : canParse = false; return;
//depth equals to DN depth
case 0 : param.add(NO_DEPTH);
//empty description
//$FALL-THROUGH$
case 1 : param.add("");
//no attribute to look for
//$FALL-THROUGH$
155,7 → 159,12
if (authzModule.eval(principalDn, value,
EnumSet.<AuthorizationType>of(AuthorizationType.READ),
Arrays.asList(attrs)).containsAll(Arrays.asList(attrs))) {
Name baseDn = dn.getPrefix(((Long)displayParameter.get(DEPTH)).intValue()+1);
Name baseDn;
if (((Long)displayParameter.get(DEPTH)).equals(NO_DEPTH)) {
baseDn = dn;
} else {
baseDn = dn.getPrefix(((Long)displayParameter.get(DEPTH)).intValue()+1);
}
SearchControls scs = new SearchControls(SearchControls.OBJECT_SCOPE, 0, 0, attrs, true, false);
try {
List<IELdapEntry> results = repository.search(principalDn == null ? null : new LdapName(principalDn), baseDn, "(objectClass=*)", scs);
177,8 → 186,11
if (displayParameter == null) {
return "";
}
return dn.getRdn(((Long)displayParameter.get(DEPTH)).intValue()).getValue().toString();
if (((Long)displayParameter.get(DEPTH)).equals(NO_DEPTH)) {
return dn.getRdn(((Long)displayParameter.get(DEPTH)).intValue()).getValue().toString();
} else {
return dn.getRdn(dn.size() - 1).getValue().toString();
}
}
public String getValue() {
/trunk/src/main/java/org/linid/dm/wui/t5lib/mixins/DnAutocomplete.java New file
0,0 → 1,75
package org.linid.dm.wui.t5lib.mixins;
import java.util.List;
 
import org.apache.tapestry5.Field;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.InitializationPriority;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.got5.tapestry5.jquery.mixins.Autocomplete;
@Import(library = {"DnAutocomplete.js"})
public class DnAutocomplete extends Autocomplete {
@InjectContainer
private Field field;
@Parameter
private String hiddenDnClientId;
@Parameter
private JSONObject extra;
@Inject
private JavaScriptSupport javaScriptSupport;
 
public void afterRender() {
javaScriptSupport.addInitializerCall(InitializationPriority.LATE, "dnAutocomplete",
new JSONObject("field", field.getClientId(), "hiddenDn", hiddenDnClientId, "extra", extra.toString()));
}
@Override
protected JSONArray generateResponseJSON(@SuppressWarnings("rawtypes") List matches) {
JSONArray array = new JSONArray();
for (Object o: matches) {
if (o instanceof JSONObject) {
array.put(o);
} else if (o instanceof DnValuePair) {
array.put(((DnValuePair)o).toJSONObject());
} else {
array.put(o.toString());
}
}
return array;
}
public static class DnValuePair {
public static final String DN_KEY = "dn";
public static final String VALUE_KEY = "value";
private String dn;
private String value;
public DnValuePair(String dn, String value) {
this.dn = dn;
this.value = value;
}
 
public String getDn() {
return dn;
}
 
public String getValue() {
return value;
}
public JSONObject toJSONObject() {
return new JSONObject(DN_KEY, dn, VALUE_KEY, value);
}
}
}
trunk/src/main/java/org/linid/dm/wui/t5lib/mixins/DnAutocomplete.java Property changes : Added: svn:mime-type + text/plain
/trunk/src/main/java/org/linid/dm/wui/model/LdapSelectParameters.java
21,6 → 21,8
private String pageLink;
private int sizeLimit;
public LdapSelectParameters(String searchBase, int searchScope, String searchFilter,
String searchKeyAttr, String searchValueAttr,
List<List<Object>> displayParameters) {
29,7 → 31,19
 
public LdapSelectParameters(String searchBase, int searchScope, String searchFilter,
String searchKeyAttr, String searchValueAttr,
List<List<Object>> displayParameters, int sizeLimit) {
this(searchBase, searchScope, searchFilter, searchKeyAttr, searchValueAttr, displayParameters, false, null, sizeLimit);
}
 
public LdapSelectParameters(String searchBase, int searchScope, String searchFilter,
String searchKeyAttr, String searchValueAttr,
List<List<Object>> displayParameters, boolean displayLink, String pageLink) {
this(searchBase, searchScope, searchFilter, searchKeyAttr, searchValueAttr, displayParameters, displayLink, pageLink, 0);
}
public LdapSelectParameters(String searchBase, int searchScope, String searchFilter,
String searchKeyAttr, String searchValueAttr,
List<List<Object>> displayParameters, boolean displayLink, String pageLink, int sizeLimit) {
this.searchBase = searchBase;
this.searchScope = searchScope;
this.searchFilter = searchFilter;
38,6 → 52,7
this.displayParameters = displayParameters;
this.displayLink = displayLink;
this.pageLink = pageLink;
this.sizeLimit = sizeLimit;
}
 
public String getSearchBase() {
71,4 → 86,8
public String getPageLink() {
return pageLink;
}
public int getSizeLimit() {
return sizeLimit;
}
}
/trunk/src/main/resources/org/linid/dm/wui/t5lib/pages/DnAutocompleteBlock.tml New file
0,0 → 1,5
<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<t:block t:id="dnAutocomplete">
<input t:id="dnAutocompleter"/>
</t:block>
</t:container>
/trunk/src/main/resources/org/linid/dm/wui/t5lib/components/DnAutocompleter.tml New file
0,0 → 1,6
<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
 
<t:hidden t:id="autocompleteDn"/>
<t:textfield t:id="autocompleteDescription"/>
 
</t:container>
/trunk/src/main/resources/org/linid/dm/wui/t5lib/mixins/DnAutocomplete.js New file
0,0 → 1,6
Tapestry.Initializer.dnAutocomplete = function(specs) {
jQuery("#" + specs.field).data("extra", specs.extra);
jQuery("#" + specs.field).on("autocompleteselect", function(event, ui) {
jQuery("#" + specs.hiddenDn).val(ui.item.dn);
} );
}
trunk/src/main/resources/org/linid/dm/wui/t5lib/mixins/DnAutocomplete.js Property changes : Added: svn:mime-type + text/plain