Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/storage/AlmanacInterface.php
12256 views
1
<?php
2
3
final class AlmanacInterface
4
extends AlmanacDAO
5
implements
6
PhabricatorPolicyInterface,
7
PhabricatorDestructibleInterface,
8
PhabricatorExtendedPolicyInterface,
9
PhabricatorApplicationTransactionInterface,
10
PhabricatorConduitResultInterface {
11
12
protected $devicePHID;
13
protected $networkPHID;
14
protected $address;
15
protected $port;
16
17
private $device = self::ATTACHABLE;
18
private $network = self::ATTACHABLE;
19
20
public static function initializeNewInterface() {
21
return id(new AlmanacInterface());
22
}
23
24
protected function getConfiguration() {
25
return array(
26
self::CONFIG_AUX_PHID => true,
27
self::CONFIG_COLUMN_SCHEMA => array(
28
'address' => 'text64',
29
'port' => 'uint32',
30
),
31
self::CONFIG_KEY_SCHEMA => array(
32
'key_location' => array(
33
'columns' => array('networkPHID', 'address', 'port'),
34
),
35
'key_device' => array(
36
'columns' => array('devicePHID'),
37
),
38
'key_unique' => array(
39
'columns' => array('devicePHID', 'networkPHID', 'address', 'port'),
40
'unique' => true,
41
),
42
),
43
) + parent::getConfiguration();
44
}
45
46
public function generatePHID() {
47
return PhabricatorPHID::generateNewPHID(
48
AlmanacInterfacePHIDType::TYPECONST);
49
}
50
51
public function getDevice() {
52
return $this->assertAttached($this->device);
53
}
54
55
public function attachDevice(AlmanacDevice $device) {
56
$this->device = $device;
57
return $this;
58
}
59
60
public function getNetwork() {
61
return $this->assertAttached($this->network);
62
}
63
64
public function attachNetwork(AlmanacNetwork $network) {
65
$this->network = $network;
66
return $this;
67
}
68
69
public function toAddress() {
70
return AlmanacAddress::newFromParts(
71
$this->getNetworkPHID(),
72
$this->getAddress(),
73
$this->getPort());
74
}
75
76
public function getAddressHash() {
77
return $this->toAddress()->toHash();
78
}
79
80
public function renderDisplayAddress() {
81
return $this->getAddress().':'.$this->getPort();
82
}
83
84
public function loadIsInUse() {
85
$binding = id(new AlmanacBindingQuery())
86
->setViewer(PhabricatorUser::getOmnipotentUser())
87
->withInterfacePHIDs(array($this->getPHID()))
88
->setLimit(1)
89
->executeOne();
90
91
return (bool)$binding;
92
}
93
94
95
/* -( PhabricatorPolicyInterface )----------------------------------------- */
96
97
98
public function getCapabilities() {
99
return array(
100
PhabricatorPolicyCapability::CAN_VIEW,
101
PhabricatorPolicyCapability::CAN_EDIT,
102
);
103
}
104
105
public function getPolicy($capability) {
106
return $this->getDevice()->getPolicy($capability);
107
}
108
109
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
110
return $this->getDevice()->hasAutomaticCapability($capability, $viewer);
111
}
112
113
public function describeAutomaticCapability($capability) {
114
$notes = array(
115
pht('An interface inherits the policies of the device it belongs to.'),
116
pht(
117
'You must be able to view the network an interface resides on to '.
118
'view the interface.'),
119
);
120
121
return $notes;
122
}
123
124
125
/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
126
127
128
public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
129
switch ($capability) {
130
case PhabricatorPolicyCapability::CAN_EDIT:
131
if ($this->getDevice()->isClusterDevice()) {
132
return array(
133
array(
134
new PhabricatorAlmanacApplication(),
135
AlmanacManageClusterServicesCapability::CAPABILITY,
136
),
137
);
138
}
139
break;
140
}
141
142
return array();
143
}
144
145
146
/* -( PhabricatorDestructibleInterface )----------------------------------- */
147
148
149
public function destroyObjectPermanently(
150
PhabricatorDestructionEngine $engine) {
151
152
$bindings = id(new AlmanacBindingQuery())
153
->setViewer($engine->getViewer())
154
->withInterfacePHIDs(array($this->getPHID()))
155
->execute();
156
foreach ($bindings as $binding) {
157
$engine->destroyObject($binding);
158
}
159
160
$this->delete();
161
}
162
163
164
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
165
166
167
public function getApplicationTransactionEditor() {
168
return new AlmanacInterfaceEditor();
169
}
170
171
public function getApplicationTransactionTemplate() {
172
return new AlmanacInterfaceTransaction();
173
}
174
175
176
/* -( PhabricatorConduitResultInterface )---------------------------------- */
177
178
179
public function getFieldSpecificationsForConduit() {
180
return array(
181
id(new PhabricatorConduitSearchFieldSpecification())
182
->setKey('devicePHID')
183
->setType('phid')
184
->setDescription(pht('The device the interface is on.')),
185
id(new PhabricatorConduitSearchFieldSpecification())
186
->setKey('networkPHID')
187
->setType('phid')
188
->setDescription(pht('The network the interface is part of.')),
189
id(new PhabricatorConduitSearchFieldSpecification())
190
->setKey('address')
191
->setType('string')
192
->setDescription(pht('The address of the interface.')),
193
id(new PhabricatorConduitSearchFieldSpecification())
194
->setKey('port')
195
->setType('int')
196
->setDescription(pht('The port number of the interface.')),
197
);
198
}
199
200
public function getFieldValuesForConduit() {
201
return array(
202
'devicePHID' => $this->getDevicePHID(),
203
'networkPHID' => $this->getNetworkPHID(),
204
'address' => (string)$this->getAddress(),
205
'port' => (int)$this->getPort(),
206
);
207
}
208
209
public function getConduitSearchAttachments() {
210
return array();
211
}
212
213
}
214
215