2013-11-21 12:35:36 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PassphraseCredentialControl extends AphrontFormControl {
|
|
|
|
|
Show a queue utilization statistic in the Daemon console
Summary:
This came up recently in a discussion with @lifeihuang, and then tangentally with @hach-que. Make it easier for users to get a sense of whether they might need to add more daemons. Although we've improved the transparency of daemons, it's not easy for non-experts to determine at a glance how close to overflowing the queue is.
This number is approximate, but should be good enough for determining if your queue is more like 25% or 95% full.
If this goes over, say, 80%, it's probably a good idea to think about adding a couple of daemons. If it's under that, you should generally be fine.
Test Plan: {F88331}
Reviewers: btrahan, hach-que, lifeihuang
Reviewed By: btrahan
CC: hach-que, lifeihuang, aran, chad
Differential Revision: https://secure.phabricator.com/D7747
2013-12-09 13:22:22 -08:00
|
|
|
private $options = array();
|
2013-11-21 12:35:36 -08:00
|
|
|
private $credentialType;
|
2013-11-22 14:35:35 -08:00
|
|
|
private $defaultUsername;
|
|
|
|
private $allowNull;
|
|
|
|
|
|
|
|
public function setAllowNull($allow_null) {
|
|
|
|
$this->allowNull = $allow_null;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDefaultUsername($default_username) {
|
|
|
|
$this->defaultUsername = $default_username;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-11-21 12:35:36 -08:00
|
|
|
|
|
|
|
public function setCredentialType($credential_type) {
|
|
|
|
$this->credentialType = $credential_type;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCredentialType() {
|
|
|
|
return $this->credentialType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOptions(array $options) {
|
|
|
|
assert_instances_of($options, 'PassphraseCredential');
|
|
|
|
$this->options = $options;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCustomControlClass() {
|
|
|
|
return 'passphrase-credential-control';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderInput() {
|
|
|
|
|
|
|
|
$options_map = array();
|
|
|
|
foreach ($this->options as $option) {
|
|
|
|
$options_map[$option->getPHID()] = pht(
|
2014-06-09 11:36:49 -07:00
|
|
|
'%s %s',
|
2013-11-21 12:35:36 -08:00
|
|
|
'K'.$option->getID(),
|
|
|
|
$option->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
$disabled = $this->getDisabled();
|
2013-11-22 14:35:35 -08:00
|
|
|
if ($this->allowNull) {
|
|
|
|
$options_map = array('' => pht('(No Credentials)')) + $options_map;
|
|
|
|
} else {
|
|
|
|
if (!$options_map) {
|
|
|
|
$options_map[''] = pht('(No Existing Credentials)');
|
|
|
|
$disabled = true;
|
|
|
|
}
|
2013-11-21 12:35:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Javelin::initBehavior('passphrase-credential-control');
|
|
|
|
|
|
|
|
$options = AphrontFormSelectControl::renderSelectTag(
|
|
|
|
$this->getValue(),
|
|
|
|
$options_map,
|
|
|
|
array(
|
|
|
|
'id' => $this->getControlID(),
|
|
|
|
'name' => $this->getName(),
|
|
|
|
'disabled' => $disabled ? 'disabled' : null,
|
|
|
|
'sigil' => 'passphrase-credential-select',
|
|
|
|
));
|
|
|
|
|
2013-11-22 15:23:50 -08:00
|
|
|
if ($this->credentialType) {
|
|
|
|
$button = javelin_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => '#',
|
|
|
|
'class' => 'button grey',
|
|
|
|
'sigil' => 'passphrase-credential-add',
|
|
|
|
'mustcapture' => true,
|
|
|
|
),
|
|
|
|
pht('Add Credential'));
|
|
|
|
} else {
|
|
|
|
$button = null;
|
|
|
|
}
|
2013-11-21 12:35:36 -08:00
|
|
|
|
|
|
|
return javelin_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'sigil' => 'passphrase-credential-control',
|
|
|
|
'meta' => array(
|
|
|
|
'type' => $this->getCredentialType(),
|
2013-11-22 14:35:35 -08:00
|
|
|
'username' => $this->defaultUsername,
|
|
|
|
'allowNull' => $this->allowNull,
|
2013-11-21 12:35:36 -08:00
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
$options,
|
|
|
|
$button,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-03-25 16:13:27 -07:00
|
|
|
/**
|
|
|
|
* Verify that a given actor has permission to use all of the credentials
|
|
|
|
* in a list of credential transactions.
|
|
|
|
*
|
|
|
|
* In general, the rule here is:
|
|
|
|
*
|
|
|
|
* - If you're editing an object and it uses a credential you can't use,
|
|
|
|
* that's fine as long as you don't change the credential.
|
|
|
|
* - If you do change the credential, the new credential must be one you
|
|
|
|
* can use.
|
|
|
|
*
|
|
|
|
* @param PhabricatorUser The acting user.
|
|
|
|
* @param list<PhabricatorApplicationTransaction> List of credential altering
|
|
|
|
* transactions.
|
|
|
|
* @return bool True if the transactions are valid.
|
|
|
|
*/
|
|
|
|
public static function validateTransactions(
|
|
|
|
PhabricatorUser $actor,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
$new_phids = array();
|
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
$new = $xaction->getNewValue();
|
|
|
|
if (!$new) {
|
|
|
|
// Removing a credential, so this is OK.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$old = $xaction->getOldValue();
|
|
|
|
if ($old == $new) {
|
|
|
|
// This is a no-op transaction, so this is also OK.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to check this credential.
|
|
|
|
$new_phids[] = $new;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$new_phids) {
|
|
|
|
// No new credentials being set, so this is fine.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$usable_credentials = id(new PassphraseCredentialQuery())
|
|
|
|
->setViewer($actor)
|
|
|
|
->withPHIDs($new_phids)
|
|
|
|
->execute();
|
|
|
|
$usable_credentials = mpull($usable_credentials, null, 'getPHID');
|
|
|
|
|
|
|
|
foreach ($new_phids as $phid) {
|
|
|
|
if (empty($usable_credentials[$phid])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-21 12:35:36 -08:00
|
|
|
}
|