Differential D20014 Diff 47778 src/applications/people/xaction/PhabricatorUserSystemAgentTransaction.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/people/xaction/PhabricatorUserSystemAgentTransaction.php
- This file was added.
<?php | |||||
final class PhabricatorUserSystemAgentTransaction | |||||
extends PhabricatorUserTransactionType { | |||||
const TRANSACTIONTYPE = 'user.system-agent'; | |||||
public function generateOldValue($object) { | |||||
return (bool)$object->getIsSystemAgent(); | |||||
} | |||||
public function generateNewValue($object, $value) { | |||||
return (bool)$value; | |||||
} | |||||
public function applyInternalEffects($object, $value) { | |||||
$object->setIsSystemAgent((int)$value); | |||||
} | |||||
public function applyExternalEffects($object, $value) { | |||||
$user = $object; | |||||
$this->newUserLog(PhabricatorUserLog::ACTION_SYSTEM_AGENT) | |||||
->setOldValue($this->getOldValue()) | |||||
->setNewValue($value) | |||||
->save(); | |||||
} | |||||
public function validateTransactions($object, array $xactions) { | |||||
$user = $object; | |||||
$actor = $this->getActor(); | |||||
$errors = array(); | |||||
if ($object->getIsMailingList()) { | |||||
$errors[] = $this->newInvalidError( | |||||
pht('Users cannot be both bots and mailing lists.')); | |||||
} | |||||
foreach ($xactions as $xaction) { | |||||
$old = $xaction->getOldValue(); | |||||
$new = $xaction->getNewValue(); | |||||
if ($old === $new) { | |||||
continue; | |||||
} | |||||
$is_admin = $actor->getIsAdmin(); | |||||
$is_omnipotent = $actor->isOmnipotent(); | |||||
if (!$is_admin && !$is_omnipotent) { | |||||
$errors[] = $this->newInvalidError( | |||||
pht('You must be an administrator to turn users into bots.'), | |||||
$xaction); | |||||
} | |||||
} | |||||
return $errors; | |||||
} | |||||
public function getTitle() { | |||||
$new = $this->getNewValue(); | |||||
if ($new) { | |||||
return pht( | |||||
'%s transformed this user into a bot.', | |||||
$this->renderAuthor()); | |||||
} else { | |||||
return pht( | |||||
'%s transformed this user from a bot into a regular user.', | |||||
$this->renderAuthor()); | |||||
} | |||||
} | |||||
public function getTitleForFeed() { | |||||
$new = $this->getNewValue(); | |||||
if ($new) { | |||||
return pht( | |||||
'%s transformed user %s into a bot.', | |||||
$this->renderAuthor(), | |||||
$this->renderObject()); | |||||
} else { | |||||
return pht( | |||||
'%s transformed %s from a bot into a regular user.', | |||||
$this->renderAuthor(), | |||||
$this->renderObject()); | |||||
} | |||||
} | |||||
public function getRequiredCapabilities( | |||||
$object, | |||||
PhabricatorApplicationTransaction $xaction) { | |||||
// Unlike normal user edits, making users into system agents requires admin | |||||
// permissions, which is enforced by validateTransactions(). | |||||
return null; | |||||
} | |||||
} |