Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getProgramId(context, cb) {
cb = cb || createPromiseCallback();
// If we are accessing an existing model, get the program id from the existing data.
if (context.modelId) {
debug(`fetching program id for existing model with id ${context.modelId}`);
context.model.findById(context.modelId).then(modelInstance => cb(null, modelInstance.programId));
}
// If we are creating a new model, get the programId from the incoming data.
else if (context.remotingContext.args.data.programId) {
debug(`fetching program id using incoming programId ${context.remotingContext.args.data.programId}`);
process.nextTick(() => cb(null, context.remotingContext.args.data.programId));
}
// Otherwise, return null
else {
debug('teamMember unable to determine program context');
process.nextTick(cb);
}
if (options === undefined && cb === undefined) {
if (typeof data === 'function') {
// UPSERt(cb)
cb = data;
data = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
// upsert(data, cb)
cb = options;
options = {};
}
}
cb = cb || utils.createPromiseCallback();
data = data || {};
options = options || {};
assert(typeof data === 'object', 'The data argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
if (Array.isArray(data)) {
cb(new Error('updateOrCreate does not support bulk mode or any array input'));
return cb.promise;
}
var self = this;
var Model = this;
var id = oeutils.getIdValue(this, data);
Role.registerResolver(accessGroup, (role, context, cb) => {
cb = cb || createPromiseCallback()
const modelClass = context.model
const { modelId } = context
const userId = context.getUserId()
const roleName = this.extractRoleName(role)
const GroupAccess = this.app.models[this.options.groupAccessModel]
const scope = { }
debug(`Role resolver for ${role}: evaluate ${modelClass.modelName} with id: ${modelId} for user: ${userId}`)
// No userId is present
if (!userId) {
process.nextTick(() => {
debug('Deny access for anonymous user')
cb(null, false)
})
return cb.promise
getTargetGroupId(context, cb) {
cb = cb || createPromiseCallback()
debug('getTargetGroupId context.remotingContext.args: %o', context.remotingContext.args)
let groupId = null
// Get the target group id from the incoming data.
if (_get(context, `remotingContext.args.data[${this.options.foreignKey}]`)) {
debug(`determined target group id ${groupId} from incoming data`)
groupId = context.remotingContext.args.data[this.options.foreignKey]
}
// Otherwise, return null.
else {
debug('unable to determine target group context')
}
process.nextTick(() => cb(null, groupId))
Model.itemsWithChangedProperties = function itemsWithChangedProperties(conditions, newVals, properties, cb) {
debug('itemsWithChangedProperties: Looking for items with changed properties...')
debug('itemsWithChangedProperties: conditions is: %o', conditions)
debug('itemsWithChangedProperties: newVals is: %o', newVals)
debug('itemsWithChangedProperties: properties is 1 : %o', properties)
cb = cb || utils.createPromiseCallback()
conditions = conditions || {}
newVals = typeof newVals.toJSON === 'function' ? newVals.toJSON() : newVals || {}
properties = properties || {}
const filterFields = [
Model.getIdName(),
]
// Build up a list of property conditions to include in the query.
let propertyConditions = { or: [] }
_.forEach(newVals, (value, key) => {
if (_.includes(properties, key)) {
const fieldFilter = {}
Person.changeStatus = function(args, cb) {
cb = cb || utils.createPromiseCallback()
debug('this.changeStatus() called with %o', args)
process.nextTick(function() {
cb(null)
})
return cb.promise
}
Model.prototype.itemHasChangedProperties = function itemHasChangedProperties(data, properties, cb) {
cb = cb || utils.createPromiseCallback()
properties = properties || {}
if (_.isEmpty(properties)) {
process.nextTick(() => {
cb(null, false)
})
return cb.promise
}
Model.findById(this.getId())
.then(instance => {
const changedProperties = Model.getChangedProperties(instance, data, properties)
debug('itemHasChangedProperties: found supposedly changed items: %o', changedProperties)
cb(null, changedProperties)
return connectionPromise;
}
if (options === undefined && cb === undefined) {
if (typeof query === 'function') {
cb = query;
query = {};
}
} else if (cb === undefined) {
if (typeof options === 'function') {
cb = options;
options = {};
}
}
cb = cb || utils.createPromiseCallback();
query = query || {};
options = options || {};
assert(typeof query === 'object', 'The query argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
query.limit = 1;
this.find(query, options, function (err, collection) {
if (err || !collection) return cb(err, null);
if (Array.isArray(collection)) {
if (!collection.length > 0) return cb(err, null);
cb(err, collection[0]);
} else {
cb(err, collection);
}
Meta.getModels = function (cb) {
cb = cb || utils.createPromiseCallback()
const modelNames = Object.keys(Meta.app.models)
process.nextTick(() => {
cb(null, modelNames.sort().map((modelName) => getModelInfo(modelName)))
})
return cb.promise
}
hasRoleInGroup(userId, role, group, cb) {
debug('hasRoleInGroup: role: %o, group: %o, userId: %o', role, group, userId)
cb = cb || createPromiseCallback()
const GroupAccess = this.app.models[this.options.groupAccessModel]
const conditions = { userId, role }
conditions[this.options.foreignKey] = group
GroupAccess.count(conditions)
.then(count => {
const res = count > 0
debug(`User ${userId} ${res ? 'HAS' : 'DOESNT HAVE'} ${role} role in group ${group}`)
cb(null, res)
})
return cb.promise
}