UI: Move legacy ACLs to use the new searchables/changeable-sets (#4933)

pull/5729/head
John Cowen 2018-11-19 14:49:16 +00:00 committed by John Cowen
parent c532e53d9a
commit 104e6bac71
7 changed files with 150 additions and 93 deletions

View File

@ -1,11 +1,12 @@
import Controller from '@ember/controller'; import Controller from '@ember/controller';
import { computed, get } from '@ember/object'; import { computed, get } from '@ember/object';
import WithFiltering from 'consul-ui/mixins/with-filtering'; import WithFiltering from 'consul-ui/mixins/with-filtering';
import WithSearching from 'consul-ui/mixins/with-searching';
import ucfirst from 'consul-ui/utils/ucfirst'; import ucfirst from 'consul-ui/utils/ucfirst';
const countType = function(items, type) { const countType = function(items, type) {
return type === '' ? get(items, 'length') : items.filterBy('Type', type).length; return type === '' ? get(items, 'length') : items.filterBy('Type', type).length;
}; };
export default Controller.extend(WithFiltering, { export default Controller.extend(WithSearching, WithFiltering, {
queryParams: { queryParams: {
type: { type: {
as: 'type', as: 'type',
@ -15,6 +16,17 @@ export default Controller.extend(WithFiltering, {
replace: true, replace: true,
}, },
}, },
init: function() {
this.searchParams = {
acl: 's',
};
this._super(...arguments);
},
searchable: computed('filtered', function() {
return get(this, 'searchables.acl')
.add(get(this, 'filtered'))
.search(get(this, this.searchParams.acl));
}),
typeFilters: computed('items', function() { typeFilters: computed('items', function() {
const items = get(this, 'items'); const items = get(this, 'items');
return ['', 'management', 'client'].map(function(item) { return ['', 'management', 'client'].map(function(item) {
@ -27,18 +39,8 @@ export default Controller.extend(WithFiltering, {
}; };
}); });
}), }),
// TODO: This should be using a searchable filter: function(item, { type = '' }) {
filter: function(item, { s = '', type = '' }) { return type === '' || get(item, 'Type') === type;
const sLower = s.toLowerCase();
return (
(get(item, 'Name')
.toLowerCase()
.indexOf(sLower) !== -1 ||
get(item, 'ID')
.toLowerCase()
.indexOf(sLower) !== -1) &&
(type === '' || get(item, 'Type') === type)
);
}, },
actions: { actions: {
sendClone: function(item) { sendClone: function(item) {

View File

@ -2,6 +2,7 @@ import intention from 'consul-ui/search/filters/intention';
import token from 'consul-ui/search/filters/token'; import token from 'consul-ui/search/filters/token';
import policy from 'consul-ui/search/filters/policy'; import policy from 'consul-ui/search/filters/policy';
import kv from 'consul-ui/search/filters/kv'; import kv from 'consul-ui/search/filters/kv';
import acl from 'consul-ui/search/filters/acl';
import node from 'consul-ui/search/filters/node'; import node from 'consul-ui/search/filters/node';
// service instance // service instance
import nodeService from 'consul-ui/search/filters/node/service'; import nodeService from 'consul-ui/search/filters/node/service';
@ -16,6 +17,7 @@ export function initialize(application) {
const searchables = { const searchables = {
intention: intention(filterable), intention: intention(filterable),
token: token(filterable), token: token(filterable),
acl: acl(filterable),
policy: policy(filterable), policy: policy(filterable),
kv: kv(filterable), kv: kv(filterable),
healthyNode: node(filterable), healthyNode: node(filterable),

View File

@ -0,0 +1,14 @@
import { get } from '@ember/object';
export default function(filterable) {
return filterable(function(item, { s = '' }) {
const sLower = s.toLowerCase();
return (
get(item, 'Name')
.toLowerCase()
.indexOf(sLower) !== -1 ||
get(item, 'ID')
.toLowerCase()
.indexOf(sLower) !== -1
);
});
}

View File

@ -1,4 +1,4 @@
{{!<form>}} {{!<form>}}
{{freetext-filter onchange=(action onchange) value=search placeholder="Search by name/token"}} {{freetext-filter searchable=searchable value=search placeholder="Search by name/token"}}
{{radio-group name="type" value=type items=filters onchange=(action onchange)}} {{radio-group name="type" value=type items=filters onchange=(action onchange)}}
{{!</form>}} {{!</form>}}

View File

@ -13,11 +13,12 @@
{{/block-slot}} {{/block-slot}}
{{#block-slot 'toolbar'}} {{#block-slot 'toolbar'}}
{{#if (gt items.length 0) }} {{#if (gt items.length 0) }}
{{acl-filter filters=typeFilters search=filters.s type=filters.type onchange=(action 'filter')}} {{acl-filter searchable=searchable filters=typeFilters search=filters.s type=filters.type onchange=(action 'filter')}}
{{/if}} {{/if}}
{{/block-slot}} {{/block-slot}}
{{#block-slot 'content'}} {{#block-slot 'content'}}
{{#if (gt filtered.length 0)}} {{#changeable-set dispatcher=searchable}}
{{#block-slot 'set' as |filtered|}}
{{#tabular-collection {{#tabular-collection
items=(sort-by 'Name:asc' filtered) as |item index| items=(sort-by 'Name:asc' filtered) as |item index|
}} }}
@ -90,10 +91,12 @@
{{/confirmation-dialog}} {{/confirmation-dialog}}
{{/block-slot}} {{/block-slot}}
{{/tabular-collection}} {{/tabular-collection}}
{{else}} {{/block-slot}}
{{#block-slot 'empty'}}
<p> <p>
There are no ACLs. There are no ACLs.
</p> </p>
{{/if}} {{/block-slot}}
{{/changeable-set}}
{{/block-slot}} {{/block-slot}}
{{/app-view}} {{/app-view}}

View File

@ -2,7 +2,7 @@ import { moduleFor, test } from 'ember-qunit';
moduleFor('controller:dc/acls/index', 'Unit | Controller | dc/acls/index', { moduleFor('controller:dc/acls/index', 'Unit | Controller | dc/acls/index', {
// Specify the other units that are required for this test. // Specify the other units that are required for this test.
// needs: ['controller:foo'] needs: ['service:search', 'service:dom'],
}); });
// Replace this with your real tests. // Replace this with your real tests.

View File

@ -0,0 +1,36 @@
import getFilter from 'consul-ui/search/filters/acl';
import { module, test } from 'qunit';
module('Unit | Search | Filter | acl');
const filter = getFilter(cb => cb);
test('items are found by properties', function(assert) {
[
{
ID: 'HIT-id',
Name: 'name',
},
{
ID: 'id',
Name: 'name-HIT',
},
].forEach(function(item) {
const actual = filter(item, {
s: 'hit',
});
assert.ok(actual);
});
});
test('items are not found', function(assert) {
[
{
ID: 'id',
Name: 'name',
},
].forEach(function(item) {
const actual = filter(item, {
s: 'hit',
});
assert.notOk(actual);
});
});