Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / people / xaction / PhabricatorUserUsernameTransaction.php
blobe30a131cffd4743f251ead2ab8139cb3fe56e4ee
1 <?php
3 final class PhabricatorUserUsernameTransaction
4 extends PhabricatorUserTransactionType {
6 const TRANSACTIONTYPE = 'user.rename';
8 public function generateOldValue($object) {
9 return $object->getUsername();
12 public function generateNewValue($object, $value) {
13 return $value;
16 public function applyInternalEffects($object, $value) {
17 $object->setUsername($value);
20 public function applyExternalEffects($object, $value) {
21 $actor = $this->getActor();
22 $user = $object;
24 $old_username = $this->getOldValue();
25 $new_username = $this->getNewValue();
27 // The SSH key cache currently includes usernames, so dirty it. See T12554
28 // for discussion.
29 PhabricatorAuthSSHKeyQuery::deleteSSHKeyCache();
31 id(new PhabricatorPeopleUsernameMailEngine())
32 ->setSender($actor)
33 ->setRecipient($object)
34 ->setOldUsername($old_username)
35 ->setNewUsername($new_username)
36 ->sendMail();
39 public function getTitle() {
40 return pht(
41 '%s renamed this user from %s to %s.',
42 $this->renderAuthor(),
43 $this->renderOldValue(),
44 $this->renderNewValue());
47 public function getTitleForFeed() {
48 return pht(
49 '%s renamed %s from %s to %s.',
50 $this->renderAuthor(),
51 $this->renderObject(),
52 $this->renderOldValue(),
53 $this->renderNewValue());
56 public function validateTransactions($object, array $xactions) {
57 $actor = $this->getActor();
58 $errors = array();
60 foreach ($xactions as $xaction) {
61 $new = $xaction->getNewValue();
62 $old = $xaction->getOldValue();
64 if ($old === $new) {
65 continue;
68 if (!$actor->getIsAdmin()) {
69 $errors[] = $this->newInvalidError(
70 pht('You must be an administrator to rename users.'));
73 if (!strlen($new)) {
74 $errors[] = $this->newInvalidError(
75 pht('New username is required.'),
76 $xaction);
77 } else if (!PhabricatorUser::validateUsername($new)) {
78 $errors[] = $this->newInvalidError(
79 PhabricatorUser::describeValidUsername(),
80 $xaction);
83 $user = id(new PhabricatorPeopleQuery())
84 ->setViewer(PhabricatorUser::getOmnipotentUser())
85 ->withUsernames(array($new))
86 ->executeOne();
87 if ($user) {
88 // See T13446. We may be changing the letter case of a username, which
89 // is a perfectly fine edit.
90 $is_self = ($user->getPHID() === $object->getPHID());
91 if (!$is_self) {
92 $errors[] = $this->newInvalidError(
93 pht(
94 'Another user already has the username "%s".',
95 $new),
96 $xaction);
102 return $errors;
105 public function getRequiredCapabilities(
106 $object,
107 PhabricatorApplicationTransaction $xaction) {
109 // Unlike normal user edits, renames require admin permissions, which
110 // is enforced by validateTransactions().
112 return null;
115 public function shouldTryMFA(
116 $object,
117 PhabricatorApplicationTransaction $xaction) {
118 return true;