Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return async (context) => {
// Throw if the hook is being called from an unexpected location.
checkContext(context, null, ['find', 'get', 'create', 'update', 'patch', 'remove']);
// Get the authenticated user.
// eslint-disable-next-line no-unused-vars
const { user } = context.params;
// Get the record(s) from context.data (before), context.result.data or context.result (after).
// getItems always returns an array to simplify your processing.
const records = getItems(context);
/*
Modify records and/or context.
*/
// Place the modified records back in the context.
replaceItems(context, records);
// Best practice: hooks should always return the context.
return context;
};
};
return async (context) => {
// Throw if the hook is being called from an unexpected location.
checkContext(context, 'after', ['find', 'get', 'create', 'update', 'patch', 'remove']);
// Get the authenticated user.
// eslint-disable-next-line no-unused-vars
const { user } = context.params;
// Get the record(s) from context.data (before), context.result.data or context.result (after).
// getItems always returns an array to simplify your processing.
const records = getItems(context);
/*
Modify records and/or context.
*/
// Place the modified records back in the context.
replaceItems(context, records);
// Best practice: hooks should always return the context.
return context;
};
};
return hook => {
checkContext(hook, 'after');
// Retrieve the items from the hook
let users = getItems(hook);
if (!users) return;
const isArray = Array.isArray(users);
users = (isArray ? users : [users]);
users.forEach(user => {
if (!('isVerified' in user) && hook.method === 'create') {
/* eslint-disable no-console */
console.warn('Property isVerified not found in user properties. (removeVerification)');
console.warn('Have you added authManagement\'s properties to your model? (Refer to README.md)');
console.warn('Have you added the addVerification hook on users::create?');
/* eslint-enable */
}
if (hook.params.provider && user) { // noop if initiated by server
delete user.verifyExpires;
delete user.resetExpires;
return async function (hook) {
let hookObject = hook
// Handle error hooks as usual
if (hook.type === 'error') hookObject = hook.original
// Retrieve the items from the hook and send it to healthcheck function
options.function(getItems(hookObject), Healthcheck)
debug('Applied function on Healthcheck', Healthcheck)
}
}
return function (hook) {
if(!_.get(hook, `params.user[${settings.idField}]`)) {
throw new errors.NotAuthenticated(`The current user is missing. You must not be authenticated.`);
}
( _.castArray( getItems(hook) ) )
.forEach(item => { item[settings.fieldToSet] = _.get(hook, `params.user[${settings.idField}]`); });
// Hooks can either return nothing or a promise
// that resolves with the `hook` object for asynchronous operations
return Promise.resolve(hook);
};
};
hook.app.service('/users').find({query: {}}).then(function(found) {
console.log("Checking if first user");
if(!Array.isArray(found) && found.data) {
found = found.data
}
if(found.length === 0) {
var firstUser = _.castArray(getItems(hook))[0];
firstUser.role = options.role || 'admin'
console.log('set role to admin');
}
resolve(hook)
}, function(err) {
reject(err)
return new Promise(async (resolve, reject) => {
if(hook.data) {
let [ err, defaultRole ] = await to( hook.app.service('settings').find({ name: 'defaultRole' }) );
if(!err) {
defaultRole = _.get(defaultRole, '0');
const role = _.get(defaultRole, 'value.role') || 'basic';
( _.castArray( getItems(hook) ) )
.forEach(item => { item.role = role; });
} else {
console.log('Error setting default role', err);
}
resolve(hook);
}
})
}
return async function (hook) {
const item = getItems(hook)
const store = await getStoreFromHook(hook, 'readXML', options)
if (!store.path && !store.buffers) {
throw new Error('The \'readXML\' hook only work with the fs or memory blob store.')
}
let xml
const xmlName = item.id
if (store.path) {
const filePath = path.join(store.path, xmlName)
debug('Reading XML file ' + filePath)
xml = await fs.readFile(filePath)
} else {
debug('Parsing XML for ' + xmlName)
xml = store.buffers[xmlName]
}
return async function (hook) {
const item = getItems(hook)
const store = await getStoreFromHook(hook, 'readJson', options)
if (!store.path && !store.buffers) {
throw new Error('The \'readJson\' hook only work with the fs or memory blob store.')
}
let json = {}
const jsonName = template(item, options.key || item.id)
if (store.path) {
const filePath = path.join(store.path, jsonName)
debug('Reading JSON file ' + filePath)
json = await fs.readJson(filePath)
} else {
debug('Parsing JSON for ' + jsonName)
const data = store.buffers[jsonName].toString()
return async function (hook) {
const item = getItems(hook)
const store = await getStoreFromHook(hook, 'readYAML', options)
if (!store.path && !store.buffers) {
throw new Error('The \'readYAML\' hook only work with the fs or memory blob store.')
}
let yaml
const yamlName = template(item, options.key || item.id)
if (store.path) {
const filePath = path.join(store.path, yamlName)
debug('Reading YAML file ' + filePath)
yaml = await fs.readFile(filePath)
} else {
debug('Parsing YAML for ' + yamlName)
yaml = store.buffers[yamlName]
}