Skip to content

Commit 8f715d8

Browse files
author
epriestley
committed
Add a credential selection control to Passphrase
Summary: Ref T4122. Adds a control for choosing credentials. Test Plan: See screenshots. Reviewers: btrahan Reviewed By: btrahan CC: aran, lsave Maniphest Tasks: T4122 Differential Revision: https://secure.phabricator.com/D7617
1 parent 7b718bb commit 8f715d8

File tree

6 files changed

+185
-9
lines changed

6 files changed

+185
-9
lines changed

src/__celerity_resource_map__.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,20 @@
19801980
),
19811981
'disk' => '/rsrc/js/application/owners/owners-path-editor.js',
19821982
),
1983+
'javelin-behavior-passphrase-credential-control' =>
1984+
array(
1985+
'uri' => '/res/b599c028/rsrc/js/application/passphrase/phame-credential-control.js',
1986+
'type' => 'js',
1987+
'requires' =>
1988+
array(
1989+
0 => 'javelin-behavior',
1990+
1 => 'javelin-dom',
1991+
2 => 'javelin-stratcom',
1992+
3 => 'javelin-workflow',
1993+
4 => 'javelin-util',
1994+
),
1995+
'disk' => '/rsrc/js/application/passphrase/phame-credential-control.js',
1996+
),
19831997
'javelin-behavior-persona-login' =>
19841998
array(
19851999
'uri' => '/res/128fdf56/rsrc/js/application/auth/behavior-persona-login.js',

src/__phutil_library_map__.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@
946946
'PackageModifyMail' => 'applications/owners/mail/PackageModifyMail.php',
947947
'PassphraseController' => 'applications/passphrase/controller/PassphraseController.php',
948948
'PassphraseCredential' => 'applications/passphrase/storage/PassphraseCredential.php',
949+
'PassphraseCredentialControl' => 'applications/passphrase/view/PassphraseCredentialControl.php',
949950
'PassphraseCredentialCreateController' => 'applications/passphrase/controller/PassphraseCredentialCreateController.php',
950951
'PassphraseCredentialDestroyController' => 'applications/passphrase/controller/PassphraseCredentialDestroyController.php',
951952
'PassphraseCredentialEditController' => 'applications/passphrase/controller/PassphraseCredentialEditController.php',
@@ -3329,6 +3330,7 @@
33293330
0 => 'PassphraseDAO',
33303331
1 => 'PhabricatorPolicyInterface',
33313332
),
3333+
'PassphraseCredentialControl' => 'AphrontFormControl',
33323334
'PassphraseCredentialCreateController' => 'PassphraseController',
33333335
'PassphraseCredentialDestroyController' => 'PassphraseController',
33343336
'PassphraseCredentialEditController' => 'PassphraseController',

