open-vault/ui/app/components/tool-actions-form.js
Matthew Irish 463a3ebea9
UI - identity details (#4502)
* add popups
* add ability to disable entity and banner when entity is disabled
* re-add alias-popup template
* add accpetance tests for creating entities
* add more entity creation acceptance tests
* add delete to edit-form
* add more identity tests and associated selectors
* add onSuccess hook and use UnloadModel route mixins
* add ability to toggle entity disabling from the popover
* fix store list cache because unloadAll isn't synchronous
* fill out tests for identity items and aliases
* add ability to enable entity from the detail page
* toArray on the peekAll
* fix other tests/behavior that relied on a RecordArray
* adjust layout for disabled entity and label for disabling an entity on the edit form
* add item-details integration tests
* move disable field on the entity form
* use ghost buttons for delete in identity and policy edit forms
* adding computed macros for lazy capability fetching and using them in the identity models
2018-05-23 22:10:21 -05:00

136 lines
3.3 KiB
JavaScript

import Ember from 'ember';
import moment from 'moment';
const { get, set, computed, setProperties } = Ember;
const DEFAULTS = {
token: null,
rewrap_token: null,
errors: [],
wrap_info: null,
creation_time: null,
creation_ttl: null,
data: '{\n}',
unwrap_data: null,
wrapTTL: null,
sum: null,
random_bytes: null,
input: null,
};
const WRAPPING_ENDPOINTS = ['lookup', 'wrap', 'unwrap', 'rewrap'];
export default Ember.Component.extend(DEFAULTS, {
store: Ember.inject.service(),
// putting these attrs here so they don't get reset when you click back
//random
bytes: 32,
//hash
format: 'base64',
algorithm: 'sha2-256',
tagName: '',
didReceiveAttrs() {
this._super(...arguments);
this.checkAction();
},
selectedAction: null,
reset() {
if (this.isDestroyed || this.isDestroying) {
return;
}
setProperties(this, DEFAULTS);
},
checkAction() {
const currentAction = get(this, 'selectedAction');
const oldAction = get(this, 'oldSelectedAction');
if (currentAction !== oldAction) {
this.reset();
}
set(this, 'oldSelectedAction', currentAction);
},
dataIsEmpty: computed.match('data', new RegExp(DEFAULTS.data)),
expirationDate: computed('creation_time', 'creation_ttl', function() {
const { creation_time, creation_ttl } = this.getProperties('creation_time', 'creation_ttl');
if (!(creation_time && creation_ttl)) {
return null;
}
return moment(creation_time).add(moment.duration(creation_ttl, 'seconds'));
}),
handleError(e) {
set(this, 'errors', e.errors);
},
handleSuccess(resp, action) {
let props = {};
let secret = (resp && resp.data) || resp.auth;
if (secret && action === 'unwrap') {
props = Ember.assign({}, props, { unwrap_data: secret });
}
props = Ember.assign({}, props, secret);
if (resp && resp.wrap_info) {
const keyName = action === 'rewrap' ? 'rewrap_token' : 'token';
props = Ember.assign({}, props, { [keyName]: resp.wrap_info.token });
}
setProperties(this, props);
},
getData() {
const action = get(this, 'selectedAction');
if (WRAPPING_ENDPOINTS.includes(action)) {
return get(this, 'dataIsEmpty')
? { token: (get(this, 'token') || '').trim() }
: JSON.parse(get(this, 'data'));
}
if (action === 'random') {
return this.getProperties('bytes');
}
if (action === 'hash') {
return this.getProperties('input', 'format', 'algorithm');
}
},
actions: {
doSubmit(evt) {
evt.preventDefault();
const action = get(this, 'selectedAction');
const wrapTTL = action === 'wrap' ? get(this, 'wrapTTL') : null;
const data = this.getData();
setProperties(this, {
errors: null,
wrap_info: null,
creation_time: null,
creation_ttl: null,
});
get(this, 'store')
.adapterFor('tools')
.toolAction(action, data, { wrapTTL })
.then(resp => this.handleSuccess(resp, action), (...errArgs) => this.handleError(...errArgs));
},
onClear() {
this.reset();
},
codemirrorUpdated(val, codemirror) {
codemirror.performLint();
const hasErrors = codemirror.state.lint.marked.length > 0;
setProperties(this, {
buttonDisabled: hasErrors,
data: val,
});
},
},
});