The Unified and Autonomous Development Platform
Add permission check for file edit and delete.
robin shinec43cf3bf92af
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.javaindex 3d9a3d9614..84293cd883 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java@@ -93,7 +93,7 @@ public abstract class GitCommand<V> implements Callable<V> {String gitExe = getGitExe();Commandline cmd = new Commandline(gitExe).workingDir(repoDir);if (environments != null)- cmd.environment(environments);+ cmd.environments(environments);return cmd;}diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java b/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.javaindex 33a6120a46..403deaf998 100644--- a/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java@@ -32,7 +32,7 @@ public class Commandline {private File workingDir;- private Map<String, String> environment = new HashMap<String, String>();+ private Map<String, String> environments = new HashMap<String, String>();public Commandline(String command) {String[] parts = StringUtils.parseQuoteTokens(command);@@ -68,9 +68,9 @@ public class Commandline {return this;}- public Commandline environment(Map<String, String> environment) {- this.environment.clear();- this.environment.putAll(environment);+ public Commandline environments(Map<String, String> environments) {+ this.environments.clear();+ this.environments.putAll(environments);return this;}@@ -95,7 +95,7 @@ public class Commandline {public Commandline clear() {executable = null;arguments.clear();- environment.clear();+ environments.clear();workingDir = null;return this;@@ -129,7 +129,7 @@ public class Commandline {ProcessBuilder processBuilder = new ProcessBuilder(command);processBuilder.directory(workingDir);- processBuilder.environment().putAll(environment);+ processBuilder.environment().putAll(environments);if (logger.isDebugEnabled()) {logger.debug("Executing command: " + this);diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/AbstractGateKeeper.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/AbstractGateKeeper.javaindex d4b459592f..c89b417307 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/AbstractGateKeeper.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/AbstractGateKeeper.java@@ -2,6 +2,8 @@ package com.pmease.gitplex.core.gatekeeper;import java.util.List;+import javax.annotation.Nullable;+import org.eclipse.jgit.lib.ObjectId;import com.pmease.commons.wicket.editable.annotation.Editable;@@ -77,7 +79,7 @@ public abstract class AbstractGateKeeper implements GateKeeper {* @return* result of the check*/- protected abstract CheckResult doCheckFile(Account user, Depot depot, String branch, String file);+ protected abstract CheckResult doCheckFile(Account user, Depot depot, String branch, @Nullable String file);/*** Check if specified user can push specified commit to specified branch, without considering enable flag.diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/GateKeeper.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/GateKeeper.javaindex 64cc8e3360..9ddba7646a 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/GateKeeper.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/GateKeeper.java@@ -2,6 +2,8 @@ package com.pmease.gitplex.core.gatekeeper;import java.io.Serializable;+import javax.annotation.Nullable;+import org.eclipse.jgit.lib.ObjectId;import com.pmease.commons.wicket.editable.annotation.Editable;@@ -38,11 +40,12 @@ public interface GateKeeper extends Serializable {* @param branch* branch to be checked* @param file- * file to be checked+ * file to be checked, use <tt>null</tt> to ignore checking of file, that is,+ * only check the branch* @return* result of the check.*/- CheckResult checkFile(Account user, Depot depot, String branch, String file);+ CheckResult checkFile(Account user, Depot depot, String branch, @Nullable String file);/*** Check if specified user can push specified commit to specified ref.diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfTouchSpecifiedFiles.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfTouchSpecifiedFiles.javaindex 1030a7e6a2..52bb2926b9 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfTouchSpecifiedFiles.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfTouchSpecifiedFiles.java@@ -59,10 +59,14 @@ public class IfTouchSpecifiedFiles extends AbstractGateKeeper {@Overrideprotected CheckResult doCheckFile(Account user, Depot depot, String branch, String file) {- if (matches(file))- return passed(Lists.newArrayList("Touched files match '" + pathMatch + "'"));- else- return failed(Lists.newArrayList("No touched files match '" + pathMatch + "'"));+ if (file != null) {+ if (matches(file))+ return passed(Lists.newArrayList("Touched files match '" + pathMatch + "'"));+ else+ return failed(Lists.newArrayList("No touched files match '" + pathMatch + "'"));+ } else {+ return ignored();+ }}@Overridediff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultAuxiliaryManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultAuxiliaryManager.javaindex 93c442bcc2..44e2eeb711 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultAuxiliaryManager.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultAuxiliaryManager.java@@ -13,11 +13,14 @@ import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;+import java.util.Optional;import java.util.Set;import java.util.Stack;import java.util.concurrent.Callable;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutionException;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.SynchronousQueue;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicReference;@@ -101,6 +104,8 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,private final UnitOfWork unitOfWork;+ private final ExecutorService executorService;+private final Map<Long, Environment> envs = new HashMap<>();private final Map<Long, List<String>> filesCache = new ConcurrentHashMap<>();@@ -111,12 +116,14 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,@Injectpublic DefaultAuxiliaryManager(DepotManager depotManager, StorageManager storageManager,- WorkManager workManager, SequentialWorkManager sequentialWorkManager, UnitOfWork unitOfWork) {+ WorkManager workManager, SequentialWorkManager sequentialWorkManager,+ UnitOfWork unitOfWork, ExecutorService executorService) {this.depotManager = depotManager;this.storageManager = storageManager;this.workManager = workManager;this.sequentialWorkManager = sequentialWorkManager;this.unitOfWork = unitOfWork;+ this.executorService = executorService;}private String getSequentialExecutorKey(Depot depot) {@@ -143,6 +150,7 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,env.executeInTransaction(new TransactionalExecutable() {+ @SuppressWarnings("unchecked")@Overridepublic void execute(final Transaction txn) {byte[] bytes = getBytes(defaultStore.get(txn, LAST_COMMIT_KEY));@@ -178,11 +186,51 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,AtomicReference<Set<String>> files = new AtomicReference<>(null);AtomicBoolean filesChanged = new AtomicBoolean(false);- log.revisions(revisions).listChangedFiles(true).run(new CommitConsumer() {+ /*+ * Use a synchronous queue to achieve below purpose:+ * 1. Add commit to Xodus transactional store in the same thread opening the transaction+ * as this is required by Xodus+ * 2. Do not pile up commits to use minimal memory+ */+ SynchronousQueue<Optional<Commit>> queue = new SynchronousQueue<>();+ executorService.execute(new Runnable() {- @SuppressWarnings("unchecked")@Override- public void consume(Commit commit) {+ public void run() {+ try {+ log.revisions(revisions).listChangedFiles(true).run(new CommitConsumer() {++ @Override+ public void consume(Commit commit) {+ try {+ queue.put(Optional.of(commit));+ } catch (InterruptedException e) {+ }+ }++ });+ } catch (Exception e) {+ logger.error("Error running git log command", e);+ } finally {+ try {+ queue.put(Optional.empty());+ } catch (InterruptedException e) {+ }+ }+ }++ });+ while (true) {+ Optional<Commit> optionalCommit = null;+ try {+ optionalCommit = queue.take();+ } catch (InterruptedException e) {+ }+ if (optionalCommit == null || !optionalCommit.isPresent())+ break;++ Commit commit = optionalCommit.get();+ try {byte[] keyBytes = new byte[20];ObjectId commitId = ObjectId.fromString(commit.getHash());commitId.copyRawTo(keyBytes, 0);@@ -224,7 +272,7 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,commitsStore.put(txn, key, new ArrayByteIterable(newValueBytes));}if (contributors.get() == null) {- byte[] bytes = getBytes(defaultStore.get(txn, CONTRIBUTORS_KEY));+ bytes = getBytes(defaultStore.get(txn, CONTRIBUTORS_KEY));if (bytes != null)contributors.set((Set<NameAndEmail>) SerializationUtils.deserialize(bytes));else@@ -238,9 +286,9 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,contributorsChanged.set(true);}}-+if (files.get() == null) {- byte[] bytes = getBytes(defaultStore.get(txn, FILES_KEY));+ bytes = getBytes(defaultStore.get(txn, FILES_KEY));if (bytes != null)files.set((Set<String>) SerializationUtils.deserialize(bytes));else@@ -249,7 +297,7 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,for (String file: commit.getChangedFiles()) {ByteIterable fileKey = new StringByteIterable(file);- byte[] bytes = getBytes(contributionsStore.get(txn, fileKey));+ bytes = getBytes(contributionsStore.get(txn, fileKey));Map<NameAndEmail, Long> fileContributions;if (bytes != null)fileContributions = (Map<NameAndEmail, Long>) SerializationUtils.deserialize(bytes);@@ -263,7 +311,7 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,if (lastContributionTime == null || lastContributionTime.longValue() < contributionTime)fileContributions.put(contributor, contributionTime);}-+bytes = SerializationUtils.serialize((Serializable) fileContributions);contributionsStore.put(txn, fileKey, new ArrayByteIterable(bytes));@@ -275,10 +323,11 @@ public class DefaultAuxiliaryManager implements AuxiliaryManager, DepotListener,if (lastCommit.get() == null)lastCommit.set(commit.getHash());- }+ }+ } catch (Exception e) {+ logger.error("Error processing commit " + commit.getHash(), e);}-- });+ }if (contributorsChanged.get()) {bytes = SerializationUtils.serialize((Serializable) contributors.get());diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/security/SecurityUtils.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/security/SecurityUtils.javaindex 7a0f34f6aa..3f080e11bd 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/security/SecurityUtils.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/security/SecurityUtils.java@@ -4,6 +4,8 @@ import java.util.Collection;import java.util.HashSet;import java.util.Set;+import javax.annotation.Nullable;+import org.eclipse.jgit.lib.ObjectId;import com.pmease.gitplex.core.GitPlex;@@ -71,6 +73,10 @@ public class SecurityUtils extends org.apache.shiro.SecurityUtils {&& currentUser.asSubject().isPermitted(ObjectPermission.ofDepotWrite(depot))&& depot.getGateKeeper().checkPush(currentUser, depot, refName, oldCommit, newCommit).isPassed();}++ public static boolean canModify(Depot depot, String branch, @Nullable String file) {+ return canWrite(depot) && depot.getGateKeeper().checkFile(getAccount(), depot, branch, file).isPassed();+ }public static boolean canManage(Account account) {return getSubject().isPermitted(ObjectPermission.ofAccountAdmin(account));diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.javaindex 4c20719c54..0ecd7167e2 100644--- a/gitplex.product/src/test/java/gitplex/product/Test.java+++ b/gitplex.product/src/test/java/gitplex/product/Test.java@@ -1,8 +1,12 @@package gitplex.product;+import java.util.concurrent.SynchronousQueue;+public class Test {@org.junit.Test- public void test() {+ public void test() throws InterruptedException {+ SynchronousQueue<String> queue = new SynchronousQueue<>();+ queue.take();}}\ No newline at end of filediff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/filenavigator/FileNavigator.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/filenavigator/FileNavigator.javaindex 933dbbc8bd..cba82fc376 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/filenavigator/FileNavigator.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/repofile/filenavigator/FileNavigator.java@@ -59,6 +59,7 @@ import com.pmease.commons.wicket.component.floating.FloatingPanel;import com.pmease.commons.wicket.component.menu.MenuItem;import com.pmease.commons.wicket.component.menu.MenuLink;import com.pmease.gitplex.core.entity.PullRequest;+import com.pmease.gitplex.core.security.SecurityUtils;import com.pmease.gitplex.web.component.BlobIcon;import com.pmease.gitplex.web.component.repofile.blobview.BlobNameChangeCallback;import com.pmease.gitplex.web.component.repofile.blobview.BlobViewContext;@@ -417,8 +418,12 @@ public abstract class FileNavigator extends Panel {});}+ String path = file.path;+ if (path != null && file.isTree())+ path += "/";+List<MenuItem> changeItems = new ArrayList<>();- if (file.isTree() && context.isOnBranch()) {+ if (file.isTree() && context.isOnBranch() && SecurityUtils.canModify(context.getDepot(), file.revision, path)) {changeItems.add(new MenuItem() {@Override@@ -453,8 +458,10 @@ public abstract class FileNavigator extends Panel {});}+ PullRequest request = context.getPullRequest();if (file.isFile() && context.getDepot().getBlob(file).getText() != null- && context.isAtSourceBranchHead()) {+ && context.isAtSourceBranchHead()+ && SecurityUtils.canModify(request.getSourceDepot(), request.getSourceBranch(), path)) {changeItems.add(new MenuItem() {@Override@@ -495,7 +502,8 @@ public abstract class FileNavigator extends Panel {});}if (file.isFile() && context.getDepot().getBlob(file).getText() != null- && context.isOnBranch()) {+ && context.isOnBranch()+ && SecurityUtils.canModify(context.getDepot(), file.revision, path)) {changeItems.add(new MenuItem() {@Override@@ -540,7 +548,8 @@ public abstract class FileNavigator extends Panel {});}- if (file.isFile() && context.isAtSourceBranchHead()) {+ if (file.isFile() && context.isAtSourceBranchHead()+ && SecurityUtils.canModify(request.getSourceDepot(), request.getSourceBranch(), path)) {changeItems.add(new MenuItem() {@Override@@ -574,7 +583,8 @@ public abstract class FileNavigator extends Panel {});}- if (file.isFile() && context.isOnBranch()) {+ if (file.isFile() && context.isOnBranch()+ && SecurityUtils.canModify(context.getDepot(), file.revision, path)) {changeItems.add(new MenuItem() {@Overridediff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/account/setting/AccountSettingPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/account/setting/AccountSettingPage.javaindex a67bb4005f..02128bb1e7 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/account/setting/AccountSettingPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/account/setting/AccountSettingPage.java@@ -28,7 +28,7 @@ public abstract class AccountSettingPage extends AccountLayoutPage {super.onInitialize();List<PageTab> tabs = new ArrayList<>();- tabs.add(new AccountTab("Profile", "", 2, ProfileEditPage.class));+ tabs.add(new AccountTab("Profile", "", 0, ProfileEditPage.class));tabs.add(new AccountTab("Avatar", "", 0, AvatarEditPage.class));if (!getAccount().isOrganization())diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/base/BasePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/base/BasePage.javaindex 9b974c20fb..236a1929cf 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/base/BasePage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/base/BasePage.java@@ -49,16 +49,19 @@ public abstract class BasePage extends CommonPage {return GitPlex.getInstance(AccountManager.class).getCurrent();}+ public void unauthorized() {+ if (getLoginUser() != null)+ throw new UnauthorizedException();+ else+ throw new RestartResponseAtInterceptPageException(LoginPage.class);+ }+@Overrideprotected void onInitialize() {super.onInitialize();- if (!isPermitted()) {- if (getLoginUser() != null)- throw new UnauthorizedException();- else- throw new RestartResponseAtInterceptPageException(LoginPage.class);- }+ if (!isPermitted())+ unauthorized();add(new Label("pageTitle", getPageTitle()));diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.javaindex 168b3665ca..baa9f7e3c6 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java@@ -60,6 +60,7 @@ import com.pmease.gitplex.core.GitPlex;import com.pmease.gitplex.core.entity.Depot;import com.pmease.gitplex.core.entity.PullRequest;import com.pmease.gitplex.core.listener.RefListener;+import com.pmease.gitplex.core.security.SecurityUtils;import com.pmease.gitplex.search.IndexListener;import com.pmease.gitplex.search.IndexManager;import com.pmease.gitplex.search.SearchManager;@@ -218,6 +219,17 @@ public class DepotFilePage extends DepotPage implements BlobViewContext {}viewState = params.get(PARAM_VIEW_STATE).toString();++ if (mode == Mode.EDIT || mode == Mode.DELETE) {+ if (!isOnBranch())+ throw new RuntimeException("Files can only be edited on branch");++ String path = blobIdent.path;+ if (path != null && blobIdent.isTree())+ path += "/";+ if (!SecurityUtils.canModify(getDepot(), blobIdent.revision, path))+ unauthorized();+ }}@Override