From b4ed66fda03d83fd281ca9cbeae0a1c7785489e4 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 9 May 2014 07:57:14 -0400 Subject: [PATCH] Move delete repository function into the edit page --- releases.moxie | 5 +- src/main/distrib/data/gitblit.properties | 2 +- .../com/gitblit/manager/GitblitManager.java | 5 ++ .../gitblit/manager/IRepositoryManager.java | 9 ++++ .../gitblit/manager/RepositoryManager.java | 17 +++++++ .../wicket/pages/EditRepositoryPage.html | 2 +- .../wicket/pages/EditRepositoryPage.java | 34 +++++++++++++ .../wicket/panels/ProjectRepositoryPanel.html | 9 ---- .../wicket/panels/ProjectRepositoryPanel.java | 50 +------------------ .../wicket/panels/RepositoriesPanel.html | 4 -- .../wicket/panels/RepositoriesPanel.java | 49 +----------------- 11 files changed, 74 insertions(+), 112 deletions(-) diff --git a/releases.moxie b/releases.moxie index 96a0ae91..a5cfed95 100644 --- a/releases.moxie +++ b/releases.moxie @@ -10,7 +10,8 @@ r24: { text: ~ security: ~ fixes: ~ - changes: ~ + changes: + - Move repository deletion functions to the edit repository page AND allow deletion to be disabled (pr-180, ticket-67) additions: - Add FORK_REPOSITORY RPC request type (issue-371, pr-161, ticket-65) - Add object type (ot) parameter for RSS queries to retrieve tag details (pr-165, ticket-66) @@ -18,6 +19,8 @@ r24: { contributors: - Manisha Gayathri - Gerard Smyth + settings: + - { name: 'web.allowDeletingNonEmptyRepositories', defaultValue: 'true' } } # diff --git a/src/main/distrib/data/gitblit.properties b/src/main/distrib/data/gitblit.properties index 30e97896..ab5311ce 100644 --- a/src/main/distrib/data/gitblit.properties +++ b/src/main/distrib/data/gitblit.properties @@ -766,7 +766,7 @@ web.authenticateAdminPages = true # SINCE 0.5.0 web.allowCookieAuthentication = true -# Allow deleting of non empty repositories through the user interface. +# Allow deletion of non-empty repositories. This is enforced for all delete vectors. # # SINCE 1.6.0 web.allowDeletingNonEmptyRepositories = true diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index 2782e724..16c71ba6 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -1014,6 +1014,11 @@ public class GitblitManager implements IGitblit { repositoryManager.updateConfiguration(r, repository); } + @Override + public boolean canDelete(RepositoryModel model) { + return repositoryManager.canDelete(model); + } + @Override public boolean deleteRepositoryModel(RepositoryModel model) { return repositoryManager.deleteRepositoryModel(model); diff --git a/src/main/java/com/gitblit/manager/IRepositoryManager.java b/src/main/java/com/gitblit/manager/IRepositoryManager.java index 69177f1e..f5cf03fa 100644 --- a/src/main/java/com/gitblit/manager/IRepositoryManager.java +++ b/src/main/java/com/gitblit/manager/IRepositoryManager.java @@ -338,6 +338,15 @@ public interface IRepositoryManager extends IManager { */ void updateConfiguration(Repository r, RepositoryModel repository); + /** + * Returns true if the repository can be deleted. + * + * @param model + * @return true if the repository can be deleted + * @since 1.6.0 + */ + boolean canDelete(RepositoryModel model); + /** * Deletes the repository from the file system and removes the repository * permission from all repository users. diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index 7351eb97..c141f088 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -1532,6 +1532,17 @@ public class RepositoryManager implements IRepositoryManager { } } + /** + * Returns true if the repository can be deleted. + * + * @return true if the repository can be deleted + */ + @Override + public boolean canDelete(RepositoryModel repository) { + return settings.getBoolean(Keys.web.allowDeletingNonEmptyRepositories, true) + || !repository.hasCommits; + } + /** * Deletes the repository from the file system and removes the repository * permission from all repository users. @@ -1553,6 +1564,12 @@ public class RepositoryManager implements IRepositoryManager { */ @Override public boolean deleteRepository(String repositoryName) { + RepositoryModel repository = getRepositoryModel(repositoryName); + if (!canDelete(repository)) { + logger.warn("Attempt to delete {} rejected!", repositoryName); + return false; + } + try { close(repositoryName); // clear the repository cache diff --git a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.html b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.html index f82ac3d5..b7a1976f 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.html +++ b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.html @@ -112,7 +112,7 @@
-
 
+
   
diff --git a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java index cd0da5f4..412c0ecc 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -42,6 +42,7 @@ import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.IChoiceRenderer; import org.apache.wicket.markup.html.form.RadioChoice; import org.apache.wicket.markup.html.form.TextField; +import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.model.CompoundPropertyModel; @@ -68,6 +69,7 @@ import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.StringChoiceRenderer; import com.gitblit.wicket.WicketUtils; +import com.gitblit.wicket.panels.BasePanel.JavascriptEventConfirmation; import com.gitblit.wicket.panels.BulletListPanel; import com.gitblit.wicket.panels.RegistrantPermissionsPanel; @@ -614,6 +616,38 @@ public class EditRepositoryPage extends RootSubPage { cancel.setDefaultFormProcessing(false); form.add(cancel); + // the user can delete if deletions are allowed AND the user is an admin or the personal owner + // assigned ownership is not sufficient to allow deletion + boolean canDelete = !isCreate && app().repositories().canDelete(repositoryModel) + && (user.canAdmin() || user.isMyPersonalRepository(repositoryModel.name)); + + Link delete = new Link("delete") { + + private static final long serialVersionUID = 1L; + + @Override + public void onClick() { + RepositoryModel latestModel = app().repositories().getRepositoryModel(repositoryModel.name); + boolean canDelete = app().repositories().canDelete(latestModel); + if (canDelete) { + if (app().repositories().deleteRepositoryModel(latestModel)) { + info(MessageFormat.format(getString("gb.repositoryDeleted"), latestModel)); + setResponsePage(RepositoriesPage.class); + } else { + error(MessageFormat.format(getString("gb.repositoryDeleteFailed"), latestModel)); + } + } else { + error(MessageFormat.format(getString("gb.repositoryDeleteFailed"), latestModel)); + } + } + }; + + if (canDelete) { + delete.add(new JavascriptEventConfirmation("onclick", MessageFormat.format( + getString("gb.deleteRepository"), repositoryModel))); + } + form.add(delete.setVisible(canDelete)); + add(form); } diff --git a/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.html b/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.html index d5a87d3f..33345a0a 100644 --- a/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.html +++ b/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.html @@ -5,15 +5,6 @@ lang="en"> - - - - | - | [edit] - | [delete] - - - diff --git a/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java b/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java index 2c84ac06..8630d201 100644 --- a/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/ProjectRepositoryPanel.java @@ -15,7 +15,6 @@ */ package com.gitblit.wicket.panels; -import java.text.MessageFormat; import java.util.Map; import org.apache.wicket.Component; @@ -24,7 +23,6 @@ import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.link.BookmarkablePageLink; import org.apache.wicket.markup.html.link.ExternalLink; -import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.panel.Fragment; import com.gitblit.Constants.AccessRestrictionType; @@ -129,54 +127,10 @@ public class ProjectRepositoryPanel extends BasePanel { user = UserModel.ANONYMOUS; } Fragment repositoryLinks; - boolean showOwner = entry.isOwner(user.username); - // owner of personal repository gets admin powers - boolean showAdmin = isAdmin || entry.isUsersPersonalRepository(user.username); - - if (showAdmin || showOwner) { - repositoryLinks = new Fragment("repositoryLinks", showAdmin ? "repositoryAdminLinks" - : "repositoryOwnerLinks", this); + if (user.canAdmin(entry)) { + repositoryLinks = new Fragment("repositoryLinks", "repositoryOwnerLinks", this); repositoryLinks.add(new BookmarkablePageLink("editRepository", EditRepositoryPage.class, WicketUtils.newRepositoryParameter(entry.name))); - if (showAdmin) { - Link deleteLink = new Link("deleteRepository") { - - private static final long serialVersionUID = 1L; - - @Override - public void onClick() { - // refresh the model - RepositoryModel model = app().repositories().getRepositoryModel(entry.name); - if (isDeleteAllowed(model) && - app().repositories().deleteRepositoryModel(model)) { - // redirect to the owning page - if (model.isPersonalRepository()) { - setResponsePage(getPage().getClass(), WicketUtils.newUsernameParameter(model.projectPath.substring(1))); - } else { - setResponsePage(getPage().getClass(), WicketUtils.newProjectParameter(model.projectPath)); - } - } else { - error(MessageFormat.format(getString("gb.repositoryDeleteFailed"), model)); - } - } - - @Override - public boolean isEnabled() { - return isDeleteAllowed(entry); - } - - private boolean isDeleteAllowed( - final RepositoryModel model) { - return app().settings().getBoolean(Keys.web.allowDeletingNonEmptyRepositories, true) - || !model.hasCommits; - } - }; - if (deleteLink.isEnabled()) { - deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format( - localizer.getString("gb.deleteRepository", parent), entry))); - } - repositoryLinks.add(deleteLink); - } } else { repositoryLinks = new Fragment("repositoryLinks", "repositoryUserLinks", this); } diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.html b/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.html index 0cf3ef29..c3ec2e27 100644 --- a/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.html +++ b/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.html @@ -42,10 +42,6 @@ - - [edit] | [delete] - - [edit] diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java b/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java index 40cd77e6..54e2aab9 100644 --- a/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java @@ -15,7 +15,6 @@ */ package com.gitblit.wicket.panels; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -320,53 +319,7 @@ public class RepositoriesPanel extends BasePanel { WicketUtils.setHtmlTooltip(lastChangeLabel, getString("gb.author") + ": " + entry.lastChangeAuthor); } - boolean showOwner = user != null && entry.isOwner(user.username); - boolean myPersonalRepository = showOwner && entry.isUsersPersonalRepository(user.username); - if (showAdmin || myPersonalRepository) { - Fragment repositoryLinks = new Fragment("repositoryLinks", - "repositoryAdminLinks", this); - repositoryLinks.add(new BookmarkablePageLink("editRepository", - EditRepositoryPage.class, WicketUtils - .newRepositoryParameter(entry.name))); - Link deleteLink = new Link("deleteRepository") { - - private static final long serialVersionUID = 1L; - - @Override - public void onClick() { - // refresh the model - RepositoryModel model = app().repositories().getRepositoryModel(entry.name); - if (isDeleteAllowed(model) && - app().repositories().deleteRepositoryModel(model)) { - if (dp instanceof SortableRepositoriesProvider) { - info(MessageFormat.format(getString("gb.repositoryDeleted"), model)); - ((SortableRepositoriesProvider) dp).remove(model); - } else { - setResponsePage(getPage().getClass(), getPage().getPageParameters()); - } - } else { - error(MessageFormat.format(getString("gb.repositoryDeleteFailed"), model)); - } - } - - @Override - public boolean isEnabled() { - return isDeleteAllowed(entry); - } - - private boolean isDeleteAllowed( - final RepositoryModel model) { - return app().settings().getBoolean(Keys.web.allowDeletingNonEmptyRepositories, true) - || !model.hasCommits; - } - }; - if (deleteLink.isEnabled()) { - deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format( - getString("gb.deleteRepository"), entry))); - } - repositoryLinks.add(deleteLink); - row.add(repositoryLinks); - } else if (showOwner) { + if (user != null && user.canAdmin(entry)) { Fragment repositoryLinks = new Fragment("repositoryLinks", "repositoryOwnerLinks", this); repositoryLinks.add(new BookmarkablePageLink("editRepository", -- 2.39.5