Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/xaction/AlmanacBindingInterfaceTransaction.php
12256 views
1
<?php
2
3
final class AlmanacBindingInterfaceTransaction
4
extends AlmanacBindingTransactionType {
5
6
const TRANSACTIONTYPE = 'almanac:binding:interface';
7
8
public function generateOldValue($object) {
9
return $object->getInterfacePHID();
10
}
11
12
public function applyInternalEffects($object, $value) {
13
$interface = $this->loadInterface($value);
14
15
$object
16
->setDevicePHID($interface->getDevicePHID())
17
->setInterfacePHID($interface->getPHID());
18
}
19
20
public function applyExternalEffects($object, $value) {
21
22
// When we change which services a device is bound to, we need to
23
// recalculate whether it is a cluster device or not so we can tell if
24
// the "Can Manage Cluster Services" permission applies to it.
25
26
$viewer = PhabricatorUser::getOmnipotentUser();
27
$interface_phids = array();
28
29
$interface_phids[] = $this->getOldValue();
30
$interface_phids[] = $this->getNewValue();
31
32
$interface_phids = array_filter($interface_phids);
33
$interface_phids = array_unique($interface_phids);
34
35
$interfaces = id(new AlmanacInterfaceQuery())
36
->setViewer($viewer)
37
->withPHIDs($interface_phids)
38
->execute();
39
40
$device_phids = array();
41
foreach ($interfaces as $interface) {
42
$device_phids[] = $interface->getDevicePHID();
43
}
44
45
$device_phids = array_unique($device_phids);
46
47
$devices = id(new AlmanacDeviceQuery())
48
->setViewer($viewer)
49
->withPHIDs($device_phids)
50
->execute();
51
52
foreach ($devices as $device) {
53
$device->rebuildClusterBindingStatus();
54
}
55
}
56
57
public function getTitle() {
58
if ($this->getOldValue() === null) {
59
return pht(
60
'%s set the interface for this binding to %s.',
61
$this->renderAuthor(),
62
$this->renderNewHandle());
63
} else if ($this->getNewValue() == null) {
64
return pht(
65
'%s removed the interface for this binding.',
66
$this->renderAuthor());
67
} else {
68
return pht(
69
'%s changed the interface for this binding from %s to %s.',
70
$this->renderAuthor(),
71
$this->renderOldHandle(),
72
$this->renderNewHandle());
73
}
74
}
75
76
public function validateTransactions($object, array $xactions) {
77
$errors = array();
78
79
$interface_phid = $object->getInterfacePHID();
80
if ($this->isEmptyTextTransaction($interface_phid, $xactions)) {
81
$errors[] = $this->newRequiredError(
82
pht('Bindings must specify an interface.'));
83
}
84
85
foreach ($xactions as $xaction) {
86
$interface_phid = $xaction->getNewValue();
87
88
$interface = $this->loadInterface($interface_phid);
89
if (!$interface) {
90
$errors[] = $this->newInvalidError(
91
pht(
92
'You can not bind a service to an invalid or restricted '.
93
'interface.'),
94
$xaction);
95
continue;
96
}
97
98
$binding = id(new AlmanacBindingQuery())
99
->setViewer(PhabricatorUser::getOmnipotentUser())
100
->withServicePHIDs(array($object->getServicePHID()))
101
->withInterfacePHIDs(array($interface_phid))
102
->executeOne();
103
if ($binding && ($binding->getID() != $object->getID())) {
104
$errors[] = $this->newInvalidError(
105
pht(
106
'You can not bind a service to the same interface multiple '.
107
'times.'),
108
$xaction);
109
continue;
110
}
111
}
112
113
return $errors;
114
}
115
116
private function loadInterface($phid) {
117
return id(new AlmanacInterfaceQuery())
118
->setViewer($this->getActor())
119
->withPHIDs(array($phid))
120
->executeOne();
121
}
122
}
123
124