Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/xaction/AlmanacInterfaceDeviceTransaction.php
12256 views
1
<?php
2
3
final class AlmanacInterfaceDeviceTransaction
4
extends AlmanacInterfaceTransactionType {
5
6
const TRANSACTIONTYPE = 'almanac:interface:device';
7
8
public function generateOldValue($object) {
9
return $object->getDevicePHID();
10
}
11
12
public function applyInternalEffects($object, $value) {
13
$object->setDevicePHID($value);
14
}
15
16
public function getTitle() {
17
return pht(
18
'%s changed the device for this interface from %s to %s.',
19
$this->renderAuthor(),
20
$this->renderOldHandle(),
21
$this->renderNewHandle());
22
}
23
24
public function validateTransactions($object, array $xactions) {
25
$errors = array();
26
27
$device_phid = $object->getDevicePHID();
28
if ($this->isEmptyTextTransaction($device_phid, $xactions)) {
29
$errors[] = $this->newRequiredError(
30
pht('Interfaces must have a device.'));
31
}
32
33
foreach ($xactions as $xaction) {
34
if (!$this->isNewObject()) {
35
$errors[] = $this->newInvalidError(
36
pht(
37
'The device for an interface can not be changed once it has '.
38
'been created.'),
39
$xaction);
40
continue;
41
}
42
43
$device_phid = $xaction->getNewValue();
44
$devices = id(new AlmanacDeviceQuery())
45
->setViewer($this->getActor())
46
->withPHIDs(array($device_phid))
47
->execute();
48
if (!$devices) {
49
$errors[] = $this->newInvalidError(
50
pht(
51
'You can not attach an interface to a nonexistent or restricted '.
52
'device.'),
53
$xaction);
54
continue;
55
}
56
57
$device = head($devices);
58
$can_edit = PhabricatorPolicyFilter::hasCapability(
59
$this->getActor(),
60
$device,
61
PhabricatorPolicyCapability::CAN_EDIT);
62
if (!$can_edit) {
63
$errors[] = $this->newInvalidError(
64
pht(
65
'You can not attach an interface to a device which you do not '.
66
'have permission to edit.'));
67
continue;
68
}
69
}
70
71
return $errors;
72
}
73
74
}
75
76