src/applications/passphrase/controller/PassphraseCredentialEditController.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,16 @@ public function processRequest() {
129129

130130
$credential->saveTransaction();
131131

132-
return id(new AphrontRedirectResponse())
133-
->setURI('/K'.$credential->getID());
132+
if ($request->isAjax()) {
133+
return id(new AphrontAjaxResponse())->setContent(
134+
array(
135+
'phid' => $credential->getPHID(),
136+
'name' => 'K'.$credential->getID().' '.$credential->getName(),
137+
));
138+
} else {
139+
return id(new AphrontRedirectResponse())
140+
->setURI('/K'.$credential->getID());
141+
}
134142
} catch (PhabricatorApplicationTransactionValidationException $ex) {
135143
$credential->killTransaction();
136144

@@ -151,8 +159,14 @@ public function processRequest() {
151159

152160
$secret_control = $type->newSecretControl();
153161

154-
$form = id(new AphrontFormView())
155-
->setUser($viewer)
162+
if ($request->isAjax()) {
163+
$form = new PHUIFormLayoutView();
164+
} else {
165+
$form = id(new AphrontFormView())
166+
->setUser($viewer);
167+
}
168+
169+
$form
156170
->appendChild(
157171
id(new AphrontFormTextControl())
158172
->setName('name')
@@ -197,11 +211,6 @@ public function processRequest() {
197211
->setLabel($type->getSecretLabel())
198212
->setValue($v_secret));
199213

200-
$form->appendChild(
201-
id(new AphrontFormSubmitControl())
202-
->setValue(pht('Save'))
203-
->addCancelButton($this->getApplicationURI()));
204-
205214
$crumbs = $this->buildApplicationCrumbs();
206215

207216
if ($is_new) {
@@ -222,6 +231,23 @@ public function processRequest() {
222231
->setName(pht('Edit')));
223232
}
224233

234+
if ($request->isAjax()) {
235+
$dialog = id(new AphrontDialogView())
236+
->setUser($viewer)
237+
->setWidth(AphrontDialogView::WIDTH_FORM)
238+
->setTitle($title)
239+
->appendChild($form)
240+
->addSubmitButton(pht('Create Credential'))
241+
->addCancelButton($this->getApplicationURI());
242+
243+
return id(new AphrontDialogResponse())->setDialog($dialog);
244+
}
245+
246+
$form->appendChild(
247+
id(new AphrontFormSubmitControl())
248+
->setValue(pht('Save'))
249+
->addCancelButton($this->getApplicationURI()));
250+
225251
$box = id(new PHUIObjectBoxView())
226252
->setHeaderText($header)
227253
->setValidationException($validation_exception)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
final class PassphraseCredentialControl extends AphrontFormControl {
4+
5+
private $options;
6+
private $credentialType;
7+
8+
public function setCredentialType($credential_type) {
9+
$this->credentialType = $credential_type;
10+
return $this;
11+
}
12+
13+
public function getCredentialType() {
14+
return $this->credentialType;
15+
}
16+
17+
public function setOptions(array $options) {
18+
assert_instances_of($options, 'PassphraseCredential');
19+
$this->options = $options;
20+
return $this;
21+
}
22+
23+
protected function getCustomControlClass() {
24+
return 'passphrase-credential-control';
25+
}
26+
27+
protected function renderInput() {
28+
29+
$options_map = array();
30+
foreach ($this->options as $option) {
31+
$options_map[$option->getPHID()] = pht(
32+
"%s %s",
33+
'K'.$option->getID(),
34+
$option->getName());
35+
}
36+
37+
$disabled = $this->getDisabled();
38+
if (!$options_map) {
39+
$options_map[''] = pht('(No Existing Credentials)');
40+
$disabled = true;
41+
}
42+
43+
Javelin::initBehavior('passphrase-credential-control');
44+
45+
$options = AphrontFormSelectControl::renderSelectTag(
46+
$this->getValue(),
47+
$options_map,
48+
array(
49+
'id' => $this->getControlID(),
50+
'name' => $this->getName(),
51+
'disabled' => $disabled ? 'disabled' : null,
52+
'sigil' => 'passphrase-credential-select',
53+
));
54+
55+
$button = javelin_tag(
56+
'a',
57+
array(
58+
'href' => '#',
59+
'class' => 'button grey',
60+
'sigil' => 'passphrase-credential-add',
61+
'mustcapture' => true,
62+
),
63+
pht('Add Credential'));
64+
65+
return javelin_tag(
66+
'div',
67+
array(
68+
'sigil' => 'passphrase-credential-control',
69+
'meta' => array(
70+
'type' => $this->getCredentialType(),
71+
),
72+
),
73+
array(
74+
$options,
75+
$button,
76+
));
77+
}
78+
79+
}

src/applications/uiexample/examples/PhabricatorFormExample.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,22 @@ public function renderExample() {
3838
$null_value = $null_time->readValueFromRequest($request);
3939
}
4040

41+
$divider_control = new AphrontFormDividerControl();
42+
43+
$credentials = array();
44+
$password_control = id(new PassphraseCredentialControl())
45+
->setName('credentialPHID')
46+
->setLabel(pht('Password'))
47+
->setCredentialType('password')
48+
->setOptions($credentials);
49+
4150
$form = id(new AphrontFormView())
4251
->setUser($user)
4352
->appendChild($start_time)
4453
->appendChild($end_time)
4554
->appendChild($null_time)
55+
->appendChild($divider_control)
56+
->appendChild($password_control)
4657
->appendChild(
4758
id(new AphrontFormSubmitControl())
4859
->setValue('Submit'));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @provides javelin-behavior-passphrase-credential-control
3+
* @requires javelin-behavior
4+
* javelin-dom
5+
* javelin-stratcom
6+
* javelin-workflow
7+
* javelin-util
8+
*/
9+
10+
JX.behavior('passphrase-credential-control', function(config) {
11+
12+
JX.Stratcom.listen(
13+
'click',
14+
'passphrase-credential-add',
15+
function(e) {
16+
var control = e.getNode('passphrase-credential-control');
17+
var data = e.getNodeData('passphrase-credential-control');
18+
19+
new JX.Workflow('/passphrase/edit/?type=' + data.type)
20+
.setHandler(JX.bind(null, onadd, control))
21+
.start();
22+
23+
e.kill();
24+
});
25+
26+
function onadd(control, response) {
27+
var select = JX.DOM.find(control, 'select', 'passphrase-credential-select');
28+
29+
for (var ii = 0; ii < select.options.length; ii++) {
30+
if (!select.options[ii].value) {
31+
select.remove(ii);
32+
break;
33+
}
34+
}
35+
36+
select.add(
37+
JX.$N('option', {value: response.phid}, response.name),
38+
select.options[0] || null);
39+
40+
select.value = response.phid;
41+
select.disabled = null;
42+
}
43+
44+
});

0 commit comments

Comments
 (0)