From db89e23761f09545586cdcfbd5aadc825511119a Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 19 Dec 2012 11:07:06 -0800 Subject: [PATCH] Make Repositories partially policy-aware Summary: Small step toward repository hosting. No user-visible changes. Test Plan: Looked at repositories in Diffusion. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D4227 --- src/__phutil_library_map__.php | 8 ++- .../controller/DiffusionHomeController.php | 9 +-- .../query/PhabricatorRepositoryQuery.php | 69 +++++++++++++++++++ .../storage/PhabricatorRepository.php | 44 +++++++++++- 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/applications/repository/query/PhabricatorRepositoryQuery.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 921790611f..d196470569 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1064,6 +1064,7 @@ phutil_register_library_map(array( 'PhabricatorRepositoryMercurialCommitMessageParserWorker' => 'applications/repository/worker/commitmessageparser/PhabricatorRepositoryMercurialCommitMessageParserWorker.php', 'PhabricatorRepositoryPullLocalDaemon' => 'applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php', 'PhabricatorRepositoryPullLocalDaemonTestCase' => 'applications/repository/daemon/__tests__/PhabricatorRepositoryPullLocalDaemonTestCase.php', + 'PhabricatorRepositoryQuery' => 'applications/repository/query/PhabricatorRepositoryQuery.php', 'PhabricatorRepositoryShortcut' => 'applications/repository/storage/PhabricatorRepositoryShortcut.php', 'PhabricatorRepositorySvnCommitChangeParserWorker' => 'applications/repository/worker/commitchangeparser/PhabricatorRepositorySvnCommitChangeParserWorker.php', 'PhabricatorRepositorySvnCommitMessageParserWorker' => 'applications/repository/worker/commitmessageparser/PhabricatorRepositorySvnCommitMessageParserWorker.php', @@ -2295,7 +2296,11 @@ phutil_register_library_map(array( 'PhabricatorRemarkupRulePaste' => 'PhabricatorRemarkupRuleObjectName', 'PhabricatorRemarkupRulePhriction' => 'PhutilRemarkupRule', 'PhabricatorRemarkupRuleYoutube' => 'PhutilRemarkupRule', - 'PhabricatorRepository' => 'PhabricatorRepositoryDAO', + 'PhabricatorRepository' => + array( + 0 => 'PhabricatorRepositoryDAO', + 1 => 'PhabricatorPolicyInterface', + ), 'PhabricatorRepositoryArcanistProject' => 'PhabricatorRepositoryDAO', 'PhabricatorRepositoryArcanistProjectDeleteController' => 'PhabricatorRepositoryController', 'PhabricatorRepositoryArcanistProjectEditController' => 'PhabricatorRepositoryController', @@ -2325,6 +2330,7 @@ phutil_register_library_map(array( 'PhabricatorRepositoryMercurialCommitMessageParserWorker' => 'PhabricatorRepositoryCommitMessageParserWorker', 'PhabricatorRepositoryPullLocalDaemon' => 'PhabricatorDaemon', 'PhabricatorRepositoryPullLocalDaemonTestCase' => 'PhabricatorTestCase', + 'PhabricatorRepositoryQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorRepositoryShortcut' => 'PhabricatorRepositoryDAO', 'PhabricatorRepositorySvnCommitChangeParserWorker' => 'PhabricatorRepositoryCommitChangeParserWorker', 'PhabricatorRepositorySvnCommitMessageParserWorker' => 'PhabricatorRepositoryCommitMessageParserWorker', diff --git a/src/applications/diffusion/controller/DiffusionHomeController.php b/src/applications/diffusion/controller/DiffusionHomeController.php index 8c462afb5f..8a43e35441 100644 --- a/src/applications/diffusion/controller/DiffusionHomeController.php +++ b/src/applications/diffusion/controller/DiffusionHomeController.php @@ -42,9 +42,10 @@ final class DiffusionHomeController extends DiffusionController { $shortcut_panel = null; } - $repository = new PhabricatorRepository(); + $repositories = id(new PhabricatorRepositoryQuery()) + ->setViewer($user) + ->execute(); - $repositories = $repository->loadAll(); foreach ($repositories as $key => $repo) { if (!$repo->isTracked()) { unset($repositories[$key]); @@ -56,11 +57,11 @@ final class DiffusionHomeController extends DiffusionController { $commits = array(); if ($repository_ids) { $summaries = queryfx_all( - $repository->establishConnection('r'), + id(new PhabricatorRepository())->establishConnection('r'), 'SELECT * FROM %T WHERE repositoryID IN (%Ld)', PhabricatorRepository::TABLE_SUMMARY, $repository_ids); - $summaries = ipull($summaries, null, 'repositoryID'); + $summaries = ipull($summaries, null, 'repositoryID'); $commit_ids = array_filter(ipull($summaries, 'lastCommitID')); if ($commit_ids) { diff --git a/src/applications/repository/query/PhabricatorRepositoryQuery.php b/src/applications/repository/query/PhabricatorRepositoryQuery.php new file mode 100644 index 0000000000..52a879afd8 --- /dev/null +++ b/src/applications/repository/query/PhabricatorRepositoryQuery.php @@ -0,0 +1,69 @@ +ids = $ids; + return $this; + } + + public function withPHIDs(array $phids) { + $this->phids = $phids; + return $this; + } + + public function withCallsigns(array $callsigns) { + $this->callsigns = $callsigns; + return $this; + } + + public function loadPage() { + $table = new PhabricatorRepository(); + $conn_r = $table->establishConnection('r'); + + $data = queryfx_all( + $conn_r, + 'SELECT * FROM %T %Q %Q %Q', + $table->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildOrderClause($conn_r), + $this->buildLimitClause($conn_r)); + + return $table->loadAllFromArray($data); + } + + private function buildWhereClause(AphrontDatabaseConnection $conn_r) { + $where = array(); + + if ($this->ids) { + $where[] = qsprintf( + $conn_r, + 'id IN (%Ld)', + $this->ids); + } + + if ($this->phids) { + $where[] = qsprintf( + $conn_r, + 'phid IN (%Ls)', + $this->phids); + } + + if ($this->callsigns) { + $where[] = qsprintf( + $conn_r, + 'callsign IN (%Ls)', + $this->callsigns); + } + + $where[] = $this->buildPagingClause($conn_r); + + return $this->formatWhereClause($where); + } + +} diff --git a/src/applications/repository/storage/PhabricatorRepository.php b/src/applications/repository/storage/PhabricatorRepository.php index 7913afd315..1b2a772178 100644 --- a/src/applications/repository/storage/PhabricatorRepository.php +++ b/src/applications/repository/storage/PhabricatorRepository.php @@ -3,7 +3,8 @@ /** * @task uri Repository URI Management */ -final class PhabricatorRepository extends PhabricatorRepositoryDAO { +final class PhabricatorRepository extends PhabricatorRepositoryDAO + implements PhabricatorPolicyInterface { const TABLE_PATH = 'repository_path'; const TABLE_PATHCHANGE = 'repository_pathchange'; @@ -602,4 +603,45 @@ final class PhabricatorRepository extends PhabricatorRepositoryDAO { $this->saveTransaction(); return $result; } + + public function isGit() { + $vcs = $this->getVersionControlSystem(); + return ($vcs == PhabricatorRepositoryType::REPOSITORY_TYPE_GIT); + } + + public function isSVN() { + $vcs = $this->getVersionControlSystem(); + return ($vcs == PhabricatorRepositoryType::REPOSITORY_TYPE_SVN); + } + + public function isHg() { + $vcs = $this->getVersionControlSystem(); + return ($vcs == PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL); + } + + + +/* -( PhabricatorPolicyInterface )----------------------------------------- */ + + + public function getCapabilities() { + return array( + PhabricatorPolicyCapability::CAN_VIEW, + PhabricatorPolicyCapability::CAN_EDIT, + ); + } + + public function getPolicy($capability) { + switch ($capability) { + case PhabricatorPolicyCapability::CAN_VIEW: + return PhabricatorPolicies::POLICY_USER; + case PhabricatorPolicyCapability::CAN_EDIT: + return PhabricatorPolicies::POLICY_ADMIN; + } + } + + public function hasAutomaticCapability($capability, PhabricatorUser $user) { + return false; + } + }