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 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.platform.usermanager; 020 021import java.io.Serializable; 022import java.security.Principal; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.LinkedHashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Set; 029import java.util.regex.Matcher; 030import java.util.regex.Pattern; 031 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentModelList; 034import org.nuxeo.ecm.core.api.NuxeoGroup; 035import org.nuxeo.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.ecm.core.api.security.ACP; 037import org.nuxeo.ecm.core.api.security.SecurityConstants; 038import org.nuxeo.ecm.core.query.sql.model.QueryBuilder; 039import org.nuxeo.runtime.services.event.Event; 040 041/** 042 * @author Florent Guillaume 043 */ 044public class FakeUserManagerImpl implements UserManager { 045 046 private static final long serialVersionUID = 1L; 047 048 String userListingMode; 049 050 String groupListingMode; 051 052 List<String> defaultAdministratorIds; 053 054 List<String> administratorsGroups; 055 056 String defaultGroup; 057 058 String userSortField; 059 060 String groupSortField; 061 062 String userDirectoryName; 063 064 String userEmailField; 065 066 Map<String, MatchType> userSearchFields; 067 068 Pattern userPasswordPattern; 069 070 String groupDirectoryName; 071 072 String groupMembersField; 073 074 Map<String, MatchType> groupSearchFields; 075 076 String groupSubGroupsField; 077 078 String groupParentGroupsField; 079 080 VirtualUser anonymousUser; 081 082 final Map<String, VirtualUserDescriptor> virtualUsers; 083 084 public FakeUserManagerImpl() { 085 virtualUsers = new HashMap<>(); 086 } 087 088 @Override 089 public String getUserListingMode() { 090 return userListingMode; 091 } 092 093 public void setUserListingMode(String userListingMode) { 094 this.userListingMode = userListingMode; 095 } 096 097 @Override 098 public String getGroupListingMode() { 099 return groupListingMode; 100 } 101 102 public void setGroupListingMode(String groupListingMode) { 103 this.groupListingMode = groupListingMode; 104 } 105 106 @Override 107 public String getDefaultGroup() { 108 return defaultGroup; 109 } 110 111 public void setDefaultGroup(String defaultGroup) { 112 this.defaultGroup = defaultGroup; 113 } 114 115 @Override 116 public String getUserSortField() { 117 return userSortField; 118 } 119 120 public void setUserSortField(String sortField) { 121 userSortField = sortField; 122 } 123 124 public void setGroupSortField(String sortField) { 125 groupSortField = sortField; 126 } 127 128 public void setUserDirectoryName(String userDirectoryName) { 129 this.userDirectoryName = userDirectoryName; 130 } 131 132 @Override 133 public String getUserDirectoryName() { 134 return userDirectoryName; 135 } 136 137 public void setUserEmailField(String userEmailField) { 138 this.userEmailField = userEmailField; 139 } 140 141 @Override 142 public String getUserEmailField() { 143 return userEmailField; 144 } 145 146 public void setUserSearchFields(Set<String> userSearchFields) { 147 this.userSearchFields = new LinkedHashMap<>(); 148 for (String searchField : userSearchFields) { 149 this.userSearchFields.put(searchField, MatchType.SUBSTRING); 150 } 151 } 152 153 public void setUserSearchFields(Map<String, MatchType> userSearchFields) { 154 this.userSearchFields = userSearchFields; 155 } 156 157 @Override 158 public Set<String> getUserSearchFields() { 159 return userSearchFields.keySet(); 160 } 161 162 public void setGroupSearchFields(Map<String, MatchType> groupSearchFields) { 163 this.groupSearchFields = groupSearchFields; 164 } 165 166 @Override 167 public Set<String> getGroupSearchFields() { 168 return groupSearchFields.keySet(); 169 } 170 171 public void setGroupDirectoryName(String groupDirectoryName) { 172 this.groupDirectoryName = groupDirectoryName; 173 } 174 175 @Override 176 public String getGroupDirectoryName() { 177 return groupDirectoryName; 178 } 179 180 public void setGroupMembersField(String groupMembersField) { 181 this.groupMembersField = groupMembersField; 182 } 183 184 @Override 185 public String getGroupMembersField() { 186 return groupMembersField; 187 } 188 189 public void setGroupSubGroupsField(String groupSubGroupsField) { 190 this.groupSubGroupsField = groupSubGroupsField; 191 } 192 193 @Override 194 public String getGroupSubGroupsField() { 195 return groupSubGroupsField; 196 } 197 198 public void setGroupParentGroupsField(String groupParentGroupsField) { 199 this.groupParentGroupsField = groupParentGroupsField; 200 } 201 202 @Override 203 public String getGroupParentGroupsField() { 204 return groupParentGroupsField; 205 } 206 207 @Override 208 public Boolean areGroupsReadOnly() { 209 throw new UnsupportedOperationException(); 210 } 211 212 @Override 213 public Boolean areUsersReadOnly() { 214 throw new UnsupportedOperationException(); 215 } 216 217 @Override 218 public boolean checkUsernamePassword(String username, String password) { 219 return username.equals(password); // ok for a mock 220 } 221 222 @Override 223 public boolean validatePassword(String password) { 224 if (userPasswordPattern == null) { 225 return true; 226 } else { 227 Matcher userPasswordMatcher = userPasswordPattern.matcher(password); 228 return userPasswordMatcher.find(); 229 } 230 } 231 232 @Override 233 public List<String> getGroupsInGroup(String parentId) { 234 throw new UnsupportedOperationException(); 235 } 236 237 @Override 238 public NuxeoPrincipal getPrincipal(String username, boolean fetchReferences) { 239 NuxeoPrincipalImpl principal = new NuxeoPrincipalImpl(SecurityConstants.ADMINISTRATOR, false, true); 240 if (fetchReferences) { 241 principal.setGroups(Collections.singletonList(SecurityConstants.ADMINISTRATORS)); 242 } 243 principal.setEmail("admin@example.com"); 244 return principal; 245 } 246 247 @Override 248 public List<String> getTopLevelGroups() { 249 throw new UnsupportedOperationException(); 250 } 251 252 @Override 253 public List<String> getUsersInGroup(String groupId) { 254 throw new UnsupportedOperationException(); 255 } 256 257 @Override 258 public List<String> getUsersInGroupAndSubGroups(String groupId) { 259 throw new UnsupportedOperationException(); 260 } 261 262 @Override 263 public DocumentModelList searchGroups(String pattern) { 264 throw new UnsupportedOperationException(); 265 } 266 267 @Override 268 public List<NuxeoPrincipal> searchPrincipals(String pattern) { 269 throw new UnsupportedOperationException(); 270 } 271 272 @Override 273 public Pattern getUserPasswordPattern() { 274 return userPasswordPattern; 275 } 276 277 public void setUserPasswordPattern(Pattern userPasswordPattern) { 278 this.userPasswordPattern = userPasswordPattern; 279 } 280 281 public void setAnonymousUser(VirtualUser anonymousUser) { 282 this.anonymousUser = anonymousUser; 283 } 284 285 public void setVirtualUsers(Map<String, VirtualUserDescriptor> virtualUsers) { 286 this.virtualUsers.clear(); 287 if (virtualUsers != null) { 288 this.virtualUsers.putAll(virtualUsers); 289 } 290 } 291 292 @Override 293 public String getAnonymousUserId() { 294 if (anonymousUser == null) { 295 return null; 296 } 297 return anonymousUser.getId(); 298 } 299 300 @Override 301 public String getDigestAuthDirectory() { 302 return null; 303 } 304 305 @Override 306 public String getDigestAuthRealm() { 307 return null; 308 } 309 310 @Override 311 public void setConfiguration(UserManagerDescriptor descriptor) { 312 setDefaultGroup(descriptor.defaultGroup); 313 defaultAdministratorIds = descriptor.defaultAdministratorIds; 314 administratorsGroups = descriptor.administratorsGroups; 315 setUserSortField(descriptor.userSortField); 316 setGroupSortField(descriptor.groupSortField); 317 setUserListingMode(descriptor.userListingMode); 318 setGroupListingMode(descriptor.groupListingMode); 319 setUserDirectoryName(descriptor.userDirectoryName); 320 setUserEmailField(descriptor.userEmailField); 321 setUserSearchFields(descriptor.userSearchFields); 322 setUserPasswordPattern(descriptor.userPasswordPattern); 323 setGroupDirectoryName(descriptor.groupDirectoryName); 324 setGroupMembersField(descriptor.groupMembersField); 325 setGroupSubGroupsField(descriptor.groupSubGroupsField); 326 setGroupParentGroupsField(descriptor.groupParentGroupsField); 327 setGroupSearchFields(descriptor.groupSearchFields); 328 setAnonymousUser(descriptor.anonymousUser); 329 setVirtualUsers(descriptor.virtualUsers); 330 } 331 332 @Override 333 public DocumentModel getBareUserModel() { 334 throw new UnsupportedOperationException(); 335 } 336 337 @Override 338 public DocumentModel createGroup(DocumentModel groupModel) { 339 throw new UnsupportedOperationException(); 340 } 341 342 @Override 343 public DocumentModel createUser(DocumentModel userModel) { 344 throw new UnsupportedOperationException(); 345 } 346 347 @Override 348 public void deleteGroup(DocumentModel groupModel) { 349 throw new UnsupportedOperationException(); 350 } 351 352 @Override 353 public void deleteGroup(String groupId) { 354 throw new UnsupportedOperationException(); 355 } 356 357 @Override 358 public void deleteUser(DocumentModel userModel) { 359 throw new UnsupportedOperationException(); 360 } 361 362 @Override 363 public void deleteUser(String userId) { 364 throw new UnsupportedOperationException(); 365 } 366 367 @Override 368 public DocumentModel getBareGroupModel() { 369 throw new UnsupportedOperationException(); 370 } 371 372 @Override 373 public NuxeoGroup getGroup(String groupName) { 374 throw new UnsupportedOperationException(); 375 } 376 377 @Override 378 public List<String> getGroupIds() { 379 throw new UnsupportedOperationException(); 380 } 381 382 @Override 383 public List<String> getUserIds() { 384 throw new UnsupportedOperationException(); 385 } 386 387 @Override 388 public DocumentModelList searchGroups(Map<String, Serializable> filter, Set<String> fulltext) { 389 throw new UnsupportedOperationException(); 390 } 391 392 @Override 393 public DocumentModelList searchGroups(QueryBuilder queryBuilder) { 394 throw new UnsupportedOperationException(); 395 } 396 397 @Override 398 public DocumentModelList searchUsers(Map<String, Serializable> filter, Set<String> fulltext) { 399 throw new UnsupportedOperationException(); 400 } 401 402 @Override 403 public DocumentModelList searchUsers(String pattern) { 404 throw new UnsupportedOperationException(); 405 } 406 407 @Override 408 public DocumentModelList searchUsers(QueryBuilder queryBuilder) { 409 throw new UnsupportedOperationException(); 410 } 411 412 @Override 413 public void updateGroup(DocumentModel groupModel) { 414 throw new UnsupportedOperationException(); 415 } 416 417 @Override 418 public void updateUser(DocumentModel userModel) { 419 throw new UnsupportedOperationException(); 420 } 421 422 @Override 423 public DocumentModel getGroupModel(String groupName) { 424 throw new UnsupportedOperationException(); 425 } 426 427 @Override 428 public DocumentModel getUserModel(String userName) { 429 throw new UnsupportedOperationException(); 430 } 431 432 @Override 433 public String getGroupIdField() { 434 return "groupname"; 435 } 436 437 @Override 438 public String getGroupLabelField() { 439 return "grouplabel"; 440 } 441 442 @Override 443 public String getGroupSchemaName() { 444 return "group"; 445 } 446 447 @Override 448 public String getUserIdField() { 449 return "username"; 450 } 451 452 @Override 453 public String getUserSchemaName() { 454 return "user"; 455 } 456 457 @Override 458 public List<String> getAdministratorsGroups() { 459 throw new UnsupportedOperationException(); 460 } 461 462 @Override 463 public String[] getUsersForPermission(String perm, ACP acp) { 464 throw new UnsupportedOperationException(); 465 } 466 467 @Override 468 public Principal authenticate(String name, String password) { 469 return checkUsernamePassword(name, password) ? getPrincipal(name) : null; 470 } 471 472 @Override 473 public void handleEvent(Event event) { 474 } 475 476 @Override 477 public List<String> getAncestorGroups(String groupId) { 478 throw new UnsupportedOperationException(); 479 } 480 481 @Override 482 public List<String> getDescendantGroups(String groupId) { 483 throw new UnsupportedOperationException(); 484 } 485 486 @Override 487 public GroupConfig getGroupConfig() { 488 throw new UnsupportedOperationException(); 489 } 490 491 @Override 492 public void notifyUserChanged(String userName, String eventId) { 493 throw new UnsupportedOperationException(); 494 } 495 496 @Override 497 public void notifyGroupChanged(String groupName, String eventId, List<String> ancestorGroupNames) { 498 throw new UnsupportedOperationException(); 499 } 500 501}