mirror of
https://github.com/datahub-project/datahub.git
synced 2025-10-19 21:07:38 +00:00
adds the user-lookup service
This commit is contained in:
parent
cf635ce330
commit
cae270b16d
84
wherehows-web/app/services/user-lookup.js
Normal file
84
wherehows-web/app/services/user-lookup.js
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import Ember from 'ember';
|
||||||
|
|
||||||
|
const {
|
||||||
|
isEmpty,
|
||||||
|
Service,
|
||||||
|
$: { getJSON }
|
||||||
|
} = Ember;
|
||||||
|
const partyEntitiesUrl = '/api/v1/party/entities';
|
||||||
|
|
||||||
|
const cache = {
|
||||||
|
// Cache containing results from the last request to partyEntities api
|
||||||
|
partyEntities: null
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async request for partyEntities. Caches the value from the initial request
|
||||||
|
* @return {Promise.<Object|void>}
|
||||||
|
*/
|
||||||
|
const getLDAPUsers = () =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
const cachedResults = cache.partyEntities;
|
||||||
|
|
||||||
|
// Resolve with cachedResults if this has been previously requested
|
||||||
|
if (!isEmpty(cachedResults)) {
|
||||||
|
return resolve(cachedResults);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cast $.getJSON to native Promise
|
||||||
|
Promise.resolve(getJSON(partyEntitiesUrl))
|
||||||
|
.then(({ status, userEntities = [] }) => {
|
||||||
|
if (status === 'ok' && userEntities.length) {
|
||||||
|
/**
|
||||||
|
* @type {Object} userEntitiesMaps hash of userEntities: label -> displayName
|
||||||
|
*/
|
||||||
|
const userEntitiesMaps = userEntities.reduce(
|
||||||
|
(map, { label, displayName }) => ((map[label] = displayName), map),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
|
||||||
|
return ({
|
||||||
|
userEntities,
|
||||||
|
userEntitiesMaps,
|
||||||
|
userEntitiesSource: Object.keys(userEntitiesMaps)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(results => cache.partyEntities = results)
|
||||||
|
.then(resolve);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes a userNameQuery query and find userNames that match by starting with
|
||||||
|
* the pattern
|
||||||
|
* @param {String} userNameQuery pattern to search for
|
||||||
|
* @param {Function} syncResults callback
|
||||||
|
* @param {Function} asyncResults callback
|
||||||
|
*/
|
||||||
|
const ldapResolver = (userNameQuery, syncResults, asyncResults) => {
|
||||||
|
const regex = new RegExp(`^${userNameQuery}.*`, 'i');
|
||||||
|
|
||||||
|
getLDAPUsers()
|
||||||
|
.then(({ userEntitiesSource = {} }) =>
|
||||||
|
userEntitiesSource.filter(entity => regex.test(entity)))
|
||||||
|
.then(asyncResults);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For a given userName, find the userEntity object that contains the userName
|
||||||
|
* @param {String} userName the unique userName
|
||||||
|
* @return {Promise.<TResult|null>} resolves with the userEntity or null otherwise
|
||||||
|
*/
|
||||||
|
const getPartyEntityWithUserName = userName => {
|
||||||
|
return getLDAPUsers()
|
||||||
|
.then(
|
||||||
|
({ userEntities }) => userEntities.find(({ label }) => label === userName) ||
|
||||||
|
null
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Service.extend({
|
||||||
|
getPartyEntityWithUserName,
|
||||||
|
userNamesResolver: ldapResolver,
|
||||||
|
fetchUserNames: getLDAPUsers
|
||||||
|
});
|
12
wherehows-web/tests/unit/services/user-lookup-test.js
Normal file
12
wherehows-web/tests/unit/services/user-lookup-test.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { moduleFor, test } from 'ember-qunit';
|
||||||
|
|
||||||
|
moduleFor('service:user-lookup', 'Unit | Service | user lookup', {
|
||||||
|
// Specify the other units that are required for this test.
|
||||||
|
// needs: ['service:foo']
|
||||||
|
});
|
||||||
|
|
||||||
|
// Replace this with your real tests.
|
||||||
|
test('it exists', function(assert) {
|
||||||
|
let service = this.subject();
|
||||||
|
assert.ok(service);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user