View Javadoc
1   package org.argeo.cms.e4.users.providers;
2   
3   import static org.argeo.eclipse.ui.EclipseUiUtils.notEmpty;
4   
5   import org.argeo.api.NodeConstants;
6   import org.argeo.cms.auth.UserAdminUtils;
7   import org.argeo.naming.LdapAttrs;
8   import org.eclipse.jface.viewers.Viewer;
9   import org.eclipse.jface.viewers.ViewerFilter;
10  import org.osgi.service.useradmin.User;
11  
12  /**
13   * Filter user list using JFace mechanism on the client (yet on the server) side
14   * rather than having the UserAdmin to process the search
15   */
16  public class UserFilter extends ViewerFilter {
17  	private static final long serialVersionUID = 5082509381672880568L;
18  
19  	private String searchString;
20  	private boolean showSystemRole = true;
21  
22  	private final String[] knownProps = { LdapAttrs.DN, LdapAttrs.cn.name(), LdapAttrs.givenName.name(),
23  			LdapAttrs.sn.name(), LdapAttrs.uid.name(), LdapAttrs.description.name(), LdapAttrs.mail.name() };
24  
25  	public void setSearchText(String s) {
26  		// ensure that the value can be used for matching
27  		if (notEmpty(s))
28  			searchString = ".*" + s.toLowerCase() + ".*";
29  		else
30  			searchString = ".*";
31  	}
32  
33  	public void setShowSystemRole(boolean showSystemRole) {
34  		this.showSystemRole = showSystemRole;
35  	}
36  
37  	@Override
38  	public boolean select(Viewer viewer, Object parentElement, Object element) {
39  		User user = (User) element;
40  		if (!showSystemRole && user.getName().matches(".*(" + NodeConstants.ROLES_BASEDN + ")"))
41  			// UserAdminUtils.getProperty(user, LdifName.dn.name())
42  			// .toLowerCase().endsWith(AuthConstants.ROLES_BASEDN))
43  			return false;
44  
45  		if (searchString == null || searchString.length() == 0)
46  			return true;
47  
48  		if (user.getName().matches(searchString))
49  			return true;
50  
51  		for (String key : knownProps) {
52  			String currVal = UserAdminUtils.getProperty(user, key);
53  			if (notEmpty(currVal) && currVal.toLowerCase().matches(searchString))
54  				return true;
55  		}
56  		return false;
57  	}
58  }