Skip to content

Commit a1df1f2

Browse files
author
epriestley
committed
Allow projects to be set as policies
Summary: - Renames `PhabricatorPolicyQuery` to `PhabricatorPolicyAwareQuery` (a query which respects policy settings). - Introduces `PhabricatorPolicyQuery`, which loads available policies (e.g., "member of project X"). - Introduces `PhabricatorPolicy`, which describes a policy. - Allows projects to be set as policies. - Allows Paste policies to be edited. - Covers crazy cases where you make projects depend on themselves or each other because you are a dastardly villan. Test Plan: Set paste and project policies, including crazy policies like A -> B -> A, A -> A, etc. Reviewers: vrana, btrahan Reviewed By: vrana CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D3476
1 parent b391753 commit a1df1f2

20 files changed

+525
-73
lines changed

src/__phutil_library_map__.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@
626626
'PhabricatorCountdownEditController' => 'applications/countdown/controller/PhabricatorCountdownEditController.php',
627627
'PhabricatorCountdownListController' => 'applications/countdown/controller/PhabricatorCountdownListController.php',
628628
'PhabricatorCountdownViewController' => 'applications/countdown/controller/PhabricatorCountdownViewController.php',
629-
'PhabricatorCursorPagedPolicyQuery' => 'infrastructure/query/policy/PhabricatorCursorPagedPolicyQuery.php',
629+
'PhabricatorCursorPagedPolicyAwareQuery' => 'infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php',
630630
'PhabricatorDaemon' => 'infrastructure/daemon/PhabricatorDaemon.php',
631631
'PhabricatorDaemonCombinedLogController' => 'applications/daemon/controller/PhabricatorDaemonCombinedLogController.php',
632632
'PhabricatorDaemonConsoleController' => 'applications/daemon/controller/PhabricatorDaemonConsoleController.php',
@@ -909,15 +909,18 @@
909909
'PhabricatorPeopleProfileController' => 'applications/people/controller/PhabricatorPeopleProfileController.php',
910910
'PhabricatorPeopleQuery' => 'applications/people/PhabricatorPeopleQuery.php',
911911
'PhabricatorPolicies' => 'applications/policy/constants/PhabricatorPolicies.php',
912+
'PhabricatorPolicy' => 'applications/policy/filter/PhabricatorPolicy.php',
913+
'PhabricatorPolicyAwareQuery' => 'infrastructure/query/policy/PhabricatorPolicyAwareQuery.php',
914+
'PhabricatorPolicyAwareTestQuery' => 'applications/policy/__tests__/PhabricatorPolicyAwareTestQuery.php',
912915
'PhabricatorPolicyCapability' => 'applications/policy/constants/PhabricatorPolicyCapability.php',
913916
'PhabricatorPolicyConstants' => 'applications/policy/constants/PhabricatorPolicyConstants.php',
914917
'PhabricatorPolicyException' => 'applications/policy/exception/PhabricatorPolicyException.php',
915918
'PhabricatorPolicyFilter' => 'applications/policy/filter/PhabricatorPolicyFilter.php',
916919
'PhabricatorPolicyInterface' => 'applications/policy/interface/PhabricatorPolicyInterface.php',
917-
'PhabricatorPolicyQuery' => 'infrastructure/query/policy/PhabricatorPolicyQuery.php',
920+
'PhabricatorPolicyQuery' => 'applications/policy/query/PhabricatorPolicyQuery.php',
918921
'PhabricatorPolicyTestCase' => 'applications/policy/__tests__/PhabricatorPolicyTestCase.php',
919922
'PhabricatorPolicyTestObject' => 'applications/policy/__tests__/PhabricatorPolicyTestObject.php',
920-
'PhabricatorPolicyTestQuery' => 'applications/policy/__tests__/PhabricatorPolicyTestQuery.php',
923+
'PhabricatorPolicyType' => 'applications/policy/constants/PhabricatorPolicyType.php',
921924
'PhabricatorProfileHeaderView' => 'view/layout/PhabricatorProfileHeaderView.php',
922925
'PhabricatorProject' => 'applications/project/storage/PhabricatorProject.php',
923926
'PhabricatorProjectConstants' => 'applications/project/constants/PhabricatorProjectConstants.php',
@@ -1753,7 +1756,7 @@
17531756
1 => 'PhabricatorPolicyInterface',
17541757
),
17551758
'PhabricatorChatLogEventType' => 'PhabricatorChatLogConstants',
1756-
'PhabricatorChatLogQuery' => 'PhabricatorCursorPagedPolicyQuery',
1759+
'PhabricatorChatLogQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
17571760
'PhabricatorConduitAPIController' => 'PhabricatorConduitController',
17581761
'PhabricatorConduitCertificateToken' => 'PhabricatorConduitDAO',
17591762
'PhabricatorConduitConnectionLog' => 'PhabricatorConduitDAO',
@@ -1772,7 +1775,7 @@
17721775
'PhabricatorCountdownEditController' => 'PhabricatorCountdownController',
17731776
'PhabricatorCountdownListController' => 'PhabricatorCountdownController',
17741777
'PhabricatorCountdownViewController' => 'PhabricatorCountdownController',
1775-
'PhabricatorCursorPagedPolicyQuery' => 'PhabricatorPolicyQuery',
1778+
'PhabricatorCursorPagedPolicyAwareQuery' => 'PhabricatorPolicyAwareQuery',
17761779
'PhabricatorDaemon' => 'PhutilDaemon',
17771780
'PhabricatorDaemonCombinedLogController' => 'PhabricatorDaemonController',
17781781
'PhabricatorDaemonConsoleController' => 'PhabricatorDaemonController',
@@ -1828,7 +1831,7 @@
18281831
'PhabricatorFeedController' => 'PhabricatorController',
18291832
'PhabricatorFeedDAO' => 'PhabricatorLiskDAO',
18301833
'PhabricatorFeedPublicStreamController' => 'PhabricatorFeedController',
1831-
'PhabricatorFeedQuery' => 'PhabricatorCursorPagedPolicyQuery',
1834+
'PhabricatorFeedQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
18321835
'PhabricatorFeedStory' => 'PhabricatorPolicyInterface',
18331836
'PhabricatorFeedStoryAggregate' => 'PhabricatorFeedStory',
18341837
'PhabricatorFeedStoryAudit' => 'PhabricatorFeedStory',
@@ -1996,7 +1999,7 @@
19961999
0 => 'PhabricatorOwnersDAO',
19972000
1 => 'PhabricatorPolicyInterface',
19982001
),
1999-
'PhabricatorOwnersPackageQuery' => 'PhabricatorCursorPagedPolicyQuery',
2002+
'PhabricatorOwnersPackageQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
20002003
'PhabricatorOwnersPath' => 'PhabricatorOwnersDAO',
20012004
'PhabricatorPHIDController' => 'PhabricatorController',
20022005
'PhabricatorPHIDLookupController' => 'PhabricatorPHIDController',
@@ -2009,7 +2012,7 @@
20092012
'PhabricatorPasteDAO' => 'PhabricatorLiskDAO',
20102013
'PhabricatorPasteEditController' => 'PhabricatorPasteController',
20112014
'PhabricatorPasteListController' => 'PhabricatorPasteController',
2012-
'PhabricatorPasteQuery' => 'PhabricatorCursorPagedPolicyQuery',
2015+
'PhabricatorPasteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
20132016
'PhabricatorPasteViewController' => 'PhabricatorPasteController',
20142017
'PhabricatorPeopleController' => 'PhabricatorController',
20152018
'PhabricatorPeopleEditController' => 'PhabricatorPeopleController',
@@ -2019,12 +2022,14 @@
20192022
'PhabricatorPeopleProfileController' => 'PhabricatorPeopleController',
20202023
'PhabricatorPeopleQuery' => 'PhabricatorOffsetPagedQuery',
20212024
'PhabricatorPolicies' => 'PhabricatorPolicyConstants',
2025+
'PhabricatorPolicyAwareQuery' => 'PhabricatorOffsetPagedQuery',
2026+
'PhabricatorPolicyAwareTestQuery' => 'PhabricatorPolicyAwareQuery',
20222027
'PhabricatorPolicyCapability' => 'PhabricatorPolicyConstants',
20232028
'PhabricatorPolicyException' => 'Exception',
2024-
'PhabricatorPolicyQuery' => 'PhabricatorOffsetPagedQuery',
2029+
'PhabricatorPolicyQuery' => 'PhabricatorQuery',
20252030
'PhabricatorPolicyTestCase' => 'PhabricatorTestCase',
20262031
'PhabricatorPolicyTestObject' => 'PhabricatorPolicyInterface',
2027-
'PhabricatorPolicyTestQuery' => 'PhabricatorPolicyQuery',
2032+
'PhabricatorPolicyType' => 'PhabricatorPolicyConstants',
20282033
'PhabricatorProfileHeaderView' => 'AphrontView',
20292034
'PhabricatorProject' =>
20302035
array(
@@ -2041,7 +2046,7 @@
20412046
'PhabricatorProjectProfile' => 'PhabricatorProjectDAO',
20422047
'PhabricatorProjectProfileController' => 'PhabricatorProjectController',
20432048
'PhabricatorProjectProfileEditController' => 'PhabricatorProjectController',
2044-
'PhabricatorProjectQuery' => 'PhabricatorCursorPagedPolicyQuery',
2049+
'PhabricatorProjectQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
20452050
'PhabricatorProjectTransaction' => 'PhabricatorProjectDAO',
20462051
'PhabricatorProjectTransactionType' => 'PhabricatorProjectConstants',
20472052
'PhabricatorProjectUpdateController' => 'PhabricatorProjectController',

src/applications/chatlog/PhabricatorChatLogQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
final class PhabricatorChatLogQuery extends PhabricatorCursorPagedPolicyQuery {
19+
final class PhabricatorChatLogQuery
20+
extends PhabricatorCursorPagedPolicyAwareQuery {
2021

2122
private $channels;
2223
private $maximumEpoch;

src/applications/chatlog/storage/PhabricatorChatLogEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getCapabilities() {
3535

3636
public function getPolicy($capability) {
3737
// TODO: This is sort of silly and mostly just so that we can use
38-
// CursorPagedPolicyQuery; once we implement Channel objects we should
38+
// CursorPagedPolicyAwareQuery; once we implement Channel objects we should
3939
// just delegate policy to them.
4040
return PhabricatorPolicies::POLICY_PUBLIC;
4141
}

src/applications/feed/PhabricatorFeedQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
final class PhabricatorFeedQuery extends PhabricatorCursorPagedPolicyQuery {
19+
final class PhabricatorFeedQuery
20+
extends PhabricatorCursorPagedPolicyAwareQuery {
2021

2122
private $filterPHIDs;
2223

src/applications/owners/query/PhabricatorOwnersPackageQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
final class PhabricatorOwnersPackageQuery
20-
extends PhabricatorCursorPagedPolicyQuery {
20+
extends PhabricatorCursorPagedPolicyAwareQuery {
2121

2222
private $ownerPHIDs;
2323

src/applications/paste/controller/PhabricatorPasteEditController.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public function processRequest() {
8787

8888
$paste->setTitle($request->getStr('title'));
8989
$paste->setLanguage($request->getStr('language'));
90+
$paste->setViewPolicy($request->getStr('can_view'));
91+
92+
// NOTE: The author is the only editor and can always view the paste,
93+
// so it's impossible for them to choose an invalid policy.
9094

9195
if (!$errors) {
9296
if ($is_create) {
@@ -139,6 +143,19 @@ public function processRequest() {
139143
->setValue($paste->getLanguage())
140144
->setOptions($langs));
141145

146+
$policies = id(new PhabricatorPolicyQuery())
147+
->setViewer($user)
148+
->setObject($paste)
149+
->execute();
150+
151+
$form->appendChild(
152+
id(new AphrontFormPolicyControl())
153+
->setUser($user)
154+
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
155+
->setPolicyObject($paste)
156+
->setPolicies($policies)
157+
->setName('can_view'));
158+
142159
if ($is_create) {
143160
$form
144161
->appendChild(
@@ -151,16 +168,6 @@ public function processRequest() {
151168
->setName('text'));
152169
}
153170

154-
/* TODO: Doesn't have any useful options yet.
155-
->appendChild(
156-
id(new AphrontFormPolicyControl())
157-
->setLabel('Visible To')
158-
->setUser($user)
159-
->setValue(
160-
$new_paste->getPolicy(PhabricatorPolicyCapability::CAN_VIEW))
161-
->setName('policy'))
162-
*/
163-
164171
$submit = new AphrontFormSubmitControl();
165172

166173
if (!$is_create) {

src/applications/paste/controller/PhabricatorPasteViewController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ private function buildPropertyView(
147147
$this->renderHandlesForPHIDs($child_phids));
148148
}
149149

150+
$descriptions = PhabricatorPolicyQuery::renderPolicyDescriptions(
151+
$user,
152+
$paste);
153+
154+
$properties->addProperty(
155+
pht('Visible To'),
156+
$descriptions[PhabricatorPolicyCapability::CAN_VIEW]);
157+
150158
return $properties;
151159
}
152160

src/applications/paste/query/PhabricatorPasteQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
final class PhabricatorPasteQuery extends PhabricatorCursorPagedPolicyQuery {
19+
final class PhabricatorPasteQuery
20+
extends PhabricatorCursorPagedPolicyAwareQuery {
2021

2122
private $ids;
2223
private $phids;

src/applications/phid/handle/PhabricatorObjectHandleData.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,15 @@ public function loadHandles() {
345345
case PhabricatorPHIDConstants::PHID_TYPE_PROJ:
346346
$object = new PhabricatorProject();
347347

348-
$projects = $object->loadAllWhere('phid IN (%Ls)', $phids);
348+
if ($this->viewer) {
349+
$projects = id(new PhabricatorProjectQuery())
350+
->setViewer($this->viewer)
351+
->withPHIDs($phids)
352+
->execute();
353+
} else {
354+
$projects = $object->loadAllWhere('phid IN (%Ls)', $phids);
355+
}
356+
349357
$projects = mpull($projects, null, 'getPHID');
350358

351359
foreach ($phids as $phid) {

src/applications/policy/__tests__/PhabricatorPolicyTestQuery.php renamed to src/applications/policy/__tests__/PhabricatorPolicyAwareTestQuery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
/**
2020
* Configurable test query for implementing Policy unit tests.
2121
*/
22-
final class PhabricatorPolicyTestQuery
23-
extends PhabricatorPolicyQuery {
22+
final class PhabricatorPolicyAwareTestQuery
23+
extends PhabricatorPolicyAwareQuery {
2424

2525
private $results;
2626
private $offset = 0;

0 commit comments

Comments
 (0)