Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/oauthserver/editor/PhabricatorOAuthServerEditor.php
12241 views
1
<?php
2
3
final class PhabricatorOAuthServerEditor
4
extends PhabricatorApplicationTransactionEditor {
5
6
public function getEditorApplicationClass() {
7
return 'PhabricatorOAuthServerApplication';
8
}
9
10
public function getEditorObjectsDescription() {
11
return pht('OAuth Applications');
12
}
13
14
public function getTransactionTypes() {
15
$types = parent::getTransactionTypes();
16
17
$types[] = PhabricatorOAuthServerTransaction::TYPE_NAME;
18
$types[] = PhabricatorOAuthServerTransaction::TYPE_REDIRECT_URI;
19
$types[] = PhabricatorOAuthServerTransaction::TYPE_DISABLED;
20
21
$types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;
22
$types[] = PhabricatorTransactions::TYPE_EDIT_POLICY;
23
24
return $types;
25
}
26
27
protected function getCustomTransactionOldValue(
28
PhabricatorLiskDAO $object,
29
PhabricatorApplicationTransaction $xaction) {
30
31
switch ($xaction->getTransactionType()) {
32
case PhabricatorOAuthServerTransaction::TYPE_NAME:
33
return $object->getName();
34
case PhabricatorOAuthServerTransaction::TYPE_REDIRECT_URI:
35
return $object->getRedirectURI();
36
case PhabricatorOAuthServerTransaction::TYPE_DISABLED:
37
return $object->getIsDisabled();
38
}
39
}
40
41
protected function getCustomTransactionNewValue(
42
PhabricatorLiskDAO $object,
43
PhabricatorApplicationTransaction $xaction) {
44
45
switch ($xaction->getTransactionType()) {
46
case PhabricatorOAuthServerTransaction::TYPE_NAME:
47
case PhabricatorOAuthServerTransaction::TYPE_REDIRECT_URI:
48
return $xaction->getNewValue();
49
case PhabricatorOAuthServerTransaction::TYPE_DISABLED:
50
return (int)$xaction->getNewValue();
51
}
52
}
53
54
protected function applyCustomInternalTransaction(
55
PhabricatorLiskDAO $object,
56
PhabricatorApplicationTransaction $xaction) {
57
58
switch ($xaction->getTransactionType()) {
59
case PhabricatorOAuthServerTransaction::TYPE_NAME:
60
$object->setName($xaction->getNewValue());
61
return;
62
case PhabricatorOAuthServerTransaction::TYPE_REDIRECT_URI:
63
$object->setRedirectURI($xaction->getNewValue());
64
return;
65
case PhabricatorOAuthServerTransaction::TYPE_DISABLED:
66
$object->setIsDisabled($xaction->getNewValue());
67
return;
68
}
69
70
return parent::applyCustomInternalTransaction($object, $xaction);
71
}
72
73
protected function applyCustomExternalTransaction(
74
PhabricatorLiskDAO $object,
75
PhabricatorApplicationTransaction $xaction) {
76
77
switch ($xaction->getTransactionType()) {
78
case PhabricatorOAuthServerTransaction::TYPE_NAME:
79
case PhabricatorOAuthServerTransaction::TYPE_REDIRECT_URI:
80
case PhabricatorOAuthServerTransaction::TYPE_DISABLED:
81
return;
82
}
83
84
return parent::applyCustomExternalTransaction($object, $xaction);
85
}
86
87
protected function validateTransaction(
88
PhabricatorLiskDAO $object,
89
$type,
90
array $xactions) {
91
92
$errors = parent::validateTransaction($object, $type, $xactions);
93
94
switch ($type) {
95
case PhabricatorOAuthServerTransaction::TYPE_NAME:
96
$missing = $this->validateIsEmptyTextField(
97
$object->getName(),
98
$xactions);
99
100
if ($missing) {
101
$error = new PhabricatorApplicationTransactionValidationError(
102
$type,
103
pht('Required'),
104
pht('OAuth applications must have a name.'),
105
nonempty(last($xactions), null));
106
107
$error->setIsMissingFieldError(true);
108
$errors[] = $error;
109
}
110
break;
111
case PhabricatorOAuthServerTransaction::TYPE_REDIRECT_URI:
112
$missing = $this->validateIsEmptyTextField(
113
$object->getRedirectURI(),
114
$xactions);
115
if ($missing) {
116
$error = new PhabricatorApplicationTransactionValidationError(
117
$type,
118
pht('Required'),
119
pht('OAuth applications must have a valid redirect URI.'),
120
nonempty(last($xactions), null));
121
122
$error->setIsMissingFieldError(true);
123
$errors[] = $error;
124
} else {
125
foreach ($xactions as $xaction) {
126
$redirect_uri = $xaction->getNewValue();
127
128
try {
129
$server = new PhabricatorOAuthServer();
130
$server->assertValidRedirectURI($redirect_uri);
131
} catch (Exception $ex) {
132
$errors[] = new PhabricatorApplicationTransactionValidationError(
133
$type,
134
pht('Invalid'),
135
$ex->getMessage(),
136
$xaction);
137
}
138
}
139
}
140
break;
141
}
142
143
return $errors;
144
}
145
146
}
147
148