Page MenuHomec4science

RepositoryAuthorPolicyRule.php
No OneTemporary

File Metadata

Created
Sun, Dec 22, 21:31

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
|| $object instanceof PhabricatorRepositoryCommit
|| $object instanceof PhabricatorRepositoryPullEvent
|| $object instanceof PhabricatorRepositoryPushEvent
|| $object instanceof PhabricatorRepositoryPushLog
|| $object instanceof PhabricatorRepositoryRefCursor
|| $object instanceof HarbormasterBuildable
|| $object instanceof HarbormasterBuild
);
}
public function applyRule(
PhabricatorUser $viewer,
$value,
PhabricatorPolicyInterface $object) {
$viewer_phid = $viewer->getPHID();
if (!$viewer_phid) {
return false;
}
// Get repo phid when accessing a commit
if($object instanceof PhabricatorRepository){
$repo_phid = $object->getPHID();
} else if($object instanceof HarbormasterBuildable) {
$repo_phid = $object->getHarbormasterContainerPHID();
} else if($object instanceof HarbormasterBuild) {
$repo_phid = $object->getBuildable()->getHarbormasterContainerPHID();
} else {
$repo_phid = $object->getRepository()->getPHID();
}
// If the repository doesn't exist, we allow the user to create it
if(!$repo_phid){
return true;
}
// Get all repositories the user has created
$repo_transaction = id(new PhabricatorRepositoryTransactionQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withAuthorPHIDs(array($viewer_phid))
->withObjectPHIDs(array($repo_phid))
->withTransactionTypes(array(PhabricatorTransactions::TYPE_CREATE))
->executeOne();
if(!empty($repo_transaction)) {
return true;
}
return false;
}
public function getValueControlType() {
return self::CONTROL_TYPE_NONE;
}
}

Event Timeline