From: Alfred Schmid Date: Thu, 13 Feb 2014 08:47:53 +0000 (+0100) Subject: Show "Displayname (username)" in palettes for edit team and repository pages X-Git-Tag: v1.4.0~115^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Ftickets%2F08%2F8%2F3;p=gitblit.git Show "Displayname (username)" in palettes for edit team and repository pages --- diff --git a/releases.moxie b/releases.moxie index 116fca95..66643cf9 100644 --- a/releases.moxie +++ b/releases.moxie @@ -33,6 +33,7 @@ r20: { - Reversed line links in blob view (issue-309) - Dashboard and Activity pages now obey the web.generateActivityGraph setting (issue-310) - Do not log passwords on failed authentication attempts (issue-316) + - Show displayname and username in palettes (issue-364) - Updated default binary and Lucene ignore extensions - Change the WAR baseFolder context parameter to a JNDI env-entry to improve enterprise deployments - Removed internal Gitblit ref exclusions in the upload pack diff --git a/src/main/java/com/gitblit/models/UserChoice.java b/src/main/java/com/gitblit/models/UserChoice.java new file mode 100644 index 00000000..845ab7c7 --- /dev/null +++ b/src/main/java/com/gitblit/models/UserChoice.java @@ -0,0 +1,112 @@ +/* + * Copyright 2014 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.models; + +import java.io.Serializable; + +import com.gitblit.utils.StringUtils; + +/** + * @author Alfred Schmid + * @author James Moger + * + */ +public class UserChoice implements Serializable { + + private static final long serialVersionUID = 1L; + + private final String displayName; + private final String userId; + private final String email; + + /** + * Create a UserChoice without email and displayName. + * + * @param userId + * the unique id of the user (in most cases the unique username + * from user store). Can never be null or empty string. + * + */ + public UserChoice(String userId) { + this(null, userId, null); + } + + /** + * Create a UserChoice without email. + * + * @param displayName + * the display name for the user. Can be null or empty string. + * @param userId + * the unique id of the user (in most cases the unique username + * from user store). Can never be null or empty string. + * + */ + public UserChoice(String displayName, String userId) { + this(displayName, userId, null); + } + + /** + * Create a UserChoice with email and displayName. + * + * @param displayName + * the display name for the user. Can be null or empty string. + * @param userId + * the unique id of the user (in most cases the unique username + * from user store). Can never be null or empty string. + * @param email + * the email from the user. Can be null or empty string. + * + */ + public UserChoice(String displayName, String userId, String email) { + if (userId == null) { + throw new IllegalArgumentException("The argument userId can't be null!"); + } + if ("".equals(userId)) { + throw new IllegalArgumentException("The argument userId can't be an empty String!"); + } + this.displayName = displayName; + this.userId = userId; + this.email = email; + } + + public String getDisplayName() { + return displayName; + } + + public String getDisplayNameOrUserId() { + if (StringUtils.isEmpty(displayName)) { + return userId; + } + return displayName; + } + + public String getUserId() { + return userId; + } + + public String getEmail() { + return email; + } + + @Override + public String toString() { + String dn = getDisplayNameOrUserId(); + if (dn.equals(userId)) { + return dn; + } + return dn + " (" + userId + ")"; + } +} diff --git a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java index a986fd53..bb166101 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -36,6 +36,7 @@ import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; +import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.IChoiceRenderer; @@ -59,6 +60,7 @@ import com.gitblit.GitBlitException; import com.gitblit.Keys; import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.RepositoryModel; +import com.gitblit.models.UserChoice; import com.gitblit.models.UserModel; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.StringUtils; @@ -172,10 +174,26 @@ public class EditRepositoryPage extends RootSubPage { RegistrantType.TEAM, app().users().getAllTeamNames(), repositoryTeams, getAccessPermissions()); // owners palette - List owners = new ArrayList(repositoryModel.owners); - List persons = app().users().getAllUsernames(); - final Palette ownersPalette = new Palette("owners", new ListModel(owners), new CollectionModel( - persons), new StringChoiceRenderer(), 12, true); + List owners = new ArrayList(); + for (String owner : repositoryModel.owners) { + UserModel o = app().users().getUserModel(owner); + if (o != null) { + owners.add(new UserChoice(o.getDisplayName(), o.username, o.emailAddress)); + } else { + owners.add(new UserChoice(owner)); + } + } + List persons = new ArrayList(); + for (String person : app().users().getAllUsernames()) { + UserModel o = app().users().getUserModel(person); + if (o != null) { + persons.add(new UserChoice(o.getDisplayName(), o.username, o.emailAddress)); + } else { + persons.add(new UserChoice(person)); + } + } + final Palette ownersPalette = new Palette("owners", new ListModel(owners), new CollectionModel( + persons), new ChoiceRenderer(null, "userId"), 12, true); // indexed local branches palette List allLocalBranches = new ArrayList(); @@ -358,9 +376,9 @@ public class EditRepositoryPage extends RootSubPage { // owners repositoryModel.owners.clear(); - Iterator owners = ownersPalette.getSelectedChoices(); + Iterator owners = ownersPalette.getSelectedChoices(); while (owners.hasNext()) { - repositoryModel.addOwner(owners.next()); + repositoryModel.addOwner(owners.next().getUserId()); } // pre-receive scripts diff --git a/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java b/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java index 82b28fc4..a0d11a05 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java @@ -18,6 +18,7 @@ package com.gitblit.wicket.pages; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -28,6 +29,7 @@ import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.extensions.markup.html.form.palette.Palette; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; +import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.model.CompoundPropertyModel; @@ -41,6 +43,8 @@ import com.gitblit.GitBlitException; import com.gitblit.Keys; import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.TeamModel; +import com.gitblit.models.UserChoice; +import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.RequiresAdminRole; import com.gitblit.wicket.StringChoiceRenderer; @@ -97,7 +101,6 @@ public class EditTeamPage extends RootSubPage { List repos = getAccessRestrictedRepositoryList(true, null); List teamUsers = new ArrayList(teamModel.users); - Collections.sort(teamUsers); List preReceiveScripts = new ArrayList(); List postReceiveScripts = new ArrayList(); @@ -105,9 +108,8 @@ public class EditTeamPage extends RootSubPage { final List permissions = teamModel.getRepositoryPermissions(); // users palette - final Palette users = new Palette("users", new ListModel( - new ArrayList(teamUsers)), new CollectionModel(app().users() - .getAllUsernames()), new StringChoiceRenderer(), 10, false); + final Palette users = new Palette("users", new ListModel( + getTeamUsers(teamUsers)), new CollectionModel(sortByDisplayName(getTeamUsers(app().users().getAllUsernames()))), new ChoiceRenderer(null, "userId"), 10, false); // pre-receive palette if (teamModel.preReceiveScripts != null) { @@ -155,10 +157,10 @@ public class EditTeamPage extends RootSubPage { teamModel.setRepositoryPermission(repositoryPermission.registrant, repositoryPermission.permission); } - Iterator selectedUsers = users.getSelectedChoices(); + Iterator selectedUsers = users.getSelectedChoices(); List members = new ArrayList(); while (selectedUsers.hasNext()) { - members.add(selectedUsers.next().toLowerCase()); + members.add(selectedUsers.next().getUserId().toLowerCase()); } teamModel.users.clear(); teamModel.users.addAll(members); @@ -255,4 +257,26 @@ public class EditTeamPage extends RootSubPage { add(form); } + + private List getTeamUsers(List teamUserIds) { + List teamUsers = new ArrayList(); + for (String teamUserId : teamUserIds) { + UserModel userModel = app().users().getUserModel(teamUserId); + if (userModel!=null) { + teamUsers.add(new UserChoice(userModel.displayName, userModel.username, userModel.emailAddress)); + } + } + return sortByDisplayName(teamUsers); + } + + private List sortByDisplayName(List teamUsers) { + Collections.sort(teamUsers, new Comparator() { + + @Override + public int compare(UserChoice o1, UserChoice o2) { + return o1.getDisplayNameOrUserId().compareTo(o2.getDisplayNameOrUserId()); + } + }); + return teamUsers; + } } diff --git a/src/main/resources/gitblit.css b/src/main/resources/gitblit.css index 58c0aed5..91825f7e 100644 --- a/src/main/resources/gitblit.css +++ b/src/main/resources/gitblit.css @@ -1291,6 +1291,11 @@ table.palette td.header { } table.palette td.pane { padding: 0px; + width: 250px !important; +} + +table.palette td.pane select { + width: 250px !important; } table.gitnotes { diff --git a/src/test/java/com/gitblit/tests/GitBlitSuite.java b/src/test/java/com/gitblit/tests/GitBlitSuite.java index 81180276..7b38c183 100644 --- a/src/test/java/com/gitblit/tests/GitBlitSuite.java +++ b/src/test/java/com/gitblit/tests/GitBlitSuite.java @@ -56,6 +56,7 @@ import com.gitblit.utils.JGitUtils; @RunWith(Suite.class) @SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class, StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class, + UserModelTest.class, UserChoiceTest.class, ObjectCacheTest.class, PermissionsTest.class, UserServiceTest.class, LdapAuthenticationTest.class, MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class, DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class, diff --git a/src/test/java/com/gitblit/tests/UserChoiceTest.java b/src/test/java/com/gitblit/tests/UserChoiceTest.java new file mode 100644 index 00000000..b0effcd1 --- /dev/null +++ b/src/test/java/com/gitblit/tests/UserChoiceTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2014 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.tests; + +import org.junit.Test; + +import com.gitblit.models.UserChoice; + +/** + * Test behavior of UserChoice class. + * + * @author Alfred Schmid + * + */ +public class UserChoiceTest extends GitblitUnitTest { + + @Test(expected=IllegalArgumentException.class) + public void creatingUserChoiceWithNullAsUserIdIsImpossible() { + new UserChoice(null); + } + + @Test(expected=IllegalArgumentException.class) + public void creatingUserChoiceWithEmptyStringAsUserIdIsImpossible() { + new UserChoice(""); + } + + @Test + public void toStringPrintsPlainUserIdWhenDisplayNameIsNull() { + String userId = "runnerr"; + UserChoice userChoice = new UserChoice(userId); + assertEquals("", userId, userChoice.toString()); + } + + @Test + public void toStringPrintsPlainUserIdWhenDisplayNameIsEmpty() { + String userId = "runnerr"; + UserChoice userChoice = new UserChoice("", userId); + assertEquals("", userId, userChoice.toString()); + } + + @Test + public void toStringPrintsDisplaNameWithUserIdInBracketsWhenDisplayNameIsSet() { + String userId = "runnerr"; + String displayName = "The Road Runner"; + UserChoice userChoice = new UserChoice(displayName, userId); + assertEquals( + "displayName + userId have to be concatenated to: displayName (userId)", + displayName + " (" + userId + ")", userChoice.toString()); + } +} diff --git a/src/test/java/com/gitblit/tests/UserModelTest.java b/src/test/java/com/gitblit/tests/UserModelTest.java new file mode 100644 index 00000000..0de02d57 --- /dev/null +++ b/src/test/java/com/gitblit/tests/UserModelTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.tests; + +import org.junit.Test; + +import com.gitblit.models.UserModel; + +/** + * @author Alfred Schmid + * + */ +public class UserModelTest extends GitblitUnitTest { + + @Test + public void whenDisplayNameIsEmptyUsernameIsUsed() { + String username = "test"; + UserModel userModel = new UserModel(username); + userModel.displayName = ""; + assertEquals("When displayName is empty the username has to be returnd from getDisplayName().", username, userModel.getDisplayName()); + } + + @Test + public void whenDisplayNameIsNullUsernameIsUsed() { + String username = "test"; + UserModel userModel = new UserModel(username); + userModel.displayName = null; + assertEquals("When displayName is null the username has to be returnd from getDisplayName().", username, userModel.getDisplayName()); + } + + @Test + public void whenDisplayNameIsNotEmptyDisplayNameIsUsed() { + String displayName = "Test User"; + UserModel userModel = new UserModel("test"); + userModel.displayName = displayName; + assertEquals("When displayName is not empty its value has to be returnd from getDisplayName().", displayName, userModel.getDisplayName()); + } + +}