Page MenuHomec4science

RepositoryAuthorPolicyRule.php
No OneTemporary

File Metadata

Created
Wed, May 29, 02:16

RepositoryAuthorPolicyRule.php

<?php
final class RepositoryAuthorPolicyRule
extends PhabricatorPolicyRule {
public function getObjectPolicyKey() {
return 'repository.author';
}
public function getObjectPolicyName() {
return pht('Repository Author');
}
public function getPolicyExplanation() {
return pht('The author of this repository can take this action.');
}
public function getRuleDescription() {
return pht('repository author');
}
public function canApplyToObject(PhabricatorPolicyInterface $object) {
phlog($object);
return ($object instanceof PhabricatorRepository);
}
public function applyRule(
PhabricatorUser $viewer,
$value,
PhabricatorPolicyInterface $object) {
$viewer_phid = $viewer->getPHID();
if (!$viewer_phid) {
return false;
}
// If the repository doesn't exist, we allow the user to create it
$repo_phid = $object->getPHID();
if(!$repo_phid){
return true;
}
// Get all repositories the user has created
$repo_transaction = id(new PhabricatorRepositoryTransactionQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withAuthorPHIDs(array($viewer_phid))
->withTransactionTypes(array(PhabricatorTransactions::TYPE_CREATE))
->execute();
// Check if the current repository has been created by the user
if(!empty($repo_transaction)) {
$repo_phids = mpull($repo_transaction, 'getObjectPHID');
if(in_array($repo_phid, $repo_phids, true) == true){
return true;
}
}
return false;
}
public function getValueControlType() {
return self::CONTROL_TYPE_NONE;
}
}

Event Timeline