ShibbolethUserUtils { @Transactional(value = TxType.REQUIRES_NEW) protected UserDetails setupUser(ServletRequest request, ShibbolethUserConfiguration config) throws Exception { UserRepository userRepository = ApplicationContextHolder.get().getBean(UserRepository.class); GroupRepository groupRepository = ApplicationContextHolder.get().getBean(GroupRepository.class); UserGroupRepository userGroupRepository = ApplicationContextHolder.get().getBean(UserGroupRepository.class); GeonetworkAuthenticationProvider authProvider = ApplicationContextHolder.get() .getBean(GeonetworkAuthenticationProvider.class); HttpServletRequest req = (HttpServletRequest) request; String username = getHeader(req, config.getUsernameKey(), ""); String surname = getHeader(req, config.getSurnameKey(), ""); String firstname = getHeader(req, config.getFirstnameKey(), ""); String organisation = getHeader(req, config.getOrganisationKey(), ""); String email = getHeader(req, config.getEmailKey(), ""); String arraySeparator = config.getArraySeparator(); String roleGroupSeparator = config.getRoleGroupSeparator(); String roleGroup_header = getHeader(req, config.getRoleGroupKey(), ""); String[] roleGroups = new String[0]; if (!StringUtils.isEmpty(roleGroup_header)) { roleGroups = roleGroup_header.split(arraySeparator); } else { String profile_header = getHeader(req, config.getProfileKey(), Profile.Guest.name()); String[] profiles = new String[0]; if (!StringUtils.isEmpty(profile_header)) { profiles = profile_header.split(arraySeparator); } String group_header = getHeader(req, config.getGroupKey(), config.getDefaultGroup()); String[] groups = new String[0]; if (!StringUtils.isEmpty(group_header)) { groups = group_header.split(arraySeparator); } List<String> roleGroupsList = new ArrayList<>(groups.length); for (int i = 0; i < groups.length; i++) { String profile; if (profiles.length > i) { profile = profiles[i]; } else { profile = Profile.Guest.toString(); } roleGroupsList.add(groups[i] + roleGroupSeparator + profile); } roleGroups = roleGroupsList.stream().toArray(String[]::new); } if (!StringUtils.isEmpty(username)) { if (username.length() > 256) { username = username.substring(0, 256); } User user = null; try { user = (User) authProvider.loadUserByUsername(username); if (config.isUpdateGroup()) { userGroupRepository.deleteAll(UserGroupSpecs.hasUserId(user.getId())); assignGroups(groupRepository, userGroupRepository, roleGroups, roleGroupSeparator, user); } if (config.isUpdateProfile()) { assignProfile(roleGroups, roleGroupSeparator, user); userRepository.save(user); } } catch (UsernameNotFoundException e) { user = new User(); user.setUsername(username); user.setSurname(surname); user.setName(firstname); user.setOrganisation(organisation); if (!StringUtils.isEmpty(email)) { user.getEmailAddresses().add(email); } assignProfile(roleGroups, roleGroupSeparator, user); userRepository.save(user); assignGroups(groupRepository, userGroupRepository, roleGroups, roleGroupSeparator, user); } if (udetailsmapper != null) { LDAPUser ldapUserDetails = null; try { ldapUserDetails = (LDAPUser) userDetailsManager.loadUserByUsername(username); } catch (Throwable t) { Log.error(Geonet.GEONETWORK, "Shibboleth setupUser error: " + t.getMessage(), t); } if (ldapUserDetails == null) { ldapUserDetails = new LDAPUser(username); ldapUserDetails.getUser().setName(firstname).setSurname(surname); ldapUserDetails.getUser().setOrganisation(organisation); ldapUserDetails.getUser().setProfile(user.getProfile()); ldapUserDetails.getUser().getEmailAddresses().clear(); if (StringUtils.isEmpty(email)) { ldapUserDetails.getUser().getEmailAddresses().add(username + "@unknownIdp"); } else { ldapUserDetails.getUser().getEmailAddresses().add(email); } } udetailsmapper.saveUser(ldapUserDetails); user = ldapUserDetails.getUser(); } return user; } return null; }    }
@Test public void twoConsecutiveLogins() throws Exception { User user = userRepo.findOneByUsername(username); assertNull("User already exists", user); String group = groupname + "1"; String groups = group + config.getArraySeparator() + group; String profile = Profile.UserAdmin.name() + config.getArraySeparator() + Profile.Administrator.name(); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader(this.config.getEmailKey(), email); request.addHeader(this.config.getFirstnameKey(), firstname); request.addHeader(this.config.getGroupKey(), groups); request.addHeader(this.config.getProfileKey(), profile); request.addHeader(this.config.getSurnameKey(), surname); request.addHeader(this.config.getUsernameKey(), username); utils.setupUser(request, this.config); user = userRepo.findOneByUsername(username); assertNotNull("User was not created", user); assertSame("The profile should be the highest in the list", Profile.Administrator, user.getProfile()); List<Integer> idGroups = userGroupRepo.findGroupIds(UserGroupSpecs.hasUserId(user.getId())); assertSame("Groups size is wrong", idGroups.size(), 1); assertEquals("The group assigned is wrong", Integer.valueOf(groupRepo.findByName(group).getId()), idGroups.get(0)); group = groupname + "3"; groups = group + config.getArraySeparator() + group; profile = Profile.Guest.name() + config.getArraySeparator() + Profile.Editor.name(); request = new MockHttpServletRequest(); request.addHeader(this.config.getEmailKey(), email); request.addHeader(this.config.getFirstnameKey(), firstname); request.addHeader(this.config.getGroupKey(), groups); request.addHeader(this.config.getProfileKey(), profile); request.addHeader(this.config.getSurnameKey(), surname); request.addHeader(this.config.getUsernameKey(), username); utils.setupUser(request, this.config); user = userRepo.findOneByUsername(username); assertNotNull("User was removed", user); idGroups = userGroupRepo.findGroupIds(UserGroupSpecs.hasUserId(user.getId())); assertSame("The profile should be the highest in the list", Profile.Editor, user.getProfile()); assertSame("Groups size is wrong", idGroups.size(), 1); assertEquals("The group assigned is wrong", Integer.valueOf(groupRepo.findByName(group).getId()), idGroups.get(0)); }
@Test public void twoConsecutiveLoginsNoAuthorization() throws Exception { config.setUpdateGroup(false); config.setUpdateProfile(false); User user = userRepo.findOneByUsername(username); assertNull("User already exists", user); String group = groupname + "1"; String groups = group + config.getArraySeparator() + group ; String profile = Profile.UserAdmin.name() + config.getArraySeparator() + Profile.Administrator.name(); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader(this.config.getEmailKey(), email); request.addHeader(this.config.getFirstnameKey(), firstname); request.addHeader(this.config.getGroupKey(), groups); request.addHeader(this.config.getProfileKey(), profile); request.addHeader(this.config.getSurnameKey(), surname); request.addHeader(this.config.getUsernameKey(), username); utils.setupUser(request, this.config); user = userRepo.findOneByUsername(username); assertNotNull("User was not created", user); assertSame("The profile should be the highest in the list", Profile.Administrator, user.getProfile()); List<Integer> idGroups = userGroupRepo.findGroupIds(UserGroupSpecs.hasUserId(user.getId())); assertSame("Groups size is wrong", idGroups.size(), 1); assertEquals("The group assigned is wrong", Integer.valueOf(groupRepo.findByName(group).getId()), idGroups.get(0)); String groupNew = groupname + "3"; String groupsgroupNew = groupNew + config.getArraySeparator() + groupNew ; request = new MockHttpServletRequest(); request.addHeader(this.config.getEmailKey(), email); request.addHeader(this.config.getFirstnameKey(), firstname); request.addHeader(this.config.getGroupKey(), groupsgroupNew); request.addHeader(this.config.getProfileKey(), Profile.Guest.name() + config.getArraySeparator() + Profile.Editor.name()); request.addHeader(this.config.getSurnameKey(), surname); request.addHeader(this.config.getUsernameKey(), username); utils.setupUser(request, this.config); user = userRepo.findOneByUsername(username); assertNotNull("User was removed", user); idGroups = userGroupRepo.findGroupIds(UserGroupSpecs.hasUserId(user.getId())); assertSame("The profile should be the highest in the list", Profile.Administrator, user.getProfile()); assertSame("Groups size is wrong", idGroups.size(), 1); assertEquals("The group assigned is wrong", Integer.valueOf(groupRepo.findByName(group).getId()), idGroups.get(0)); }
@Test public void groupLengthNotMatchProfileLength() throws Exception { User user = userRepo.findOneByUsername(username); assertNull("User already exists", user); String group = groupname + "1" + config.getArraySeparator() + groupname + "2" + config.getArraySeparator() + groupname + "3"; String profile = Profile.Editor.name(); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader(this.config.getEmailKey(), email); request.addHeader(this.config.getFirstnameKey(), firstname); request.addHeader(this.config.getGroupKey(), group); request.addHeader(this.config.getProfileKey(), profile); request.addHeader(this.config.getSurnameKey(), surname); request.addHeader(this.config.getUsernameKey(), username); request.addHeader(this.config.getOrganisationKey(), organisation); utils.setupUser(request, this.config); user = userRepo.findOneByUsername(username); assertNotNull("User was not created", user); assertSame("The profile should be the highest in the list", Profile.Editor, user.getProfile()); List<Integer> idGroups = userGroupRepo.findGroupIds(UserGroupSpecs.hasUserId(user.getId())); assertSame("Groups size is wrong", idGroups.size(), 3); List<UserGroup> groups = userGroupRepo.findAll(UserGroupSpecs.hasUserId(user.getId())); for (UserGroup ug : groups) { if (ug.getProfile().equals(Profile.Editor)) { assertTrue(ug.getGroup().getName().equalsIgnoreCase(groupname + "1")); } else if (ug.getProfile().equals(Profile.Guest)) { assertTrue(ug.getGroup().getName().equalsIgnoreCase(groupname + "2") || ug.getGroup().getName().equalsIgnoreCase(groupname + "3")); } else { assertTrue("We have a usergroup we shouldn't have", false); } } }
@Test public void severalGroups() throws Exception { User user = userRepo.findOneByUsername(username); assertNull("User already exists", user); String group = groupname + "1"; String profile = Profile.Reviewer.name(); for (int i = 2; i < 5; i++) { group = group + config.getArraySeparator() + groupname + i; profile = profile + config.getArraySeparator() + Profile.Editor.name(); } MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader(this.config.getEmailKey(), email); request.addHeader(this.config.getFirstnameKey(), firstname); request.addHeader(this.config.getGroupKey(), group); request.addHeader(this.config.getProfileKey(), profile); request.addHeader(this.config.getSurnameKey(), surname); request.addHeader(this.config.getUsernameKey(), username); utils.setupUser(request, this.config); user = userRepo.findOneByUsername(username); assertNotNull("User was not created", user); assertSame("The profile should be the highest in the list", Profile.Reviewer, user.getProfile()); List<Integer> idGroups = userGroupRepo.findGroupIds(UserGroupSpecs.hasUserId(user.getId())); assertSame("Groups size is wrong", idGroups.size(), 4); List<UserGroup> groups = userGroupRepo.findAll(UserGroupSpecs.hasUserId(user.getId())); for (UserGroup ug : groups) { assertNotSame("No profile can be guest as we have defined a role for all groups.", Profile.Guest, ug.getProfile()); } }