/
QueryNewUserGroupBean.java
110 lines (93 loc) · 3.89 KB
/
QueryNewUserGroupBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* $Id$
*
* Copyright 2010-2014 Glencoe Software, Inc. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.security.auth;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import ome.conditions.ValidationException;
import ome.security.SecuritySystem;
import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.HardcodedFilter;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Handles the ":query:" specifier from etc/omero.properties.
*
* The string following ":query:" is interpreted as an LDAP query to be run in
* combination with the "omero.ldap.group_filter" value. Properties of the form
* "${}" will be replaced with found user properties.
*
* @author Josh Moore, josh at glencoesoftware.com
* @see SecuritySystem
* @since Beta4.2
*/
public class QueryNewUserGroupBean implements NewUserGroupBean, NewUserGroupOwnerBean {
private final static Logger log = LoggerFactory.getLogger(QueryNewUserGroupBean.class);
private final String grpQuery;
public QueryNewUserGroupBean(String grpQuery) {
this.grpQuery = grpQuery;
}
private String parseQuery(final AttributeSet attrSet, final String query) {
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("@{",
"}", null, false);
return helper.replacePlaceholders(query,
new PlaceholderResolver() {
public String resolvePlaceholder(String arg0) {
if (attrSet.size(arg0) > 1) {
throw new ValidationException(
"Multivalued property used in @{} format:"
+ query + "="
+ attrSet.getAll(arg0).toString());
}
return attrSet.getFirst(arg0);
}
});
}
@SuppressWarnings("unchecked")
private List<Long> _groups(boolean owner, String username, LdapConfig config,
LdapOperations ldap, RoleProvider provider, final AttributeSet attrSet) {
String query = parseQuery(attrSet, grpQuery);
String ownerQuery = null;
if (owner) {
ownerQuery = config.getNewUserGroupOwner();
if (StringUtils.isBlank(ownerQuery)) {
log.debug("Owner query disabled");
return Collections.emptyList(); // EARLY EXIT
}
ownerQuery = parseQuery(attrSet, ownerQuery);
}
AndFilter and = new AndFilter();
and.and(config.getGroupFilter());
and.and(new HardcodedFilter(query));
if (owner) {
and.and(new HardcodedFilter(ownerQuery));
}
log.debug("Running query: {}", and.encode());
List<String> groupNames = (List<String>) ldap.search("", and.encode(),
new GroupAttributeMapper(config));
List<Long> groups = new ArrayList<Long>(groupNames.size());
for (String groupName : groupNames) {
groups.add(provider.createGroup(groupName, null, false, true));
}
return groups;
}
@Override
public List<Long> groups(String username, LdapConfig config,
LdapOperations ldap, RoleProvider provider,
AttributeSet attrSet) {
return _groups(false, username, config, ldap, provider, attrSet);
}
@Override
public List<Long> ownerOfGroups(String username, LdapConfig config,
LdapOperations ldap, RoleProvider provider, AttributeSet attrSet) {
return _groups(true, username, config, ldap, provider, attrSet);
}
}