From 6d23cab9f5387ca2b57f60b26936ff8af0acc8fa Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Fri, 19 Oct 2012 22:45:30 -0400 Subject: [PATCH] Added UserModel null check --- src/com/gitblit/wicket/pages/EditRepositoryPage.java | 71 +++++++++++++++++++++++++++++++---- 1 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java index 0176249..a2dad10 100644 --- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -36,6 +36,8 @@ import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.IChoiceRenderer; +import org.apache.wicket.markup.html.form.Radio; +import org.apache.wicket.markup.html.form.RadioGroup; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; @@ -47,6 +49,7 @@ import com.gitblit.Constants; import com.gitblit.Constants.AccessRestrictionType; +import com.gitblit.Constants.AuthorizationControl; import com.gitblit.Constants.FederationStrategy; import com.gitblit.GitBlit; import com.gitblit.GitBlitException; @@ -75,6 +78,21 @@ RepositoryModel model = new RepositoryModel(); String restriction = GitBlit.getString(Keys.git.defaultAccessRestriction, null); model.accessRestriction = AccessRestrictionType.fromName(restriction); + String authorization = GitBlit.getString(Keys.git.defaultAuthorizationControl, null); + model.authorizationControl = AuthorizationControl.fromName(authorization); + + GitBlitWebSession session = GitBlitWebSession.get(); + UserModel user = session.getUser(); + if (user != null && user.canCreate() && !user.canAdmin()) { + // personal create permissions, inject personal repository path + model.name = user.getPersonalPath() + "/"; + model.projectPath = user.getPersonalPath(); + model.owner = user.username; + // personal repositories are private by default + model.accessRestriction = AccessRestrictionType.VIEW; + model.authorizationControl = AuthorizationControl.NAMED; + } + setupPage(model); } @@ -98,8 +116,15 @@ List<String> preReceiveScripts = new ArrayList<String>(); List<String> postReceiveScripts = new ArrayList<String>(); + GitBlitWebSession session = GitBlitWebSession.get(); + final UserModel user = session.getUser() == null ? UserModel.ANONYMOUS : session.getUser(); + if (isCreate) { - super.setupPage(getString("gb.newRepository"), ""); + if (user.canAdmin()) { + super.setupPage(getString("gb.newRepository"), ""); + } else { + super.setupPage(getString("gb.newRepository"), user.getDisplayName()); + } } else { super.setupPage(getString("gb.edit"), repositoryModel.name); if (repositoryModel.accessRestriction.exceeds(AccessRestrictionType.NONE)) { @@ -190,11 +215,14 @@ protected void onSubmit() { try { // confirm a repository name was entered - if (StringUtils.isEmpty(repositoryModel.name)) { + if (repositoryModel.name == null && StringUtils.isEmpty(repositoryModel.name)) { error(getString("gb.pleaseSetRepositoryName")); return; } - + + // ensure name is trimmed + repositoryModel.name = repositoryModel.name.trim(); + // automatically convert backslashes to forward slashes repositoryModel.name = repositoryModel.name.replace('\\', '/'); // Automatically replace // with / @@ -223,6 +251,22 @@ error(MessageFormat.format(getString("gb.illegalCharacterRepositoryName"), c)); return; + } + + if (user.canCreate() && !user.canAdmin()) { + // ensure repository name begins with the user's path + if (!repositoryModel.name.startsWith(user.getPersonalPath())) { + error(MessageFormat.format(getString("gb.illegalPersonalRepositoryLocation"), + user.getPersonalPath())); + return; + } + + if (repositoryModel.name.equals(user.getPersonalPath())) { + // reset path prefix and show error + repositoryModel.name = user.getPersonalPath() + "/"; + error(getString("gb.pleaseSetRepositoryName")); + return; + } } // confirm access restriction selection @@ -334,10 +378,11 @@ form.add(new SimpleAttributeModifier("autocomplete", "off")); // field names reflective match RepositoryModel fields - form.add(new TextField<String>("name").setEnabled(isCreate || isAdmin)); + form.add(new TextField<String>("name").setEnabled(isCreate || isAdmin || repositoryModel.isUsersPersonalRepository(user.username))); form.add(new TextField<String>("description")); form.add(new DropDownChoice<String>("owner", GitBlit.self().getAllUsernames()) .setEnabled(GitBlitWebSession.get().canAdmin())); + form.add(new CheckBox("allowForks")); form.add(new DropDownChoice<AccessRestrictionType>("accessRestriction", Arrays .asList(AccessRestrictionType.values()), new AccessRestrictionRenderer())); form.add(new CheckBox("isFrozen")); @@ -361,7 +406,7 @@ form.add(new DropDownChoice<FederationStrategy>("federationStrategy", federationStrategies, new FederationTypeRenderer())); form.add(new CheckBox("useTickets")); - form.add(new CheckBox("useDocs")); + form.add(new CheckBox("useDocs")); form.add(new CheckBox("showRemoteBranches")); form.add(new CheckBox("showReadme")); form.add(new CheckBox("skipSizeCalculation")); @@ -370,6 +415,16 @@ : StringUtils.flattenStrings(repositoryModel.mailingLists, " ")); form.add(new TextField<String>("mailingLists", mailingLists)); form.add(indexedBranchesPalette); + + RadioGroup<AuthorizationControl> group = new RadioGroup<AuthorizationControl>("authorizationControl"); + Radio<AuthorizationControl> allowAuthenticated = new Radio<AuthorizationControl>("allowAuthenticated", new Model<AuthorizationControl>(AuthorizationControl.AUTHENTICATED)); + Radio<AuthorizationControl> allowNamed = new Radio<AuthorizationControl>("allowNamed", new Model<AuthorizationControl>(AuthorizationControl.NAMED)); + group.add(allowAuthenticated); + group.add(allowNamed); + form.add(group); + + form.add(new CheckBox("verifyCommitter")); + form.add(usersPalette); form.add(teamsPalette); form.add(federationSetsPalette); @@ -421,13 +476,13 @@ } if (isCreate) { // Create Repository - if (!user.canAdmin) { - // Only Administrators May Create + if (!user.canCreate() && !user.canAdmin()) { + // Only administrators or permitted users may create error(getString("gb.errorOnlyAdminMayCreateRepository"), true); } } else { // Edit Repository - if (user.canAdmin) { + if (user.canAdmin()) { // Admins can edit everything isAdmin = true; return; -- Gitblit v1.9.1