@Override public User delegate(UserManager um) { return um.getUser(name); } });
@Override public User delegate(UserManager um) { return um.getUser(subject); } });
protected void validateUsername(String name) { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException(name + " is not a valid username."); } User user; if (isAllowCrossRealmDuplicateNames()) { user = this.getUser(name); } else { user = Security.getUserManager().getUser(name); } if (user != null) { throw new IllegalArgumentException("User with name " + name + " already exists."); } }
@Override public void execute(InstallContext installContext) throws TaskExecutionException { UserManager userManager = securitySupport.getUserManager(); User anonymous = userManager.getUser("anonymous"); userManager.addRole(anonymous, "categorization-base"); } }
protected User getUser(Context ctx) { User user = null; String attributeUsername = ctx.getAttribute(Context.ATTRIBUTE_USERNAME); if (!StringUtils.isEmpty(attributeUsername)) { user = securitySupport.getUserManager().getUser(attributeUsername); } return user == null ? ctx.getUser() : user; } }
@Test public void getGroupsReturnsDirectGroupsWithoutDuplicates() { final Collection<String> groups = userManager.getUser("georges").getGroups(); assertThat(groups.size(), is(2)); assertThat(groups, containsInAnyOrder("groupA", "groupB")); }
@Test public void getAllGroupsWhenRecursiveGroups() { final Collection<String> groups = userManager.getUser("recursive").getAllGroups(); assertThat(groups.size(), is(2)); assertThat(groups, containsInAnyOrder("groupRecursiveA", "groupRecursiveB")); }
@Test public void getAllRolesReturnsDirectAndInheritedRoles() throws Exception { final Collection<String> rolesG = userManager.getUser("georges").getAllRoles(); assertThat(rolesG.size(), is(5)); assertThat(rolesG, containsInAnyOrder("roleY", "roleX", "roleV", "roleW", "roleZ")); final Collection<String> rolesJ = userManager.getUser("julien").getAllRoles(); assertThat(rolesJ.size(), is(3)); assertThat(rolesJ, containsInAnyOrder("roleY", "roleX", "roleZ")); }
@Test public void getAllGroupsReturnsDirectAndInheritedGroups() { final Collection<String> groups = userManager.getUser("georges").getAllGroups(); assertThat(groups.size(), is(4)); assertThat(groups, containsInAnyOrder("groupA", "groupB", "groupC", "groupD")); }
@Test public void getRolesReturnsDirectRoles() { final Collection<String> roles = userManager.getUser("georges").getRoles(); assertThat(roles.size(), is(3)); assertThat(roles, containsInAnyOrder("roleV", "roleW", "roleX")); }
@Test public void getRolesReturnsDirectRolesWithoutDuplicates() { final Collection<String> roles = userManager.getUser("julien").getRoles(); assertThat(roles.size(), is(2)); assertThat(roles, containsInAnyOrder("roleY", "roleX")); }
@Test public void userManagerReturnsActualUserExceptSuperuser() throws Exception { //WHEN User user = securitySupport.getUserManager().getUser("georges"); //THEN assertThat(user.getName(), is("georges")); assertThat(user instanceof MgnlUser, is(true)); //WHEN user = securitySupport.getUserManager().getUser(UserManager.SYSTEM_USER); //THEN assertThat(user instanceof RescueSecuritySupport.RescueUser, is(true)); }
@Override public void execute(InstallContext ctx) throws TaskExecutionException { final UserManager userManager = SecuritySupport.Factory.getInstance().getUserManager(); final User user = userManager.getUser(username); if (user == null) { ctx.warn("User \"" + username + "\" not found, can't add him/her to the \"" + groupname + "\" group."); } else { // TODO this saves at node level, thus breaking the "save once per module install/update" rule :( try { userManager.addGroup(user, groupname); } catch (UnsupportedOperationException e) { ctx.warn("Can't add the user \"" + username + "\" to the \"" + groupname + "\" group due to an unsupported operation exception. This is most likely the case if the users are managed externaly."); } } } }
@Override public void execute(InstallContext installContext) throws TaskExecutionException { if (SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_BOOTSTRAP_SAMPLES)) { new BootstrapConditionally("Bootstrap templater role", "", "/mgnl-bootstrap-samples/inplace-templating/userroles.templater.xml").execute(installContext); User tim = Security.getUserManager().getUser("tim"); // check is the sample user with no modifications if (tim.getAllRoles().size() == 0 && tim.getAllGroups().size() == 0) { // overwrite tim as addRole does not work because tim has a reference to a role that does not exist new BootstrapSingleResource("Overwrite user Tim", "Tim will be overwritted with the right permissions.", "/mgnl-bootstrap-samples/inplace-templating/users.admin.tim.xml", ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING).execute(installContext); installContext.warn("User Tim has been overwritten."); } } }
@Test public void inGroup() { User georges = userManager.getUser("georges"); assertThat(georges.inGroup("groupB"), is(true)); assertThat(georges.inGroup("groupE"), is(false)); assertThat(georges.inGroup("notExistingGroup"), is(false)); User julien = userManager.getUser("julien"); assertThat(julien.inGroup("groupC"), is(true)); assertThat(julien.inGroup("groupA"), is(false)); User user = userManager.getUser("jaques"); assertThat("Jaques should not be in any group", user.inGroup("doesNotMatterButShouldNotShowExceptionsInLog"), is(false)); }
@Test public void getGroupsReturnsOnlyDirectGroups() { final Collection<String> g = userManager.getUser("julien").getGroups(); assertThat(g.size(), is(1)); assertThat(g, hasItem("groupC")); }
@Test public void userManagerReturnsRescueUserWithEnglishAsDefaultLanguage() throws Exception { //WHEN User user = securitySupport.getUserManager().getUser(UserManager.SYSTEM_USER); //THEN assertThat(user.getLanguage(), is("en")); }
@Test public void inGroupWithUsersCustomWorkspace() throws Exception { MockUtil.createAndSetHierarchyManager("meh", getClass().getResourceAsStream("sample-users.properties")); userManager = createMgnlUserManager("test", "meh"); User georges = userManager.getUser("georges"); assertThat(georges.inGroup("groupB"), is(true)); assertThat(georges.inGroup("groupE"), is(false)); assertThat(georges.inGroup("notExistingGroup"), is(false)); User julien = userManager.getUser("julien"); assertThat(julien.inGroup("groupC"), is(true)); assertThat(julien.inGroup("groupA"), is(false)); User user = userManager.getUser("jaques"); assertThat("Jaques should not be in any group", user.inGroup("doesNotMatterButShouldNotShowExceptionsInLog"), is(false)); }
@Test public void userManagerReturnsEnabledRescueUser() throws Exception { //WHEN User user = securitySupport.getUserManager().getUser(UserManager.SYSTEM_USER); //THEN assertThat(user.isEnabled(), is(true)); }
@Test public void userManagerReturnsCorrectSystemUserByName() throws Exception { //WHEN User user = securitySupport.getUserManager().getUser(UserManager.SYSTEM_USER); //THEN assertThat(user.getName(), is(UserManager.SYSTEM_USER)); assertThat(SecurityUtil.matchBCrypted(UserManager.SYSTEM_PSWD, user.getPassword()), is(true)); }