001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.usermanager;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.regex.Pattern;
027
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XNodeMap;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.ecm.platform.usermanager.UserManager.MatchType;
033
034/**
035 * APG-240 All attributes are defined public because the user manager service do not get access to the fields. OSGI
036 * don't allow splitted packages having access to public members defined from an another package provider.
037 *
038 * @author matic
039 */
040@XObject(value = "userManager")
041public class UserManagerDescriptor {
042
043    @XNode("@class")
044    public Class<?> userManagerClass;
045
046    @XNode("defaultGroup")
047    public String defaultGroup;
048
049    @XNodeList(value = "defaultAdministratorId", type = ArrayList.class, componentType = String.class)
050    public List<String> defaultAdministratorIds;
051
052    @XNodeList(value = "administratorsGroup", type = ArrayList.class, componentType = String.class)
053    public List<String> administratorsGroups;
054
055    @XNode("disableDefaultAdministratorsGroup")
056    public Boolean disableDefaultAdministratorsGroup;
057
058    @XNode("userSortField")
059    public String userSortField;
060
061    @XNode("groupSortField")
062    public String groupSortField;
063
064    @XNode("users/directory")
065    public String userDirectoryName;
066
067    @XNode("users/emailField")
068    public String userEmailField;
069
070    @XNode("users/listingMode")
071    public String userListingMode;
072
073    // BBB old syntax
074    @XNode("userListingMode")
075    public void setUserListingMode(String userListingMode) {
076        this.userListingMode = userListingMode;
077    }
078
079    public boolean userSearchFieldsPresent = false;
080
081    @XNode("users/searchFields")
082    public void setUserSearchFieldsPresent(@SuppressWarnings("unused") String text) {
083        userSearchFieldsPresent = true;
084    }
085
086    @XNode("users/searchFields@append")
087    public boolean userSearchFieldsAppend;
088
089    public Map<String, MatchType> userSearchFields = new LinkedHashMap<>();
090
091    @XNodeList(value = "users/searchFields/exactMatchSearchField", componentType = String.class, type = String[].class)
092    public void setExactMatchUserSearchFields(String[] fields) {
093        for (String field : fields) {
094            userSearchFields.put(field, MatchType.EXACT);
095        }
096    }
097
098    @XNodeList(value = "users/searchFields/substringMatchSearchField", componentType = String.class, type = String[].class)
099    public void setSubstringMatchUserSearchFields(String[] fields) {
100        for (String field : fields) {
101            userSearchFields.put(field, MatchType.SUBSTRING);
102        }
103    }
104
105    public Pattern userPasswordPattern;
106
107    @XNode("userPasswordPattern")
108    public void setUserPasswordPattern(String pattern) {
109        userPasswordPattern = Pattern.compile(pattern);
110    }
111
112    @XNode("users/anonymousUser")
113    public VirtualUserDescriptor anonymousUser;
114
115    @XNodeMap(value = "users/virtualUser", key = "@id", type = HashMap.class, componentType = VirtualUserDescriptor.class)
116    public Map<String, VirtualUserDescriptor> virtualUsers;
117
118    @XNode("groups/directory")
119    public String groupDirectoryName;
120
121    @XNode("groups/groupLabelField")
122    public String groupLabelField;
123
124    @XNode("groups/membersField")
125    public String groupMembersField;
126
127    @XNode("groups/subGroupsField")
128    public String groupSubGroupsField;
129
130    @XNode("groups/parentGroupsField")
131    public String groupParentGroupsField;
132
133    @XNode("groups/listingMode")
134    public String groupListingMode;
135
136    public boolean groupSearchFieldsPresent = false;
137
138    @XNode("groups/searchFields")
139    public void setGroupSearchFieldsPresent(@SuppressWarnings("unused") String text) {
140        groupSearchFieldsPresent = true;
141    }
142
143    @XNode("groups/searchFields@append")
144    public boolean groupSearchFieldsAppend;
145
146    public Map<String, MatchType> groupSearchFields = new LinkedHashMap<>();
147
148    @XNodeList(value = "groups/searchFields/exactMatchSearchField", componentType = String.class, type = String[].class)
149    public void setExactMatchGroupSearchFields(String[] fields) {
150        for (String field : fields) {
151            groupSearchFields.put(field, MatchType.EXACT);
152        }
153    }
154
155    @XNodeList(value = "groups/searchFields/substringMatchSearchField", componentType = String.class, type = String[].class)
156    public void setSubstringMatchGroupSearchFields(String[] fields) {
157        for (String field : fields) {
158            groupSearchFields.put(field, MatchType.SUBSTRING);
159        }
160    }
161
162    @XNode("digestAuthDirectory")
163    public String digestAuthDirectory;
164
165    @XNode("digestAuthRealm")
166    public String digestAuthRealm;
167
168    @XNode("userCacheName")
169    public String userCacheName;
170
171    /**
172     * Merge with data from another descriptor.
173     */
174    public void merge(UserManagerDescriptor other) {
175        if (other.userManagerClass != null) {
176            userManagerClass = other.userManagerClass;
177        }
178        if (other.userCacheName != null) {
179            userCacheName = other.userCacheName;
180        }
181        if (other.userListingMode != null) {
182            userListingMode = other.userListingMode;
183        }
184        if (other.groupListingMode != null) {
185            groupListingMode = other.groupListingMode;
186        }
187        if (other.defaultGroup != null) {
188            defaultGroup = other.defaultGroup;
189        }
190        if (other.defaultAdministratorIds != null) {
191            if (defaultAdministratorIds == null) {
192                defaultAdministratorIds = new ArrayList<>();
193            }
194            defaultAdministratorIds.addAll(other.defaultAdministratorIds);
195        }
196        if (other.administratorsGroups != null) {
197            if (administratorsGroups == null) {
198                administratorsGroups = new ArrayList<>();
199            }
200            administratorsGroups.addAll(other.administratorsGroups);
201        }
202        if (other.disableDefaultAdministratorsGroup != null) {
203            disableDefaultAdministratorsGroup = other.disableDefaultAdministratorsGroup;
204        }
205        if (other.userSearchFieldsPresent) {
206            if (other.userSearchFieldsAppend) {
207                userSearchFields.putAll(other.userSearchFields);
208            } else {
209                userSearchFields = other.userSearchFields;
210            }
211        }
212
213        if (other.userSortField != null) {
214            userSortField = other.userSortField;
215        }
216        if (other.groupSortField != null) {
217            groupSortField = other.groupSortField;
218        }
219        if (other.userDirectoryName != null) {
220            userDirectoryName = other.userDirectoryName;
221        }
222        if (other.userEmailField != null) {
223            userEmailField = other.userEmailField;
224        }
225        if (other.userSearchFieldsPresent) {
226            if (other.userSearchFieldsAppend) {
227                userSearchFields.putAll(other.userSearchFields);
228            } else {
229                userSearchFields = other.userSearchFields;
230            }
231        }
232        if (other.userPasswordPattern != null) {
233            userPasswordPattern = other.userPasswordPattern;
234        }
235        if (other.groupDirectoryName != null) {
236            groupDirectoryName = other.groupDirectoryName;
237        }
238        if (other.groupLabelField != null) {
239            groupLabelField = other.groupLabelField;
240        }
241        if (other.groupMembersField != null) {
242            groupMembersField = other.groupMembersField;
243        }
244        if (other.groupSubGroupsField != null) {
245            groupSubGroupsField = other.groupSubGroupsField;
246        }
247        if (other.groupParentGroupsField != null) {
248            groupParentGroupsField = other.groupParentGroupsField;
249        }
250        if (other.groupSearchFieldsPresent) {
251            if (other.groupSearchFieldsAppend) {
252                groupSearchFields.putAll(other.groupSearchFields);
253            } else {
254                groupSearchFields = other.groupSearchFields;
255            }
256        }
257        if (other.anonymousUser != null) {
258            if (other.anonymousUser.remove) {
259                anonymousUser = null;
260            } else {
261                anonymousUser = other.anonymousUser;
262            }
263        }
264        if (other.virtualUsers != null) {
265            if (virtualUsers == null) {
266                virtualUsers = other.virtualUsers;
267            } else {
268                for (VirtualUserDescriptor otherVirtualUser : other.virtualUsers.values()) {
269                    if (virtualUsers.containsKey(otherVirtualUser.id) && otherVirtualUser.remove) {
270                        virtualUsers.remove(otherVirtualUser.id);
271                    } else {
272                        virtualUsers.put(otherVirtualUser.id, otherVirtualUser);
273                    }
274                }
275            }
276        }
277        if (other.digestAuthDirectory != null) {
278            digestAuthDirectory = other.digestAuthDirectory;
279        }
280        if (other.digestAuthRealm != null) {
281            digestAuthRealm = other.digestAuthRealm;
282        }
283    }
284
285}